check-test-wrapper 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. import sys
  3. import os
  4. import subprocess
  5. import resource
  6. env = os.environ.copy()
  7. args = sys.argv[1:]
  8. targettype = args.pop(0)
  9. if targettype == "user":
  10. qemuargs = os.environ.get("QEMU_OPTIONS", "").split()
  11. if not os.path.exists(qemuargs[0]):
  12. # ensure qemu args has a valid absolute path
  13. for i in os.environ.get("PATH", "").split(":"):
  14. if os.path.exists(os.path.join(i, qemuargs[0])):
  15. qemuargs[0] = os.path.join(i, qemuargs[0])
  16. break
  17. sysroot = os.environ.get("QEMU_SYSROOT", None)
  18. if not sysroot:
  19. sys.exit(-1)
  20. libpaths = [sysroot + "/usr/lib", sysroot + "/lib"]
  21. if args[0] == "env":
  22. args.pop(0)
  23. if len(args) == 0:
  24. args = ["env"]
  25. else:
  26. # process options
  27. while args[0].startswith("-"):
  28. opt = args.pop(0).lstrip("-")
  29. if "i" in opt:
  30. env.clear()
  31. # process environment vars
  32. while "=" in args[0]:
  33. key, val = args.pop(0).split("=", 1)
  34. if key == "LD_LIBRARY_PATH":
  35. libpaths += val.split(":")
  36. else:
  37. env[key] = val
  38. if args[0] == "cp":
  39. # ignore copies, the filesystem is the same
  40. sys.exit(0)
  41. qemuargs += ["-L", sysroot]
  42. qemuargs += ["-E", "LD_LIBRARY_PATH={}".format(":".join(libpaths))]
  43. command = qemuargs + args
  44. # We've seen qemu-arm using up all system memory for some glibc
  45. # tests e.g. nptl/tst-pthread-timedlock-lockloop
  46. # Cap at 8GB since no test should need more than that
  47. # (5GB adds 7 failures for qemuarm glibc test run)
  48. limit = 8*1024*1024*1024
  49. resource.setrlimit(resource.RLIMIT_AS, (limit, limit))
  50. elif targettype == "ssh":
  51. host = os.environ.get("SSH_HOST", None)
  52. user = os.environ.get("SSH_HOST_USER", None)
  53. port = os.environ.get("SSH_HOST_PORT", None)
  54. command = ["ssh", "-o", "UserKnownHostsFile=/dev/null", "-o", "StrictHostKeyChecking=no"]
  55. if port:
  56. command += ["-p", str(port)]
  57. if not host:
  58. sys.exit(-1)
  59. command += ["{}@{}".format(user, host) if user else host]
  60. # wrap and replace quotes for correct transformation on ssh
  61. wrapped = " ".join(["'{0}'".format(i.replace("'", r"'\''")) for i in ["cd", os.getcwd()]]) + "; "
  62. wrapped += " ".join(["'{0}'".format(i.replace("'", r"'\''")) for i in args])
  63. command += ["sh", "-c", "\"{}\"".format(wrapped)]
  64. else:
  65. sys.exit(-1)
  66. try:
  67. r = subprocess.run(command, timeout = 1800, env = env)
  68. sys.exit(r.returncode)
  69. except subprocess.TimeoutExpired:
  70. sys.exit(-1)