Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 2 | # vim: set syntax=python ts=4 : |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3 | """Zephyr Sanity Tests |
| 4 | |
| 5 | This script scans for the set of unit test applications in the git |
| 6 | repository and attempts to execute them. By default, it tries to |
| 7 | build each test case on one platform per architecture, using a precedence |
Paul Sokolovsky | ff70add | 2017-06-16 01:31:54 +0300 | [diff] [blame] | 8 | list defined in an architecture configuration file, and if possible |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 9 | run the tests in the QEMU emulator. |
| 10 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 11 | Test cases are detected by the presence of a 'testcase.yaml' or a sample.yaml |
| 12 | files in the application's project directory. This file may contain one or more |
| 13 | blocks, each identifying a test scenario. The title of the block is a name for |
| 14 | the test case, which only needs to be unique for the test cases specified in |
| 15 | that testcase meta-data. The full canonical name for each test case is <path to |
| 16 | test case>/<block>. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 17 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 18 | Each test block in the testcase meta data can define the following key/value pairs: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 19 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 20 | tags: <list of tags> (required) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 21 | A set of string tags for the testcase. Usually pertains to |
| 22 | functional domains but can be anything. Command line invocations |
| 23 | of this script can filter the set of tests to run based on tag. |
| 24 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 25 | skip: <True|False> (default False) |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 26 | skip testcase unconditionally. This can be used for broken tests. |
| 27 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 28 | slow: <True|False> (default False) |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 29 | Don't run this test case unless --enable-slow was passed in on the |
| 30 | command line. Intended for time-consuming test cases that are only |
| 31 | run under certain circumstances, like daily builds. These test cases |
| 32 | are still compiled. |
| 33 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 34 | extra_args: <list of extra arguments> |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 35 | Extra arguments to pass to Make when building or running the |
| 36 | test case. |
| 37 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 38 | build_only: <True|False> (default False) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 39 | If true, don't try to run the test under QEMU even if the |
| 40 | selected platform supports it. |
| 41 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 42 | build_on_all: <True|False> (default False) |
| 43 | If true, attempt to build test on all available platforms. |
| 44 | |
| 45 | depends_on: <list of features> |
| 46 | A board or platform can announce what features it supports, this option |
| 47 | will enable the test only those platforms that provide this feature. |
| 48 | |
| 49 | min_ram: <integer> |
| 50 | minimum amount of RAM needed for this test to build and run. This is |
| 51 | compared with information provided by the board metadata. |
| 52 | |
| 53 | min_flash: <integer> |
| 54 | minimum amount of ROM needed for this test to build and run. This is |
| 55 | compared with information provided by the board metadata. |
| 56 | |
| 57 | timeout: <number of seconds> |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 58 | Length of time to run test in QEMU before automatically killing it. |
| 59 | Default to 60 seconds. |
| 60 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 61 | arch_whitelist: <list of arches, such as x86, arm, arc> |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 62 | Set of architectures that this test case should only be run for. |
| 63 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 64 | arch_exclude: <list of arches, such as x86, arm, arc> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 65 | Set of architectures that this test case should not run on. |
| 66 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 67 | platform_whitelist: <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 68 | Set of platforms that this test case should only be run for. |
| 69 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 70 | platform_exclude: <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 71 | Set of platforms that this test case should not run on. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 72 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 73 | extra_sections: <list of extra binary sections> |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 74 | When computing sizes, sanitycheck will report errors if it finds |
| 75 | extra, unexpected sections in the Zephyr binary unless they are named |
| 76 | here. They will not be included in the size calculation. |
| 77 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 78 | filter: <expression> |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 79 | Filter whether the testcase should be run by evaluating an expression |
| 80 | against an environment containing the following values: |
| 81 | |
| 82 | { ARCH : <architecture>, |
| 83 | PLATFORM : <platform>, |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 84 | <all CONFIG_* key/value pairs in the test's generated defconfig>, |
| 85 | *<env>: any environment variable available |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | The grammar for the expression language is as follows: |
| 89 | |
| 90 | expression ::= expression "and" expression |
| 91 | | expression "or" expression |
| 92 | | "not" expression |
| 93 | | "(" expression ")" |
| 94 | | symbol "==" constant |
| 95 | | symbol "!=" constant |
| 96 | | symbol "<" number |
| 97 | | symbol ">" number |
| 98 | | symbol ">=" number |
| 99 | | symbol "<=" number |
| 100 | | symbol "in" list |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 101 | | symbol ":" string |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 102 | | symbol |
| 103 | |
| 104 | list ::= "[" list_contents "]" |
| 105 | |
| 106 | list_contents ::= constant |
| 107 | | list_contents "," constant |
| 108 | |
| 109 | constant ::= number |
| 110 | | string |
| 111 | |
| 112 | |
| 113 | For the case where expression ::= symbol, it evaluates to true |
| 114 | if the symbol is defined to a non-empty string. |
| 115 | |
| 116 | Operator precedence, starting from lowest to highest: |
| 117 | |
| 118 | or (left associative) |
| 119 | and (left associative) |
| 120 | not (right associative) |
| 121 | all comparison operators (non-associative) |
| 122 | |
| 123 | arch_whitelist, arch_exclude, platform_whitelist, platform_exclude |
| 124 | are all syntactic sugar for these expressions. For instance |
| 125 | |
| 126 | arch_exclude = x86 arc |
| 127 | |
| 128 | Is the same as: |
| 129 | |
| 130 | filter = not ARCH in ["x86", "arc"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 131 | |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 132 | The ':' operator compiles the string argument as a regular expression, |
| 133 | and then returns a true value only if the symbol's value in the environment |
| 134 | matches. For example, if CONFIG_SOC="quark_se" then |
| 135 | |
| 136 | filter = CONFIG_SOC : "quark.*" |
| 137 | |
| 138 | Would match it. |
| 139 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 140 | The set of test cases that actually run depends on directives in the testcase |
| 141 | filed and options passed in on the command line. If there is any confusion, |
| 142 | running with -v or --discard-report can help show why particular test cases |
| 143 | were skipped. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 144 | |
| 145 | Metrics (such as pass/fail state and binary size) for the last code |
| 146 | release are stored in scripts/sanity_chk/sanity_last_release.csv. |
| 147 | To update this, pass the --all --release options. |
| 148 | |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 149 | To load arguments from a file, write '+' before the file name, e.g., |
| 150 | +file_name. File content must be one or more valid arguments separated by |
| 151 | line break instead of white spaces. |
| 152 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 153 | Most everyday users will run with no arguments. |
| 154 | """ |
| 155 | |
| 156 | import argparse |
| 157 | import os |
| 158 | import sys |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 159 | import configparser |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 160 | import re |
| 161 | import tempfile |
| 162 | import subprocess |
| 163 | import multiprocessing |
| 164 | import select |
| 165 | import shutil |
| 166 | import signal |
| 167 | import threading |
| 168 | import time |
| 169 | import csv |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 170 | import glob |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 171 | import concurrent |
| 172 | import concurrent.futures |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 173 | import xml.etree.ElementTree as ET |
Anas Nashif | e6fcc01 | 2017-05-17 09:29:09 -0400 | [diff] [blame] | 174 | from xml.sax.saxutils import escape |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 175 | from collections import OrderedDict |
| 176 | from itertools import islice |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 177 | import yaml |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 178 | |
| 179 | if "ZEPHYR_BASE" not in os.environ: |
Anas Nashif | 427cdd3 | 2015-08-06 07:25:42 -0400 | [diff] [blame] | 180 | sys.stderr.write("$ZEPHYR_BASE environment variable undefined.\n") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 181 | exit(1) |
| 182 | ZEPHYR_BASE = os.environ["ZEPHYR_BASE"] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 183 | |
| 184 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/")) |
| 185 | |
| 186 | import expr_parser |
| 187 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 188 | VERBOSE = 0 |
| 189 | LAST_SANITY = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 190 | "last_sanity.csv") |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 191 | LAST_SANITY_XUNIT = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 192 | "last_sanity.xml") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 193 | RELEASE_DATA = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 194 | "sanity_last_release.csv") |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 195 | CPU_COUNTS = multiprocessing.cpu_count() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 196 | |
| 197 | if os.isatty(sys.stdout.fileno()): |
| 198 | TERMINAL = True |
| 199 | COLOR_NORMAL = '\033[0m' |
| 200 | COLOR_RED = '\033[91m' |
| 201 | COLOR_GREEN = '\033[92m' |
| 202 | COLOR_YELLOW = '\033[93m' |
| 203 | else: |
| 204 | TERMINAL = False |
| 205 | COLOR_NORMAL = "" |
| 206 | COLOR_RED = "" |
| 207 | COLOR_GREEN = "" |
| 208 | COLOR_YELLOW = "" |
| 209 | |
| 210 | class SanityCheckException(Exception): |
| 211 | pass |
| 212 | |
| 213 | class SanityRuntimeError(SanityCheckException): |
| 214 | pass |
| 215 | |
| 216 | class ConfigurationError(SanityCheckException): |
| 217 | def __init__(self, cfile, message): |
| 218 | self.cfile = cfile |
| 219 | self.message = message |
| 220 | |
| 221 | def __str__(self): |
| 222 | return repr(self.cfile + ": " + self.message) |
| 223 | |
| 224 | class MakeError(SanityCheckException): |
| 225 | pass |
| 226 | |
| 227 | class BuildError(MakeError): |
| 228 | pass |
| 229 | |
| 230 | class ExecutionError(MakeError): |
| 231 | pass |
| 232 | |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 233 | log_file = None |
| 234 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 235 | # Debug Functions |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 236 | def info(what): |
| 237 | sys.stdout.write(what + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 238 | if log_file: |
| 239 | log_file.write(what + "\n") |
| 240 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 241 | |
| 242 | def error(what): |
| 243 | sys.stderr.write(COLOR_RED + what + COLOR_NORMAL + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 244 | if log_file: |
| 245 | log_file(what + "\n") |
| 246 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 247 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 248 | def debug(what): |
| 249 | if VERBOSE >= 1: |
| 250 | info(what) |
| 251 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 252 | def verbose(what): |
| 253 | if VERBOSE >= 2: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 254 | info(what) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 255 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 256 | class Handler: |
| 257 | RUN_PASSED = "PROJECT EXECUTION SUCCESSFUL" |
| 258 | RUN_FAILED = "PROJECT EXECUTION FAILED" |
| 259 | def __init__(self, name, outdir, log_fn, timeout, unit=False): |
| 260 | """Constructor |
| 261 | |
| 262 | @param name Arbitrary name of the created thread |
| 263 | @param outdir Working directory, should be where qemu.pid gets created |
| 264 | by kbuild |
| 265 | @param log_fn Absolute path to write out QEMU's log data |
| 266 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 267 | the given number of seconds |
| 268 | """ |
| 269 | self.lock = threading.Lock() |
| 270 | self.state = "waiting" |
| 271 | self.metrics = {} |
| 272 | self.metrics["qemu_time"] = 0 |
| 273 | self.metrics["ram_size"] = 0 |
| 274 | self.metrics["rom_size"] = 0 |
| 275 | self.unit = unit |
| 276 | |
| 277 | def set_state(self, state, metrics): |
| 278 | self.lock.acquire() |
| 279 | self.state = state |
| 280 | self.metrics.update(metrics) |
| 281 | self.lock.release() |
| 282 | |
| 283 | def get_state(self): |
| 284 | self.lock.acquire() |
| 285 | ret = (self.state, self.metrics) |
| 286 | self.lock.release() |
| 287 | return ret |
| 288 | |
| 289 | class UnitHandler(Handler): |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 290 | def __init__(self, name, sourcedir, outdir, run_log, valgrind_log, timeout): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 291 | """Constructor |
| 292 | |
| 293 | @param name Arbitrary name of the created thread |
| 294 | @param outdir Working directory containing the test binary |
| 295 | @param run_log Absolute path to runtime logs |
| 296 | @param valgrind Absolute path to valgrind's log |
| 297 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 298 | the given number of seconds |
| 299 | """ |
| 300 | super().__init__(name, outdir, run_log, timeout, True) |
| 301 | |
| 302 | self.timeout = timeout |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 303 | self.sourcedir = sourcedir |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 304 | self.outdir = outdir |
| 305 | self.run_log = run_log |
| 306 | self.valgrind_log = valgrind_log |
| 307 | self.returncode = 0 |
| 308 | self.set_state("running", {}) |
| 309 | |
| 310 | def handle(self): |
| 311 | out_state = "failed" |
| 312 | |
| 313 | with open(self.run_log, "wt") as rl, open(self.valgrind_log, "wt") as vl: |
| 314 | try: |
| 315 | binary = os.path.join(self.outdir, "testbinary") |
| 316 | command = [binary] |
| 317 | if shutil.which("valgrind"): |
| 318 | command = ["valgrind", "--error-exitcode=2", |
| 319 | "--leak-check=full"] + command |
| 320 | returncode = subprocess.call(command, timeout=self.timeout, |
| 321 | stdout=rl, stderr=vl) |
| 322 | self.returncode = returncode |
| 323 | if returncode != 0: |
| 324 | if self.returncode == 1: |
| 325 | out_state = "failed" |
| 326 | else: |
| 327 | out_state = "failed valgrind" |
| 328 | else: |
| 329 | out_state = "passed" |
| 330 | except subprocess.TimeoutExpired: |
| 331 | out_state = "timeout" |
| 332 | self.returncode = 1 |
| 333 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 334 | returncode = subprocess.call(["GCOV_PREFIX=" + self.outdir, "gcov", self.sourcedir, "-s", self.outdir], shell=True) |
| 335 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 336 | self.set_state(out_state, {}) |
| 337 | |
| 338 | class QEMUHandler(Handler): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 339 | """Spawns a thread to monitor QEMU output from pipes |
| 340 | |
| 341 | We pass QEMU_PIPE to 'make qemu' and monitor the pipes for output. |
| 342 | We need to do this as once qemu starts, it runs forever until killed. |
| 343 | Test cases emit special messages to the console as they run, we check |
| 344 | for these to collect whether the test passed or failed. |
| 345 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 346 | |
| 347 | @staticmethod |
| 348 | def _thread(handler, timeout, outdir, logfile, fifo_fn, pid_fn, results): |
| 349 | fifo_in = fifo_fn + ".in" |
| 350 | fifo_out = fifo_fn + ".out" |
| 351 | |
| 352 | # These in/out nodes are named from QEMU's perspective, not ours |
| 353 | if os.path.exists(fifo_in): |
| 354 | os.unlink(fifo_in) |
| 355 | os.mkfifo(fifo_in) |
| 356 | if os.path.exists(fifo_out): |
| 357 | os.unlink(fifo_out) |
| 358 | os.mkfifo(fifo_out) |
| 359 | |
| 360 | # We don't do anything with out_fp but we need to open it for |
| 361 | # writing so that QEMU doesn't block, due to the way pipes work |
| 362 | out_fp = open(fifo_in, "wb") |
| 363 | # Disable internal buffering, we don't |
| 364 | # want read() or poll() to ever block if there is data in there |
| 365 | in_fp = open(fifo_out, "rb", buffering=0) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 366 | log_out_fp = open(logfile, "wt") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 367 | |
| 368 | start_time = time.time() |
| 369 | timeout_time = start_time + timeout |
| 370 | p = select.poll() |
| 371 | p.register(in_fp, select.POLLIN) |
| 372 | |
| 373 | metrics = {} |
| 374 | line = "" |
| 375 | while True: |
| 376 | this_timeout = int((timeout_time - time.time()) * 1000) |
| 377 | if this_timeout < 0 or not p.poll(this_timeout): |
| 378 | out_state = "timeout" |
| 379 | break |
| 380 | |
Genaro Saucedo Tejada | 2968b36 | 2016-08-09 15:11:53 -0500 | [diff] [blame] | 381 | try: |
| 382 | c = in_fp.read(1).decode("utf-8") |
| 383 | except UnicodeDecodeError: |
| 384 | # Test is writing something weird, fail |
| 385 | out_state = "unexpected byte" |
| 386 | break |
| 387 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 388 | if c == "": |
| 389 | # EOF, this shouldn't happen unless QEMU crashes |
| 390 | out_state = "unexpected eof" |
| 391 | break |
| 392 | line = line + c |
| 393 | if c != "\n": |
| 394 | continue |
| 395 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 396 | # line contains a full line of data output from QEMU |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 397 | log_out_fp.write(line) |
| 398 | log_out_fp.flush() |
| 399 | line = line.strip() |
| 400 | verbose("QEMU: %s" % line) |
| 401 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 402 | if line == handler.RUN_PASSED: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 403 | out_state = "passed" |
| 404 | break |
| 405 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 406 | if line == handler.RUN_FAILED: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 407 | out_state = "failed" |
| 408 | break |
| 409 | |
| 410 | # TODO: Add support for getting numerical performance data |
| 411 | # from test cases. Will involve extending test case reporting |
| 412 | # APIs. Add whatever gets reported to the metrics dictionary |
| 413 | line = "" |
| 414 | |
| 415 | metrics["qemu_time"] = time.time() - start_time |
| 416 | verbose("QEMU complete (%s) after %f seconds" % |
| 417 | (out_state, metrics["qemu_time"])) |
| 418 | handler.set_state(out_state, metrics) |
| 419 | |
| 420 | log_out_fp.close() |
| 421 | out_fp.close() |
| 422 | in_fp.close() |
| 423 | |
| 424 | pid = int(open(pid_fn).read()) |
| 425 | os.unlink(pid_fn) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 426 | try: |
| 427 | os.kill(pid, signal.SIGTERM) |
| 428 | except ProcessLookupError: |
| 429 | # Oh well, as long as it's dead! User probably sent Ctrl-C |
| 430 | pass |
| 431 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 432 | os.unlink(fifo_in) |
| 433 | os.unlink(fifo_out) |
| 434 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 435 | def __init__(self, name, outdir, log_fn, timeout): |
| 436 | """Constructor |
| 437 | |
| 438 | @param name Arbitrary name of the created thread |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 439 | @param outdir Working directory, should be where qemu.pid gets created |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 440 | by kbuild |
| 441 | @param log_fn Absolute path to write out QEMU's log data |
| 442 | @param timeout Kill the QEMU process if it doesn't finish up within |
| 443 | the given number of seconds |
| 444 | """ |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 445 | super().__init__(name, outdir, log_fn, timeout) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 446 | self.results = {} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 447 | |
| 448 | # We pass this to QEMU which looks for fifos with .in and .out |
| 449 | # suffixes. |
| 450 | self.fifo_fn = os.path.join(outdir, "qemu-fifo") |
| 451 | |
| 452 | self.pid_fn = os.path.join(outdir, "qemu.pid") |
| 453 | if os.path.exists(self.pid_fn): |
| 454 | os.unlink(self.pid_fn) |
| 455 | |
| 456 | self.log_fn = log_fn |
| 457 | self.thread = threading.Thread(name=name, target=QEMUHandler._thread, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 458 | args=(self, timeout, outdir, |
| 459 | self.log_fn, self.fifo_fn, |
| 460 | self.pid_fn, self.results)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 461 | self.thread.daemon = True |
| 462 | verbose("Spawning QEMU process for %s" % name) |
| 463 | self.thread.start() |
| 464 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 465 | def get_fifo(self): |
| 466 | return self.fifo_fn |
| 467 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 468 | class SizeCalculator: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 469 | |
Andrew Boie | 8eed4b0 | 2017-06-14 16:08:16 -0700 | [diff] [blame] | 470 | alloc_sections = ["bss", "noinit", "app_bss", "app_noinit"] |
Andrew Boie | 18ba153 | 2017-01-17 13:47:06 -0800 | [diff] [blame] | 471 | rw_sections = ["datas", "initlevel", "_k_task_list", "_k_event_list", |
Allan Stephens | 3f5c74c | 2016-11-01 14:37:15 -0500 | [diff] [blame] | 472 | "_k_memory_pool", "exceptions", "initshell", |
| 473 | "_static_thread_area", "_k_timer_area", |
| 474 | "_k_mem_slab_area", "_k_mem_pool_area", |
| 475 | "_k_sem_area", "_k_mutex_area", "_k_alert_area", |
| 476 | "_k_fifo_area", "_k_lifo_area", "_k_stack_area", |
Tomasz Bursztyka | b56c4df | 2016-08-18 14:07:22 +0200 | [diff] [blame] | 477 | "_k_msgq_area", "_k_mbox_area", "_k_pipe_area", |
Tomasz Bursztyka | e38a9e8 | 2017-03-08 09:30:03 +0100 | [diff] [blame] | 478 | "net_if", "net_if_event", "net_stack", "net_l2_data", |
Andrew Boie | 8eed4b0 | 2017-06-14 16:08:16 -0700 | [diff] [blame] | 479 | "_k_queue_area", "_net_buf_pool_area", "app_datas" ] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 480 | # These get copied into RAM only on non-XIP |
Andrew Boie | 3b93021 | 2016-06-07 13:11:45 -0700 | [diff] [blame] | 481 | ro_sections = ["text", "ctors", "init_array", "reset", |
Anas Nashif | bfcdfaf | 2016-12-15 10:02:14 -0500 | [diff] [blame] | 482 | "rodata", "devconfig", "net_l2", "vector"] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 483 | |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 484 | def __init__(self, filename, extra_sections): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 485 | """Constructor |
| 486 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 487 | @param filename Path to the output binary |
| 488 | The <filename> is parsed by objdump to determine section sizes |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 489 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 490 | # Make sure this is an ELF binary |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 491 | with open(filename, "rb") as f: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 492 | magic = f.read(4) |
| 493 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 494 | if (magic != b'\x7fELF'): |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 495 | raise SanityRuntimeError("%s is not an ELF binary" % filename) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 496 | |
| 497 | # Search for CONFIG_XIP in the ELF's list of symbols using NM and AWK. |
| 498 | # GREP can not be used as it returns an error if the symbol is not found. |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 499 | is_xip_command = "nm " + filename + " | awk '/CONFIG_XIP/ { print $3 }'" |
Andrew Boie | 8f0211d | 2016-03-02 20:40:29 -0800 | [diff] [blame] | 500 | is_xip_output = subprocess.check_output(is_xip_command, shell=True, |
| 501 | stderr=subprocess.STDOUT).decode("utf-8").strip() |
| 502 | if is_xip_output.endswith("no symbols"): |
| 503 | raise SanityRuntimeError("%s has no symbol information" % filename) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 504 | self.is_xip = (len(is_xip_output) != 0) |
| 505 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 506 | self.filename = filename |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 507 | self.sections = [] |
| 508 | self.rom_size = 0 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 509 | self.ram_size = 0 |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 510 | self.extra_sections = extra_sections |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 511 | |
| 512 | self._calculate_sizes() |
| 513 | |
| 514 | def get_ram_size(self): |
| 515 | """Get the amount of RAM the application will use up on the device |
| 516 | |
| 517 | @return amount of RAM, in bytes |
| 518 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 519 | return self.ram_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 520 | |
| 521 | def get_rom_size(self): |
| 522 | """Get the size of the data that this application uses on device's flash |
| 523 | |
| 524 | @return amount of ROM, in bytes |
| 525 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 526 | return self.rom_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 527 | |
| 528 | def unrecognized_sections(self): |
| 529 | """Get a list of sections inside the binary that weren't recognized |
| 530 | |
David B. Kinder | 29963c3 | 2017-06-16 12:32:42 -0700 | [diff] [blame] | 531 | @return list of unrecognized section names |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 532 | """ |
| 533 | slist = [] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 534 | for v in self.sections: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 535 | if not v["recognized"]: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 536 | slist.append(v["name"]) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 537 | return slist |
| 538 | |
| 539 | def _calculate_sizes(self): |
| 540 | """ Calculate RAM and ROM usage by section """ |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 541 | objdump_command = "objdump -h " + self.filename |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 542 | objdump_output = subprocess.check_output(objdump_command, |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 543 | shell=True).decode("utf-8").splitlines() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 544 | |
| 545 | for line in objdump_output: |
| 546 | words = line.split() |
| 547 | |
| 548 | if (len(words) == 0): # Skip lines that are too short |
| 549 | continue |
| 550 | |
| 551 | index = words[0] |
| 552 | if (not index[0].isdigit()): # Skip lines that do not start |
| 553 | continue # with a digit |
| 554 | |
| 555 | name = words[1] # Skip lines with section names |
| 556 | if (name[0] == '.'): # starting with '.' |
| 557 | continue |
| 558 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 559 | # TODO this doesn't actually reflect the size in flash or RAM as |
| 560 | # it doesn't include linker-imposed padding between sections. |
| 561 | # It is close though. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 562 | size = int(words[2], 16) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 563 | if size == 0: |
| 564 | continue |
| 565 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 566 | load_addr = int(words[4], 16) |
| 567 | virt_addr = int(words[3], 16) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 568 | |
| 569 | # Add section to memory use totals (for both non-XIP and XIP scenarios) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 570 | # Unrecognized section names are not included in the calculations. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 571 | recognized = True |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 572 | if name in SizeCalculator.alloc_sections: |
| 573 | self.ram_size += size |
| 574 | stype = "alloc" |
| 575 | elif name in SizeCalculator.rw_sections: |
| 576 | self.ram_size += size |
| 577 | self.rom_size += size |
| 578 | stype = "rw" |
| 579 | elif name in SizeCalculator.ro_sections: |
| 580 | self.rom_size += size |
| 581 | if not self.is_xip: |
| 582 | self.ram_size += size |
| 583 | stype = "ro" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 584 | else: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 585 | stype = "unknown" |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 586 | if name not in self.extra_sections: |
| 587 | recognized = False |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 588 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 589 | self.sections.append({"name" : name, "load_addr" : load_addr, |
| 590 | "size" : size, "virt_addr" : virt_addr, |
Andy Ross | 8d801a5 | 2016-10-04 11:48:21 -0700 | [diff] [blame] | 591 | "type" : stype, "recognized" : recognized}) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 592 | |
| 593 | |
| 594 | class MakeGoal: |
| 595 | """Metadata class representing one of the sub-makes called by MakeGenerator |
| 596 | |
David B. Kinder | 29963c3 | 2017-06-16 12:32:42 -0700 | [diff] [blame] | 597 | MakeGenerator returns a dictionary of these which can then be associated |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 598 | with TestInstances to get a complete picture of what happened during a test. |
| 599 | MakeGenerator is used for tasks outside of building tests (such as |
| 600 | defconfigs) which is why MakeGoal is a separate class from TestInstance. |
| 601 | """ |
| 602 | def __init__(self, name, text, qemu, make_log, build_log, run_log, |
| 603 | qemu_log): |
| 604 | self.name = name |
| 605 | self.text = text |
| 606 | self.qemu = qemu |
| 607 | self.make_log = make_log |
| 608 | self.build_log = build_log |
| 609 | self.run_log = run_log |
| 610 | self.qemu_log = qemu_log |
| 611 | self.make_state = "waiting" |
| 612 | self.failed = False |
| 613 | self.finished = False |
| 614 | self.reason = None |
| 615 | self.metrics = {} |
| 616 | |
| 617 | def get_error_log(self): |
| 618 | if self.make_state == "waiting": |
| 619 | # Shouldn't ever see this; breakage in the main Makefile itself. |
| 620 | return self.make_log |
| 621 | elif self.make_state == "building": |
| 622 | # Failure when calling the sub-make to build the code |
| 623 | return self.build_log |
| 624 | elif self.make_state == "running": |
| 625 | # Failure in sub-make for "make qemu", qemu probably failed to start |
| 626 | return self.run_log |
| 627 | elif self.make_state == "finished": |
| 628 | # QEMU finished, but timed out or otherwise wasn't successful |
| 629 | return self.qemu_log |
| 630 | |
| 631 | def fail(self, reason): |
| 632 | self.failed = True |
| 633 | self.finished = True |
| 634 | self.reason = reason |
| 635 | |
| 636 | def success(self): |
| 637 | self.finished = True |
| 638 | |
| 639 | def __str__(self): |
| 640 | if self.finished: |
| 641 | if self.failed: |
| 642 | return "[%s] failed (%s: see %s)" % (self.name, self.reason, |
| 643 | self.get_error_log()) |
| 644 | else: |
| 645 | return "[%s] passed" % self.name |
| 646 | else: |
| 647 | return "[%s] in progress (%s)" % (self.name, self.make_state) |
| 648 | |
| 649 | |
| 650 | class MakeGenerator: |
| 651 | """Generates a Makefile which just calls a bunch of sub-make sessions |
| 652 | |
| 653 | In any given test suite we may need to build dozens if not hundreds of |
| 654 | test cases. The cleanest way to parallelize this is to just let Make |
| 655 | do the parallelization, sharing the jobserver among all the different |
| 656 | sub-make targets. |
| 657 | """ |
| 658 | |
| 659 | GOAL_HEADER_TMPL = """.PHONY: {goal} |
| 660 | {goal}: |
| 661 | """ |
| 662 | |
| 663 | MAKE_RULE_TMPL = """\t@echo sanity_test_{phase} {goal} >&2 |
Andrew Boie | f7a6e28 | 2017-02-02 13:04:57 -0800 | [diff] [blame] | 664 | \t$(MAKE) -C {directory} O={outdir} V={verb} EXTRA_CFLAGS="-Werror {cflags}" EXTRA_ASMFLAGS=-Wa,--fatal-warnings EXTRA_LDFLAGS=--fatal-warnings {args} >{logfile} 2>&1 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 665 | """ |
| 666 | |
| 667 | GOAL_FOOTER_TMPL = "\t@echo sanity_test_finished {goal} >&2\n\n" |
| 668 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 669 | re_make = re.compile("sanity_test_([A-Za-z0-9]+) (.+)|$|make[:] \*\*\* \[(.+:.+: )?(.+)\] Error.+$") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 670 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 671 | def __init__(self, base_outdir, asserts=False, deprecations=False, ccache=0): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 672 | """MakeGenerator constructor |
| 673 | |
| 674 | @param base_outdir Intended to be the base out directory. A make.log |
| 675 | file will be created here which contains the output of the |
| 676 | top-level Make session, as well as the dynamic control Makefile |
| 677 | @param verbose If true, pass V=1 to all the sub-makes which greatly |
| 678 | increases their verbosity |
| 679 | """ |
| 680 | self.goals = {} |
| 681 | if not os.path.exists(base_outdir): |
| 682 | os.makedirs(base_outdir) |
| 683 | self.logfile = os.path.join(base_outdir, "make.log") |
| 684 | self.makefile = os.path.join(base_outdir, "Makefile") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 685 | self.asserts = asserts |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 686 | self.deprecations = deprecations |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 687 | self.ccache = ccache |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 688 | |
| 689 | def _get_rule_header(self, name): |
| 690 | return MakeGenerator.GOAL_HEADER_TMPL.format(goal=name) |
| 691 | |
| 692 | def _get_sub_make(self, name, phase, workdir, outdir, logfile, args): |
| 693 | verb = "1" if VERBOSE else "0" |
| 694 | args = " ".join(args) |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 695 | |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 696 | if self.asserts: |
| 697 | cflags="-DCONFIG_ASSERT=1 -D__ASSERT_ON=2" |
| 698 | else: |
| 699 | cflags="" |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 700 | |
| 701 | if self.deprecations: |
| 702 | cflags = cflags + " -Wno-deprecated-declarations" |
Andrew Boie | f7a6e28 | 2017-02-02 13:04:57 -0800 | [diff] [blame] | 703 | |
| 704 | if self.ccache: |
| 705 | args = args + " USE_CCACHE=1" |
| 706 | |
| 707 | return MakeGenerator.MAKE_RULE_TMPL.format(phase=phase, goal=name, |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 708 | outdir=outdir, cflags=cflags, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 709 | directory=workdir, verb=verb, |
| 710 | args=args, logfile=logfile) |
| 711 | |
| 712 | def _get_rule_footer(self, name): |
| 713 | return MakeGenerator.GOAL_FOOTER_TMPL.format(goal=name) |
| 714 | |
| 715 | def _add_goal(self, outdir): |
| 716 | if not os.path.exists(outdir): |
| 717 | os.makedirs(outdir) |
| 718 | |
Kumar Gala | 914d92e | 2017-06-22 16:19:11 -0500 | [diff] [blame] | 719 | def add_build_goal(self, name, directory, outdir, args, buildlog): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 720 | """Add a goal to invoke a Kbuild session |
| 721 | |
| 722 | @param name A unique string name for this build goal. The results |
| 723 | dictionary returned by execute() will be keyed by this name. |
| 724 | @param directory Absolute path to working directory, will be passed |
| 725 | to make -C |
| 726 | @param outdir Absolute path to output directory, will be passed to |
| 727 | Kbuild via -O=<path> |
| 728 | @param args Extra command line arguments to pass to 'make', typically |
| 729 | environment variables or specific Make goals |
| 730 | """ |
| 731 | self._add_goal(outdir) |
Kumar Gala | 914d92e | 2017-06-22 16:19:11 -0500 | [diff] [blame] | 732 | build_logfile = os.path.join(outdir, buildlog) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 733 | text = (self._get_rule_header(name) + |
| 734 | self._get_sub_make(name, "building", directory, |
| 735 | outdir, build_logfile, args) + |
| 736 | self._get_rule_footer(name)) |
| 737 | self.goals[name] = MakeGoal(name, text, None, self.logfile, build_logfile, |
| 738 | None, None) |
| 739 | |
| 740 | def add_qemu_goal(self, name, directory, outdir, args, timeout=30): |
| 741 | """Add a goal to build a Zephyr project and then run it under QEMU |
| 742 | |
| 743 | The generated make goal invokes Make twice, the first time it will |
| 744 | build the default goal, and the second will invoke the 'qemu' goal. |
| 745 | The output of the QEMU session will be monitored, and terminated |
| 746 | either upon pass/fail result of the test program, or the timeout |
| 747 | is reached. |
| 748 | |
| 749 | @param name A unique string name for this build goal. The results |
| 750 | dictionary returned by execute() will be keyed by this name. |
| 751 | @param directory Absolute path to working directory, will be passed |
| 752 | to make -C |
| 753 | @param outdir Absolute path to output directory, will be passed to |
| 754 | Kbuild via -O=<path> |
| 755 | @param args Extra command line arguments to pass to 'make', typically |
| 756 | environment variables. Do not pass specific Make goals here. |
| 757 | @param timeout Maximum length of time QEMU session should be allowed |
| 758 | to run before automatically killing it. Default is 30 seconds. |
| 759 | """ |
| 760 | |
| 761 | self._add_goal(outdir) |
| 762 | build_logfile = os.path.join(outdir, "build.log") |
| 763 | run_logfile = os.path.join(outdir, "run.log") |
| 764 | qemu_logfile = os.path.join(outdir, "qemu.log") |
| 765 | |
| 766 | q = QEMUHandler(name, outdir, qemu_logfile, timeout) |
| 767 | args.append("QEMU_PIPE=%s" % q.get_fifo()) |
| 768 | text = (self._get_rule_header(name) + |
| 769 | self._get_sub_make(name, "building", directory, |
| 770 | outdir, build_logfile, args) + |
| 771 | self._get_sub_make(name, "running", directory, |
| 772 | outdir, run_logfile, |
Mazen NEIFER | 7f04637 | 2017-01-31 12:41:33 +0100 | [diff] [blame] | 773 | args + ["run"]) + |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 774 | self._get_rule_footer(name)) |
| 775 | self.goals[name] = MakeGoal(name, text, q, self.logfile, build_logfile, |
| 776 | run_logfile, qemu_logfile) |
| 777 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 778 | def add_unit_goal(self, name, directory, outdir, args, timeout=30, coverage=False): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 779 | self._add_goal(outdir) |
| 780 | build_logfile = os.path.join(outdir, "build.log") |
| 781 | run_logfile = os.path.join(outdir, "run.log") |
| 782 | qemu_logfile = os.path.join(outdir, "qemu.log") |
| 783 | valgrind_logfile = os.path.join(outdir, "valgrind.log") |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 784 | if coverage: |
| 785 | args += ["COVERAGE=1"] |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 786 | |
| 787 | # we handle running in the UnitHandler class |
| 788 | text = (self._get_rule_header(name) + |
| 789 | self._get_sub_make(name, "building", directory, |
| 790 | outdir, build_logfile, args) + |
| 791 | self._get_rule_footer(name)) |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 792 | q = UnitHandler(name, directory, outdir, run_logfile, valgrind_logfile, timeout) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 793 | self.goals[name] = MakeGoal(name, text, q, self.logfile, build_logfile, |
| 794 | run_logfile, valgrind_logfile) |
| 795 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 796 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 797 | def add_test_instance(self, ti, build_only=False, enable_slow=False, coverage=False, |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 798 | extra_args=[]): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 799 | """Add a goal to build/test a TestInstance object |
| 800 | |
| 801 | @param ti TestInstance object to build. The status dictionary returned |
| 802 | by execute() will be keyed by its .name field. |
| 803 | """ |
| 804 | args = ti.test.extra_args[:] |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 805 | args.extend(["ARCH=%s" % ti.platform.arch, |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 806 | "BOARD=%s" % ti.platform.name]) |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 807 | args.extend(extra_args) |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 808 | if (ti.platform.qemu_support and (not ti.build_only) and |
| 809 | (not build_only) and (enable_slow or not ti.test.slow)): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 810 | self.add_qemu_goal(ti.name, ti.test.code_location, ti.outdir, |
| 811 | args, ti.test.timeout) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 812 | elif ti.test.type == "unit": |
| 813 | self.add_unit_goal(ti.name, ti.test.code_location, ti.outdir, |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 814 | args, ti.test.timeout, coverage) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 815 | else: |
Kumar Gala | 914d92e | 2017-06-22 16:19:11 -0500 | [diff] [blame] | 816 | self.add_build_goal(ti.name, ti.test.code_location, ti.outdir, |
| 817 | args, "build.log") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 818 | |
| 819 | def execute(self, callback_fn=None, context=None): |
| 820 | """Execute all the registered build goals |
| 821 | |
| 822 | @param callback_fn If not None, a callback function will be called |
| 823 | as individual goals transition between states. This function |
| 824 | should accept two parameters: a string state and an arbitrary |
| 825 | context object, supplied here |
| 826 | @param context Context object to pass to the callback function. |
| 827 | Type and semantics are specific to that callback function. |
| 828 | @return A dictionary mapping goal names to final status. |
| 829 | """ |
| 830 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 831 | with open(self.makefile, "wt") as tf, \ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 832 | open(os.devnull, "wb") as devnull, \ |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 833 | open(self.logfile, "wt") as make_log: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 834 | # Create our dynamic Makefile and execute it. |
| 835 | # Watch stderr output which is where we will keep |
| 836 | # track of build state |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 837 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 838 | tf.write(goal.text) |
| 839 | tf.write("all: %s\n" % (" ".join(self.goals.keys()))) |
| 840 | tf.flush() |
| 841 | |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 842 | cmd = ["make", "-k", "-j", str(CPU_COUNTS * 2), "-f", tf.name, "all"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 843 | p = subprocess.Popen(cmd, stderr=subprocess.PIPE, |
| 844 | stdout=devnull) |
| 845 | |
| 846 | for line in iter(p.stderr.readline, b''): |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 847 | line = line.decode("utf-8") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 848 | make_log.write(line) |
| 849 | verbose("MAKE: " + repr(line.strip())) |
| 850 | m = MakeGenerator.re_make.match(line) |
| 851 | if not m: |
| 852 | continue |
| 853 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 854 | state, name, _, error = m.groups() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 855 | if error: |
| 856 | goal = self.goals[error] |
| 857 | else: |
| 858 | goal = self.goals[name] |
| 859 | goal.make_state = state |
| 860 | |
| 861 | |
| 862 | if error: |
Andrew Boie | 822b087 | 2017-01-10 13:32:40 -0800 | [diff] [blame] | 863 | # Sometimes QEMU will run an image and then crash out, which |
| 864 | # will cause the 'make qemu' invocation to exit with |
| 865 | # nonzero status. |
| 866 | # Need to distinguish this case from a compilation failure. |
| 867 | if goal.qemu: |
| 868 | goal.fail("qemu_crash") |
| 869 | else: |
| 870 | goal.fail("build_error") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 871 | else: |
| 872 | if state == "finished": |
| 873 | if goal.qemu: |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 874 | if goal.qemu.unit: |
| 875 | # We can't run unit tests with Make |
| 876 | goal.qemu.handle() |
| 877 | if goal.qemu.returncode == 2: |
| 878 | goal.qemu_log = goal.qemu.valgrind_log |
| 879 | elif goal.qemu.returncode: |
| 880 | goal.qemu_log = goal.qemu.run_log |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 881 | thread_status, metrics = goal.qemu.get_state() |
| 882 | goal.metrics.update(metrics) |
| 883 | if thread_status == "passed": |
| 884 | goal.success() |
| 885 | else: |
| 886 | goal.fail(thread_status) |
| 887 | else: |
| 888 | goal.success() |
| 889 | |
| 890 | if callback_fn: |
| 891 | callback_fn(context, self.goals, goal) |
| 892 | |
| 893 | p.wait() |
| 894 | return self.goals |
| 895 | |
| 896 | |
| 897 | # "list" - List of strings |
| 898 | # "list:<type>" - List of <type> |
| 899 | # "set" - Set of unordered, unique strings |
| 900 | # "set:<type>" - Set of <type> |
| 901 | # "float" - Floating point |
| 902 | # "int" - Integer |
| 903 | # "bool" - Boolean |
| 904 | # "str" - String |
| 905 | |
| 906 | # XXX Be sure to update __doc__ if you change any of this!! |
| 907 | |
| 908 | arch_valid_keys = {"name" : {"type" : "str", "required" : True}, |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 909 | "platforms" : {"type" : "list", "required" : True}, |
| 910 | "supported_toolchains" : {"type" : "list", "required" : True}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 911 | |
| 912 | platform_valid_keys = {"qemu_support" : {"type" : "bool", "default" : False}, |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 913 | "supported_toolchains" : {"type" : "list", "default" : []}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 914 | |
| 915 | testcase_valid_keys = {"tags" : {"type" : "set", "required" : True}, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 916 | "type" : {"type" : "str", "default": "integration"}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 917 | "extra_args" : {"type" : "list"}, |
| 918 | "build_only" : {"type" : "bool", "default" : False}, |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 919 | "build_on_all" : {"type" : "bool", "default" : False}, |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 920 | "skip" : {"type" : "bool", "default" : False}, |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 921 | "slow" : {"type" : "bool", "default" : False}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 922 | "timeout" : {"type" : "int", "default" : 60}, |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 923 | "min_ram" : {"type" : "int", "default" : 8}, |
| 924 | "depends_on": {"type" : "set"}, |
| 925 | "min_flash" : {"type" : "int", "default" : 32}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 926 | "arch_whitelist" : {"type" : "set"}, |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 927 | "arch_exclude" : {"type" : "set"}, |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 928 | "extra_sections" : {"type" : "list", "default" : []}, |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 929 | "platform_exclude" : {"type" : "set"}, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 930 | "platform_whitelist" : {"type" : "set"}, |
Anas Nashif | b17e1ca | 2017-06-27 18:05:30 -0400 | [diff] [blame] | 931 | "toolchain_exclude" : {"type" : "set"}, |
| 932 | "toolchain_whitelist" : {"type" : "set"}, |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 933 | "filter" : {"type" : "str"}} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 934 | |
| 935 | |
| 936 | class SanityConfigParser: |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 937 | """Class to read test case files with semantic checking |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 938 | """ |
| 939 | def __init__(self, filename): |
| 940 | """Instantiate a new SanityConfigParser object |
| 941 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 942 | @param filename Source .yaml file to read |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 943 | """ |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 944 | with open(filename, 'r') as stream: |
| 945 | cp = yaml.load(stream) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 946 | self.filename = filename |
| 947 | self.cp = cp |
| 948 | |
| 949 | def _cast_value(self, value, typestr): |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 950 | |
| 951 | if type(value) is str: |
| 952 | v = value.strip() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 953 | if typestr == "str": |
| 954 | return v |
| 955 | |
| 956 | elif typestr == "float": |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 957 | return float(value) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 958 | |
| 959 | elif typestr == "int": |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 960 | return int(value) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 961 | |
| 962 | elif typestr == "bool": |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 963 | return value |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 964 | |
| 965 | elif typestr.startswith("list"): |
| 966 | vs = v.split() |
| 967 | if len(typestr) > 4 and typestr[4] == ":": |
| 968 | return [self._cast_value(vsi, typestr[5:]) for vsi in vs] |
| 969 | else: |
| 970 | return vs |
| 971 | |
| 972 | elif typestr.startswith("set"): |
| 973 | vs = v.split() |
| 974 | if len(typestr) > 3 and typestr[3] == ":": |
| 975 | return set([self._cast_value(vsi, typestr[4:]) for vsi in vs]) |
| 976 | else: |
| 977 | return set(vs) |
| 978 | |
| 979 | else: |
| 980 | raise ConfigurationError(self.filename, "unknown type '%s'" % value) |
| 981 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 982 | def section(self,name): |
| 983 | for s in self.sections(): |
| 984 | if name in s: |
| 985 | return s.get(name, {}) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 986 | |
| 987 | def sections(self): |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 988 | """Get the set of test sections within the .yaml file |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 989 | |
| 990 | @return a list of string section names""" |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 991 | return self.cp['tests'] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 992 | |
| 993 | def get_section(self, section, valid_keys): |
| 994 | """Get a dictionary representing the keys/values within a section |
| 995 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 996 | @param section The section in the .yaml file to retrieve data from |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 997 | @param valid_keys A dictionary representing the intended semantics |
| 998 | for this section. Each key in this dictionary is a key that could |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 999 | be specified, if a key is given in the .yaml file which isn't in |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1000 | here, it will generate an error. Each value in this dictionary |
| 1001 | is another dictionary containing metadata: |
| 1002 | |
| 1003 | "default" - Default value if not given |
| 1004 | "type" - Data type to convert the text value to. Simple types |
| 1005 | supported are "str", "float", "int", "bool" which will get |
| 1006 | converted to respective Python data types. "set" and "list" |
| 1007 | may also be specified which will split the value by |
| 1008 | whitespace (but keep the elements as strings). finally, |
| 1009 | "list:<type>" and "set:<type>" may be given which will |
| 1010 | perform a type conversion after splitting the value up. |
| 1011 | "required" - If true, raise an error if not defined. If false |
| 1012 | and "default" isn't specified, a type conversion will be |
| 1013 | done on an empty string |
| 1014 | @return A dictionary containing the section key-value pairs with |
| 1015 | type conversion and default values filled in per valid_keys |
| 1016 | """ |
| 1017 | |
| 1018 | d = {} |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1019 | for k, v in self.section(section).items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1020 | if k not in valid_keys: |
| 1021 | raise ConfigurationError(self.filename, |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1022 | "Unknown config key '%s' in definition for '%s'" |
| 1023 | % (k, section)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1024 | d[k] = v |
| 1025 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1026 | for k, kinfo in valid_keys.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1027 | if k not in d: |
| 1028 | if "required" in kinfo: |
| 1029 | required = kinfo["required"] |
| 1030 | else: |
| 1031 | required = False |
| 1032 | |
| 1033 | if required: |
| 1034 | raise ConfigurationError(self.filename, |
| 1035 | "missing required value for '%s' in section '%s'" |
| 1036 | % (k, section)) |
| 1037 | else: |
| 1038 | if "default" in kinfo: |
| 1039 | default = kinfo["default"] |
| 1040 | else: |
| 1041 | default = self._cast_value("", kinfo["type"]) |
| 1042 | d[k] = default |
| 1043 | else: |
| 1044 | try: |
| 1045 | d[k] = self._cast_value(d[k], kinfo["type"]) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1046 | except ValueError as ve: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1047 | raise ConfigurationError(self.filename, |
| 1048 | "bad %s value '%s' for key '%s' in section '%s'" |
| 1049 | % (kinfo["type"], d[k], k, section)) |
| 1050 | |
| 1051 | return d |
| 1052 | |
| 1053 | |
| 1054 | class Platform: |
| 1055 | """Class representing metadata for a particular platform |
| 1056 | |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1057 | Maps directly to BOARD when building""" |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1058 | def __init__(self, cfile): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1059 | """Constructor. |
| 1060 | |
| 1061 | @param arch Architecture object for this platform |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1062 | @param name String name for this platform, same as BOARD |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1063 | @param plat_dict SanityConfigParser output on the relevant section |
| 1064 | in the architecture configuration file which has lots of metadata. |
| 1065 | See the Architecture class. |
| 1066 | """ |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1067 | scp = SanityConfigParser(cfile) |
| 1068 | cp = scp.cp |
| 1069 | |
| 1070 | self.name = cp['identifier'] |
| 1071 | # if no RAM size is specified by the board, take a default of 128K |
| 1072 | self.ram = cp.get("ram", 128) |
| 1073 | testing = cp.get("testing", {}) |
| 1074 | self.ignore_tags = testing.get("ignore_tags", []) |
| 1075 | self.default = testing.get("default", False) |
| 1076 | # if no flash size is specified by the board, take a default of 512K |
| 1077 | self.flash = cp.get("flash", 512) |
| 1078 | self.supported = set(cp.get("supported", [])) |
| 1079 | self.qemu_support = True if cp.get('type', "na") == 'qemu' else False |
| 1080 | self.arch = cp['arch'] |
| 1081 | self.supported_toolchains = cp.get("toolchain", []) |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1082 | self.defconfig = None |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1083 | pass |
| 1084 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1085 | def __repr__(self): |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1086 | return "<%s on %s>" % (self.name, self.arch) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1087 | |
| 1088 | |
| 1089 | class Architecture: |
| 1090 | """Class representing metadata for a particular architecture |
| 1091 | """ |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1092 | def __init__(self, name, platforms): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1093 | """Architecture constructor |
| 1094 | |
| 1095 | @param cfile Path to Architecture configuration file, which gives |
| 1096 | info about the arch and all the platforms for it |
| 1097 | """ |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1098 | self.platforms = platforms |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1099 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1100 | self.name = name |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1101 | |
| 1102 | def __repr__(self): |
| 1103 | return "<arch %s>" % self.name |
| 1104 | |
| 1105 | |
| 1106 | class TestCase: |
| 1107 | """Class representing a test application |
| 1108 | """ |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1109 | def __init__(self, testcase_root, workdir, name, tc_dict, inifile): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1110 | """TestCase constructor. |
| 1111 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1112 | This gets called by TestSuite as it finds and reads test yaml files. |
| 1113 | Multiple TestCase instances may be generated from a single testcase.yaml, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1114 | each one corresponds to a section within that file. |
| 1115 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1116 | We need to have a unique name for every single test case. Since |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1117 | a testcase.yaml can define multiple tests, the canonical name for |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1118 | the test case is <workdir>/<name>. |
| 1119 | |
| 1120 | @param testcase_root Absolute path to the root directory where |
| 1121 | all the test cases live |
| 1122 | @param workdir Relative path to the project directory for this |
| 1123 | test application from the test_case root. |
| 1124 | @param name Name of this test case, corresponding to the section name |
| 1125 | in the test case configuration file. For many test cases that just |
| 1126 | define one test, can be anything and is usually "test". This is |
| 1127 | really only used to distinguish between different cases when |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1128 | the testcase.yaml defines multiple tests |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1129 | @param tc_dict Dictionary with section values for this test case |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1130 | from the testcase.yaml file |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1131 | """ |
| 1132 | self.code_location = os.path.join(testcase_root, workdir) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1133 | self.type = tc_dict["type"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1134 | self.tags = tc_dict["tags"] |
| 1135 | self.extra_args = tc_dict["extra_args"] |
| 1136 | self.arch_whitelist = tc_dict["arch_whitelist"] |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1137 | self.arch_exclude = tc_dict["arch_exclude"] |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1138 | self.skip = tc_dict["skip"] |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1139 | self.platform_exclude = tc_dict["platform_exclude"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1140 | self.platform_whitelist = tc_dict["platform_whitelist"] |
Anas Nashif | b17e1ca | 2017-06-27 18:05:30 -0400 | [diff] [blame] | 1141 | self.toolchain_exclude = tc_dict["toolchain_exclude"] |
| 1142 | self.toolchain_whitelist = tc_dict["toolchain_whitelist"] |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1143 | self.tc_filter = tc_dict["filter"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1144 | self.timeout = tc_dict["timeout"] |
| 1145 | self.build_only = tc_dict["build_only"] |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1146 | self.build_on_all = tc_dict["build_on_all"] |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1147 | self.slow = tc_dict["slow"] |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1148 | self.min_ram = tc_dict["min_ram"] |
| 1149 | self.depends_on = tc_dict["depends_on"] |
| 1150 | self.min_flash = tc_dict["min_flash"] |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1151 | self.extra_sections = tc_dict["extra_sections"] |
Andrew Boie | 14ac902 | 2016-04-08 13:31:53 -0700 | [diff] [blame] | 1152 | self.path = os.path.join(os.path.basename(os.path.abspath(testcase_root)), |
| 1153 | workdir, name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1154 | self.name = self.path # for now |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1155 | self.defconfig = {} |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1156 | self.inifile = inifile |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1157 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1158 | def __repr__(self): |
| 1159 | return self.name |
| 1160 | |
| 1161 | |
| 1162 | |
| 1163 | class TestInstance: |
| 1164 | """Class representing the execution of a particular TestCase on a platform |
| 1165 | |
| 1166 | @param test The TestCase object we want to build/execute |
| 1167 | @param platform Platform object that we want to build and run against |
| 1168 | @param base_outdir Base directory for all test results. The actual |
| 1169 | out directory used is <outdir>/<platform>/<test case name> |
| 1170 | """ |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1171 | def __init__(self, test, platform, base_outdir, build_only=False, |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1172 | slow=False, coverage=False): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1173 | self.test = test |
| 1174 | self.platform = platform |
| 1175 | self.name = os.path.join(platform.name, test.path) |
| 1176 | self.outdir = os.path.join(base_outdir, platform.name, test.path) |
| 1177 | self.build_only = build_only or test.build_only |
| 1178 | |
| 1179 | def calculate_sizes(self): |
| 1180 | """Get the RAM/ROM sizes of a test case. |
| 1181 | |
| 1182 | This can only be run after the instance has been executed by |
| 1183 | MakeGenerator, otherwise there won't be any binaries to measure. |
| 1184 | |
| 1185 | @return A SizeCalculator object |
| 1186 | """ |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1187 | fns = glob.glob(os.path.join(self.outdir, "*.elf")) |
Anas Nashif | f8dcad4 | 2016-10-27 18:10:08 -0400 | [diff] [blame] | 1188 | fns = [x for x in fns if not x.endswith('_prebuilt.elf')] |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1189 | if (len(fns) != 1): |
| 1190 | raise BuildError("Missing/multiple output ELF binary") |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1191 | return SizeCalculator(fns[0], self.test.extra_sections) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1192 | |
| 1193 | def __repr__(self): |
| 1194 | return "<TestCase %s on %s>" % (self.test.name, self.platform.name) |
| 1195 | |
| 1196 | |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1197 | def defconfig_cb(context, goals, goal): |
| 1198 | if not goal.failed: |
| 1199 | return |
| 1200 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1201 | |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1202 | info("%sCould not build defconfig for %s%s" % |
| 1203 | (COLOR_RED, goal.name, COLOR_NORMAL)); |
| 1204 | if INLINE_LOGS: |
| 1205 | with open(goal.get_error_log()) as fp: |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1206 | data = fp.read() |
| 1207 | sys.stdout.write(data) |
| 1208 | if log_file: |
| 1209 | log_file.write(data) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1210 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1211 | info("\tsee: " + COLOR_YELLOW + goal.get_error_log() + COLOR_NORMAL) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1212 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1213 | |
| 1214 | class TestSuite: |
Andrew Boie | 60823c2 | 2017-02-10 09:43:07 -0800 | [diff] [blame] | 1215 | config_re = re.compile('(CONFIG_[A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1216 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1217 | def __init__(self, arch_root, testcase_roots, outdir, coverage): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1218 | # Keep track of which test cases we've filtered out and why |
| 1219 | discards = {} |
| 1220 | self.arches = {} |
| 1221 | self.testcases = {} |
| 1222 | self.platforms = [] |
Andrew Boie | b391e66 | 2015-08-31 15:25:45 -0700 | [diff] [blame] | 1223 | self.outdir = os.path.abspath(outdir) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1224 | self.instances = {} |
| 1225 | self.goals = None |
| 1226 | self.discards = None |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1227 | self.coverage = coverage |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1228 | |
| 1229 | arch_root = os.path.abspath(arch_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1230 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1231 | for testcase_root in testcase_roots: |
| 1232 | testcase_root = os.path.abspath(testcase_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1233 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1234 | debug("Reading test case configuration files under %s..." % |
| 1235 | testcase_root) |
| 1236 | for dirpath, dirnames, filenames in os.walk(testcase_root, |
| 1237 | topdown=True): |
| 1238 | verbose("scanning %s" % dirpath) |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1239 | if "sample.yaml" in filenames or "testcase.yaml" in filenames: |
| 1240 | verbose("Found possible test case in " + dirpath) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1241 | dirnames[:] = [] |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1242 | if "sample.yaml" in filenames: |
| 1243 | yaml_path = os.path.join(dirpath, "sample.yaml") |
| 1244 | else: |
| 1245 | yaml_path = os.path.join(dirpath, "testcase.yaml") |
| 1246 | cp = SanityConfigParser(yaml_path) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1247 | workdir = os.path.relpath(dirpath, testcase_root) |
| 1248 | |
| 1249 | for section in cp.sections(): |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1250 | name = list(section.keys())[0] |
| 1251 | tc_dict = cp.get_section(name, testcase_valid_keys) |
| 1252 | tc = TestCase(testcase_root, workdir, name, tc_dict, |
| 1253 | yaml_path) |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1254 | self.testcases[tc.name] = tc |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1255 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1256 | debug("Reading platform configuration files under %s..." % arch_root) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1257 | for dirpath, dirnames, filenames in os.walk(arch_root): |
| 1258 | for filename in filenames: |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1259 | if filename.endswith(".yaml"): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1260 | fn = os.path.join(dirpath, filename) |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1261 | verbose("Found plaform configuration " + fn) |
| 1262 | platform = Platform(fn) |
| 1263 | self.platforms.append(platform) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1264 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1265 | arches = [] |
| 1266 | for p in self.platforms: |
| 1267 | arches.append(p.arch) |
| 1268 | for a in list(set(arches)): |
| 1269 | aplatforms = [ p for p in self.platforms if p.arch == a ] |
| 1270 | arch = Architecture(a, aplatforms) |
| 1271 | self.arches[a] = arch |
| 1272 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1273 | self.instances = {} |
| 1274 | |
| 1275 | def get_last_failed(self): |
| 1276 | if not os.path.exists(LAST_SANITY): |
Anas Nashif | d9616b9 | 2016-11-29 13:34:53 -0500 | [diff] [blame] | 1277 | raise SanityRuntimeError("Couldn't find last sanity run.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1278 | result = [] |
| 1279 | with open(LAST_SANITY, "r") as fp: |
| 1280 | cr = csv.DictReader(fp) |
| 1281 | for row in cr: |
| 1282 | if row["passed"] == "True": |
| 1283 | continue |
| 1284 | test = row["test"] |
| 1285 | platform = row["platform"] |
| 1286 | result.append((test, platform)) |
| 1287 | return result |
| 1288 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1289 | def apply_filters(self, platform_filter, arch_filter, tag_filter, exclude_tag, |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1290 | config_filter, testcase_filter, last_failed, all_plats, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1291 | platform_limit, toolchain, extra_args, enable_ccache): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1292 | instances = [] |
| 1293 | discards = {} |
| 1294 | verbose("platform filter: " + str(platform_filter)) |
| 1295 | verbose(" arch_filter: " + str(arch_filter)) |
| 1296 | verbose(" tag_filter: " + str(tag_filter)) |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1297 | verbose(" exclude_tag: " + str(exclude_tag)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1298 | verbose(" config_filter: " + str(config_filter)) |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1299 | verbose(" enable_ccache: " + str(enable_ccache)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1300 | |
| 1301 | if last_failed: |
| 1302 | failed_tests = self.get_last_failed() |
| 1303 | |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1304 | default_platforms = False |
| 1305 | |
| 1306 | if all_plats: |
| 1307 | info("Selecting all possible platforms per test case") |
| 1308 | # When --all used, any --platform arguments ignored |
| 1309 | platform_filter = [] |
| 1310 | elif not platform_filter: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1311 | info("Selecting default platforms per test case") |
| 1312 | default_platforms = True |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1313 | |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1314 | mg = MakeGenerator(self.outdir, ccache=enable_ccache) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1315 | dlist = {} |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1316 | for tc_name, tc in self.testcases.items(): |
| 1317 | for arch_name, arch in self.arches.items(): |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1318 | for plat in arch.platforms: |
| 1319 | instance = TestInstance(tc, plat, self.outdir) |
| 1320 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1321 | if (arch_name == "unit") != (tc.type == "unit"): |
| 1322 | continue |
| 1323 | |
Anas Nashif | bfab06b | 2017-06-22 09:22:24 -0400 | [diff] [blame] | 1324 | if tc.build_on_all and not platform_filter: |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1325 | platform_filter = [] |
| 1326 | |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1327 | if tc.skip: |
| 1328 | continue |
| 1329 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1330 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 1331 | continue |
| 1332 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1333 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 1334 | continue |
| 1335 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1336 | if testcase_filter and tc_name not in testcase_filter: |
| 1337 | continue |
| 1338 | |
| 1339 | if last_failed and (tc.name, plat.name) not in failed_tests: |
| 1340 | continue |
| 1341 | |
| 1342 | if arch_filter and arch_name not in arch_filter: |
| 1343 | continue |
| 1344 | |
| 1345 | if tc.arch_whitelist and arch.name not in tc.arch_whitelist: |
| 1346 | continue |
| 1347 | |
| 1348 | if tc.arch_exclude and arch.name in tc.arch_exclude: |
| 1349 | continue |
| 1350 | |
| 1351 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 1352 | continue |
| 1353 | |
Anas Nashif | b17e1ca | 2017-06-27 18:05:30 -0400 | [diff] [blame] | 1354 | if tc.toolchain_exclude and toolchain in tc.toolchain_exclude: |
| 1355 | continue |
| 1356 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1357 | if platform_filter and plat.name not in platform_filter: |
| 1358 | continue |
| 1359 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1360 | if plat.ram <= tc.min_ram: |
| 1361 | continue |
| 1362 | |
| 1363 | if set(plat.ignore_tags) & tc.tags: |
| 1364 | continue |
| 1365 | |
| 1366 | if not tc.depends_on.issubset(set(plat.supported)): |
| 1367 | continue |
| 1368 | |
| 1369 | if plat.flash < tc.min_flash: |
| 1370 | continue |
| 1371 | |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1372 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 1373 | continue |
| 1374 | |
Anas Nashif | b17e1ca | 2017-06-27 18:05:30 -0400 | [diff] [blame] | 1375 | if tc.toolchain_whitelist and toolchain not in tc.toolchain_whitelist: |
| 1376 | continue |
| 1377 | |
Anas Nashif | cf21f5f | 2017-06-22 06:48:34 -0400 | [diff] [blame] | 1378 | if (tc.tc_filter and (plat.default or all_plats or platform_filter) and |
| 1379 | toolchain in plat.supported_toolchains): |
Anas Nashif | 8ea9d02 | 2015-11-10 12:24:20 -0500 | [diff] [blame] | 1380 | args = tc.extra_args[:] |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1381 | args.extend(["ARCH=" + plat.arch, |
Andy Gross | 25309ab | 2017-06-06 21:29:09 -0500 | [diff] [blame] | 1382 | "BOARD=" + plat.name, "config-sanitycheck"]) |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 1383 | args.extend(extra_args) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1384 | # FIXME would be nice to use a common outdir for this so that |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1385 | # conf, gen_idt, etc aren't rebuilt for every combination, |
David B. Kinder | 29963c3 | 2017-06-16 12:32:42 -0700 | [diff] [blame] | 1386 | # need a way to avoid different Make processes from clobbering |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1387 | # each other since they all try to build them simultaneously |
| 1388 | |
| 1389 | o = os.path.join(self.outdir, plat.name, tc.path) |
Andy Gross | 25309ab | 2017-06-06 21:29:09 -0500 | [diff] [blame] | 1390 | dlist[tc, plat, tc.name.split("/")[-1]] = os.path.join(o,".config-sanitycheck") |
| 1391 | goal = "_".join([plat.name, "_".join(tc.name.split("/")), "config-sanitycheck"]) |
Kumar Gala | 914d92e | 2017-06-22 16:19:11 -0500 | [diff] [blame] | 1392 | mg.add_build_goal(goal, os.path.join(ZEPHYR_BASE, tc.code_location), o, |
| 1393 | args, "config-sanitycheck.log") |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1394 | |
| 1395 | info("Building testcase defconfigs...") |
| 1396 | results = mg.execute(defconfig_cb) |
| 1397 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1398 | for name, goal in results.items(): |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1399 | if goal.failed: |
| 1400 | raise SanityRuntimeError("Couldn't build some defconfigs") |
| 1401 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1402 | for k, out_config in dlist.items(): |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1403 | test, plat, name = k |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1404 | defconfig = {} |
| 1405 | with open(out_config, "r") as fp: |
| 1406 | for line in fp.readlines(): |
| 1407 | m = TestSuite.config_re.match(line) |
| 1408 | if not m: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1409 | if line.strip() and not line.startswith("#"): |
| 1410 | sys.stderr.write("Unrecognized line %s\n" % line) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1411 | continue |
| 1412 | defconfig[m.group(1)] = m.group(2).strip() |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1413 | test.defconfig[plat] = defconfig |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1414 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1415 | for tc_name, tc in self.testcases.items(): |
| 1416 | for arch_name, arch in self.arches.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1417 | instance_list = [] |
| 1418 | for plat in arch.platforms: |
| 1419 | instance = TestInstance(tc, plat, self.outdir) |
| 1420 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 1421 | if (arch_name == "unit") != (tc.type == "unit"): |
| 1422 | # Discard silently |
| 1423 | continue |
| 1424 | |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 1425 | if tc.skip: |
| 1426 | discards[instance] = "Skip filter" |
| 1427 | continue |
| 1428 | |
Anas Nashif | bfab06b | 2017-06-22 09:22:24 -0400 | [diff] [blame] | 1429 | if tc.build_on_all and not platform_filter: |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1430 | platform_filter = [] |
| 1431 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1432 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 1433 | discards[instance] = "Command line testcase tag filter" |
| 1434 | continue |
| 1435 | |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1436 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 1437 | discards[instance] = "Command line testcase exclude filter" |
| 1438 | continue |
| 1439 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1440 | if testcase_filter and tc_name not in testcase_filter: |
| 1441 | discards[instance] = "Testcase name filter" |
| 1442 | continue |
| 1443 | |
| 1444 | if last_failed and (tc.name, plat.name) not in failed_tests: |
| 1445 | discards[instance] = "Passed or skipped during last run" |
| 1446 | continue |
| 1447 | |
| 1448 | if arch_filter and arch_name not in arch_filter: |
| 1449 | discards[instance] = "Command line testcase arch filter" |
| 1450 | continue |
| 1451 | |
| 1452 | if tc.arch_whitelist and arch.name not in tc.arch_whitelist: |
| 1453 | discards[instance] = "Not in test case arch whitelist" |
| 1454 | continue |
| 1455 | |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 1456 | if tc.arch_exclude and arch.name in tc.arch_exclude: |
| 1457 | discards[instance] = "In test case arch exclude" |
| 1458 | continue |
| 1459 | |
| 1460 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 1461 | discards[instance] = "In test case platform exclude" |
| 1462 | continue |
| 1463 | |
Anas Nashif | b17e1ca | 2017-06-27 18:05:30 -0400 | [diff] [blame] | 1464 | if tc.toolchain_exclude and toolchain in tc.toolchain_exclude: |
| 1465 | discards[instance] = "In test case toolchain exclude" |
| 1466 | continue |
| 1467 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1468 | if platform_filter and plat.name not in platform_filter: |
| 1469 | discards[instance] = "Command line platform filter" |
| 1470 | continue |
| 1471 | |
| 1472 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 1473 | discards[instance] = "Not in testcase platform whitelist" |
| 1474 | continue |
| 1475 | |
Anas Nashif | b17e1ca | 2017-06-27 18:05:30 -0400 | [diff] [blame] | 1476 | if tc.toolchain_whitelist and toolchain not in tc.toolchain_whitelist: |
| 1477 | discards[instance] = "Not in testcase toolchain whitelist" |
| 1478 | continue |
| 1479 | |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1480 | if toolchain and toolchain not in plat.supported_toolchains: |
| 1481 | discards[instance] = "Not supported by the toolchain" |
| 1482 | continue |
| 1483 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1484 | if plat.ram <= tc.min_ram: |
| 1485 | discards[instance] = "Not enough RAM" |
| 1486 | continue |
| 1487 | |
| 1488 | if not tc.depends_on.issubset(set(plat.supported)): |
| 1489 | discards[instance] = "No hardware support" |
| 1490 | continue |
| 1491 | |
| 1492 | if plat.flash< tc.min_flash: |
| 1493 | discards[instance] = "Not enough FLASH" |
| 1494 | continue |
| 1495 | |
| 1496 | if set(plat.ignore_tags) & tc.tags: |
| 1497 | discards[instance] = "Excluded tags per platform" |
| 1498 | continue |
| 1499 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1500 | defconfig = {"ARCH" : arch.name, "PLATFORM" : plat.name} |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 1501 | defconfig.update(os.environ) |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1502 | for p, tdefconfig in tc.defconfig.items(): |
| 1503 | if p == plat: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1504 | defconfig.update(tdefconfig) |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1505 | break |
| 1506 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1507 | if tc.tc_filter: |
| 1508 | try: |
| 1509 | res = expr_parser.parse(tc.tc_filter, defconfig) |
Andrew Boie | c09b4b8 | 2017-04-18 11:46:07 -0700 | [diff] [blame] | 1510 | except (ValueError, SyntaxError) as se: |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 1511 | sys.stderr.write("Failed processing %s\n" % tc.inifile) |
| 1512 | raise se |
| 1513 | if not res: |
| 1514 | discards[instance] = ("defconfig doesn't satisfy expression '%s'" % |
| 1515 | tc.tc_filter) |
| 1516 | continue |
Anas Nashif | 2cf0df0 | 2015-10-10 09:29:43 -0400 | [diff] [blame] | 1517 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1518 | instance_list.append(instance) |
| 1519 | |
| 1520 | if not instance_list: |
| 1521 | # Every platform in this arch was rejected already |
| 1522 | continue |
| 1523 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1524 | if default_platforms and not tc.build_on_all: |
| 1525 | if not tc.platform_whitelist: |
| 1526 | instances = list(filter(lambda tc: tc.platform.default, instance_list)) |
| 1527 | self.add_instances(instances) |
| 1528 | else: |
| 1529 | self.add_instances(instance_list[:platform_limit]) |
| 1530 | |
| 1531 | for instance in list(filter(lambda tc: not tc.platform.default, instance_list)): |
| 1532 | discards[instance] = "Not a default test platform" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1533 | else: |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1534 | self.add_instances(instance_list) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1535 | self.discards = discards |
| 1536 | return discards |
| 1537 | |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1538 | def add_instances(self, ti_list): |
| 1539 | for ti in ti_list: |
| 1540 | self.instances[ti.name] = ti |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1541 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1542 | def execute(self, cb, cb_context, build_only, enable_slow, enable_asserts, enable_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1543 | extra_args, enable_ccache): |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1544 | |
| 1545 | def calc_one_elf_size(name, goal): |
| 1546 | if not goal.failed: |
| 1547 | i = self.instances[name] |
| 1548 | sc = i.calculate_sizes() |
| 1549 | goal.metrics["ram_size"] = sc.get_ram_size() |
| 1550 | goal.metrics["rom_size"] = sc.get_rom_size() |
| 1551 | goal.metrics["unrecognized"] = sc.unrecognized_sections() |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1552 | |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1553 | mg = MakeGenerator(self.outdir, asserts=enable_asserts, deprecations=enable_deprecations, |
| 1554 | ccache=enable_ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1555 | for i in self.instances.values(): |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1556 | mg.add_test_instance(i, build_only, enable_slow, self.coverage, extra_args) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1557 | self.goals = mg.execute(cb, cb_context) |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1558 | |
| 1559 | # Parallelize size calculation |
| 1560 | executor = concurrent.futures.ThreadPoolExecutor(CPU_COUNTS) |
| 1561 | futures = [executor.submit(calc_one_elf_size, name, goal) \ |
| 1562 | for name, goal in self.goals.items()] |
| 1563 | concurrent.futures.wait(futures) |
| 1564 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1565 | return self.goals |
| 1566 | |
| 1567 | def discard_report(self, filename): |
| 1568 | if self.discards == None: |
| 1569 | raise SanityRuntimeException("apply_filters() hasn't been run!") |
| 1570 | |
| 1571 | with open(filename, "wb") as csvfile: |
| 1572 | fieldnames = ["test", "arch", "platform", "reason"] |
| 1573 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 1574 | cw.writeheader() |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1575 | for instance, reason in self.discards.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1576 | rowdict = {"test" : i.test.name, |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1577 | "arch" : i.platform.arch, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1578 | "platform" : i.platform.name, |
| 1579 | "reason" : reason} |
| 1580 | cw.writerow(rowdict) |
| 1581 | |
| 1582 | def compare_metrics(self, filename): |
| 1583 | # name, datatype, lower results better |
| 1584 | interesting_metrics = [("ram_size", int, True), |
| 1585 | ("rom_size", int, True)] |
| 1586 | |
| 1587 | if self.goals == None: |
| 1588 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1589 | |
| 1590 | if not os.path.exists(filename): |
| 1591 | info("Cannot compare metrics, %s not found" % filename) |
| 1592 | return [] |
| 1593 | |
| 1594 | results = [] |
| 1595 | saved_metrics = {} |
| 1596 | with open(filename) as fp: |
| 1597 | cr = csv.DictReader(fp) |
| 1598 | for row in cr: |
| 1599 | d = {} |
| 1600 | for m, _, _ in interesting_metrics: |
| 1601 | d[m] = row[m] |
| 1602 | saved_metrics[(row["test"], row["platform"])] = d |
| 1603 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1604 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1605 | i = self.instances[name] |
| 1606 | mkey = (i.test.name, i.platform.name) |
| 1607 | if mkey not in saved_metrics: |
| 1608 | continue |
| 1609 | sm = saved_metrics[mkey] |
| 1610 | for metric, mtype, lower_better in interesting_metrics: |
| 1611 | if metric not in goal.metrics: |
| 1612 | continue |
| 1613 | if sm[metric] == "": |
| 1614 | continue |
| 1615 | delta = goal.metrics[metric] - mtype(sm[metric]) |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1616 | if delta == 0: |
| 1617 | continue |
| 1618 | results.append((i, metric, goal.metrics[metric], delta, |
| 1619 | lower_better)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1620 | return results |
| 1621 | |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 1622 | def testcase_xunit_report(self, filename, duration, args): |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1623 | if self.goals == None: |
| 1624 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1625 | |
| 1626 | fails = 0 |
| 1627 | passes = 0 |
| 1628 | errors = 0 |
| 1629 | |
| 1630 | for name, goal in self.goals.items(): |
| 1631 | if goal.failed: |
| 1632 | if goal.reason in ['build_error', 'qemu_crash']: |
| 1633 | errors += 1 |
| 1634 | else: |
| 1635 | fails += 1 |
| 1636 | else: |
| 1637 | passes += 1 |
| 1638 | |
| 1639 | run = "Sanitycheck" |
| 1640 | eleTestsuite = None |
| 1641 | append = args.only_failed |
| 1642 | |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 1643 | if os.path.exists(filename) and append: |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1644 | tree = ET.parse(filename) |
| 1645 | eleTestsuites = tree.getroot() |
| 1646 | eleTestsuite = tree.findall('testsuite')[0]; |
| 1647 | else: |
| 1648 | eleTestsuites = ET.Element('testsuites') |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 1649 | eleTestsuite = ET.SubElement(eleTestsuites, 'testsuite', name=run, time="%d" %duration, |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1650 | tests="%d" %(errors + passes + fails), failures="%d" %fails, errors="%d" %errors, skip="0") |
| 1651 | |
| 1652 | qemu_time = "0" |
| 1653 | for name, goal in self.goals.items(): |
| 1654 | |
| 1655 | i = self.instances[name] |
| 1656 | if append: |
| 1657 | for tc in eleTestsuite.findall('testcase'): |
Anas Nashif | 202d130 | 2017-05-07 08:19:20 -0400 | [diff] [blame] | 1658 | if tc.get('classname') == "%s:%s" %(i.platform.name, i.test.name): |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1659 | eleTestsuite.remove(tc) |
| 1660 | |
| 1661 | if not goal.failed and goal.qemu: |
| 1662 | qemu_time = "%s" %(goal.metrics["qemu_time"]) |
| 1663 | |
Anas Nashif | 202d130 | 2017-05-07 08:19:20 -0400 | [diff] [blame] | 1664 | eleTestcase = ET.SubElement(eleTestsuite, 'testcase', classname="%s:%s" %(i.platform.name, i.test.name), name="%s" %(name), time=qemu_time) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1665 | if goal.failed: |
| 1666 | failure = ET.SubElement(eleTestcase, 'failure', type="failure", message=goal.reason) |
| 1667 | p = ("%s/%s/%s" %(args.outdir, i.platform.name, i.test.name)) |
| 1668 | bl = os.path.join(p, "build.log") |
Anas Nashif | accc8eb | 2017-05-01 16:33:43 -0400 | [diff] [blame] | 1669 | if goal.reason != 'build_error': |
| 1670 | bl = os.path.join(p, "qemu.log") |
| 1671 | |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1672 | if os.path.exists(bl): |
| 1673 | with open(bl, "r") as f: |
| 1674 | log = f.read() |
Anas Nashif | e6fcc01 | 2017-05-17 09:29:09 -0400 | [diff] [blame] | 1675 | ansi_escape = re.compile(r'\x1b[^m]*m') |
| 1676 | output = ansi_escape.sub('', str(log)) |
| 1677 | failure.text = (escape(output)) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 1678 | |
| 1679 | result = ET.tostring(eleTestsuites) |
| 1680 | f = open(filename, 'wb') |
| 1681 | f.write(result) |
| 1682 | f.close() |
| 1683 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1684 | def testcase_report(self, filename): |
| 1685 | if self.goals == None: |
| 1686 | raise SanityRuntimeException("execute() hasn't been run!") |
| 1687 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1688 | with open(filename, "wt") as csvfile: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1689 | fieldnames = ["test", "arch", "platform", "passed", "status", |
| 1690 | "extra_args", "qemu", "qemu_time", "ram_size", |
| 1691 | "rom_size"] |
| 1692 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 1693 | cw.writeheader() |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1694 | for name, goal in self.goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1695 | i = self.instances[name] |
| 1696 | rowdict = {"test" : i.test.name, |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1697 | "arch" : i.platform.arch, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1698 | "platform" : i.platform.name, |
| 1699 | "extra_args" : " ".join(i.test.extra_args), |
| 1700 | "qemu" : i.platform.qemu_support} |
| 1701 | if goal.failed: |
| 1702 | rowdict["passed"] = False |
| 1703 | rowdict["status"] = goal.reason |
| 1704 | else: |
| 1705 | rowdict["passed"] = True |
| 1706 | if goal.qemu: |
| 1707 | rowdict["qemu_time"] = goal.metrics["qemu_time"] |
| 1708 | rowdict["ram_size"] = goal.metrics["ram_size"] |
| 1709 | rowdict["rom_size"] = goal.metrics["rom_size"] |
| 1710 | cw.writerow(rowdict) |
| 1711 | |
| 1712 | |
| 1713 | def parse_arguments(): |
| 1714 | |
| 1715 | parser = argparse.ArgumentParser(description = __doc__, |
| 1716 | formatter_class = argparse.RawDescriptionHelpFormatter) |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 1717 | parser.fromfile_prefix_chars = "+" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1718 | |
| 1719 | parser.add_argument("-p", "--platform", action="append", |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1720 | help="Platform filter for testing. This option may be used multiple " |
| 1721 | "times. Testcases will only be built/run on the platforms " |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1722 | "specified. If this option is not used, then platforms marked " |
| 1723 | "as default in the platform metadata file will be chosen " |
| 1724 | "to build and test. ") |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1725 | parser.add_argument("-L", "--platform-limit", action="store", type=int, |
| 1726 | metavar="N", default=1, |
| 1727 | help="Controls what platforms are tested if --platform or " |
| 1728 | "--all are not used. For each architecture specified by " |
| 1729 | "--arch (defaults to all of them), choose the first " |
| 1730 | "N platforms to test in the arch-specific .ini file " |
| 1731 | "'platforms' list. Defaults to 1.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1732 | parser.add_argument("-a", "--arch", action="append", |
| 1733 | help="Arch filter for testing. Takes precedence over --platform. " |
| 1734 | "If unspecified, test all arches. Multiple invocations " |
| 1735 | "are treated as a logical 'or' relationship") |
| 1736 | parser.add_argument("-t", "--tag", action="append", |
| 1737 | help="Specify tags to restrict which tests to run by tag value. " |
| 1738 | "Default is to not do any tag filtering. Multiple invocations " |
| 1739 | "are treated as a logical 'or' relationship") |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1740 | parser.add_argument("-e", "--exclude-tag", action="append", |
Paul Sokolovsky | ff70add | 2017-06-16 01:31:54 +0300 | [diff] [blame] | 1741 | help="Specify tags of tests that should not run. " |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1742 | "Default is to run all tests with all tags.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1743 | parser.add_argument("-f", "--only-failed", action="store_true", |
| 1744 | help="Run only those tests that failed the previous sanity check " |
| 1745 | "invocation.") |
| 1746 | parser.add_argument("-c", "--config", action="append", |
| 1747 | help="Specify platform configuration values filtering. This can be " |
| 1748 | "specified two ways: <config>=<value> or just <config>. The " |
Andrew Boie | 4187822 | 2016-11-03 11:58:53 -0700 | [diff] [blame] | 1749 | "defconfig for all platforms will be " |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1750 | "checked. For the <config>=<value> case, only match defconfig " |
| 1751 | "that have that value defined. For the <config> case, match " |
| 1752 | "defconfig that have that value assigned to any value. " |
| 1753 | "Prepend a '!' to invert the match.") |
| 1754 | parser.add_argument("-s", "--test", action="append", |
| 1755 | help="Run only the specified test cases. These are named by " |
| 1756 | "<path to test project relative to " |
| 1757 | "--testcase-root>/<testcase.ini section name>") |
| 1758 | parser.add_argument("-l", "--all", action="store_true", |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1759 | help="Build/test on all platforms. Any --platform arguments " |
| 1760 | "ignored.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1761 | |
| 1762 | parser.add_argument("-o", "--testcase-report", |
| 1763 | help="Output a CSV spreadsheet containing results of the test run") |
| 1764 | parser.add_argument("-d", "--discard-report", |
David B. Kinder | 29963c3 | 2017-06-16 12:32:42 -0700 | [diff] [blame] | 1765 | help="Output a CSV spreadsheet showing tests that were skipped " |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1766 | "and why") |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 1767 | parser.add_argument("--compare-report", |
David B. Kinder | 29963c3 | 2017-06-16 12:32:42 -0700 | [diff] [blame] | 1768 | help="Use this report file for size comparison") |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 1769 | |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1770 | parser.add_argument("--ccache", action="store_const", const=1, default=0, |
| 1771 | help="Enable the use of ccache when building") |
| 1772 | |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 1773 | parser.add_argument("-B", "--subset", |
| 1774 | help="Only run a subset of the tests, 1/4 for running the first 25%%, " |
| 1775 | "3/5 means run the 3rd fifth of the total. " |
| 1776 | "This option is useful when running a large number of tests on " |
| 1777 | "different hosts to speed up execution time.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1778 | parser.add_argument("-y", "--dry-run", action="store_true", |
| 1779 | help="Create the filtered list of test cases, but don't actually " |
| 1780 | "run them. Useful if you're just interested in " |
| 1781 | "--discard-report") |
| 1782 | |
| 1783 | parser.add_argument("-r", "--release", action="store_true", |
| 1784 | help="Update the benchmark database with the results of this test " |
| 1785 | "run. Intended to be run by CI when tagging an official " |
| 1786 | "release. This database is used as a basis for comparison " |
| 1787 | "when looking for deltas in metrics such as footprint") |
| 1788 | parser.add_argument("-w", "--warnings-as-errors", action="store_true", |
| 1789 | help="Treat warning conditions as errors") |
| 1790 | parser.add_argument("-v", "--verbose", action="count", default=0, |
| 1791 | help="Emit debugging information, call multiple times to increase " |
| 1792 | "verbosity") |
| 1793 | parser.add_argument("-i", "--inline-logs", action="store_true", |
| 1794 | help="Upon test failure, print relevant log data to stdout " |
| 1795 | "instead of just a path to it") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1796 | parser.add_argument("--log-file", metavar="FILENAME", action="store", |
| 1797 | help="log also to file") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1798 | parser.add_argument("-m", "--last-metrics", action="store_true", |
| 1799 | help="Instead of comparing metrics from the last --release, " |
| 1800 | "compare with the results of the previous sanity check " |
| 1801 | "invocation") |
| 1802 | parser.add_argument("-u", "--no-update", action="store_true", |
| 1803 | help="do not update the results of the last run of the sanity " |
| 1804 | "checks") |
| 1805 | parser.add_argument("-b", "--build-only", action="store_true", |
| 1806 | help="Only build the code, do not execute any of it in QEMU") |
| 1807 | parser.add_argument("-j", "--jobs", type=int, |
| 1808 | help="Number of cores to use when building, defaults to " |
| 1809 | "number of CPUs * 2") |
| 1810 | parser.add_argument("-H", "--footprint-threshold", type=float, default=5, |
| 1811 | help="When checking test case footprint sizes, warn the user if " |
| 1812 | "the new app size is greater then the specified percentage " |
| 1813 | "from the last release. Default is 5. 0 to warn on any " |
| 1814 | "increase on app size") |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 1815 | parser.add_argument("-D", "--all-deltas", action="store_true", |
| 1816 | help="Show all footprint deltas, positive or negative. Implies " |
| 1817 | "--footprint-threshold=0") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1818 | parser.add_argument("-O", "--outdir", |
| 1819 | default="%s/sanity-out" % ZEPHYR_BASE, |
| 1820 | help="Output directory for logs and binaries.") |
Andrew Boie | ae9e7f7b | 2015-07-31 12:26:12 -0700 | [diff] [blame] | 1821 | parser.add_argument("-n", "--no-clean", action="store_true", |
| 1822 | help="Do not delete the outdir before building. Will result in " |
| 1823 | "faster compilation since builds will be incremental") |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1824 | parser.add_argument("-T", "--testcase-root", action="append", default=[], |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1825 | help="Base directory to recursively search for test cases. All " |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1826 | "testcase.ini files under here will be processed. May be " |
| 1827 | "called multiple times. Defaults to the 'samples' and " |
| 1828 | "'tests' directories in the Zephyr tree.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1829 | parser.add_argument("-A", "--arch-root", |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1830 | default="%s/boards" % ZEPHYR_BASE, |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1831 | help="Directory to search for arch configuration files. All .ini " |
| 1832 | "files in the directory will be processed.") |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1833 | parser.add_argument("-z", "--size", action="append", |
| 1834 | help="Don't run sanity checks. Instead, produce a report to " |
| 1835 | "stdout detailing RAM/ROM sizes on the specified filenames. " |
| 1836 | "All other command line arguments ignored.") |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 1837 | parser.add_argument("-S", "--enable-slow", action="store_true", |
| 1838 | help="Execute time-consuming test cases that have been marked " |
| 1839 | "as 'slow' in testcase.ini. Normally these are only built.") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 1840 | parser.add_argument("-R", "--enable-asserts", action="store_true", |
| 1841 | help="Build all test cases with assertions enabled.") |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 1842 | parser.add_argument("-Q", "--error-on-deprecations", action="store_false", |
| 1843 | help="Error on deprecation warnings.") |
Andrew Boie | ba61200 | 2016-09-01 10:41:03 -0700 | [diff] [blame] | 1844 | parser.add_argument("-x", "--extra-args", action="append", default=[], |
| 1845 | help="Extra arguments to pass to the build when compiling test " |
| 1846 | "cases. May be called multiple times. These will be passed " |
| 1847 | "in after any sanitycheck-supplied options.") |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1848 | parser.add_argument("-C", "--coverage", action="store_true", |
| 1849 | help="Scan for unit test coverage with gcov + lcov.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1850 | |
| 1851 | return parser.parse_args() |
| 1852 | |
| 1853 | def log_info(filename): |
Mazen NEIFER | 8f1dd3a | 2017-02-04 14:32:04 +0100 | [diff] [blame] | 1854 | filename = os.path.relpath(os.path.realpath(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1855 | if INLINE_LOGS: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1856 | info("{:-^100}".format(filename)) |
Andrew Boie | d01535c | 2017-01-10 13:15:02 -0800 | [diff] [blame] | 1857 | |
| 1858 | try: |
| 1859 | with open(filename) as fp: |
| 1860 | data = fp.read() |
| 1861 | except Exception as e: |
| 1862 | data = "Unable to read log data (%s)\n" % (str(e)) |
| 1863 | |
| 1864 | sys.stdout.write(data) |
| 1865 | if log_file: |
| 1866 | log_file.write(data) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1867 | info("{:-^100}".format(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1868 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1869 | info("\tsee: " + COLOR_YELLOW + filename + COLOR_NORMAL) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1870 | |
| 1871 | def terse_test_cb(instances, goals, goal): |
| 1872 | total_tests = len(goals) |
| 1873 | total_done = 0 |
| 1874 | total_failed = 0 |
| 1875 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1876 | for k, g in goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1877 | if g.finished: |
| 1878 | total_done += 1 |
| 1879 | if g.failed: |
| 1880 | total_failed += 1 |
| 1881 | |
| 1882 | if goal.failed: |
| 1883 | i = instances[goal.name] |
| 1884 | info("\n\n{:<25} {:<50} {}FAILED{}: {}".format(i.platform.name, |
| 1885 | i.test.name, COLOR_RED, COLOR_NORMAL, goal.reason)) |
| 1886 | log_info(goal.get_error_log()) |
| 1887 | info("") |
| 1888 | |
Andrew Boie | 7a992ae | 2017-01-17 10:40:02 -0800 | [diff] [blame] | 1889 | sys.stdout.write("\rtotal complete: %s%4d/%4d%s %2d%% failed: %s%4d%s" % ( |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1890 | COLOR_GREEN, total_done, total_tests, COLOR_NORMAL, |
Andrew Boie | 7a992ae | 2017-01-17 10:40:02 -0800 | [diff] [blame] | 1891 | int((float(total_done) / total_tests) * 100), |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1892 | COLOR_RED if total_failed > 0 else COLOR_NORMAL, |
| 1893 | total_failed, COLOR_NORMAL)) |
| 1894 | sys.stdout.flush() |
| 1895 | |
| 1896 | def chatty_test_cb(instances, goals, goal): |
| 1897 | i = instances[goal.name] |
| 1898 | |
| 1899 | if VERBOSE < 2 and not goal.finished: |
| 1900 | return |
| 1901 | |
| 1902 | if goal.failed: |
| 1903 | status = COLOR_RED + "FAILED" + COLOR_NORMAL + ": " + goal.reason |
| 1904 | elif goal.finished: |
| 1905 | status = COLOR_GREEN + "PASSED" + COLOR_NORMAL |
| 1906 | else: |
| 1907 | status = goal.make_state |
| 1908 | |
| 1909 | info("{:<25} {:<50} {}".format(i.platform.name, i.test.name, status)) |
| 1910 | if goal.failed: |
| 1911 | log_info(goal.get_error_log()) |
| 1912 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1913 | |
| 1914 | def size_report(sc): |
| 1915 | info(sc.filename) |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1916 | info("SECTION NAME VMA LMA SIZE HEX SZ TYPE") |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1917 | for i in range(len(sc.sections)): |
| 1918 | v = sc.sections[i] |
| 1919 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1920 | info("%-17s 0x%08x 0x%08x %8d 0x%05x %-7s" % |
| 1921 | (v["name"], v["virt_addr"], v["load_addr"], v["size"], v["size"], |
| 1922 | v["type"])) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1923 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1924 | info("Totals: %d bytes (ROM), %d bytes (RAM)" % |
| 1925 | (sc.rom_size, sc.ram_size)) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1926 | info("") |
| 1927 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1928 | def generate_coverage(outdir, ignores): |
| 1929 | with open(os.path.join(outdir, "coverage.log"), "a") as coveragelog: |
| 1930 | coveragefile = os.path.join(outdir, "coverage.info") |
| 1931 | ztestfile = os.path.join(outdir, "ztest.info") |
| 1932 | subprocess.call(["lcov", "--capture", "--directory", outdir, |
| 1933 | "--output-file", coveragefile], stdout=coveragelog) |
| 1934 | # We want to remove tests/* and tests/ztest/test/* but save tests/ztest |
| 1935 | subprocess.call(["lcov", "--extract", coveragefile, |
| 1936 | os.path.join(ZEPHYR_BASE, "tests", "ztest", "*"), |
| 1937 | "--output-file", ztestfile], stdout=coveragelog) |
| 1938 | subprocess.call(["lcov", "--remove", ztestfile, |
| 1939 | os.path.join(ZEPHYR_BASE, "tests/ztest/test/*"), |
| 1940 | "--output-file", ztestfile], stdout=coveragelog) |
| 1941 | for i in ignores: |
| 1942 | subprocess.call(["lcov", "--remove", coveragefile, i, |
| 1943 | "--output-file", coveragefile], stdout=coveragelog) |
| 1944 | subprocess.call(["genhtml", "-output-directory", |
| 1945 | os.path.join(outdir, "coverage"), |
| 1946 | coveragefile, ztestfile], stdout=coveragelog) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1947 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1948 | def main(): |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 1949 | start_time = time.time() |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1950 | global VERBOSE, INLINE_LOGS, CPU_COUNTS, log_file |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1951 | args = parse_arguments() |
Javier B Perez | 4b554ba | 2016-08-15 13:25:33 -0500 | [diff] [blame] | 1952 | toolchain = os.environ.get("ZEPHYR_GCC_VARIANT", None) |
Andrew Boie | 1e4e68b | 2017-02-10 11:40:54 -0800 | [diff] [blame] | 1953 | if toolchain == "zephyr": |
| 1954 | os.environ["DISABLE_TRYRUN"] = "1" |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1955 | |
| 1956 | if args.size: |
| 1957 | for fn in args.size: |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1958 | size_report(SizeCalculator(fn, [])) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1959 | sys.exit(0) |
| 1960 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1961 | VERBOSE += args.verbose |
| 1962 | INLINE_LOGS = args.inline_logs |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 1963 | if args.log_file: |
| 1964 | log_file = open(args.log_file, "w") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1965 | if args.jobs: |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 1966 | CPU_COUNTS = args.jobs |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1967 | |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 1968 | if args.subset: |
| 1969 | subset, sets = args.subset.split("/") |
| 1970 | if int(subset) > 0 and int(sets) >= int(subset): |
| 1971 | info("Running only a subset: %s/%s" %(subset,sets)) |
| 1972 | else: |
| 1973 | error("You have provided a wrong subset value: %s." %args.subset) |
| 1974 | return |
| 1975 | |
Andrew Boie | ae9e7f7b | 2015-07-31 12:26:12 -0700 | [diff] [blame] | 1976 | if os.path.exists(args.outdir) and not args.no_clean: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1977 | info("Cleaning output directory " + args.outdir) |
| 1978 | shutil.rmtree(args.outdir) |
| 1979 | |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 1980 | if not args.testcase_root: |
| 1981 | args.testcase_root = [os.path.join(ZEPHYR_BASE, "tests"), |
| 1982 | os.path.join(ZEPHYR_BASE, "samples")] |
| 1983 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 1984 | ts = TestSuite(args.arch_root, args.testcase_root, args.outdir, args.coverage) |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 1985 | discards = ts.apply_filters(args.platform, args.arch, args.tag, args.exclude_tag, args.config, |
Andrew Boie | 821d832 | 2016-03-22 10:08:35 -0700 | [diff] [blame] | 1986 | args.test, args.only_failed, args.all, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 1987 | args.platform_limit, toolchain, args.extra_args, args.ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1988 | |
| 1989 | if args.discard_report: |
| 1990 | ts.discard_report(args.discard_report) |
| 1991 | |
| 1992 | if VERBOSE: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1993 | for i, reason in discards.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1994 | debug("{:<25} {:<50} {}SKIPPED{}: {}".format(i.platform.name, |
| 1995 | i.test.name, COLOR_YELLOW, COLOR_NORMAL, reason)) |
| 1996 | |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 1997 | ts.instancets = OrderedDict(sorted(ts.instances.items(), key=lambda t: t[0])) |
| 1998 | |
| 1999 | if args.subset: |
| 2000 | subset, sets = args.subset.split("/") |
| 2001 | total = len(ts.instancets) |
| 2002 | per_set = round(total / int(sets)) |
| 2003 | start = (int(subset) - 1 ) * per_set |
| 2004 | if subset == sets: |
| 2005 | end = total |
| 2006 | else: |
| 2007 | end = start + per_set |
| 2008 | |
| 2009 | sliced_instances = islice(ts.instancets.items(),start, end) |
| 2010 | ts.instances = OrderedDict(sliced_instances) |
| 2011 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2012 | info("%d tests selected, %d tests discarded due to filters" % |
| 2013 | (len(ts.instances), len(discards))) |
| 2014 | |
| 2015 | if args.dry_run: |
| 2016 | return |
| 2017 | |
| 2018 | if VERBOSE or not TERMINAL: |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 2019 | goals = ts.execute(chatty_test_cb, ts.instances, args.build_only, |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 2020 | args.enable_slow, args.enable_asserts, args.error_on_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 2021 | args.extra_args, args.ccache) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2022 | else: |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 2023 | goals = ts.execute(terse_test_cb, ts.instances, args.build_only, |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 2024 | args.enable_slow, args.enable_asserts, args.error_on_deprecations, |
Kumar Gala | d5719a6 | 2016-10-26 12:30:26 -0500 | [diff] [blame] | 2025 | args.extra_args, args.ccache) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 2026 | info("") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2027 | |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 2028 | # figure out which report to use for size comparison |
| 2029 | if args.compare_report: |
| 2030 | report_to_use = args.compare_report |
| 2031 | elif args.last_metrics: |
| 2032 | report_to_use = LAST_SANITY |
| 2033 | else: |
| 2034 | report_to_use = RELEASE_DATA |
| 2035 | |
| 2036 | deltas = ts.compare_metrics(report_to_use) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2037 | warnings = 0 |
| 2038 | if deltas: |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 2039 | for i, metric, value, delta, lower_better in deltas: |
| 2040 | if not args.all_deltas and ((delta < 0 and lower_better) or |
| 2041 | (delta > 0 and not lower_better)): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2042 | continue |
| 2043 | |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 2044 | percentage = (float(delta) / float(value - delta)) |
| 2045 | if not args.all_deltas and (percentage < |
| 2046 | (args.footprint_threshold / 100.0)): |
| 2047 | continue |
| 2048 | |
Daniel Leung | 00525c2 | 2016-04-11 10:27:56 -0700 | [diff] [blame] | 2049 | info("{:<25} {:<60} {}{}{}: {} {:<+4}, is now {:6} {:+.2%}".format( |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 2050 | i.platform.name, i.test.name, COLOR_YELLOW, |
| 2051 | "INFO" if args.all_deltas else "WARNING", COLOR_NORMAL, |
Andrew Boie | 829c056 | 2015-10-13 09:44:19 -0700 | [diff] [blame] | 2052 | metric, delta, value, percentage)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2053 | warnings += 1 |
| 2054 | |
| 2055 | if warnings: |
| 2056 | info("Deltas based on metrics from last %s" % |
| 2057 | ("release" if not args.last_metrics else "run")) |
| 2058 | |
| 2059 | failed = 0 |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 2060 | for name, goal in goals.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2061 | if goal.failed: |
| 2062 | failed += 1 |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 2063 | elif goal.metrics.get("unrecognized"): |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 2064 | info("%sFAILED%s: %s has unrecognized binary sections: %s" % |
| 2065 | (COLOR_RED, COLOR_NORMAL, goal.name, |
| 2066 | str(goal.metrics["unrecognized"]))) |
| 2067 | failed += 1 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2068 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 2069 | if args.coverage: |
| 2070 | info("Generating coverage files...") |
| 2071 | generate_coverage(args.outdir, ["tests/*", "samples/*"]) |
| 2072 | |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 2073 | duration = time.time() - start_time |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 2074 | info("%s%d of %d%s tests passed with %s%d%s warnings in %d seconds" % |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2075 | (COLOR_RED if failed else COLOR_GREEN, len(goals) - failed, |
| 2076 | len(goals), COLOR_NORMAL, COLOR_YELLOW if warnings else COLOR_NORMAL, |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 2077 | warnings, COLOR_NORMAL, duration)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2078 | |
| 2079 | if args.testcase_report: |
| 2080 | ts.testcase_report(args.testcase_report) |
| 2081 | if not args.no_update: |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 2082 | ts.testcase_xunit_report(LAST_SANITY_XUNIT, duration, args) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2083 | ts.testcase_report(LAST_SANITY) |
| 2084 | if args.release: |
| 2085 | ts.testcase_report(RELEASE_DATA) |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 2086 | if log_file: |
| 2087 | log_file.close() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2088 | if failed or (warnings and args.warnings_as_errors): |
| 2089 | sys.exit(1) |
| 2090 | |
| 2091 | if __name__ == "__main__": |
| 2092 | main() |