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 | # |
Andrew Walbran | 692b325 | 2019-03-07 15:51:31 +0000 | [diff] [blame] | 3 | # Copyright 2018 The Hafnium Authors. |
Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 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 | |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 35 | def qemu(image, initrd, args, log): |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 36 | qemu_args = [ |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 37 | "timeout", "--foreground", "10s", |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 38 | "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt,gic_version=3", |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 39 | "-cpu", "cortex-a57", "-smp", "4", "-m", "64M", "-machine", "virtualization=true", |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 40 | "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", image, |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 41 | ] |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 42 | if initrd: |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 43 | qemu_args += ["-initrd", initrd] |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 44 | if args: |
| 45 | qemu_args += ["-append", args] |
| 46 | # Save the log to a file. |
| 47 | with open(log, "w") as f: |
| 48 | f.write("$ {}\r\n".format(" ".join(qemu_args))) |
| 49 | f.flush() |
| 50 | subprocess.check_call(qemu_args, stdout=f, stderr=f) |
| 51 | # Return that log for processing. |
| 52 | with open(log, "r") as f: |
| 53 | return f.read() |
| 54 | |
| 55 | |
| 56 | def ensure_dir(path): |
| 57 | try: |
| 58 | os.makedirs(path) |
| 59 | except OSError: |
| 60 | if not os.path.isdir(path): |
| 61 | raise |
| 62 | |
| 63 | |
| 64 | def hftest_lines(raw): |
| 65 | prefix = "[hftest] " |
| 66 | return [ |
| 67 | line[len(prefix):] |
| 68 | for line in raw.splitlines() |
| 69 | if line.startswith(prefix) |
| 70 | ] |
| 71 | |
| 72 | |
| 73 | def Main(): |
| 74 | parser = argparse.ArgumentParser() |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 75 | parser.add_argument("image") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 76 | parser.add_argument("--out", required=True) |
Andrew Scull | 23e93a8 | 2018-10-26 14:56:04 +0100 | [diff] [blame] | 77 | parser.add_argument("--log", required=True) |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 78 | parser.add_argument("--initrd") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 79 | parser.add_argument("--suite") |
| 80 | parser.add_argument("--test") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 81 | parser.add_argument("--vm_args") |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 82 | args = parser.parse_args() |
| 83 | # Resolve some paths. |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 84 | image = os.path.join(args.out, args.image + ".bin") |
| 85 | initrd = None |
| 86 | suite = args.image |
| 87 | if args.initrd: |
Andrew Walbran | 377bd8b | 2019-02-04 17:51:04 +0000 | [diff] [blame] | 88 | initrd = os.path.join(args.out, "obj", args.initrd, "initrd.img") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 89 | suite += "_" + args.initrd |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 90 | vm_args = args.vm_args or "" |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 91 | log = os.path.join(args.log, suite) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 92 | ensure_dir(log) |
| 93 | print("Logs saved under", log) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 94 | log_file = os.path.join(log, "sponge_log.log") |
| 95 | with open(log_file, "w") as sponge_log: |
| 96 | # Query the tests in the image. |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 97 | out = qemu(image, initrd, vm_args + " json", os.path.join(log, "json.log")) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 98 | sponge_log.write(out) |
| 99 | sponge_log.write("\r\n\r\n") |
| 100 | hftest_json = "\n".join(hftest_lines(out)) |
| 101 | tests = json.loads(hftest_json) |
| 102 | # Run the selected tests. |
| 103 | tests_run = 0 |
| 104 | failures = 0 |
| 105 | suite_re = re.compile(args.suite or ".*") |
| 106 | test_re = re.compile(args.test or ".*") |
| 107 | sponge = ET.Element("testsuites") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 108 | sponge.set("name", suite) |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 109 | sponge.set( |
| 110 | "timestamp", |
| 111 | datetime.datetime.now().replace(microsecond=0).isoformat()) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 112 | for suite in tests["suites"]: |
| 113 | if not suite_re.match(suite["name"]): |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 114 | continue |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 115 | tests_run_from_suite = 0 |
| 116 | failures_from_suite = 0 |
| 117 | sponge_suite = ET.SubElement(sponge, "testsuite") |
| 118 | sponge_suite.set("name", suite["name"]) |
| 119 | for test in suite["tests"]: |
| 120 | if not test_re.match(test): |
| 121 | continue |
| 122 | sponge_test = ET.SubElement(sponge_suite, "testcase") |
| 123 | sponge_test.set("name", test) |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 124 | sponge_test.set("classname", suite['name']) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 125 | sponge_test.set("status", "run") |
| 126 | tests_run_from_suite += 1 |
| 127 | if tests_run_from_suite == 1: |
| 128 | print(" SUITE", suite["name"]) |
| 129 | print(" RUN", test) |
| 130 | test_log = os.path.join(log, |
| 131 | suite["name"] + "." + test + ".log") |
Andrew Walbran | bc342d4 | 2019-02-05 16:56:02 +0000 | [diff] [blame^] | 132 | out = qemu(image, initrd, vm_args + " run {} {}".format( |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 133 | suite["name"], test), test_log) |
| 134 | sponge_log.write(out) |
| 135 | sponge_log.write("\r\n\r\n") |
| 136 | hftest_out = hftest_lines(out) |
Andrew Walbran | 6bc52b2 | 2019-02-08 14:35:00 +0000 | [diff] [blame] | 137 | if len(hftest_out) > 0 and hftest_out[-1] == "FINISHED" and not any( |
Andrew Scull | f0551c8 | 2018-12-15 20:38:47 +0000 | [diff] [blame] | 138 | l.startswith('Failure:') for l in hftest_out): |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 139 | print(" PASS") |
| 140 | else: |
| 141 | failures_from_suite += 1 |
| 142 | sponge_failure = ET.SubElement(sponge_test, "failure") |
| 143 | # TODO: set a meaningful message and put log in CDATA |
| 144 | sponge_failure.set("message", "Test failed") |
| 145 | print("[x] FAIL --", test_log) |
| 146 | tests_run += tests_run_from_suite |
| 147 | failures += failures_from_suite |
| 148 | sponge_suite.set("tests", str(tests_run_from_suite)) |
| 149 | sponge_suite.set("failures", str(failures_from_suite)) |
| 150 | sponge.set("tests", str(tests_run)) |
| 151 | sponge.set("failures", str(failures)) |
| 152 | with open(os.path.join(log, "sponge_log.xml"), "w") as f: |
| 153 | ET.ElementTree(sponge).write(f, encoding='utf-8', xml_declaration=True) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 154 | # If none were run, this is probably a mistake. |
| 155 | if tests_run == 0: |
| 156 | print("Error: no tests match") |
| 157 | return 10 |
| 158 | # Exit with 0 on success and 1 if any test failed. |
| 159 | if failures: |
| 160 | print("[x] FAIL:", failures, "of", tests_run, "tests failed") |
| 161 | return 1 |
| 162 | else: |
| 163 | print(" PASS: all", tests_run, "tests passed") |
| 164 | return 0 |
| 165 | |
| 166 | |
| 167 | if __name__ == "__main__": |
| 168 | sys.exit(Main()) |