Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | """Run tests. |
| 3 | |
| 4 | Runs tests on QEMU. |
| 5 | """ |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | |
| 9 | import argparse |
| 10 | import json |
| 11 | import os |
| 12 | import re |
| 13 | import subprocess |
| 14 | import sys |
| 15 | |
| 16 | |
| 17 | def qemu(hafnium, initrd, args, log): |
| 18 | qemu_args = [ |
| 19 | "timeout", "--foreground", "5s", |
| 20 | "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt", "-cpu", |
| 21 | "cortex-a57", "-m", "8M", "-machine", "virtualization=true", |
| 22 | "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", hafnium, |
| 23 | "-initrd", initrd |
| 24 | ] |
| 25 | if args: |
| 26 | qemu_args += ["-append", args] |
| 27 | # Save the log to a file. |
| 28 | with open(log, "w") as f: |
| 29 | f.write("$ {}\r\n".format(" ".join(qemu_args))) |
| 30 | f.flush() |
| 31 | subprocess.check_call(qemu_args, stdout=f, stderr=f) |
| 32 | # Return that log for processing. |
| 33 | with open(log, "r") as f: |
| 34 | return f.read() |
| 35 | |
| 36 | |
| 37 | def ensure_dir(path): |
| 38 | try: |
| 39 | os.makedirs(path) |
| 40 | except OSError: |
| 41 | if not os.path.isdir(path): |
| 42 | raise |
| 43 | |
| 44 | |
| 45 | def hftest_lines(raw): |
| 46 | prefix = "[hftest] " |
| 47 | return [ |
| 48 | line[len(prefix):] |
| 49 | for line in raw.splitlines() |
| 50 | if line.startswith(prefix) |
| 51 | ] |
| 52 | |
| 53 | |
| 54 | def Main(): |
| 55 | parser = argparse.ArgumentParser() |
| 56 | parser.add_argument("--out", required=True) |
| 57 | parser.add_argument("--initrd", required=True) |
| 58 | parser.add_argument("--suite") |
| 59 | parser.add_argument("--test") |
| 60 | args = parser.parse_args() |
| 61 | # Resolve some paths. |
| 62 | hafnium = os.path.join(args.out, "hafnium.bin") |
| 63 | initrd = os.path.join(args.out, "initrd", args.initrd + ".img") |
| 64 | log = os.path.join(args.out, "test_log", args.initrd) |
| 65 | ensure_dir(log) |
| 66 | print("Logs saved under", log) |
| 67 | # Query the tests in the image. |
| 68 | out = qemu(hafnium, initrd, "json", os.path.join(log, "json.log")) |
| 69 | hftest_json = "\n".join(hftest_lines(out)) |
| 70 | tests = json.loads(hftest_json) |
| 71 | # Run the selected tests. |
| 72 | tests_run = 0 |
| 73 | failures = 0 |
| 74 | suite_re = re.compile(args.suite or ".*") |
| 75 | test_re = re.compile(args.test or ".*") |
| 76 | for suite in tests['suites']: |
| 77 | if not suite_re.match(suite['name']): |
| 78 | continue |
| 79 | tests_run_from_suite = 0 |
| 80 | for test in suite['tests']: |
| 81 | if not test_re.match(test): |
| 82 | continue |
| 83 | tests_run_from_suite += 1 |
| 84 | if tests_run_from_suite == 1: |
| 85 | print(" SUITE", suite['name']) |
| 86 | print(" RUN", test) |
| 87 | test_log = os.path.join(log, suite['name'] + "." + test + ".log") |
| 88 | out = qemu(hafnium, initrd, "run {} {}".format(suite['name'], test), test_log) |
| 89 | hftest_out = hftest_lines(out) |
| 90 | if hftest_out[-1] == "PASS": |
| 91 | print(" PASS") |
| 92 | else: |
| 93 | failures += 1 |
| 94 | print("[x] FAIL --", test_log) |
| 95 | tests_run += tests_run_from_suite |
| 96 | # If none were run, this is probably a mistake. |
| 97 | if tests_run == 0: |
| 98 | print("Error: no tests match") |
| 99 | return 10 |
| 100 | # Exit with 0 on success and 1 if any test failed. |
| 101 | if failures: |
| 102 | print("[x] FAIL:", failures, "of", tests_run, "tests failed") |
| 103 | return 1 |
| 104 | else: |
| 105 | print(" PASS: all", tests_run, "tests passed") |
| 106 | return 0 |
| 107 | |
| 108 | |
| 109 | if __name__ == "__main__": |
| 110 | sys.exit(Main()) |