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 | |
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 = [ |
| 37 | "timeout", "--foreground", "5s", |
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", |
| 39 | "-cpu", "cortex-a57", "-smp", "4", "-m", "16M", "-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") |
| 81 | args = parser.parse_args() |
| 82 | # Resolve some paths. |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 83 | image = os.path.join(args.out, args.image + ".bin") |
| 84 | initrd = None |
| 85 | suite = args.image |
| 86 | if args.initrd: |
Andrew Walbran | 377bd8b | 2019-02-04 17:51:04 +0000 | [diff] [blame] | 87 | initrd = os.path.join(args.out, "obj", args.initrd, "initrd.img") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 88 | suite += "_" + args.initrd |
| 89 | log = os.path.join(args.log, suite) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 90 | ensure_dir(log) |
| 91 | print("Logs saved under", log) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 92 | log_file = os.path.join(log, "sponge_log.log") |
| 93 | with open(log_file, "w") as sponge_log: |
| 94 | # Query the tests in the image. |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 95 | out = qemu(image, initrd, "json", os.path.join(log, "json.log")) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 96 | sponge_log.write(out) |
| 97 | sponge_log.write("\r\n\r\n") |
| 98 | hftest_json = "\n".join(hftest_lines(out)) |
| 99 | tests = json.loads(hftest_json) |
| 100 | # Run the selected tests. |
| 101 | tests_run = 0 |
| 102 | failures = 0 |
| 103 | suite_re = re.compile(args.suite or ".*") |
| 104 | test_re = re.compile(args.test or ".*") |
| 105 | sponge = ET.Element("testsuites") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 106 | sponge.set("name", suite) |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 107 | sponge.set( |
| 108 | "timestamp", |
| 109 | datetime.datetime.now().replace(microsecond=0).isoformat()) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 110 | for suite in tests["suites"]: |
| 111 | if not suite_re.match(suite["name"]): |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 112 | continue |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 113 | tests_run_from_suite = 0 |
| 114 | failures_from_suite = 0 |
| 115 | sponge_suite = ET.SubElement(sponge, "testsuite") |
| 116 | sponge_suite.set("name", suite["name"]) |
| 117 | for test in suite["tests"]: |
| 118 | if not test_re.match(test): |
| 119 | continue |
| 120 | sponge_test = ET.SubElement(sponge_suite, "testcase") |
| 121 | sponge_test.set("name", test) |
Andrew Scull | 04502e4 | 2018-09-03 14:54:52 +0100 | [diff] [blame] | 122 | sponge_test.set("classname", suite['name']) |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 123 | sponge_test.set("status", "run") |
| 124 | tests_run_from_suite += 1 |
| 125 | if tests_run_from_suite == 1: |
| 126 | print(" SUITE", suite["name"]) |
| 127 | print(" RUN", test) |
| 128 | test_log = os.path.join(log, |
| 129 | suite["name"] + "." + test + ".log") |
Andrew Scull | 7fd4bb7 | 2018-12-08 23:40:12 +0000 | [diff] [blame] | 130 | out = qemu(image, initrd, "run {} {}".format( |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 131 | suite["name"], test), test_log) |
| 132 | sponge_log.write(out) |
| 133 | sponge_log.write("\r\n\r\n") |
| 134 | hftest_out = hftest_lines(out) |
Andrew Walbran | 6bc52b2 | 2019-02-08 14:35:00 +0000 | [diff] [blame] | 135 | 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] | 136 | l.startswith('Failure:') for l in hftest_out): |
Andrew Scull | 3b62f2b | 2018-08-21 14:26:12 +0100 | [diff] [blame] | 137 | print(" PASS") |
| 138 | else: |
| 139 | failures_from_suite += 1 |
| 140 | sponge_failure = ET.SubElement(sponge_test, "failure") |
| 141 | # TODO: set a meaningful message and put log in CDATA |
| 142 | sponge_failure.set("message", "Test failed") |
| 143 | print("[x] FAIL --", test_log) |
| 144 | tests_run += tests_run_from_suite |
| 145 | failures += failures_from_suite |
| 146 | sponge_suite.set("tests", str(tests_run_from_suite)) |
| 147 | sponge_suite.set("failures", str(failures_from_suite)) |
| 148 | sponge.set("tests", str(tests_run)) |
| 149 | sponge.set("failures", str(failures)) |
| 150 | with open(os.path.join(log, "sponge_log.xml"), "w") as f: |
| 151 | ET.ElementTree(sponge).write(f, encoding='utf-8', xml_declaration=True) |
Andrew Scull | bc7189d | 2018-08-14 09:35:13 +0100 | [diff] [blame] | 152 | # If none were run, this is probably a mistake. |
| 153 | if tests_run == 0: |
| 154 | print("Error: no tests match") |
| 155 | return 10 |
| 156 | # Exit with 0 on success and 1 if any test failed. |
| 157 | if failures: |
| 158 | print("[x] FAIL:", failures, "of", tests_run, "tests failed") |
| 159 | return 1 |
| 160 | else: |
| 161 | print(" PASS: all", tests_run, "tests passed") |
| 162 | return 0 |
| 163 | |
| 164 | |
| 165 | if __name__ == "__main__": |
| 166 | sys.exit(Main()) |