blob: a12468235b0c3bff5b62f8c7b359ebef84d7ad12 [file] [log] [blame]
Andrew Scullbc7189d2018-08-14 09:35:13 +01001#!/usr/bin/env python
Andrew Scull18834872018-10-12 11:48:09 +01002#
Andrew Walbran692b3252019-03-07 15:51:31 +00003# Copyright 2018 The Hafnium Authors.
Andrew Scull18834872018-10-12 11:48:09 +01004#
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 = [
Andrew Walbranbc342d42019-02-05 16:56:02 +000037 "timeout", "--foreground", "10s",
Andrew Scullf0551c82018-12-15 20:38:47 +000038 "./prebuilts/linux-x64/qemu/qemu-system-aarch64", "-M", "virt,gic_version=3",
Andrew Walbranbc342d42019-02-05 16:56:02 +000039 "-cpu", "cortex-a57", "-smp", "4", "-m", "64M", "-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")
Andrew Walbranbc342d42019-02-05 16:56:02 +000081 parser.add_argument("--vm_args")
Andrew Scullbc7189d2018-08-14 09:35:13 +010082 args = parser.parse_args()
83 # Resolve some paths.
Andrew Scull7fd4bb72018-12-08 23:40:12 +000084 image = os.path.join(args.out, args.image + ".bin")
85 initrd = None
86 suite = args.image
87 if args.initrd:
Andrew Walbran377bd8b2019-02-04 17:51:04 +000088 initrd = os.path.join(args.out, "obj", args.initrd, "initrd.img")
Andrew Scull7fd4bb72018-12-08 23:40:12 +000089 suite += "_" + args.initrd
Andrew Walbranbc342d42019-02-05 16:56:02 +000090 vm_args = args.vm_args or ""
Andrew Scull7fd4bb72018-12-08 23:40:12 +000091 log = os.path.join(args.log, suite)
Andrew Scullbc7189d2018-08-14 09:35:13 +010092 ensure_dir(log)
93 print("Logs saved under", log)
Andrew Scull3b62f2b2018-08-21 14:26:12 +010094 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 Walbranbc342d42019-02-05 16:56:02 +000097 out = qemu(image, initrd, vm_args + " json", os.path.join(log, "json.log"))
Andrew Scull3b62f2b2018-08-21 14:26:12 +010098 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 Scull7fd4bb72018-12-08 23:40:12 +0000108 sponge.set("name", suite)
Andrew Scull04502e42018-09-03 14:54:52 +0100109 sponge.set(
110 "timestamp",
111 datetime.datetime.now().replace(microsecond=0).isoformat())
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100112 for suite in tests["suites"]:
113 if not suite_re.match(suite["name"]):
Andrew Scullbc7189d2018-08-14 09:35:13 +0100114 continue
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100115 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 Scull04502e42018-09-03 14:54:52 +0100124 sponge_test.set("classname", suite['name'])
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100125 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 Walbranbc342d42019-02-05 16:56:02 +0000132 out = qemu(image, initrd, vm_args + " run {} {}".format(
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100133 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 Walbran6bc52b22019-02-08 14:35:00 +0000137 if len(hftest_out) > 0 and hftest_out[-1] == "FINISHED" and not any(
Andrew Scullf0551c82018-12-15 20:38:47 +0000138 l.startswith('Failure:') for l in hftest_out):
Andrew Scull3b62f2b2018-08-21 14:26:12 +0100139 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 Scullbc7189d2018-08-14 09:35:13 +0100154 # 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
167if __name__ == "__main__":
168 sys.exit(Main())