blob: 2a2a07e04538259ab29074fefa0ece7e215bf52f [file] [log] [blame]
Andrew Scullbc7189d2018-08-14 09:35:13 +01001#!/usr/bin/env python
Andrew Scull18834872018-10-12 11:48:09 +01002#
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 Scullbc7189d2018-08-14 09:35:13 +010017"""Run tests.
18
19Runs tests on QEMU.
20"""
21
22from __future__ import print_function
23
Andrew Scull3b62f2b2018-08-21 14:26:12 +010024import xml.etree.ElementTree as ET
25
Andrew Scullbc7189d2018-08-14 09:35:13 +010026import argparse
Andrew Scull04502e42018-09-03 14:54:52 +010027import datetime
Andrew Scullbc7189d2018-08-14 09:35:13 +010028import json
29import os
30import re
31import subprocess
32import sys
33
34
Andrew Scull7fd4bb72018-12-08 23:40:12 +000035def qemu(image, initrd, args, log):
Andrew Scullbc7189d2018-08-14 09:35:13 +010036 qemu_args = [
37 "timeout", "--foreground", "5s",
Andrew Scullf0551c82018-12-15 20:38:47 +000038 "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt,gic_version=3",
39 "-cpu", "cortex-a57", "-smp", "4", "-m", "16M", "-machine", "virtualization=true",
Andrew Scull7fd4bb72018-12-08 23:40:12 +000040 "-nographic", "-nodefaults", "-serial", "stdio", "-kernel", image,
Andrew Scullbc7189d2018-08-14 09:35:13 +010041 ]
Andrew Scull7fd4bb72018-12-08 23:40:12 +000042 if initrd:
Andrew Scullf0551c82018-12-15 20:38:47 +000043 qemu_args += ["-initrd", initrd]
Andrew Scullbc7189d2018-08-14 09:35:13 +010044 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
56def ensure_dir(path):
57 try:
58 os.makedirs(path)
59 except OSError:
60 if not os.path.isdir(path):
61 raise
62
63
64def 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
73def Main():
74 parser = argparse.ArgumentParser()
Andrew Scull7fd4bb72018-12-08 23:40:12 +000075 parser.add_argument("image")
Andrew Scullbc7189d2018-08-14 09:35:13 +010076 parser.add_argument("--out", required=True)
Andrew Scull23e93a82018-10-26 14:56:04 +010077 parser.add_argument("--log", required=True)
Andrew Scull7fd4bb72018-12-08 23:40:12 +000078 parser.add_argument("--initrd")
Andrew Scullbc7189d2018-08-14 09:35:13 +010079 parser.add_argument("--suite")
80 parser.add_argument("--test")
81 args = parser.parse_args()
82 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +000083 image = os.path.join(args.out, args.image + ".bin")
84 initrd = None
85 suite = args.image
86 if args.initrd:
87 initrd = os.path.join(args.out, "initrd", args.initrd + ".img")
88 suite += "_" + args.initrd
89 log = os.path.join(args.log, suite)
Andrew Scullbc7189d2018-08-14 09:35:13 +010090 ensure_dir(log)
91 print("Logs saved under", log)
Andrew Scull3b62f2b2018-08-21 14:26:12 +010092 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 Scull7fd4bb72018-12-08 23:40:12 +000095 out = qemu(image, initrd, "json", os.path.join(log, "json.log"))
Andrew Scull3b62f2b2018-08-21 14:26:12 +010096 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 Scull7fd4bb72018-12-08 23:40:12 +0000106 sponge.set("name", suite)
Andrew Scull04502e42018-09-03 14:54:52 +0100107 sponge.set(
108 "timestamp",
109 datetime.datetime.now().replace(microsecond=0).isoformat())
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100110 for suite in tests["suites"]:
111 if not suite_re.match(suite["name"]):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100112 continue
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100113 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 Scull04502e42018-09-03 14:54:52 +0100122 sponge_test.set("classname", suite['name'])
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100123 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 Scull7fd4bb72018-12-08 23:40:12 +0000130 out = qemu(image, initrd, "run {} {}".format(
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100131 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 Scullf0551c82018-12-15 20:38:47 +0000135 if hftest_out[-1] == "FINISHED" and not any(
136 l.startswith('Failure:') for l in hftest_out):
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100137 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 Scullbc7189d2018-08-14 09:35:13 +0100152 # 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
165if __name__ == "__main__":
166 sys.exit(Main())