Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2018 Google LLC |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 17 | """Run tests. |
| 18 | |
| 19 | Runs tests on QEMU. |
| 20 | """ |
| 21 | |
| 22 | from __future__ import print_function |
| 23 | |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 24 | import xml.etree.ElementTree as ET |
| 25 | |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 26 | import argparse |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 27 | import datetime |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 28 | import json |
| 29 | import os |
| 30 | import re |
| 31 | import subprocess |
| 32 | import sys |
| 33 | |
| 34 | |
| 35 | def qemu(hafnium, initrd, args, log): |
| 36 | qemu_args = [ |
| 37 | "timeout", "--foreground", "5s", |
| 38 | "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt", "-cpu", |
Andrew Scull | abcf1fa | 2018-10-05 17:45:21 +0100 | [diff] [blame] | 39 | "cortex-a57", "-m", "16M", "-machine", "virtualization=true", |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 40 | "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", hafnium, |
| 41 | "-initrd", initrd |
| 42 | ] |
| 43 | if args: |
| 44 | qemu_args += ["-append", args] |
| 45 | # Save the log to a file. |
| 46 | with open(log, "w") as f: |
| 47 | f.write("$ {}\r\n".format(" ".join(qemu_args))) |
| 48 | f.flush() |
| 49 | subprocess.check_call(qemu_args, stdout=f, stderr=f) |
| 50 | # Return that log for processing. |
| 51 | with open(log, "r") as f: |
| 52 | return f.read() |
| 53 | |
| 54 | |
| 55 | def ensure_dir(path): |
| 56 | try: |
| 57 | os.makedirs(path) |
| 58 | except OSError: |
| 59 | if not os.path.isdir(path): |
| 60 | raise |
| 61 | |
| 62 | |
| 63 | def hftest_lines(raw): |
| 64 | prefix = "[hftest] " |
| 65 | return [ |
| 66 | line[len(prefix):] |
| 67 | for line in raw.splitlines() |
| 68 | if line.startswith(prefix) |
| 69 | ] |
| 70 | |
| 71 | |
| 72 | def Main(): |
| 73 | parser = argparse.ArgumentParser() |
| 74 | parser.add_argument("--out", required=True) |
| 75 | parser.add_argument("--initrd", required=True) |
| 76 | parser.add_argument("--suite") |
| 77 | parser.add_argument("--test") |
| 78 | args = parser.parse_args() |
| 79 | # Resolve some paths. |
| 80 | hafnium = os.path.join(args.out, "hafnium.bin") |
| 81 | initrd = os.path.join(args.out, "initrd", args.initrd + ".img") |
| 82 | log = os.path.join(args.out, "test_log", args.initrd) |
| 83 | ensure_dir(log) |
| 84 | print("Logs saved under", log) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 85 | log_file = os.path.join(log, "sponge_log.log") |
| 86 | with open(log_file, "w") as sponge_log: |
| 87 | # Query the tests in the image. |
| 88 | out = qemu(hafnium, initrd, "json", os.path.join(log, "json.log")) |
| 89 | sponge_log.write(out) |
| 90 | sponge_log.write("\r\n\r\n") |
| 91 | hftest_json = "\n".join(hftest_lines(out)) |
| 92 | tests = json.loads(hftest_json) |
| 93 | # Run the selected tests. |
| 94 | tests_run = 0 |
| 95 | failures = 0 |
| 96 | suite_re = re.compile(args.suite or ".*") |
| 97 | test_re = re.compile(args.test or ".*") |
| 98 | sponge = ET.Element("testsuites") |
| 99 | sponge.set("name", args.initrd) |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 100 | sponge.set( |
| 101 | "timestamp", |
| 102 | datetime.datetime.now().replace(microsecond=0).isoformat()) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 103 | for suite in tests["suites"]: |
| 104 | if not suite_re.match(suite["name"]): |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 105 | continue |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 106 | tests_run_from_suite = 0 |
| 107 | failures_from_suite = 0 |
| 108 | sponge_suite = ET.SubElement(sponge, "testsuite") |
| 109 | sponge_suite.set("name", suite["name"]) |
| 110 | for test in suite["tests"]: |
| 111 | if not test_re.match(test): |
| 112 | continue |
| 113 | sponge_test = ET.SubElement(sponge_suite, "testcase") |
| 114 | sponge_test.set("name", test) |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 115 | sponge_test.set("classname", suite['name']) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 116 | sponge_test.set("status", "run") |
| 117 | tests_run_from_suite += 1 |
| 118 | if tests_run_from_suite == 1: |
| 119 | print(" SUITE", suite["name"]) |
| 120 | print(" RUN", test) |
| 121 | test_log = os.path.join(log, |
| 122 | suite["name"] + "." + test + ".log") |
| 123 | out = qemu(hafnium, initrd, "run {} {}".format( |
| 124 | suite["name"], test), test_log) |
| 125 | sponge_log.write(out) |
| 126 | sponge_log.write("\r\n\r\n") |
| 127 | hftest_out = hftest_lines(out) |
| 128 | if hftest_out[-1] == "PASS": |
| 129 | print(" PASS") |
| 130 | else: |
| 131 | failures_from_suite += 1 |
| 132 | sponge_failure = ET.SubElement(sponge_test, "failure") |
| 133 | # TODO: set a meaningful message and put log in CDATA |
| 134 | sponge_failure.set("message", "Test failed") |
| 135 | print("[x] FAIL --", test_log) |
| 136 | tests_run += tests_run_from_suite |
| 137 | failures += failures_from_suite |
| 138 | sponge_suite.set("tests", str(tests_run_from_suite)) |
| 139 | sponge_suite.set("failures", str(failures_from_suite)) |
| 140 | sponge.set("tests", str(tests_run)) |
| 141 | sponge.set("failures", str(failures)) |
| 142 | with open(os.path.join(log, "sponge_log.xml"), "w") as f: |
| 143 | ET.ElementTree(sponge).write(f, encoding='utf-8', xml_declaration=True) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 144 | # If none were run, this is probably a mistake. |
| 145 | if tests_run == 0: |
| 146 | print("Error: no tests match") |
| 147 | return 10 |
| 148 | # Exit with 0 on success and 1 if any test failed. |
| 149 | if failures: |
| 150 | print("[x] FAIL:", failures, "of", tests_run, "tests failed") |
| 151 | return 1 |
| 152 | else: |
| 153 | print(" PASS: all", tests_run, "tests passed") |
| 154 | return 0 |
| 155 | |
| 156 | |
| 157 | if __name__ == "__main__": |
| 158 | sys.exit(Main()) |