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 : |
Anas Nashif | 3ae5262 | 2019-04-06 09:08:09 -0400 | [diff] [blame] | 3 | # SPDX-License-Identifier: Apache-2.0 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 4 | """Zephyr Sanity Tests |
| 5 | |
Marc Herbert | e5cedca | 2019-04-08 14:02:34 -0700 | [diff] [blame] | 6 | Also check the "User and Developer Guides" at https://docs.zephyrproject.org/ |
| 7 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 8 | This script scans for the set of unit test applications in the git |
| 9 | repository and attempts to execute them. By default, it tries to |
| 10 | build each test case on one platform per architecture, using a precedence |
Paul Sokolovsky | ff70add | 2017-06-16 01:31:54 +0300 | [diff] [blame] | 11 | list defined in an architecture configuration file, and if possible |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 12 | run the tests in any available emulators or simulators on the system. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 13 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 14 | Test cases are detected by the presence of a 'testcase.yaml' or a sample.yaml |
| 15 | files in the application's project directory. This file may contain one or more |
| 16 | blocks, each identifying a test scenario. The title of the block is a name for |
| 17 | the test case, which only needs to be unique for the test cases specified in |
| 18 | that testcase meta-data. The full canonical name for each test case is <path to |
| 19 | test case>/<block>. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 20 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 21 | Each test block in the testcase meta data can define the following key/value |
| 22 | pairs: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 23 | |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 24 | tags: <list of tags> (required) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 25 | A set of string tags for the testcase. Usually pertains to |
| 26 | functional domains but can be anything. Command line invocations |
| 27 | of this script can filter the set of tests to run based on tag. |
| 28 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 29 | skip: <True|False> (default False) |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 30 | skip testcase unconditionally. This can be used for broken tests. |
| 31 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 32 | slow: <True|False> (default False) |
Andy Ross | dc4151f | 2019-01-03 14:17:43 -0800 | [diff] [blame] | 33 | Don't build or run this test case unless --enable-slow was passed |
| 34 | in on the command line. Intended for time-consuming test cases |
| 35 | that are only run under certain circumstances, like daily |
| 36 | builds. |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 37 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 38 | extra_args: <list of extra arguments> |
Sebastian Bøe | c218261 | 2017-11-09 12:25:02 +0100 | [diff] [blame] | 39 | Extra cache entries to pass to CMake when building or running the |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 40 | test case. |
| 41 | |
Anas Nashif | ebc329d | 2017-10-17 09:00:33 -0400 | [diff] [blame] | 42 | extra_configs: <list of extra configurations> |
| 43 | Extra configuration options to be merged with a master prj.conf |
| 44 | when building or running the test case. |
| 45 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 46 | build_only: <True|False> (default False) |
Marc Herbert | e5cedca | 2019-04-08 14:02:34 -0700 | [diff] [blame] | 47 | If true, don't try to run the test even if the selected platform |
| 48 | supports it. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 49 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 50 | build_on_all: <True|False> (default False) |
| 51 | If true, attempt to build test on all available platforms. |
| 52 | |
| 53 | depends_on: <list of features> |
| 54 | A board or platform can announce what features it supports, this option |
| 55 | will enable the test only those platforms that provide this feature. |
| 56 | |
| 57 | min_ram: <integer> |
| 58 | minimum amount of RAM needed for this test to build and run. This is |
| 59 | compared with information provided by the board metadata. |
| 60 | |
| 61 | min_flash: <integer> |
| 62 | minimum amount of ROM needed for this test to build and run. This is |
| 63 | compared with information provided by the board metadata. |
| 64 | |
| 65 | timeout: <number of seconds> |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 66 | Length of time to run test in emulator before automatically killing it. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 67 | Default to 60 seconds. |
| 68 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 69 | arch_whitelist: <list of arches, such as x86, arm, arc> |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 70 | Set of architectures that this test case should only be run for. |
| 71 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 72 | arch_exclude: <list of arches, such as x86, arm, arc> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 73 | Set of architectures that this test case should not run on. |
| 74 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 75 | platform_whitelist: <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 76 | Set of platforms that this test case should only be run for. |
| 77 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 78 | platform_exclude: <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 79 | Set of platforms that this test case should not run on. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 80 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 81 | extra_sections: <list of extra binary sections> |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 82 | When computing sizes, sanitycheck will report errors if it finds |
| 83 | extra, unexpected sections in the Zephyr binary unless they are named |
| 84 | here. They will not be included in the size calculation. |
| 85 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 86 | filter: <expression> |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 87 | Filter whether the testcase should be run by evaluating an expression |
| 88 | against an environment containing the following values: |
| 89 | |
| 90 | { ARCH : <architecture>, |
| 91 | PLATFORM : <platform>, |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 92 | <all CONFIG_* key/value pairs in the test's generated defconfig>, |
Anas Nashif | 45a9786 | 2019-01-09 08:46:42 -0500 | [diff] [blame] | 93 | <all DT_* key/value pairs in the test's generated device tree file>, |
| 94 | <all CMake key/value pairs in the test's generated CMakeCache.txt file>, |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 95 | *<env>: any environment variable available |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | The grammar for the expression language is as follows: |
| 99 | |
| 100 | expression ::= expression "and" expression |
| 101 | | expression "or" expression |
| 102 | | "not" expression |
| 103 | | "(" expression ")" |
| 104 | | symbol "==" constant |
| 105 | | symbol "!=" constant |
| 106 | | symbol "<" number |
| 107 | | symbol ">" number |
| 108 | | symbol ">=" number |
| 109 | | symbol "<=" number |
| 110 | | symbol "in" list |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 111 | | symbol ":" string |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 112 | | symbol |
| 113 | |
| 114 | list ::= "[" list_contents "]" |
| 115 | |
| 116 | list_contents ::= constant |
| 117 | | list_contents "," constant |
| 118 | |
| 119 | constant ::= number |
| 120 | | string |
| 121 | |
| 122 | |
| 123 | For the case where expression ::= symbol, it evaluates to true |
| 124 | if the symbol is defined to a non-empty string. |
| 125 | |
| 126 | Operator precedence, starting from lowest to highest: |
| 127 | |
| 128 | or (left associative) |
| 129 | and (left associative) |
| 130 | not (right associative) |
| 131 | all comparison operators (non-associative) |
| 132 | |
| 133 | arch_whitelist, arch_exclude, platform_whitelist, platform_exclude |
| 134 | are all syntactic sugar for these expressions. For instance |
| 135 | |
| 136 | arch_exclude = x86 arc |
| 137 | |
| 138 | Is the same as: |
| 139 | |
| 140 | filter = not ARCH in ["x86", "arc"] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 141 | |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 142 | The ':' operator compiles the string argument as a regular expression, |
| 143 | and then returns a true value only if the symbol's value in the environment |
Anas Nashif | 578ae40 | 2019-07-12 07:54:35 -0700 | [diff] [blame] | 144 | matches. For example, if CONFIG_SOC="stm32f107xc" then |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 145 | |
Anas Nashif | 578ae40 | 2019-07-12 07:54:35 -0700 | [diff] [blame] | 146 | filter = CONFIG_SOC : "stm.*" |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 147 | |
| 148 | Would match it. |
| 149 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 150 | The set of test cases that actually run depends on directives in the testcase |
| 151 | filed and options passed in on the command line. If there is any confusion, |
| 152 | running with -v or --discard-report can help show why particular test cases |
| 153 | were skipped. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 154 | |
| 155 | Metrics (such as pass/fail state and binary size) for the last code |
| 156 | release are stored in scripts/sanity_chk/sanity_last_release.csv. |
| 157 | To update this, pass the --all --release options. |
| 158 | |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 159 | To load arguments from a file, write '+' before the file name, e.g., |
| 160 | +file_name. File content must be one or more valid arguments separated by |
| 161 | line break instead of white spaces. |
| 162 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 163 | Most everyday users will run with no arguments. |
Andy Ross | dc4151f | 2019-01-03 14:17:43 -0800 | [diff] [blame] | 164 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 165 | """ |
| 166 | |
Sebastian Bøe | 56d7471 | 2019-01-21 15:48:46 +0100 | [diff] [blame] | 167 | import os |
Sebastian Bøe | 56d7471 | 2019-01-21 15:48:46 +0100 | [diff] [blame] | 168 | if os.name == 'nt': |
| 169 | print("Running sanitycheck on Windows is not supported yet.") |
| 170 | print("https://github.com/zephyrproject-rtos/zephyr/issues/2664") |
| 171 | exit(1) |
| 172 | |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 173 | import contextlib |
Anas Nashif | a4c368e | 2018-10-15 09:45:59 -0400 | [diff] [blame] | 174 | import string |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 175 | import mmap |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 176 | import argparse |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 177 | import sys |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 178 | import re |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 179 | import subprocess |
| 180 | import multiprocessing |
| 181 | import select |
| 182 | import shutil |
Marc Herbert | af1090c | 2019-04-30 14:11:29 -0700 | [diff] [blame] | 183 | import shlex |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 184 | import signal |
| 185 | import threading |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 186 | import concurrent.futures |
| 187 | from threading import BoundedSemaphore |
| 188 | import queue |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 189 | import time |
Anas Nashif | 654ec598 | 2019-04-11 08:38:21 -0400 | [diff] [blame] | 190 | import datetime |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 191 | import csv |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 192 | import yaml |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 193 | import glob |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 194 | import serial |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 195 | import concurrent |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 196 | import xml.etree.ElementTree as ET |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 197 | from collections import OrderedDict |
| 198 | from itertools import islice |
Anas Nashif | e24350c | 2018-07-11 15:09:22 -0500 | [diff] [blame] | 199 | from pathlib import Path |
| 200 | from distutils.spawn import find_executable |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 201 | |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 202 | import logging |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 203 | |
| 204 | |
| 205 | hw_map_local = threading.Lock() |
| 206 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 207 | |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 208 | log_format = "%(levelname)s %(name)s::%(module)s.%(funcName)s():%(lineno)d: %(message)s" |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 209 | logging.basicConfig(format=log_format, level=30) |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 210 | |
Oleg Zhurakivskyy | 4282208 | 2018-08-17 14:54:41 +0300 | [diff] [blame] | 211 | ZEPHYR_BASE = os.environ.get("ZEPHYR_BASE") |
| 212 | if not ZEPHYR_BASE: |
Anas Nashif | 427cdd3 | 2015-08-06 07:25:42 -0400 | [diff] [blame] | 213 | sys.stderr.write("$ZEPHYR_BASE environment variable undefined.\n") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 214 | exit(1) |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 215 | |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 216 | # Use this for internal comparisons; that's what canonicalization is |
| 217 | # for. Don't use it when invoking other components of the build system |
| 218 | # to avoid confusing and hard to trace inconsistencies in error messages |
| 219 | # and logs, generated Makefiles, etc. compared to when users invoke these |
| 220 | # components directly. |
| 221 | # Note "normalization" is different from canonicalization, see os.path. |
| 222 | canonical_zephyr_base = os.path.realpath(ZEPHYR_BASE) |
| 223 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 224 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/")) |
| 225 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 226 | from sanity_chk import scl |
| 227 | from sanity_chk import expr_parser |
| 228 | |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 229 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 230 | VERBOSE = 0 |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 231 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 232 | RELEASE_DATA = os.path.join(ZEPHYR_BASE, "scripts", "sanity_chk", |
| 233 | "sanity_last_release.csv") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 234 | |
| 235 | if os.isatty(sys.stdout.fileno()): |
| 236 | TERMINAL = True |
| 237 | COLOR_NORMAL = '\033[0m' |
| 238 | COLOR_RED = '\033[91m' |
| 239 | COLOR_GREEN = '\033[92m' |
| 240 | COLOR_YELLOW = '\033[93m' |
| 241 | else: |
| 242 | TERMINAL = False |
| 243 | COLOR_NORMAL = "" |
| 244 | COLOR_RED = "" |
| 245 | COLOR_GREEN = "" |
| 246 | COLOR_YELLOW = "" |
| 247 | |
Anas Nashif | 45a9786 | 2019-01-09 08:46:42 -0500 | [diff] [blame] | 248 | class CMakeCacheEntry: |
| 249 | '''Represents a CMake cache entry. |
| 250 | |
| 251 | This class understands the type system in a CMakeCache.txt, and |
| 252 | converts the following cache types to Python types: |
| 253 | |
| 254 | Cache Type Python type |
| 255 | ---------- ------------------------------------------- |
| 256 | FILEPATH str |
| 257 | PATH str |
| 258 | STRING str OR list of str (if ';' is in the value) |
| 259 | BOOL bool |
| 260 | INTERNAL str OR list of str (if ';' is in the value) |
| 261 | ---------- ------------------------------------------- |
| 262 | ''' |
| 263 | |
| 264 | # Regular expression for a cache entry. |
| 265 | # |
| 266 | # CMake variable names can include escape characters, allowing a |
| 267 | # wider set of names than is easy to match with a regular |
| 268 | # expression. To be permissive here, use a non-greedy match up to |
| 269 | # the first colon (':'). This breaks if the variable name has a |
| 270 | # colon inside, but it's good enough. |
| 271 | CACHE_ENTRY = re.compile( |
| 272 | r'''(?P<name>.*?) # name |
| 273 | :(?P<type>FILEPATH|PATH|STRING|BOOL|INTERNAL) # type |
| 274 | =(?P<value>.*) # value |
| 275 | ''', re.X) |
| 276 | |
| 277 | @classmethod |
| 278 | def _to_bool(cls, val): |
| 279 | # Convert a CMake BOOL string into a Python bool. |
| 280 | # |
| 281 | # "True if the constant is 1, ON, YES, TRUE, Y, or a |
| 282 | # non-zero number. False if the constant is 0, OFF, NO, |
| 283 | # FALSE, N, IGNORE, NOTFOUND, the empty string, or ends in |
| 284 | # the suffix -NOTFOUND. Named boolean constants are |
| 285 | # case-insensitive. If the argument is not one of these |
| 286 | # constants, it is treated as a variable." |
| 287 | # |
| 288 | # https://cmake.org/cmake/help/v3.0/command/if.html |
| 289 | val = val.upper() |
| 290 | if val in ('ON', 'YES', 'TRUE', 'Y'): |
| 291 | return 1 |
| 292 | elif val in ('OFF', 'NO', 'FALSE', 'N', 'IGNORE', 'NOTFOUND', ''): |
| 293 | return 0 |
| 294 | elif val.endswith('-NOTFOUND'): |
| 295 | return 0 |
| 296 | else: |
| 297 | try: |
| 298 | v = int(val) |
| 299 | return v != 0 |
| 300 | except ValueError as exc: |
| 301 | raise ValueError('invalid bool {}'.format(val)) from exc |
| 302 | |
| 303 | @classmethod |
| 304 | def from_line(cls, line, line_no): |
| 305 | # Comments can only occur at the beginning of a line. |
| 306 | # (The value of an entry could contain a comment character). |
| 307 | if line.startswith('//') or line.startswith('#'): |
| 308 | return None |
| 309 | |
| 310 | # Whitespace-only lines do not contain cache entries. |
| 311 | if not line.strip(): |
| 312 | return None |
| 313 | |
| 314 | m = cls.CACHE_ENTRY.match(line) |
| 315 | if not m: |
| 316 | return None |
| 317 | |
| 318 | name, type_, value = (m.group(g) for g in ('name', 'type', 'value')) |
| 319 | if type_ == 'BOOL': |
| 320 | try: |
| 321 | value = cls._to_bool(value) |
| 322 | except ValueError as exc: |
| 323 | args = exc.args + ('on line {}: {}'.format(line_no, line),) |
| 324 | raise ValueError(args) from exc |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 325 | elif type_ in ['STRING','INTERNAL']: |
Anas Nashif | 45a9786 | 2019-01-09 08:46:42 -0500 | [diff] [blame] | 326 | # If the value is a CMake list (i.e. is a string which |
| 327 | # contains a ';'), convert to a Python list. |
| 328 | if ';' in value: |
| 329 | value = value.split(';') |
| 330 | |
| 331 | return CMakeCacheEntry(name, value) |
| 332 | |
| 333 | def __init__(self, name, value): |
| 334 | self.name = name |
| 335 | self.value = value |
| 336 | |
| 337 | def __str__(self): |
| 338 | fmt = 'CMakeCacheEntry(name={}, value={})' |
| 339 | return fmt.format(self.name, self.value) |
| 340 | |
| 341 | |
| 342 | class CMakeCache: |
| 343 | '''Parses and represents a CMake cache file.''' |
| 344 | |
| 345 | @staticmethod |
| 346 | def from_file(cache_file): |
| 347 | return CMakeCache(cache_file) |
| 348 | |
| 349 | def __init__(self, cache_file): |
| 350 | self.cache_file = cache_file |
| 351 | self.load(cache_file) |
| 352 | |
| 353 | def load(self, cache_file): |
| 354 | entries = [] |
| 355 | with open(cache_file, 'r') as cache: |
| 356 | for line_no, line in enumerate(cache): |
| 357 | entry = CMakeCacheEntry.from_line(line, line_no) |
| 358 | if entry: |
| 359 | entries.append(entry) |
| 360 | self._entries = OrderedDict((e.name, e) for e in entries) |
| 361 | |
| 362 | def get(self, name, default=None): |
| 363 | entry = self._entries.get(name) |
| 364 | if entry is not None: |
| 365 | return entry.value |
| 366 | else: |
| 367 | return default |
| 368 | |
| 369 | def get_list(self, name, default=None): |
| 370 | if default is None: |
| 371 | default = [] |
| 372 | entry = self._entries.get(name) |
| 373 | if entry is not None: |
| 374 | value = entry.value |
| 375 | if isinstance(value, list): |
| 376 | return value |
| 377 | elif isinstance(value, str): |
| 378 | return [value] if value else [] |
| 379 | else: |
| 380 | msg = 'invalid value {} type {}' |
| 381 | raise RuntimeError(msg.format(value, type(value))) |
| 382 | else: |
| 383 | return default |
| 384 | |
| 385 | def __contains__(self, name): |
| 386 | return name in self._entries |
| 387 | |
| 388 | def __getitem__(self, name): |
| 389 | return self._entries[name].value |
| 390 | |
| 391 | def __setitem__(self, name, entry): |
| 392 | if not isinstance(entry, CMakeCacheEntry): |
| 393 | msg = 'improper type {} for value {}, expecting CMakeCacheEntry' |
| 394 | raise TypeError(msg.format(type(entry), entry)) |
| 395 | self._entries[name] = entry |
| 396 | |
| 397 | def __delitem__(self, name): |
| 398 | del self._entries[name] |
| 399 | |
| 400 | def __iter__(self): |
| 401 | return iter(self._entries.values()) |
| 402 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 403 | class SanityCheckException(Exception): |
| 404 | pass |
| 405 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 406 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 407 | class SanityRuntimeError(SanityCheckException): |
| 408 | pass |
| 409 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 410 | class ConfigurationError(SanityCheckException): |
| 411 | def __init__(self, cfile, message): |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 412 | SanityCheckException.__init__(self, cfile + ": " + message) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 413 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 414 | class BuildError(SanityCheckException): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 415 | pass |
| 416 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 417 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 418 | class ExecutionError(SanityCheckException): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 419 | pass |
| 420 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 421 | |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 422 | log_file = None |
| 423 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 424 | # Debug Functions |
Anas Nashif | 654ec598 | 2019-04-11 08:38:21 -0400 | [diff] [blame] | 425 | def info(what, show_time=True): |
| 426 | if options.timestamps and show_time: |
| 427 | date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 428 | what = "{}: {}".format(date, what) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 429 | sys.stdout.write(what + "\n") |
Paul Sokolovsky | 3ba0876 | 2017-10-27 14:53:24 +0300 | [diff] [blame] | 430 | sys.stdout.flush() |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 431 | if log_file: |
| 432 | log_file.write(what + "\n") |
| 433 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 434 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 435 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 436 | def error(what): |
Anas Nashif | 654ec598 | 2019-04-11 08:38:21 -0400 | [diff] [blame] | 437 | if options.timestamps: |
| 438 | date = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S") |
| 439 | what = "{}: {}".format(date, what) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 440 | sys.stderr.write(COLOR_RED + what + COLOR_NORMAL + "\n") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 441 | if log_file: |
| 442 | log_file(what + "\n") |
| 443 | log_file.flush() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 444 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 445 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 446 | def debug(what): |
| 447 | if VERBOSE >= 1: |
| 448 | info(what) |
| 449 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 450 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 451 | def verbose(what): |
| 452 | if VERBOSE >= 2: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 453 | info(what) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 454 | |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 455 | class HarnessImporter: |
| 456 | |
| 457 | def __init__(self, name): |
| 458 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/sanity_chk")) |
| 459 | module = __import__("harness") |
| 460 | if name: |
| 461 | my_class = getattr(module, name) |
| 462 | else: |
| 463 | my_class = getattr(module, "Test") |
| 464 | |
| 465 | self.instance = my_class() |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 466 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 467 | class Handler: |
Anas Nashif | d18ec53 | 2019-04-11 23:20:39 -0400 | [diff] [blame] | 468 | def __init__(self, instance, type_str="build"): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 469 | """Constructor |
| 470 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 471 | """ |
| 472 | self.lock = threading.Lock() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 473 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 474 | self.state = "waiting" |
Anas Nashif | 1377375 | 2018-07-06 18:20:23 -0500 | [diff] [blame] | 475 | self.run = False |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 476 | self.duration = 0 |
Anas Nashif | d18ec53 | 2019-04-11 23:20:39 -0400 | [diff] [blame] | 477 | self.type_str = type_str |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 478 | |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 479 | self.binary = None |
Jan Kowalewski | 265895b | 2019-01-07 16:40:24 +0100 | [diff] [blame] | 480 | self.pid_fn = None |
Anas Nashif | 99f5a6c | 2018-07-07 08:45:53 -0500 | [diff] [blame] | 481 | self.call_make_run = False |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 482 | |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 483 | self.name = instance.name |
| 484 | self.instance = instance |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 485 | self.timeout = instance.testcase.timeout |
| 486 | self.sourcedir = instance.testcase.source_dir |
| 487 | self.build_dir = instance.build_dir |
| 488 | self.log = os.path.join(self.build_dir, "handler.log") |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 489 | self.returncode = 0 |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 490 | self.set_state("running", self.duration) |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 491 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 492 | self.args = [] |
| 493 | |
| 494 | def set_state(self, state, duration): |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 495 | self.lock.acquire() |
| 496 | self.state = state |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 497 | self.duration = duration |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 498 | self.lock.release() |
| 499 | |
| 500 | def get_state(self): |
| 501 | self.lock.acquire() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 502 | ret = (self.state, self.duration) |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 503 | self.lock.release() |
| 504 | return ret |
| 505 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 506 | def record(self, harness): |
| 507 | if harness.recording: |
| 508 | filename = os.path.join(options.outdir, |
| 509 | self.instance.platform.name, |
| 510 | self.instance.testcase.name, "recording.csv") |
| 511 | with open(filename, "at") as csvfile: |
| 512 | cw = csv.writer(csvfile, harness.fieldnames, lineterminator=os.linesep) |
| 513 | cw.writerow(harness.fieldnames) |
| 514 | for instance in harness.recording: |
| 515 | cw.writerow(instance) |
| 516 | |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 517 | class BinaryHandler(Handler): |
Anas Nashif | d18ec53 | 2019-04-11 23:20:39 -0400 | [diff] [blame] | 518 | def __init__(self, instance, type_str): |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 519 | """Constructor |
| 520 | |
| 521 | @param instance Test Instance |
| 522 | """ |
Anas Nashif | d18ec53 | 2019-04-11 23:20:39 -0400 | [diff] [blame] | 523 | super().__init__(instance, type_str) |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 524 | |
| 525 | self.valgrind = False |
Alberto Escolar Piedras | 661bc82 | 2018-12-11 15:13:05 +0100 | [diff] [blame] | 526 | self.terminated = False |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 527 | |
Jan Kowalewski | 265895b | 2019-01-07 16:40:24 +0100 | [diff] [blame] | 528 | def try_kill_process_by_pid(self): |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 529 | if self.pid_fn: |
Jan Kowalewski | 265895b | 2019-01-07 16:40:24 +0100 | [diff] [blame] | 530 | pid = int(open(self.pid_fn).read()) |
| 531 | os.unlink(self.pid_fn) |
| 532 | self.pid_fn = None # clear so we don't try to kill the binary twice |
| 533 | try: |
| 534 | os.kill(pid, signal.SIGTERM) |
| 535 | except ProcessLookupError: |
| 536 | pass |
| 537 | |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 538 | def _output_reader(self, proc, harness): |
| 539 | log_out_fp = open(self.log, "wt") |
| 540 | for line in iter(proc.stdout.readline, b''): |
| 541 | verbose("OUTPUT: {0}".format(line.decode('utf-8').rstrip())) |
| 542 | log_out_fp.write(line.decode('utf-8')) |
| 543 | log_out_fp.flush() |
| 544 | harness.handle(line.decode('utf-8').rstrip()) |
| 545 | if harness.state: |
Alberto Escolar Piedras | 661bc82 | 2018-12-11 15:13:05 +0100 | [diff] [blame] | 546 | try: |
| 547 | #POSIX arch based ztests end on their own, |
| 548 | #so let's give it up to 100ms to do so |
| 549 | proc.wait(0.1) |
| 550 | except subprocess.TimeoutExpired: |
| 551 | proc.terminate() |
| 552 | self.terminated = True |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 553 | break |
| 554 | |
| 555 | log_out_fp.close() |
| 556 | |
| 557 | def handle(self): |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 558 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 559 | harness_name = self.instance.testcase.harness.capitalize() |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 560 | harness_import = HarnessImporter(harness_name) |
| 561 | harness = harness_import.instance |
| 562 | harness.configure(self.instance) |
| 563 | |
Anas Nashif | 99f5a6c | 2018-07-07 08:45:53 -0500 | [diff] [blame] | 564 | if self.call_make_run: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 565 | command = [get_generator()[0], "run"] |
Anas Nashif | 99f5a6c | 2018-07-07 08:45:53 -0500 | [diff] [blame] | 566 | else: |
| 567 | command = [self.binary] |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 568 | |
Alberto Escolar Piedras | b91f374 | 2019-07-02 10:06:22 +0200 | [diff] [blame] | 569 | if self.valgrind and shutil.which("valgrind"): |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 570 | command = ["valgrind", "--error-exitcode=2", |
Alberto Escolar Piedras | 3980e0c | 2018-12-12 17:41:04 +0100 | [diff] [blame] | 571 | "--leak-check=full", |
| 572 | "--suppressions="+ZEPHYR_BASE+"/scripts/valgrind.supp", |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 573 | "--log-file="+self.build_dir+"/valgrind.log" |
Alberto Escolar Piedras | 3980e0c | 2018-12-12 17:41:04 +0100 | [diff] [blame] | 574 | ] + command |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 575 | |
Marc Herbert | af1090c | 2019-04-30 14:11:29 -0700 | [diff] [blame] | 576 | verbose("Spawning process: " + |
Jan Van Winkel | 6b9e160 | 2019-01-03 19:18:35 +0100 | [diff] [blame] | 577 | " ".join(shlex.quote(word) for word in command) + os.linesep + |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 578 | "Spawning process in directory: " + self.build_dir) |
Alberto Escolar Piedras | 649368c | 2019-07-02 09:59:35 +0200 | [diff] [blame] | 579 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 580 | start_time = time.time() |
Alberto Escolar Piedras | 649368c | 2019-07-02 09:59:35 +0200 | [diff] [blame] | 581 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 582 | with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.build_dir) as proc: |
Marc Herbert | af1090c | 2019-04-30 14:11:29 -0700 | [diff] [blame] | 583 | verbose("Spawning BinaryHandler Thread for %s" % self.name) |
Flavio Ceolin | 063ab90 | 2019-10-15 16:10:49 -0700 | [diff] [blame] | 584 | t = threading.Thread(target=self._output_reader, args=(proc, harness, ), daemon=True) |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 585 | t.start() |
| 586 | t.join(self.timeout) |
| 587 | if t.is_alive(): |
Jan Kowalewski | 265895b | 2019-01-07 16:40:24 +0100 | [diff] [blame] | 588 | self.try_kill_process_by_pid() |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 589 | proc.terminate() |
Alberto Escolar Piedras | 661bc82 | 2018-12-11 15:13:05 +0100 | [diff] [blame] | 590 | self.terminated = True |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 591 | t.join() |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 592 | proc.wait() |
| 593 | self.returncode = proc.returncode |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 594 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 595 | handler_time = time.time() - start_time |
Alberto Escolar Piedras | 649368c | 2019-07-02 09:59:35 +0200 | [diff] [blame] | 596 | |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 597 | if options.enable_coverage: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 598 | subprocess.call(["GCOV_PREFIX=" + self.build_dir, |
| 599 | "gcov", self.sourcedir, "-b", "-s", self.build_dir], shell=True) |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 600 | |
Jan Kowalewski | 265895b | 2019-01-07 16:40:24 +0100 | [diff] [blame] | 601 | self.try_kill_process_by_pid() |
| 602 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 603 | # FIXME: This is needed when killing the simulator, the console is |
Anas Nashif | 99f5a6c | 2018-07-07 08:45:53 -0500 | [diff] [blame] | 604 | # garbled and needs to be reset. Did not find a better way to do that. |
| 605 | |
| 606 | subprocess.call(["stty", "sane"]) |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 607 | self.instance.results = harness.tests |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 608 | if not self.terminated and self.returncode != 0: |
Alberto Escolar Piedras | 661bc82 | 2018-12-11 15:13:05 +0100 | [diff] [blame] | 609 | #When a process is killed, the default handler returns 128 + SIGTERM |
| 610 | #so in that case the return code itself is not meaningful |
Anas Nashif | f72d190 | 2019-10-18 17:20:44 -0400 | [diff] [blame] | 611 | self.set_state("failed", handler_time) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 612 | self.instance.reason = "Handler error" |
Alberto Escolar Piedras | 661bc82 | 2018-12-11 15:13:05 +0100 | [diff] [blame] | 613 | elif harness.state: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 614 | self.set_state(harness.state, handler_time) |
Anas Nashif | df7ee61 | 2018-07-07 06:09:01 -0500 | [diff] [blame] | 615 | else: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 616 | self.set_state("timeout", handler_time) |
| 617 | self.instance.reason = "Handler timeout" |
| 618 | |
| 619 | self.record(harness) |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 620 | |
| 621 | class DeviceHandler(Handler): |
| 622 | |
Anas Nashif | d18ec53 | 2019-04-11 23:20:39 -0400 | [diff] [blame] | 623 | def __init__(self, instance, type_str): |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 624 | """Constructor |
| 625 | |
| 626 | @param instance Test Instance |
| 627 | """ |
Anas Nashif | d18ec53 | 2019-04-11 23:20:39 -0400 | [diff] [blame] | 628 | super().__init__(instance, type_str) |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 629 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 630 | self.suite = None |
| 631 | |
Marti Bolivar | 5591ca2 | 2019-02-07 15:53:39 -0700 | [diff] [blame] | 632 | def monitor_serial(self, ser, halt_fileno, harness): |
Anas Nashif | 4a9f3e6 | 2018-07-06 18:58:18 -0500 | [diff] [blame] | 633 | log_out_fp = open(self.log, "wt") |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 634 | |
Marti Bolivar | 5591ca2 | 2019-02-07 15:53:39 -0700 | [diff] [blame] | 635 | ser_fileno = ser.fileno() |
| 636 | readlist = [halt_fileno, ser_fileno] |
| 637 | |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 638 | while ser.isOpen(): |
Marti Bolivar | 5591ca2 | 2019-02-07 15:53:39 -0700 | [diff] [blame] | 639 | readable, _, _ = select.select(readlist, [], [], self.timeout) |
| 640 | |
| 641 | if halt_fileno in readable: |
| 642 | verbose('halted') |
| 643 | ser.close() |
| 644 | break |
| 645 | if ser_fileno not in readable: |
| 646 | continue # Timeout. |
| 647 | |
| 648 | serial_line = None |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 649 | try: |
| 650 | serial_line = ser.readline() |
| 651 | except TypeError: |
| 652 | pass |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 653 | except serial.SerialException: |
Anas Nashif | 5923760 | 2019-03-03 10:36:35 -0500 | [diff] [blame] | 654 | ser.close() |
| 655 | break |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 656 | |
Marti Bolivar | 5591ca2 | 2019-02-07 15:53:39 -0700 | [diff] [blame] | 657 | # Just because ser_fileno has data doesn't mean an entire line |
| 658 | # is available yet. |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 659 | if serial_line: |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 660 | sl = serial_line.decode('utf-8', 'ignore') |
| 661 | verbose("DEVICE: {0}".format(sl.rstrip())) |
| 662 | |
| 663 | log_out_fp.write(sl) |
| 664 | log_out_fp.flush() |
| 665 | harness.handle(sl.rstrip()) |
Marti Bolivar | 5591ca2 | 2019-02-07 15:53:39 -0700 | [diff] [blame] | 666 | |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 667 | if harness.state: |
| 668 | ser.close() |
| 669 | break |
| 670 | |
| 671 | log_out_fp.close() |
| 672 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 673 | def device_is_available(self, device): |
| 674 | for i in self.suite.connected_hardware: |
| 675 | if i['platform'] == device and i['available'] and i['connected']: |
| 676 | return True |
| 677 | |
| 678 | return False |
| 679 | |
| 680 | def get_available_device(self, device): |
| 681 | for i in self.suite.connected_hardware: |
| 682 | if i['platform'] == device and i['available']: |
| 683 | i['available'] = False |
| 684 | i['counter'] += 1 |
| 685 | return i |
| 686 | |
| 687 | return None |
| 688 | |
| 689 | def make_device_available(self, serial): |
| 690 | with hw_map_local: |
| 691 | for i in self.suite.connected_hardware: |
| 692 | if i['serial'] == serial: |
| 693 | i['available'] = True |
| 694 | |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 695 | def handle(self): |
| 696 | out_state = "failed" |
| 697 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 698 | if options.west_flash: |
| 699 | command = ["west", "flash", "--skip-rebuild", "-d", self.build_dir] |
| 700 | if options.west_runner: |
Michael Scott | 421ce46 | 2019-06-18 09:37:46 -0700 | [diff] [blame] | 701 | command.append("--runner") |
| 702 | command.append(options.west_runner) |
Jorgen Kvalvaag | d50fb31 | 2019-09-02 15:25:20 +0200 | [diff] [blame] | 703 | # There are three ways this option is used. |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 704 | # 1) bare: --west-flash |
| 705 | # This results in options.west_flash == [] |
| 706 | # 2) with a value: --west-flash="--board-id=42" |
| 707 | # This results in options.west_flash == "--board-id=42" |
Jorgen Kvalvaag | d50fb31 | 2019-09-02 15:25:20 +0200 | [diff] [blame] | 708 | # 3) Multiple values: --west-flash="--board-id=42,--erase" |
| 709 | # This results in options.west_flash == "--board-id=42 --erase" |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 710 | if options.west_flash != []: |
| 711 | command.append('--') |
Jorgen Kvalvaag | d50fb31 | 2019-09-02 15:25:20 +0200 | [diff] [blame] | 712 | command.extend(options.west_flash.split(',')) |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 713 | else: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 714 | command = [get_generator()[0], "-C", self.build_dir, "flash"] |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 715 | |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 716 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 717 | while not self.device_is_available(self.instance.platform.name): |
| 718 | time.sleep(1) |
| 719 | |
| 720 | hardware = self.get_available_device(self.instance.platform.name) |
| 721 | |
| 722 | runner = hardware.get('runner', None) |
| 723 | if runner: |
| 724 | board_id = hardware.get("id", None) |
| 725 | product = hardware.get("product", None) |
| 726 | command = ["west", "flash", "--skip-rebuild", "-d", self.build_dir] |
| 727 | command.append("--runner") |
| 728 | command.append(hardware.get('runner', None)) |
| 729 | if runner == "pyocd": |
| 730 | command.append("--board-id") |
| 731 | command.append(board_id) |
| 732 | elif runner == "nrfjprog": |
| 733 | command.append('--') |
| 734 | command.append("--snr") |
| 735 | command.append(board_id) |
| 736 | elif runner == "openocd" and product == "STM32 STLink": |
| 737 | command.append('--') |
| 738 | command.append("--cmd-pre-init") |
| 739 | command.append("hla_serial %s" %(board_id)) |
| 740 | elif runner == "openocd" and product == "EDBG CMSIS-DAP": |
| 741 | command.append('--') |
| 742 | command.append("--cmd-pre-init") |
| 743 | command.append("cmsis_dap_serial %s" %(board_id)) |
| 744 | elif runner == "jlink": |
| 745 | command.append("--tool-opt=-SelectEmuBySN %s" %(board_id)) |
| 746 | |
| 747 | serial_device = hardware['serial'] |
| 748 | |
| 749 | try: |
| 750 | ser = serial.Serial( |
| 751 | serial_device, |
| 752 | baudrate=115200, |
| 753 | parity=serial.PARITY_NONE, |
| 754 | stopbits=serial.STOPBITS_ONE, |
| 755 | bytesize=serial.EIGHTBITS, |
| 756 | timeout=self.timeout |
| 757 | ) |
| 758 | except serial.SerialException as e: |
| 759 | self.set_state("failed", 0) |
| 760 | error("Serial device err: %s" %(str(e))) |
| 761 | self.make_device_available(serial_device) |
| 762 | return |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 763 | |
| 764 | ser.flush() |
| 765 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 766 | harness_name = self.instance.testcase.harness.capitalize() |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 767 | harness_import = HarnessImporter(harness_name) |
| 768 | harness = harness_import.instance |
| 769 | harness.configure(self.instance) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 770 | read_pipe, write_pipe = os.pipe() |
| 771 | start_time = time.time() |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 772 | |
Marti Bolivar | 5591ca2 | 2019-02-07 15:53:39 -0700 | [diff] [blame] | 773 | t = threading.Thread(target=self.monitor_serial, daemon=True, |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 774 | args=(ser, read_pipe, harness)) |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 775 | t.start() |
| 776 | |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 777 | logging.debug('Flash command: %s', command) |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 778 | try: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 779 | if VERBOSE and not runner: |
Marti Bolivar | 303b522 | 2019-02-07 15:50:55 -0700 | [diff] [blame] | 780 | subprocess.check_call(command) |
| 781 | else: |
| 782 | subprocess.check_output(command, stderr=subprocess.PIPE) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 783 | |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 784 | except subprocess.CalledProcessError: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 785 | os.write(write_pipe, b'x') # halt the thread |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 786 | |
| 787 | t.join(self.timeout) |
| 788 | if t.is_alive(): |
| 789 | out_state = "timeout" |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 790 | |
| 791 | if ser.isOpen(): |
| 792 | ser.close() |
| 793 | |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 794 | if out_state == "timeout": |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 795 | for c in self.instance.testcase.cases: |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 796 | if c not in harness.tests: |
| 797 | harness.tests[c] = "BLOCK" |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 798 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 799 | |
| 800 | handler_time = time.time() - start_time |
| 801 | |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 802 | self.instance.results = harness.tests |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 803 | if harness.state: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 804 | self.set_state(harness.state, handler_time) |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 805 | else: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 806 | self.set_state(out_state, handler_time) |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 807 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 808 | self.make_device_available(serial_device) |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 809 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 810 | class QEMUHandler(Handler): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 811 | """Spawns a thread to monitor QEMU output from pipes |
| 812 | |
Anas Nashif | 3ac7b3a | 2017-08-03 10:03:02 -0400 | [diff] [blame] | 813 | We pass QEMU_PIPE to 'make run' and monitor the pipes for output. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 814 | We need to do this as once qemu starts, it runs forever until killed. |
| 815 | Test cases emit special messages to the console as they run, we check |
| 816 | for these to collect whether the test passed or failed. |
| 817 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 818 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 819 | |
| 820 | def __init__(self, instance, type_str): |
| 821 | """Constructor |
| 822 | |
| 823 | @param instance Test instance |
| 824 | """ |
| 825 | |
| 826 | super().__init__(instance, type_str) |
| 827 | self.fifo_fn = os.path.join(instance.build_dir, "qemu-fifo") |
| 828 | |
| 829 | self.pid_fn = os.path.join(instance.build_dir, "qemu.pid") |
| 830 | |
| 831 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 832 | @staticmethod |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 833 | def _thread(handler, timeout, outdir, logfile, fifo_fn, pid_fn, results, harness): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 834 | fifo_in = fifo_fn + ".in" |
| 835 | fifo_out = fifo_fn + ".out" |
| 836 | |
| 837 | # These in/out nodes are named from QEMU's perspective, not ours |
| 838 | if os.path.exists(fifo_in): |
| 839 | os.unlink(fifo_in) |
| 840 | os.mkfifo(fifo_in) |
| 841 | if os.path.exists(fifo_out): |
| 842 | os.unlink(fifo_out) |
| 843 | os.mkfifo(fifo_out) |
| 844 | |
| 845 | # We don't do anything with out_fp but we need to open it for |
| 846 | # writing so that QEMU doesn't block, due to the way pipes work |
| 847 | out_fp = open(fifo_in, "wb") |
| 848 | # Disable internal buffering, we don't |
| 849 | # want read() or poll() to ever block if there is data in there |
| 850 | in_fp = open(fifo_out, "rb", buffering=0) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 851 | log_out_fp = open(logfile, "wt") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 852 | |
| 853 | start_time = time.time() |
| 854 | timeout_time = start_time + timeout |
| 855 | p = select.poll() |
| 856 | p.register(in_fp, select.POLLIN) |
Anas Nashif | 39ae72b | 2018-08-29 01:45:38 -0400 | [diff] [blame] | 857 | out_state = None |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 858 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 859 | line = "" |
Anas Nashif | 74dbe33 | 2019-01-17 17:11:37 -0500 | [diff] [blame] | 860 | timeout_extended = False |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 861 | while True: |
| 862 | this_timeout = int((timeout_time - time.time()) * 1000) |
| 863 | if this_timeout < 0 or not p.poll(this_timeout): |
Anas Nashif | 39ae72b | 2018-08-29 01:45:38 -0400 | [diff] [blame] | 864 | if not out_state: |
| 865 | out_state = "timeout" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 866 | break |
| 867 | |
Genaro Saucedo Tejada | 2968b36 | 2016-08-09 15:11:53 -0500 | [diff] [blame] | 868 | try: |
| 869 | c = in_fp.read(1).decode("utf-8") |
| 870 | except UnicodeDecodeError: |
| 871 | # Test is writing something weird, fail |
| 872 | out_state = "unexpected byte" |
| 873 | break |
| 874 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 875 | if c == "": |
| 876 | # EOF, this shouldn't happen unless QEMU crashes |
| 877 | out_state = "unexpected eof" |
| 878 | break |
| 879 | line = line + c |
| 880 | if c != "\n": |
| 881 | continue |
| 882 | |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 883 | # line contains a full line of data output from QEMU |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 884 | log_out_fp.write(line) |
| 885 | log_out_fp.flush() |
| 886 | line = line.strip() |
| 887 | verbose("QEMU: %s" % line) |
| 888 | |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 889 | harness.handle(line) |
| 890 | if harness.state: |
Anas Nashif | 39ae72b | 2018-08-29 01:45:38 -0400 | [diff] [blame] | 891 | # if we have registered a fail make sure the state is not |
| 892 | # overridden by a false success message coming from the |
| 893 | # testsuite |
| 894 | if out_state != 'failed': |
| 895 | out_state = harness.state |
| 896 | |
| 897 | # if we get some state, that means test is doing well, we reset |
Marc Herbert | 9e57338 | 2019-07-03 12:49:42 -0700 | [diff] [blame] | 898 | # the timeout and wait for 2 more seconds to catch anything |
| 899 | # printed late. We wait much longer if code |
Andrew Boie | 095b82a | 2019-07-02 17:03:48 -0700 | [diff] [blame] | 900 | # coverage is enabled since dumping this information can |
| 901 | # take some time. |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 902 | if not timeout_extended or harness.capture_coverage: |
| 903 | timeout_extended= True |
| 904 | if harness.capture_coverage: |
Andrew Boie | d62e229 | 2019-07-01 15:01:48 -0700 | [diff] [blame] | 905 | timeout_time = time.time() + 30 |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 906 | else: |
Anas Nashif | 74dbe33 | 2019-01-17 17:11:37 -0500 | [diff] [blame] | 907 | timeout_time = time.time() + 2 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 908 | line = "" |
| 909 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 910 | handler.record(harness) |
| 911 | |
| 912 | handler_time = time.time() - start_time |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 913 | verbose("QEMU complete (%s) after %f seconds" % |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 914 | (out_state, handler_time)) |
| 915 | handler.set_state(out_state, handler_time) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 916 | |
| 917 | log_out_fp.close() |
| 918 | out_fp.close() |
| 919 | in_fp.close() |
Anas Nashif | d6476ee | 2019-04-11 11:40:09 -0400 | [diff] [blame] | 920 | if os.path.exists(pid_fn): |
| 921 | pid = int(open(pid_fn).read()) |
| 922 | os.unlink(pid_fn) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 923 | |
Anas Nashif | d6476ee | 2019-04-11 11:40:09 -0400 | [diff] [blame] | 924 | try: |
| 925 | if pid: |
| 926 | os.kill(pid, signal.SIGTERM) |
| 927 | except ProcessLookupError: |
| 928 | # Oh well, as long as it's dead! User probably sent Ctrl-C |
| 929 | pass |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 930 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 931 | os.unlink(fifo_in) |
| 932 | os.unlink(fifo_out) |
| 933 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 934 | def handle(self): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 935 | self.results = {} |
Anas Nashif | 1377375 | 2018-07-06 18:20:23 -0500 | [diff] [blame] | 936 | self.run = True |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 937 | |
| 938 | # We pass this to QEMU which looks for fifos with .in and .out |
| 939 | # suffixes. |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 940 | self.fifo_fn = os.path.join(self.instance.build_dir, "qemu-fifo") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 941 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 942 | self.pid_fn = os.path.join(self.instance.build_dir, "qemu.pid") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 943 | if os.path.exists(self.pid_fn): |
| 944 | os.unlink(self.pid_fn) |
| 945 | |
Anas Nashif | 4a9f3e6 | 2018-07-06 18:58:18 -0500 | [diff] [blame] | 946 | self.log_fn = self.log |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 947 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 948 | harness_import = HarnessImporter(self.instance.testcase.harness.capitalize()) |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 949 | harness = harness_import.instance |
Anas Nashif | d3384fb | 2018-02-22 06:44:16 -0600 | [diff] [blame] | 950 | harness.configure(self.instance) |
| 951 | self.thread = threading.Thread(name=self.name, target=QEMUHandler._thread, |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 952 | args=(self, self.timeout, self.build_dir, |
Jaakko Hannikainen | ca505f8 | 2016-08-22 15:03:46 +0300 | [diff] [blame] | 953 | self.log_fn, self.fifo_fn, |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 954 | self.pid_fn, self.results, harness)) |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 955 | |
| 956 | self.instance.results = harness.tests |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 957 | self.thread.daemon = True |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 958 | verbose("Spawning QEMUHandler Thread for %s" % self.name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 959 | self.thread.start() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 960 | subprocess.call(["stty", "sane"]) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 961 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 962 | def get_fifo(self): |
| 963 | return self.fifo_fn |
| 964 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 965 | class SizeCalculator: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 966 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 967 | alloc_sections = [ |
| 968 | "bss", |
| 969 | "noinit", |
| 970 | "app_bss", |
| 971 | "app_noinit", |
| 972 | "ccm_bss", |
| 973 | "ccm_noinit" |
| 974 | ] |
| 975 | |
| 976 | rw_sections = [ |
| 977 | "datas", |
| 978 | "initlevel", |
| 979 | "exceptions", |
| 980 | "initshell", |
| 981 | "_static_thread_area", |
| 982 | "_k_timer_area", |
| 983 | "_k_mem_slab_area", |
| 984 | "_k_mem_pool_area", |
| 985 | "sw_isr_table", |
| 986 | "_k_sem_area", |
| 987 | "_k_mutex_area", |
| 988 | "app_shmem_regions", |
| 989 | "_k_fifo_area", |
| 990 | "_k_lifo_area", |
| 991 | "_k_stack_area", |
| 992 | "_k_msgq_area", |
| 993 | "_k_mbox_area", |
| 994 | "_k_pipe_area", |
| 995 | "net_if", |
| 996 | "net_if_dev", |
| 997 | "net_stack", |
| 998 | "net_l2_data", |
| 999 | "_k_queue_area", |
| 1000 | "_net_buf_pool_area", |
| 1001 | "app_datas", |
| 1002 | "kobject_data", |
| 1003 | "mmu_tables", |
| 1004 | "app_pad", |
| 1005 | "priv_stacks", |
| 1006 | "ccm_data", |
| 1007 | "usb_descriptor", |
| 1008 | "usb_data", "usb_bos_desc", |
| 1009 | 'log_backends_sections', |
| 1010 | 'log_dynamic_sections', |
| 1011 | 'log_const_sections', |
| 1012 | "app_smem", |
| 1013 | 'shell_root_cmds_sections', |
| 1014 | 'log_const_sections', |
| 1015 | "font_entry_sections", |
| 1016 | "priv_stacks_noinit", |
| 1017 | "_TEXT_SECTION_NAME_2", |
| 1018 | "_GCOV_BSS_SECTION_NAME", |
| 1019 | "gcov", |
| 1020 | "nocache" |
| 1021 | ] |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 1022 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1023 | # These get copied into RAM only on non-XIP |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1024 | ro_sections = [ |
| 1025 | "text", |
| 1026 | "ctors", |
| 1027 | "init_array", |
| 1028 | "reset", |
| 1029 | "object_access", |
| 1030 | "rodata", |
| 1031 | "devconfig", |
| 1032 | "net_l2", |
| 1033 | "vector", |
| 1034 | "sw_isr_table", |
| 1035 | "_settings_handlers_area", |
| 1036 | "_bt_channels_area", |
| 1037 | "_bt_br_channels_area", |
| 1038 | "_bt_services_area", |
| 1039 | "vectors", |
| 1040 | "net_socket_register", |
| 1041 | "net_ppp_proto" |
| 1042 | ] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1043 | |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1044 | def __init__(self, filename, extra_sections): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1045 | """Constructor |
| 1046 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1047 | @param filename Path to the output binary |
| 1048 | The <filename> is parsed by objdump to determine section sizes |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1049 | """ |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1050 | # Make sure this is an ELF binary |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1051 | with open(filename, "rb") as f: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1052 | magic = f.read(4) |
| 1053 | |
Anas Nashif | b4bdd66 | 2018-08-15 17:12:28 -0500 | [diff] [blame] | 1054 | try: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1055 | if magic != b'\x7fELF': |
Anas Nashif | b4bdd66 | 2018-08-15 17:12:28 -0500 | [diff] [blame] | 1056 | raise SanityRuntimeError("%s is not an ELF binary" % filename) |
| 1057 | except Exception as e: |
| 1058 | print(str(e)) |
| 1059 | sys.exit(2) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1060 | |
| 1061 | # Search for CONFIG_XIP in the ELF's list of symbols using NM and AWK. |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1062 | # GREP can not be used as it returns an error if the symbol is not |
| 1063 | # found. |
| 1064 | is_xip_command = "nm " + filename + \ |
| 1065 | " | awk '/CONFIG_XIP/ { print $3 }'" |
| 1066 | is_xip_output = subprocess.check_output( |
| 1067 | is_xip_command, shell=True, stderr=subprocess.STDOUT).decode( |
| 1068 | "utf-8").strip() |
Anas Nashif | b4bdd66 | 2018-08-15 17:12:28 -0500 | [diff] [blame] | 1069 | try: |
| 1070 | if is_xip_output.endswith("no symbols"): |
| 1071 | raise SanityRuntimeError("%s has no symbol information" % filename) |
| 1072 | except Exception as e: |
| 1073 | print(str(e)) |
| 1074 | sys.exit(2) |
| 1075 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1076 | self.is_xip = (len(is_xip_output) != 0) |
| 1077 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1078 | self.filename = filename |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1079 | self.sections = [] |
| 1080 | self.rom_size = 0 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1081 | self.ram_size = 0 |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1082 | self.extra_sections = extra_sections |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1083 | |
| 1084 | self._calculate_sizes() |
| 1085 | |
| 1086 | def get_ram_size(self): |
| 1087 | """Get the amount of RAM the application will use up on the device |
| 1088 | |
| 1089 | @return amount of RAM, in bytes |
| 1090 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1091 | return self.ram_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1092 | |
| 1093 | def get_rom_size(self): |
| 1094 | """Get the size of the data that this application uses on device's flash |
| 1095 | |
| 1096 | @return amount of ROM, in bytes |
| 1097 | """ |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1098 | return self.rom_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1099 | |
| 1100 | def unrecognized_sections(self): |
| 1101 | """Get a list of sections inside the binary that weren't recognized |
| 1102 | |
David B. Kinder | 29963c3 | 2017-06-16 12:32:42 -0700 | [diff] [blame] | 1103 | @return list of unrecognized section names |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1104 | """ |
| 1105 | slist = [] |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1106 | for v in self.sections: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1107 | if not v["recognized"]: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1108 | slist.append(v["name"]) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1109 | return slist |
| 1110 | |
| 1111 | def _calculate_sizes(self): |
| 1112 | """ Calculate RAM and ROM usage by section """ |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 1113 | objdump_command = "objdump -h " + self.filename |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1114 | objdump_output = subprocess.check_output( |
| 1115 | objdump_command, shell=True).decode("utf-8").splitlines() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1116 | |
| 1117 | for line in objdump_output: |
| 1118 | words = line.split() |
| 1119 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1120 | if not words: # Skip lines that are too short |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1121 | continue |
| 1122 | |
| 1123 | index = words[0] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1124 | if not index[0].isdigit(): # Skip lines that do not start |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1125 | continue # with a digit |
| 1126 | |
| 1127 | name = words[1] # Skip lines with section names |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1128 | if name[0] == '.': # starting with '.' |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1129 | continue |
| 1130 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1131 | # TODO this doesn't actually reflect the size in flash or RAM as |
| 1132 | # it doesn't include linker-imposed padding between sections. |
| 1133 | # It is close though. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1134 | size = int(words[2], 16) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 1135 | if size == 0: |
| 1136 | continue |
| 1137 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1138 | load_addr = int(words[4], 16) |
| 1139 | virt_addr = int(words[3], 16) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1140 | |
| 1141 | # 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] | 1142 | # Unrecognized section names are not included in the calculations. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1143 | recognized = True |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1144 | if name in SizeCalculator.alloc_sections: |
| 1145 | self.ram_size += size |
| 1146 | stype = "alloc" |
| 1147 | elif name in SizeCalculator.rw_sections: |
| 1148 | self.ram_size += size |
| 1149 | self.rom_size += size |
| 1150 | stype = "rw" |
| 1151 | elif name in SizeCalculator.ro_sections: |
| 1152 | self.rom_size += size |
| 1153 | if not self.is_xip: |
| 1154 | self.ram_size += size |
| 1155 | stype = "ro" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1156 | else: |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 1157 | stype = "unknown" |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 1158 | if name not in self.extra_sections: |
| 1159 | recognized = False |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1160 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1161 | self.sections.append({"name": name, "load_addr": load_addr, |
| 1162 | "size": size, "virt_addr": virt_addr, |
| 1163 | "type": stype, "recognized": recognized}) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1164 | |
| 1165 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1166 | # "list" - List of strings |
| 1167 | # "list:<type>" - List of <type> |
| 1168 | # "set" - Set of unordered, unique strings |
| 1169 | # "set:<type>" - Set of <type> |
| 1170 | # "float" - Floating point |
| 1171 | # "int" - Integer |
| 1172 | # "bool" - Boolean |
| 1173 | # "str" - String |
| 1174 | |
| 1175 | # XXX Be sure to update __doc__ if you change any of this!! |
| 1176 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1177 | platform_valid_keys = { |
Anas Nashif | 924a4e7 | 2018-10-18 12:25:55 -0400 | [diff] [blame] | 1178 | "supported_toolchains": {"type": "list", "default": []}, |
| 1179 | "env": {"type": "list", "default": []} |
| 1180 | } |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1181 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1182 | testcase_valid_keys = {"tags": {"type": "set", "required": False}, |
| 1183 | "type": {"type": "str", "default": "integration"}, |
| 1184 | "extra_args": {"type": "list"}, |
| 1185 | "extra_configs": {"type": "list"}, |
| 1186 | "build_only": {"type": "bool", "default": False}, |
| 1187 | "build_on_all": {"type": "bool", "default": False}, |
| 1188 | "skip": {"type": "bool", "default": False}, |
| 1189 | "slow": {"type": "bool", "default": False}, |
| 1190 | "timeout": {"type": "int", "default": 60}, |
| 1191 | "min_ram": {"type": "int", "default": 8}, |
| 1192 | "depends_on": {"type": "set"}, |
| 1193 | "min_flash": {"type": "int", "default": 32}, |
| 1194 | "arch_whitelist": {"type": "set"}, |
| 1195 | "arch_exclude": {"type": "set"}, |
| 1196 | "extra_sections": {"type": "list", "default": []}, |
| 1197 | "platform_exclude": {"type": "set"}, |
| 1198 | "platform_whitelist": {"type": "set"}, |
| 1199 | "toolchain_exclude": {"type": "set"}, |
| 1200 | "toolchain_whitelist": {"type": "set"}, |
Anas Nashif | ab94016 | 2017-12-08 10:17:57 -0500 | [diff] [blame] | 1201 | "filter": {"type": "str"}, |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 1202 | "harness": {"type": "str"}, |
Praful Swarnakar | cf89e28 | 2018-09-13 02:58:28 +0530 | [diff] [blame] | 1203 | "harness_config": {"type": "map", "default": {}} |
Anas Nashif | ab94016 | 2017-12-08 10:17:57 -0500 | [diff] [blame] | 1204 | } |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1205 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1206 | class SanityConfigParser: |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1207 | """Class to read test case files with semantic checking |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1208 | """ |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1209 | |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 1210 | def __init__(self, filename, schema): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1211 | """Instantiate a new SanityConfigParser object |
| 1212 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1213 | @param filename Source .yaml file to read |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1214 | """ |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1215 | self.data = {} |
| 1216 | self.schema = schema |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1217 | self.filename = filename |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1218 | self.tests = {} |
| 1219 | self.common = {} |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1220 | |
| 1221 | def load(self): |
| 1222 | self.data = scl.yaml_load_verify(self.filename, self.schema) |
| 1223 | |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1224 | if 'tests' in self.data: |
| 1225 | self.tests = self.data['tests'] |
| 1226 | if 'common' in self.data: |
| 1227 | self.common = self.data['common'] |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1228 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1229 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1230 | def _cast_value(self, value, typestr): |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1231 | if isinstance(value, str): |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1232 | v = value.strip() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1233 | if typestr == "str": |
| 1234 | return v |
| 1235 | |
| 1236 | elif typestr == "float": |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1237 | return float(value) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1238 | |
| 1239 | elif typestr == "int": |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1240 | return int(value) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1241 | |
| 1242 | elif typestr == "bool": |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1243 | return value |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1244 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1245 | elif typestr.startswith("list") and isinstance(value, list): |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 1246 | return value |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1247 | elif typestr.startswith("list") and isinstance(value, str): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1248 | vs = v.split() |
| 1249 | if len(typestr) > 4 and typestr[4] == ":": |
| 1250 | return [self._cast_value(vsi, typestr[5:]) for vsi in vs] |
| 1251 | else: |
| 1252 | return vs |
| 1253 | |
| 1254 | elif typestr.startswith("set"): |
| 1255 | vs = v.split() |
| 1256 | if len(typestr) > 3 and typestr[3] == ":": |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1257 | return {self._cast_value(vsi, typestr[4:]) for vsi in vs} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1258 | else: |
| 1259 | return set(vs) |
| 1260 | |
Anas Nashif | 576be98 | 2017-12-23 20:20:27 -0500 | [diff] [blame] | 1261 | elif typestr.startswith("map"): |
| 1262 | return value |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1263 | else: |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1264 | raise ConfigurationError( |
| 1265 | self.filename, "unknown type '%s'" % value) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1266 | |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1267 | def get_test(self, name, valid_keys): |
| 1268 | """Get a dictionary representing the keys/values within a test |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1269 | |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1270 | @param name The test in the .yaml file to retrieve data from |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1271 | @param valid_keys A dictionary representing the intended semantics |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1272 | for this test. Each key in this dictionary is a key that could |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1273 | 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] | 1274 | here, it will generate an error. Each value in this dictionary |
| 1275 | is another dictionary containing metadata: |
| 1276 | |
| 1277 | "default" - Default value if not given |
| 1278 | "type" - Data type to convert the text value to. Simple types |
| 1279 | supported are "str", "float", "int", "bool" which will get |
| 1280 | converted to respective Python data types. "set" and "list" |
| 1281 | may also be specified which will split the value by |
| 1282 | whitespace (but keep the elements as strings). finally, |
| 1283 | "list:<type>" and "set:<type>" may be given which will |
| 1284 | perform a type conversion after splitting the value up. |
| 1285 | "required" - If true, raise an error if not defined. If false |
| 1286 | and "default" isn't specified, a type conversion will be |
| 1287 | done on an empty string |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1288 | @return A dictionary containing the test key-value pairs with |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1289 | type conversion and default values filled in per valid_keys |
| 1290 | """ |
| 1291 | |
| 1292 | d = {} |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1293 | for k, v in self.common.items(): |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 1294 | d[k] = v |
Anas Nashif | 75547e2 | 2018-02-24 08:32:14 -0600 | [diff] [blame] | 1295 | |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1296 | for k, v in self.tests[name].items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1297 | if k not in valid_keys: |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1298 | raise ConfigurationError( |
| 1299 | self.filename, |
| 1300 | "Unknown config key '%s' in definition for '%s'" % |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1301 | (k, name)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1302 | |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 1303 | if k in d: |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1304 | if isinstance(d[k], str): |
Paul Sokolovsky | 8c6d2e2 | 2019-08-29 13:20:57 +0300 | [diff] [blame] | 1305 | # By default, we just concatenate string values of keys |
| 1306 | # which appear both in "common" and per-test sections, |
| 1307 | # but some keys are handled in adhoc way based on their |
| 1308 | # semantics. |
| 1309 | if k == "filter": |
| 1310 | d[k] = "(%s) and (%s)" % (d[k], v) |
| 1311 | else: |
| 1312 | d[k] += " " + v |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 1313 | else: |
| 1314 | d[k] = v |
Anas Nashif | 75547e2 | 2018-02-24 08:32:14 -0600 | [diff] [blame] | 1315 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 1316 | for k, kinfo in valid_keys.items(): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1317 | if k not in d: |
| 1318 | if "required" in kinfo: |
| 1319 | required = kinfo["required"] |
| 1320 | else: |
| 1321 | required = False |
| 1322 | |
| 1323 | if required: |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1324 | raise ConfigurationError( |
| 1325 | self.filename, |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1326 | "missing required value for '%s' in test '%s'" % |
| 1327 | (k, name)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1328 | else: |
| 1329 | if "default" in kinfo: |
| 1330 | default = kinfo["default"] |
| 1331 | else: |
| 1332 | default = self._cast_value("", kinfo["type"]) |
| 1333 | d[k] = default |
| 1334 | else: |
| 1335 | try: |
| 1336 | d[k] = self._cast_value(d[k], kinfo["type"]) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1337 | except ValueError: |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1338 | raise ConfigurationError( |
Anas Nashif | b4754ed | 2017-12-05 17:27:58 -0500 | [diff] [blame] | 1339 | self.filename, "bad %s value '%s' for key '%s' in name '%s'" % |
| 1340 | (kinfo["type"], d[k], k, name)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1341 | |
| 1342 | return d |
| 1343 | |
| 1344 | |
| 1345 | class Platform: |
| 1346 | """Class representing metadata for a particular platform |
| 1347 | |
Anas Nashif | c740608 | 2015-12-13 15:00:31 -0500 | [diff] [blame] | 1348 | Maps directly to BOARD when building""" |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 1349 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1350 | platform_schema = scl.yaml_load(os.path.join(ZEPHYR_BASE, |
| 1351 | "scripts","sanity_chk","platform-schema.yaml")) |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 1352 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1353 | def __init__(self): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1354 | """Constructor. |
| 1355 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1356 | """ |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1357 | |
| 1358 | self.name = "" |
| 1359 | self.sanitycheck = True |
| 1360 | # if no RAM size is specified by the board, take a default of 128K |
| 1361 | self.ram = 128 |
| 1362 | |
| 1363 | self.ignore_tags = [] |
| 1364 | self.default = False |
| 1365 | # if no flash size is specified by the board, take a default of 512K |
| 1366 | self.flash = 512 |
| 1367 | self.supported = set() |
| 1368 | |
| 1369 | self.arch = "" |
| 1370 | self.type = "na" |
| 1371 | self.simulation = "na" |
| 1372 | self.supported_toolchains = [] |
| 1373 | self.env = [] |
| 1374 | self.env_satisfied = True |
| 1375 | self.filter_data = dict() |
| 1376 | |
| 1377 | def load(self, platform_file): |
| 1378 | scp = SanityConfigParser(platform_file, self.platform_schema) |
| 1379 | scp.load() |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1380 | data = scp.data |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1381 | |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1382 | self.name = data['identifier'] |
Anas Nashif | f3d48e1 | 2018-07-24 08:14:42 -0500 | [diff] [blame] | 1383 | self.sanitycheck = data.get("sanitycheck", True) |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1384 | # if no RAM size is specified by the board, take a default of 128K |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1385 | self.ram = data.get("ram", 128) |
| 1386 | testing = data.get("testing", {}) |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1387 | self.ignore_tags = testing.get("ignore_tags", []) |
| 1388 | self.default = testing.get("default", False) |
| 1389 | # if no flash size is specified by the board, take a default of 512K |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1390 | self.flash = data.get("flash", 512) |
Anas Nashif | 9527693 | 2017-07-31 08:47:41 -0400 | [diff] [blame] | 1391 | self.supported = set() |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1392 | for supp_feature in data.get("supported", []): |
Anas Nashif | 9527693 | 2017-07-31 08:47:41 -0400 | [diff] [blame] | 1393 | for item in supp_feature.split(":"): |
| 1394 | self.supported.add(item) |
| 1395 | |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1396 | self.arch = data['arch'] |
Anas Nashif | cc16422 | 2017-12-26 11:02:46 -0500 | [diff] [blame] | 1397 | self.type = data.get('type', "na") |
Anas Nashif | 8acdbd7 | 2018-01-04 14:15:22 -0500 | [diff] [blame] | 1398 | self.simulation = data.get('simulation', "na") |
Anas Nashif | 255625b | 2017-12-05 15:08:26 -0500 | [diff] [blame] | 1399 | self.supported_toolchains = data.get("toolchain", []) |
Anas Nashif | 924a4e7 | 2018-10-18 12:25:55 -0400 | [diff] [blame] | 1400 | self.env = data.get("env", []) |
| 1401 | self.env_satisfied = True |
| 1402 | for env in self.env: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1403 | if not os.environ.get(env, None): |
Anas Nashif | 924a4e7 | 2018-10-18 12:25:55 -0400 | [diff] [blame] | 1404 | self.env_satisfied = False |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1405 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1406 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1407 | def __repr__(self): |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1408 | return "<%s on %s>" % (self.name, self.arch) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1409 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1410 | class TestCase(object): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1411 | """Class representing a test application |
| 1412 | """ |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1413 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1414 | |
| 1415 | def __init__(self): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1416 | """TestCase constructor. |
| 1417 | |
Anas Nashif | 877d3ca | 2017-12-05 17:39:29 -0500 | [diff] [blame] | 1418 | This gets called by TestSuite as it finds and reads test yaml files. |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1419 | Multiple TestCase instances may be generated from a single testcase.yaml, |
Anas Nashif | 877d3ca | 2017-12-05 17:39:29 -0500 | [diff] [blame] | 1420 | each one corresponds to an entry within that file. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1421 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1422 | 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] | 1423 | a testcase.yaml can define multiple tests, the canonical name for |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1424 | the test case is <workdir>/<name>. |
| 1425 | |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 1426 | @param testcase_root os.path.abspath() of one of the --testcase-root |
| 1427 | @param workdir Sub-directory of testcase_root where the |
| 1428 | .yaml test configuration file was found |
Anas Nashif | 877d3ca | 2017-12-05 17:39:29 -0500 | [diff] [blame] | 1429 | @param name Name of this test case, corresponding to the entry name |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1430 | in the test case configuration file. For many test cases that just |
| 1431 | define one test, can be anything and is usually "test". This is |
| 1432 | really only used to distinguish between different cases when |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1433 | the testcase.yaml defines multiple tests |
Anas Nashif | 877d3ca | 2017-12-05 17:39:29 -0500 | [diff] [blame] | 1434 | @param tc_dict Dictionary with test values for this test case |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 1435 | from the testcase.yaml file |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1436 | """ |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1437 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1438 | self.id = "" |
| 1439 | self.source_dir = "" |
| 1440 | self.yamlfile = "" |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1441 | self.cases = [] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1442 | self.name = "" |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 1443 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1444 | self.type = None |
| 1445 | self.tags = None |
| 1446 | self.extra_args = None |
| 1447 | self.extra_configs = None |
| 1448 | self.arch_whitelist = None |
| 1449 | self.arch_exclude = None |
| 1450 | self.skip = None |
| 1451 | self.platform_exclude = None |
| 1452 | self.platform_whitelist = None |
| 1453 | self.toolchain_exclude = None |
| 1454 | self.toolchain_whitelist = None |
| 1455 | self.tc_filter = None |
| 1456 | self.timeout = 60 |
| 1457 | self.harness = "" |
| 1458 | self.harness_config = {} |
| 1459 | self.build_only = True |
| 1460 | self.build_on_all = False |
| 1461 | self.slow = False |
| 1462 | self.min_ram = None |
| 1463 | self.depends_on = None |
| 1464 | self.min_flash = None |
| 1465 | self.extra_sections = None |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1466 | |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1467 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1468 | @staticmethod |
| 1469 | def get_unique(testcase_root, workdir, name): |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1470 | |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 1471 | canonical_testcase_root = os.path.realpath(testcase_root) |
Tyler Hall | bb35925 | 2019-06-19 02:11:35 -0400 | [diff] [blame] | 1472 | if Path(canonical_zephyr_base) in Path(canonical_testcase_root).parents: |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 1473 | # This is in ZEPHYR_BASE, so include path in name for uniqueness |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1474 | # FIXME: We should not depend on path of test for unique names. |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 1475 | relative_tc_root = os.path.relpath(canonical_testcase_root, |
| 1476 | start=canonical_zephyr_base) |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1477 | else: |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 1478 | relative_tc_root = "" |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1479 | |
Marc Herbert | 1c8632c | 2019-04-15 17:58:45 -0700 | [diff] [blame] | 1480 | # workdir can be "." |
| 1481 | unique = os.path.normpath(os.path.join(relative_tc_root, workdir, name)) |
Anas Nashif | f18ad9d | 2018-11-20 09:03:17 -0500 | [diff] [blame] | 1482 | return unique |
| 1483 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1484 | @staticmethod |
| 1485 | def scan_file(inf_name): |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1486 | suite_regex = re.compile( |
Inaky Perez-Gonzalez | 75e2d90 | 2018-04-26 00:17:01 -0700 | [diff] [blame] | 1487 | # do not match until end-of-line, otherwise we won't allow |
| 1488 | # stc_regex below to catch the ones that are declared in the same |
| 1489 | # line--as we only search starting the end of this match |
| 1490 | br"^\s*ztest_test_suite\(\s*(?P<suite_name>[a-zA-Z0-9_]+)\s*,", |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1491 | re.MULTILINE) |
| 1492 | stc_regex = re.compile( |
Inaky Perez-Gonzalez | 75e2d90 | 2018-04-26 00:17:01 -0700 | [diff] [blame] | 1493 | br"^\s*" # empy space at the beginning is ok |
| 1494 | # catch the case where it is declared in the same sentence, e.g: |
| 1495 | # |
| 1496 | # ztest_test_suite(mutex_complex, ztest_user_unit_test(TESTNAME)); |
| 1497 | br"(?:ztest_test_suite\([a-zA-Z0-9_]+,\s*)?" |
| 1498 | # Catch ztest[_user]_unit_test-[_setup_teardown](TESTNAME) |
| 1499 | br"ztest_(?:user_)?unit_test(?:_setup_teardown)?" |
| 1500 | # Consume the argument that becomes the extra testcse |
| 1501 | br"\(\s*" |
| 1502 | br"(?P<stc_name>[a-zA-Z0-9_]+)" |
| 1503 | # _setup_teardown() variant has two extra arguments that we ignore |
| 1504 | br"(?:\s*,\s*[a-zA-Z0-9_]+\s*,\s*[a-zA-Z0-9_]+)?" |
| 1505 | br"\s*\)", |
| 1506 | # We don't check how it finishes; we don't care |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1507 | re.MULTILINE) |
| 1508 | suite_run_regex = re.compile( |
| 1509 | br"^\s*ztest_run_test_suite\((?P<suite_name>[a-zA-Z0-9_]+)\)", |
| 1510 | re.MULTILINE) |
| 1511 | achtung_regex = re.compile( |
| 1512 | br"(#ifdef|#endif)", |
| 1513 | re.MULTILINE) |
| 1514 | warnings = None |
| 1515 | |
| 1516 | with open(inf_name) as inf: |
| 1517 | with contextlib.closing(mmap.mmap(inf.fileno(), 0, mmap.MAP_PRIVATE, |
| 1518 | mmap.PROT_READ, 0)) as main_c: |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1519 | suite_regex_match = suite_regex.search(main_c) |
| 1520 | if not suite_regex_match: |
| 1521 | # can't find ztest_test_suite, maybe a client, because |
| 1522 | # it includes ztest.h |
| 1523 | return None, None |
| 1524 | |
| 1525 | suite_run_match = suite_run_regex.search(main_c) |
| 1526 | if not suite_run_match: |
| 1527 | raise ValueError("can't find ztest_run_test_suite") |
| 1528 | |
| 1529 | achtung_matches = re.findall( |
| 1530 | achtung_regex, |
| 1531 | main_c[suite_regex_match.end():suite_run_match.start()]) |
| 1532 | if achtung_matches: |
| 1533 | warnings = "found invalid %s in ztest_test_suite()" \ |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1534 | % ", ".join({match.decode() for match in achtung_matches}) |
Inaky Perez-Gonzalez | 75e2d90 | 2018-04-26 00:17:01 -0700 | [diff] [blame] | 1535 | _matches = re.findall( |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1536 | stc_regex, |
| 1537 | main_c[suite_regex_match.end():suite_run_match.start()]) |
Inaky Perez-Gonzalez | 75e2d90 | 2018-04-26 00:17:01 -0700 | [diff] [blame] | 1538 | matches = [ match.decode().replace("test_", "") for match in _matches ] |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1539 | return matches, warnings |
| 1540 | |
| 1541 | def scan_path(self, path): |
| 1542 | subcases = [] |
| 1543 | for filename in glob.glob(os.path.join(path, "src", "*.c")): |
| 1544 | try: |
| 1545 | _subcases, warnings = self.scan_file(filename) |
| 1546 | if warnings: |
Inaky Perez-Gonzalez | 75e2d90 | 2018-04-26 00:17:01 -0700 | [diff] [blame] | 1547 | error("%s: %s" % (filename, warnings)) |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1548 | if _subcases: |
| 1549 | subcases += _subcases |
| 1550 | except ValueError as e: |
Flavio Ceolin | bf878ce | 2019-04-19 22:24:09 -0700 | [diff] [blame] | 1551 | error("%s: can't find: %s" % (filename, e)) |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1552 | return subcases |
| 1553 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1554 | def parse_subcases(self, test_path): |
| 1555 | results = self.scan_path(os.path.dirname(test_path)) |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1556 | for sub in results: |
Inaky Perez-Gonzalez | 75e2d90 | 2018-04-26 00:17:01 -0700 | [diff] [blame] | 1557 | name = "{}.{}".format(self.id, sub) |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1558 | self.cases.append(name) |
| 1559 | |
Anas Nashif | f16e92c | 2019-03-31 16:58:12 -0400 | [diff] [blame] | 1560 | if not results: |
| 1561 | self.cases.append(self.id) |
| 1562 | |
Anas Nashif | aae71d7 | 2018-04-21 22:26:48 -0500 | [diff] [blame] | 1563 | |
Anas Nashif | 75547e2 | 2018-02-24 08:32:14 -0600 | [diff] [blame] | 1564 | def __str__(self): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1565 | return self.name |
| 1566 | |
| 1567 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1568 | class TestInstance: |
| 1569 | """Class representing the execution of a particular TestCase on a platform |
| 1570 | |
| 1571 | @param test The TestCase object we want to build/execute |
| 1572 | @param platform Platform object that we want to build and run against |
| 1573 | @param base_outdir Base directory for all test results. The actual |
| 1574 | out directory used is <outdir>/<platform>/<test case name> |
| 1575 | """ |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 1576 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1577 | def __init__(self, testcase, platform, base_outdir): |
Anas Nashif | 7dd19ea | 2018-11-14 08:46:49 -0500 | [diff] [blame] | 1578 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1579 | self.testcase = testcase |
| 1580 | self.platform = platform |
| 1581 | |
| 1582 | self.status = None |
| 1583 | self.reason = "N/A" |
| 1584 | self.metrics = dict() |
| 1585 | self.handler = None |
| 1586 | |
| 1587 | |
| 1588 | self.name = os.path.join(platform.name, testcase.name) |
| 1589 | self.build_dir = os.path.join(base_outdir, platform.name, testcase.name) |
| 1590 | |
| 1591 | self.build_only = self.check_build_or_run() |
| 1592 | self.run = not self.build_only |
| 1593 | |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 1594 | self.results = {} |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1595 | |
Marc Herbert | 0f7255c | 2019-04-05 14:14:21 -0700 | [diff] [blame] | 1596 | def __lt__(self, other): |
| 1597 | return self.name < other.name |
| 1598 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1599 | def check_build_or_run(self): |
Anas Nashif | 7dd19ea | 2018-11-14 08:46:49 -0500 | [diff] [blame] | 1600 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1601 | build_only = True |
| 1602 | |
| 1603 | # we asked for build-only on the command line |
| 1604 | if options.build_only: |
| 1605 | return True |
| 1606 | |
| 1607 | # The testcase is designed to be build only. |
| 1608 | if self.testcase.build_only: |
| 1609 | return True |
| 1610 | |
| 1611 | # Do not run slow tests: |
| 1612 | skip_slow = self.testcase.slow and not options.enable_slow |
| 1613 | if skip_slow: |
| 1614 | return True |
| 1615 | |
| 1616 | runnable =bool(self.testcase.type == "unit" or \ |
| 1617 | self.platform.type == "native" or \ |
| 1618 | self.platform.simulation in ["nsim", "renode", "qemu"] or \ |
| 1619 | options.device_testing) |
| 1620 | |
| 1621 | if self.platform.simulation == "nsim": |
| 1622 | if not find_executable("nsimdrv"): |
| 1623 | runnable = False |
| 1624 | |
| 1625 | if self.platform.simulation == "renode": |
| 1626 | if not find_executable("renode"): |
| 1627 | runnable = False |
| 1628 | |
| 1629 | # console harness allows us to run the test and capture data. |
| 1630 | if self.testcase.harness == 'console': |
| 1631 | |
| 1632 | # if we have a fixture that is also being supplied on the |
| 1633 | # command-line, then we need to run the test, not just build it. |
| 1634 | if "fixture" in self.testcase.harness_config: |
| 1635 | fixture = self.testcase.harness_config['fixture'] |
| 1636 | if fixture in options.fixture: |
| 1637 | build_only = False |
| 1638 | else: |
| 1639 | build_only = True |
| 1640 | else: |
| 1641 | build_only = False |
| 1642 | elif self.testcase.harness: |
| 1643 | build_only = True |
| 1644 | else: |
| 1645 | build_only = False |
| 1646 | |
| 1647 | return not (not build_only and runnable) |
Anas Nashif | 7dd19ea | 2018-11-14 08:46:49 -0500 | [diff] [blame] | 1648 | |
Anas Nashif | dbd7649 | 2018-11-23 20:24:19 -0500 | [diff] [blame] | 1649 | def create_overlay(self, platform): |
Marc Herbert | c7633de | 2019-07-06 15:52:31 -0700 | [diff] [blame] | 1650 | # Create this in a "sanitycheck/" subdirectory otherwise this |
| 1651 | # will pass this overlay to kconfig.py *twice* and kconfig.cmake |
| 1652 | # will silently give that second time precedence over any |
| 1653 | # --extra-args=CONFIG_* |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1654 | subdir = os.path.join(self.build_dir, "sanitycheck") |
Marc Herbert | c7633de | 2019-07-06 15:52:31 -0700 | [diff] [blame] | 1655 | os.makedirs(subdir, exist_ok=True) |
| 1656 | file = os.path.join(subdir, "testcase_extra.conf") |
Anas Nashif | f4d8ecc | 2019-03-02 15:43:23 -0500 | [diff] [blame] | 1657 | with open(file, "w") as f: |
| 1658 | content = "" |
Anas Nashif | 3cbffef | 2018-11-07 23:50:54 -0500 | [diff] [blame] | 1659 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1660 | if self.testcase.extra_configs: |
| 1661 | content = "\n".join(self.testcase.extra_configs) |
Anas Nashif | 3cbffef | 2018-11-07 23:50:54 -0500 | [diff] [blame] | 1662 | |
Anas Nashif | f4d8ecc | 2019-03-02 15:43:23 -0500 | [diff] [blame] | 1663 | if options.enable_coverage: |
| 1664 | if platform in options.coverage_platform: |
| 1665 | content = content + "\nCONFIG_COVERAGE=y" |
| 1666 | |
| 1667 | f.write(content) |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 1668 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1669 | def calculate_sizes(self): |
| 1670 | """Get the RAM/ROM sizes of a test case. |
| 1671 | |
| 1672 | This can only be run after the instance has been executed by |
| 1673 | MakeGenerator, otherwise there won't be any binaries to measure. |
| 1674 | |
| 1675 | @return A SizeCalculator object |
| 1676 | """ |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1677 | fns = glob.glob(os.path.join(self.build_dir, "zephyr", "*.elf")) |
| 1678 | fns.extend(glob.glob(os.path.join(self.build_dir, "zephyr", "*.exe"))) |
Anas Nashif | f8dcad4 | 2016-10-27 18:10:08 -0400 | [diff] [blame] | 1679 | fns = [x for x in fns if not x.endswith('_prebuilt.elf')] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1680 | if len(fns) != 1: |
Andrew Boie | 5d4eb78 | 2015-10-02 10:04:56 -0700 | [diff] [blame] | 1681 | raise BuildError("Missing/multiple output ELF binary") |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1682 | |
| 1683 | return SizeCalculator(fns[0], self.testcase.extra_sections) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1684 | |
| 1685 | def __repr__(self): |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1686 | return "<TestCase %s on %s>" % (self.testcase.name, self.platform.name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 1687 | |
| 1688 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1689 | class CMake(): |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 1690 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1691 | config_re = re.compile('(CONFIG_[A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
| 1692 | dt_re = re.compile('([A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
| 1693 | |
| 1694 | def __init__(self, testcase, platform, source_dir, build_dir): |
| 1695 | |
| 1696 | self.cwd = None |
| 1697 | self.capture_output = True |
| 1698 | |
| 1699 | self.defconfig = {} |
| 1700 | self.cmake_cache = {} |
| 1701 | self.devicetree = {} |
| 1702 | |
| 1703 | self.instance = None |
| 1704 | self.testcase = testcase |
| 1705 | self.platform = platform |
| 1706 | self.source_dir = source_dir |
| 1707 | self.build_dir = build_dir |
| 1708 | self.log = "build.log" |
| 1709 | |
| 1710 | def parse_generated(self): |
| 1711 | self.defconfig = {} |
| 1712 | return {} |
| 1713 | |
| 1714 | def run_build(self, args=[]): |
| 1715 | |
| 1716 | verbose("Building %s for %s" % (self.source_dir, self.platform.name)) |
| 1717 | |
| 1718 | cmake_args = [] |
| 1719 | cmake_args.extend(args) |
| 1720 | cmake = shutil.which('cmake') |
| 1721 | cmd = [cmake] + cmake_args |
| 1722 | kwargs = dict() |
| 1723 | |
| 1724 | if self.capture_output: |
| 1725 | kwargs['stdout'] = subprocess.PIPE |
| 1726 | # CMake sends the output of message() to stderr unless it's STATUS |
| 1727 | kwargs['stderr'] = subprocess.STDOUT |
| 1728 | |
| 1729 | if self.cwd: |
| 1730 | kwargs['cwd'] = self.cwd |
| 1731 | |
| 1732 | p = subprocess.Popen(cmd, **kwargs) |
| 1733 | out, _ = p.communicate() |
| 1734 | |
| 1735 | results = {} |
| 1736 | if p.returncode == 0: |
| 1737 | msg = "Finished building %s for %s" %(self.source_dir, self.platform.name) |
| 1738 | |
| 1739 | self.instance.status = "passed" |
| 1740 | self.instance.reason = "" |
| 1741 | results = {'msg': msg, "returncode": p.returncode, "instance": self.instance} |
| 1742 | |
| 1743 | if out: |
| 1744 | log_msg = out.decode(sys.getdefaultencoding()) |
| 1745 | with open(os.path.join(self.build_dir, self.log), "a") as log: |
| 1746 | log.write(log_msg) |
| 1747 | |
| 1748 | else: |
| 1749 | return None |
| 1750 | else: |
| 1751 | # A real error occurred, raise an exception |
| 1752 | if out: |
| 1753 | log_msg = out.decode(sys.getdefaultencoding()) |
| 1754 | with open(os.path.join(self.build_dir, self.log), "a") as log: |
| 1755 | log.write(log_msg) |
| 1756 | |
| 1757 | overflow_flash = "region `FLASH' overflowed by" |
| 1758 | overflow_ram = "region `RAM' overflowed by" |
| 1759 | |
| 1760 | if log_msg: |
| 1761 | if log_msg.find(overflow_flash) > 0 or log_msg.find(overflow_ram) > 0: |
| 1762 | verbose("RAM/ROM Overflow") |
| 1763 | self.instance.status = "skipped" |
| 1764 | self.instance.reason = "overflow" |
| 1765 | else: |
| 1766 | self.instance.status = "failed" |
| 1767 | self.instance.reason = "Build failure" |
| 1768 | |
| 1769 | results = { |
| 1770 | "returncode": p.returncode, |
| 1771 | "instance": self.instance, |
| 1772 | } |
| 1773 | |
| 1774 | return results |
| 1775 | |
| 1776 | def run_cmake(self, args=[]): |
| 1777 | |
| 1778 | verbose("Running cmake on %s for %s" %(self.source_dir, self.platform.name)) |
| 1779 | |
Anas Nashif | a5984ab | 2019-10-22 07:36:24 -0700 | [diff] [blame] | 1780 | ldflags="-Wl,--fatal-warnings" |
| 1781 | |
| 1782 | #fixme: add additional cflags based on options |
| 1783 | cmake_args = [ |
| 1784 | '-B{}'.format(self.build_dir), |
| 1785 | '-S{}'.format(self.source_dir), |
| 1786 | '-DEXTRA_CFLAGS="-Werror ', |
| 1787 | '-DEXTRA_AFLAGS=-Wa,--fatal-warnings', |
| 1788 | '-DEXTRA_LDFLAGS="{}'.format(ldflags), |
| 1789 | '-G{}'.format(get_generator()[1]) |
| 1790 | ] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 1791 | |
| 1792 | args = ["-D{}".format(a.replace('"', '')) for a in args] |
| 1793 | cmake_args.extend(args) |
| 1794 | |
| 1795 | cmake_opts = ['-DBOARD={}'.format(self.platform.name)] |
| 1796 | cmake_args.extend(cmake_opts) |
| 1797 | |
| 1798 | cmake = shutil.which('cmake') |
| 1799 | cmd = [cmake] + cmake_args |
| 1800 | kwargs = dict() |
| 1801 | |
| 1802 | if self.capture_output: |
| 1803 | kwargs['stdout'] = subprocess.PIPE |
| 1804 | # CMake sends the output of message() to stderr unless it's STATUS |
| 1805 | kwargs['stderr'] = subprocess.STDOUT |
| 1806 | |
| 1807 | if self.cwd: |
| 1808 | kwargs['cwd'] = self.cwd |
| 1809 | |
| 1810 | p = subprocess.Popen(cmd, **kwargs) |
| 1811 | out, _ = p.communicate() |
| 1812 | |
| 1813 | if p.returncode == 0: |
| 1814 | filter_results = self.parse_generated() |
| 1815 | msg = "Finished building %s for %s" %(self.source_dir, self.platform.name) |
| 1816 | |
| 1817 | results = {'msg': msg, 'filter': filter_results} |
| 1818 | |
| 1819 | else: |
| 1820 | self.instance.status = "failed" |
| 1821 | self.instance.reason = "Cmake build failure" |
| 1822 | results = {"returncode": p.returncode} |
| 1823 | |
| 1824 | |
| 1825 | if out: |
| 1826 | with open(os.path.join(self.build_dir, self.log), "a") as log: |
| 1827 | log_msg = out.decode(sys.getdefaultencoding()) |
| 1828 | log.write(log_msg) |
| 1829 | |
| 1830 | return results |
| 1831 | |
| 1832 | |
| 1833 | class FilterBuilder(CMake): |
| 1834 | |
| 1835 | def __init__(self, testcase, platform, source_dir, build_dir): |
| 1836 | super().__init__(testcase, platform, source_dir, build_dir) |
| 1837 | |
| 1838 | self.log = "config-sanitycheck.log" |
| 1839 | |
| 1840 | def parse_generated(self): |
| 1841 | |
| 1842 | if self.platform.name == "unit_testing": |
| 1843 | return {} |
| 1844 | |
| 1845 | _generated_dt_confg = "include/generated/generated_dts_board.conf" |
| 1846 | |
| 1847 | cmake_cache_path = os.path.join(self.build_dir, "CMakeCache.txt") |
| 1848 | dt_config_path = os.path.join(self.build_dir, "zephyr", _generated_dt_confg) |
| 1849 | defconfig_path = os.path.join(self.build_dir, "zephyr", ".config") |
| 1850 | |
| 1851 | with open(defconfig_path, "r") as fp: |
| 1852 | defconfig = {} |
| 1853 | for line in fp.readlines(): |
| 1854 | m = self.config_re.match(line) |
| 1855 | if not m: |
| 1856 | if line.strip() and not line.startswith("#"): |
| 1857 | sys.stderr.write("Unrecognized line %s\n" % line) |
| 1858 | continue |
| 1859 | defconfig[m.group(1)] = m.group(2).strip() |
| 1860 | |
| 1861 | self.defconfig = defconfig |
| 1862 | |
| 1863 | cmake_conf = {} |
| 1864 | try: |
| 1865 | cache = CMakeCache.from_file(cmake_cache_path) |
| 1866 | except FileNotFoundError: |
| 1867 | cache = {} |
| 1868 | |
| 1869 | for k in iter(cache): |
| 1870 | cmake_conf[k.name] = k.value |
| 1871 | |
| 1872 | self.cmake_cache = cmake_conf |
| 1873 | |
| 1874 | dt_conf = {} |
| 1875 | if os.path.exists(dt_config_path): |
| 1876 | with open(dt_config_path, "r") as fp: |
| 1877 | for line in fp.readlines(): |
| 1878 | m = self.dt_re.match(line) |
| 1879 | if not m: |
| 1880 | if line.strip() and not line.startswith("#"): |
| 1881 | sys.stderr.write("Unrecognized line %s\n" % line) |
| 1882 | continue |
| 1883 | dt_conf[m.group(1)] = m.group(2).strip() |
| 1884 | self.devicetree = dt_conf |
| 1885 | |
| 1886 | filter_data = { |
| 1887 | "ARCH": self.platform.arch, |
| 1888 | "PLATFORM": self.platform.name |
| 1889 | } |
| 1890 | filter_data.update(os.environ) |
| 1891 | filter_data.update(self.defconfig) |
| 1892 | filter_data.update(self.cmake_cache) |
| 1893 | filter_data.update(self.devicetree) |
| 1894 | |
| 1895 | if self.testcase and self.testcase.tc_filter: |
| 1896 | try: |
| 1897 | res = expr_parser.parse(self.testcase.tc_filter, filter_data) |
| 1898 | except (ValueError, SyntaxError) as se: |
| 1899 | sys.stderr.write( |
| 1900 | "Failed processing %s\n" % self.testcase.yamlfile) |
| 1901 | raise se |
| 1902 | |
| 1903 | if not res: |
| 1904 | return {os.path.join(self.platform.name, self.testcase.name): True} |
| 1905 | else: |
| 1906 | return {os.path.join(self.platform.name, self.testcase.name): False} |
| 1907 | else: |
| 1908 | self.platform.filter_data = filter_data |
| 1909 | return filter_data |
| 1910 | |
| 1911 | |
| 1912 | class ProjectBuilder(FilterBuilder): |
| 1913 | |
| 1914 | def __init__(self, suite, instance): |
| 1915 | super().__init__(instance.testcase, instance.platform, instance.testcase.source_dir, instance.build_dir) |
| 1916 | |
| 1917 | self.log = "build.log" |
| 1918 | self.instance = instance |
| 1919 | self.suite = suite |
| 1920 | |
| 1921 | def setup_handler(self): |
| 1922 | |
| 1923 | instance = self.instance |
| 1924 | args = [] |
| 1925 | |
| 1926 | # FIXME: Needs simplification |
| 1927 | if instance.platform.simulation == "qemu": |
| 1928 | instance.handler = QEMUHandler(instance, "qemu") |
| 1929 | args.append("QEMU_PIPE=%s" % instance.handler.get_fifo()) |
| 1930 | instance.handler.call_make_run = True |
| 1931 | elif instance.testcase.type == "unit": |
| 1932 | instance.handler = BinaryHandler(instance, "unit") |
| 1933 | instance.handler.binary = os.path.join(instance.build_dir, "testbinary") |
| 1934 | elif instance.platform.type == "native": |
| 1935 | instance.handler = BinaryHandler(instance, "native") |
| 1936 | instance.handler.binary = os.path.join(instance.build_dir, "zephyr", "zephyr.exe") |
| 1937 | elif instance.platform.simulation == "nsim": |
| 1938 | if find_executable("nsimdrv"): |
| 1939 | instance.handler = BinaryHandler(instance, "nsim") |
| 1940 | instance.handler.call_make_run = True |
| 1941 | elif instance.platform.simulation == "renode": |
| 1942 | if find_executable("renode"): |
| 1943 | instance.handler = BinaryHandler(instance, "renode") |
| 1944 | instance.handler.pid_fn = os.path.join(instance.build_dir, "renode.pid") |
| 1945 | instance.handler.call_make_run = True |
| 1946 | elif options.device_testing: |
| 1947 | instance.handler = DeviceHandler(instance, "device") |
| 1948 | |
| 1949 | if instance.handler: |
| 1950 | instance.handler.args = args |
| 1951 | |
| 1952 | def process(self, message): |
| 1953 | op = message.get('op') |
| 1954 | |
| 1955 | if not self.instance.handler: |
| 1956 | self.setup_handler() |
| 1957 | |
| 1958 | # The build process, call cmake and build with configured generator |
| 1959 | if op == "cmake": |
| 1960 | results = self.cmake() |
| 1961 | if self.instance.status == "failed": |
| 1962 | pipeline.put({"op": "report", "test": self.instance}) |
| 1963 | elif options.cmake_only: |
| 1964 | pipeline.put({"op": "report", "test": self.instance}) |
| 1965 | else: |
| 1966 | if self.instance.name in results['filter'] and results['filter'][self.instance.name]: |
| 1967 | verbose("filtering %s" % self.instance.name) |
| 1968 | self.instance.status = "skipped" |
| 1969 | self.instance.reason = "filter" |
| 1970 | pipeline.put({"op": "report", "test": self.instance}) |
| 1971 | else: |
| 1972 | pipeline.put({"op": "build", "test": self.instance}) |
| 1973 | |
| 1974 | |
| 1975 | elif op == "build": |
| 1976 | verbose("build test: %s" %self.instance.name) |
| 1977 | results = self.build() |
| 1978 | |
| 1979 | if results.get('returncode', 1) > 0: |
| 1980 | pipeline.put({"op": "report", "test": self.instance}) |
| 1981 | else: |
| 1982 | if self.instance.run: |
| 1983 | pipeline.put({"op": "run", "test": self.instance}) |
| 1984 | else: |
| 1985 | pipeline.put({"op": "report", "test": self.instance}) |
| 1986 | # Run the generated binary using one of the supported handlers |
| 1987 | elif op == "run": |
| 1988 | verbose("run test: %s" %self.instance.name) |
| 1989 | self.run() |
| 1990 | self.instance.status, _ = self.instance.handler.get_state() |
| 1991 | self.instance.reason = "" |
| 1992 | pipeline.put({ |
| 1993 | "op": "report", |
| 1994 | "test": self.instance, |
| 1995 | "state": "executed", |
| 1996 | "status": self.instance.status, |
| 1997 | "reason": self.instance.status} |
| 1998 | ) |
| 1999 | |
| 2000 | # Report results and output progress to screen |
| 2001 | elif op == "report": |
| 2002 | self.report_out() |
| 2003 | |
| 2004 | def report_out(self): |
| 2005 | total_tests_width = len(str(self.suite.total_tests)) |
| 2006 | self.suite.total_done += 1 |
| 2007 | instance = self.instance |
| 2008 | |
| 2009 | if instance.status in ["failed", "timeout"]: |
| 2010 | self.suite.total_failed += 1 |
| 2011 | if VERBOSE or not TERMINAL: |
| 2012 | status = COLOR_RED + "FAILED " + COLOR_NORMAL + instance.reason |
| 2013 | else: |
| 2014 | info( |
| 2015 | "{:<25} {:<50} {}FAILED{}: {}".format( |
| 2016 | instance.platform.name, |
| 2017 | instance.testcase.name, |
| 2018 | COLOR_RED, |
| 2019 | COLOR_NORMAL, |
| 2020 | instance.reason), False) |
| 2021 | |
| 2022 | # FIXME |
| 2023 | h_log = "{}/handler.log".format(instance.build_dir) |
| 2024 | b_log = "{}/build.log".format(instance.build_dir) |
| 2025 | if os.path.exists(h_log): |
| 2026 | log_info("{}".format(h_log)) |
| 2027 | else: |
| 2028 | log_info("{}".format(b_log)) |
| 2029 | |
| 2030 | elif instance.status == "skipped": |
| 2031 | self.suite.total_skipped += 1 |
| 2032 | status = COLOR_YELLOW + "SKIPPED" + COLOR_NORMAL |
| 2033 | |
| 2034 | else: |
| 2035 | status = COLOR_GREEN + "PASSED" + COLOR_NORMAL |
| 2036 | |
| 2037 | if VERBOSE or not TERMINAL: |
| 2038 | if options.cmake_only: |
| 2039 | more_info = "cmake" |
| 2040 | elif instance.status == "skipped": |
| 2041 | more_info = instance.reason |
| 2042 | else: |
| 2043 | if instance.handler and instance.run: |
| 2044 | more_info = instance.handler.type_str |
| 2045 | htime = instance.handler.duration |
| 2046 | if htime: |
| 2047 | more_info += " {:.3f}s".format(htime) |
| 2048 | else: |
| 2049 | more_info = "build" |
| 2050 | |
| 2051 | info("{:>{}}/{} {:<25} {:<50} {} ({})".format( |
| 2052 | self.suite.total_done, total_tests_width, self.suite.total_tests, instance.platform.name, |
| 2053 | instance.testcase.name, status, more_info)) |
| 2054 | |
| 2055 | if instance.status in ["failed", "timeout"]: |
| 2056 | h_log = "{}/handler.log".format(instance.build_dir) |
| 2057 | b_log = "{}/build.log".format(instance.build_dir) |
| 2058 | if os.path.exists(h_log): |
| 2059 | log_info("{}".format(h_log)) |
| 2060 | else: |
| 2061 | log_info("{}".format(b_log)) |
| 2062 | |
| 2063 | else: |
| 2064 | sys.stdout.write("\rtotal complete: %s%4d/%4d%s %2d%% skipped: %s%4d%s, failed: %s%4d%s" % ( |
| 2065 | COLOR_GREEN, |
| 2066 | self.suite.total_done, |
| 2067 | self.suite.total_tests, |
| 2068 | COLOR_NORMAL, |
| 2069 | int((float(self.suite.total_done) / self.suite.total_tests) * 100), |
| 2070 | COLOR_YELLOW if self.suite.total_skipped > 0 else COLOR_NORMAL, |
| 2071 | self.suite.total_skipped, |
| 2072 | COLOR_NORMAL, |
| 2073 | COLOR_RED if self.suite.total_failed > 0 else COLOR_NORMAL, |
| 2074 | self.suite.total_failed, |
| 2075 | COLOR_NORMAL |
| 2076 | ) |
| 2077 | ) |
| 2078 | sys.stdout.flush() |
| 2079 | |
| 2080 | def cmake(self): |
| 2081 | |
| 2082 | instance = self.instance |
| 2083 | args = self.testcase.extra_args[:] |
| 2084 | |
| 2085 | if options.extra_args: |
| 2086 | args += options.extra_args |
| 2087 | |
| 2088 | if instance.handler: |
| 2089 | args += instance.handler.args |
| 2090 | |
| 2091 | # merge overlay files into one variable |
| 2092 | overlays = "" |
| 2093 | idx = 0 |
| 2094 | for arg in args: |
| 2095 | match = re.search('OVERLAY_CONFIG="(.*)"', arg) |
| 2096 | if match: |
| 2097 | overlays += match.group(1) |
| 2098 | del args[idx] |
| 2099 | idx += 1 |
| 2100 | |
| 2101 | if self.testcase.extra_configs or options.coverage: |
| 2102 | args.append("OVERLAY_CONFIG=\"%s %s\"" %(overlays, |
| 2103 | os.path.join(instance.build_dir, |
| 2104 | "sanitycheck", "testcase_extra.conf"))) |
| 2105 | |
| 2106 | results = self.run_cmake(args) |
| 2107 | return results |
| 2108 | |
| 2109 | def build(self): |
| 2110 | results = self.run_build(['--build', self.build_dir]) |
| 2111 | return results |
| 2112 | |
| 2113 | def run(self): |
| 2114 | |
| 2115 | instance = self.instance |
| 2116 | |
| 2117 | if instance.handler.type_str == "device": |
| 2118 | instance.handler.suite = self.suite |
| 2119 | |
| 2120 | instance.handler.handle() |
| 2121 | |
| 2122 | if instance.handler.type_str == "qemu": |
| 2123 | verbose("Running %s (%s)" %(instance.name, instance.handler.type_str)) |
| 2124 | command = [get_generator()[0]] |
| 2125 | command += ["-C", self.build_dir, "run"] |
| 2126 | |
| 2127 | with subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=self.build_dir) as proc: |
| 2128 | verbose("Spawning QEMUHandler Thread for %s" % instance.name) |
| 2129 | proc.wait() |
| 2130 | self.returncode = proc.returncode |
| 2131 | |
| 2132 | sys.stdout.flush() |
| 2133 | |
| 2134 | |
| 2135 | pipeline = queue.LifoQueue() |
| 2136 | |
| 2137 | class BoundedExecutor(concurrent.futures.ThreadPoolExecutor): |
| 2138 | """BoundedExecutor behaves as a ThreadPoolExecutor which will block on |
| 2139 | calls to submit() once the limit given as "bound" work items are queued for |
| 2140 | execution. |
| 2141 | :param bound: Integer - the maximum number of items in the work queue |
| 2142 | :param max_workers: Integer - the size of the thread pool |
| 2143 | """ |
| 2144 | def __init__(self, bound, max_workers, **kwargs): |
| 2145 | super().__init__(max_workers) |
| 2146 | #self.executor = ThreadPoolExecutor(max_workers=max_workers) |
| 2147 | self.semaphore = BoundedSemaphore(bound + max_workers) |
| 2148 | |
| 2149 | def submit(self, fn, *args, **kwargs): |
| 2150 | self.semaphore.acquire() |
| 2151 | try: |
| 2152 | future = super().submit(fn, *args, **kwargs) |
| 2153 | except: |
| 2154 | self.semaphore.release() |
| 2155 | raise |
| 2156 | else: |
| 2157 | future.add_done_callback(lambda x: self.semaphore.release()) |
| 2158 | return future |
| 2159 | |
| 2160 | def shutdown(self, wait=True): |
| 2161 | super().shutdown(wait) |
Andrew Boie | 4ef16c5 | 2015-08-28 12:36:03 -0700 | [diff] [blame] | 2162 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2163 | |
| 2164 | class TestSuite: |
Andrew Boie | 60823c2 | 2017-02-10 09:43:07 -0800 | [diff] [blame] | 2165 | config_re = re.compile('(CONFIG_[A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
Anas Nashif | 1c65b6b | 2018-12-02 19:12:21 -0500 | [diff] [blame] | 2166 | dt_re = re.compile('([A-Za-z0-9_]+)[=]\"?([^\"]*)\"?$') |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2167 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2168 | tc_schema = scl.yaml_load( |
Oleg Zhurakivskyy | 4282208 | 2018-08-17 14:54:41 +0300 | [diff] [blame] | 2169 | os.path.join(ZEPHYR_BASE, |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2170 | "scripts", "sanity_chk", "testcase-schema.yaml")) |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 2171 | |
Anas Nashif | 37f9dc5 | 2018-02-23 08:53:46 -0600 | [diff] [blame] | 2172 | def __init__(self, board_root_list, testcase_roots, outdir): |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2173 | |
| 2174 | self.roots = testcase_roots |
| 2175 | if not isinstance(board_root_list, list): |
| 2176 | self.board_roots= [board_root_list] |
| 2177 | else: |
| 2178 | self.board_roots = board_root_list |
| 2179 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2180 | # Keep track of which test cases we've filtered out and why |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2181 | self.testcases = {} |
| 2182 | self.platforms = [] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2183 | self.default_platforms = [] |
Andrew Boie | b391e66 | 2015-08-31 15:25:45 -0700 | [diff] [blame] | 2184 | self.outdir = os.path.abspath(outdir) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2185 | self.discards = None |
Kumar Gala | c84235e | 2018-04-10 13:32:51 -0500 | [diff] [blame] | 2186 | self.load_errors = 0 |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2187 | self.instances = dict() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2188 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2189 | self.total_tests = 0 # number of test instances |
| 2190 | self.total_cases = 0 # number of test cases |
| 2191 | self.total_done = 0 # tests completed |
| 2192 | self.total_failed = 0 |
| 2193 | self.total_skipped = 0 |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2194 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2195 | self.total_platforms = 0 |
| 2196 | self.start_time = 0 |
| 2197 | self.duration = 0 |
| 2198 | self.warnings = 0 |
| 2199 | self.cv = threading.Condition() |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 2200 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2201 | # hardcoded for now |
| 2202 | self.connected_hardware = [] |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 2203 | |
Inaky Perez-Gonzalez | 662dde6 | 2017-07-24 10:24:35 -0700 | [diff] [blame] | 2204 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2205 | if options.jobs: |
| 2206 | self.jobs = options.jobs |
| 2207 | elif options.build_only: |
| 2208 | self.jobs = multiprocessing.cpu_count() * 2 |
Andy Ross | 9c9162d | 2019-01-03 10:50:53 -0800 | [diff] [blame] | 2209 | else: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2210 | self.jobs = multiprocessing.cpu_count() |
Daniel Leung | 6b17007 | 2016-04-07 12:10:25 -0700 | [diff] [blame] | 2211 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2212 | info("JOBS: %d" % self.jobs) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2213 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2214 | def update(self): |
| 2215 | self.total_tests = len(self.instances) |
| 2216 | self.total_cases = len(self.testcases) |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 2217 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2218 | |
| 2219 | def compare_metrics(self, filename): |
| 2220 | # name, datatype, lower results better |
| 2221 | interesting_metrics = [("ram_size", int, True), |
| 2222 | ("rom_size", int, True)] |
| 2223 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2224 | |
| 2225 | if not os.path.exists(filename): |
| 2226 | info("Cannot compare metrics, %s not found" % filename) |
| 2227 | return [] |
| 2228 | |
| 2229 | results = [] |
| 2230 | saved_metrics = {} |
| 2231 | with open(filename) as fp: |
| 2232 | cr = csv.DictReader(fp) |
| 2233 | for row in cr: |
| 2234 | d = {} |
| 2235 | for m, _, _ in interesting_metrics: |
| 2236 | d[m] = row[m] |
| 2237 | saved_metrics[(row["test"], row["platform"])] = d |
| 2238 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2239 | for instance in self.instances.values(): |
| 2240 | mkey = (instance.testcase.name, instance.platform.name) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2241 | if mkey not in saved_metrics: |
| 2242 | continue |
| 2243 | sm = saved_metrics[mkey] |
| 2244 | for metric, mtype, lower_better in interesting_metrics: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2245 | if metric not in instance.metrics: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2246 | continue |
| 2247 | if sm[metric] == "": |
| 2248 | continue |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2249 | delta = instance.metrics.get(metric, 0) - mtype(sm[metric]) |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 2250 | if delta == 0: |
| 2251 | continue |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2252 | results.append((instance, metric, instance.metrics.get(metric, 0 ), delta, |
Andrew Boie | ea7928f | 2015-08-14 14:27:38 -0700 | [diff] [blame] | 2253 | lower_better)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2254 | return results |
| 2255 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2256 | def misc_reports(self, report, show_footprint, all_deltas, |
| 2257 | footprint_threshold, last_metrics): |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2258 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2259 | if not report: |
| 2260 | return |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2261 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2262 | deltas = self.compare_metrics(report) |
| 2263 | warnings = 0 |
| 2264 | if deltas and show_footprint: |
| 2265 | for i, metric, value, delta, lower_better in deltas: |
| 2266 | if not all_deltas and ((delta < 0 and lower_better) or |
| 2267 | (delta > 0 and not lower_better)): |
| 2268 | continue |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2269 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2270 | percentage = (float(delta) / float(value - delta)) |
| 2271 | if not all_deltas and (percentage < |
| 2272 | (footprint_threshold / 100.0)): |
| 2273 | continue |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2274 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2275 | info("{:<25} {:<60} {}{}{}: {} {:<+4}, is now {:6} {:+.2%}".format( |
| 2276 | i.platform.name, i.testcase.name, COLOR_YELLOW, |
| 2277 | "INFO" if all_deltas else "WARNING", COLOR_NORMAL, |
| 2278 | metric, delta, value, percentage)) |
| 2279 | warnings += 1 |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2280 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2281 | if warnings: |
| 2282 | info("Deltas based on metrics from last %s" % |
| 2283 | ("release" if not last_metrics else "run")) |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 2284 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2285 | def summary(self, unrecognized_sections): |
| 2286 | failed = 0 |
| 2287 | for instance in self.instances.values(): |
| 2288 | if instance.status == "failed": |
| 2289 | failed += 1 |
| 2290 | elif instance.metrics.get("unrecognized") and not unrecognized_sections: |
| 2291 | info("%sFAILED%s: %s has unrecognized binary sections: %s" % |
| 2292 | (COLOR_RED, COLOR_NORMAL, instance.name, |
| 2293 | str(instance.metrics.get("unrecognized", [])))) |
| 2294 | failed += 1 |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2295 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2296 | if self.total_tests and self.total_tests != self.total_skipped: |
| 2297 | pass_rate = (float(self.total_tests - self.total_failed - self.total_skipped)/ float(self.total_tests - self.total_skipped)) |
| 2298 | else: |
| 2299 | pass_rate = 0 |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2300 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2301 | info("{}{} of {}{} tests passed ({:.2%}), {}{}{} failed, {} skipped with {}{}{} warnings in {:.2f} seconds".format( |
| 2302 | COLOR_RED if failed else COLOR_GREEN, |
| 2303 | self.total_tests - self.total_failed - self.total_skipped, |
| 2304 | self.total_tests, |
| 2305 | COLOR_NORMAL, |
| 2306 | pass_rate, |
| 2307 | COLOR_RED if self.total_failed else COLOR_NORMAL, |
| 2308 | self.total_failed, |
| 2309 | COLOR_NORMAL, |
| 2310 | self.total_skipped, |
| 2311 | COLOR_YELLOW if self.warnings else COLOR_NORMAL, |
| 2312 | self.warnings, |
| 2313 | COLOR_NORMAL, |
| 2314 | self.duration)) |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2315 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2316 | platforms = set(p.platform for p in self.instances.values()) |
| 2317 | self.total_platforms = len(self.platforms) |
| 2318 | if self.platforms: |
| 2319 | info("In total {} test cases were executed on {} out of total {} platforms ({:02.2f}%)".format( |
| 2320 | self.total_cases, |
| 2321 | len(platforms), |
| 2322 | self.total_platforms, |
| 2323 | (100 * len(platforms) / len(self.platforms)) |
| 2324 | )) |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2325 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2326 | def save_reports(self): |
| 2327 | if not self.instances: |
| 2328 | return |
Anas Nashif | 61e2163 | 2018-04-08 13:30:16 -0500 | [diff] [blame] | 2329 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2330 | report_name = "sanitycheck" |
| 2331 | if options.report_name: |
| 2332 | report_name = options.report_name |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2333 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2334 | if options.report_dir: |
Paul Sokolovsky | 3ea1869 | 2019-10-15 17:18:39 +0300 | [diff] [blame] | 2335 | os.makedirs(options.report_dir, exist_ok=True) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2336 | filename = os.path.join(options.report_dir, report_name) |
| 2337 | outdir = options.report_dir |
| 2338 | else: |
| 2339 | filename = os.path.join(options.outdir, report_name) |
| 2340 | outdir = options.outdir |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 2341 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2342 | if not options.no_update: |
| 2343 | self.xunit_report(filename + ".xml") |
| 2344 | self.csv_report(filename + ".csv") |
| 2345 | self.target_report(outdir) |
| 2346 | if self.discards: |
| 2347 | self.discard_report(filename + "_discard.csv") |
| 2348 | |
| 2349 | if options.release: |
| 2350 | self.csv_report(RELEASE_DATA) |
| 2351 | |
| 2352 | if log_file: |
| 2353 | log_file.close() |
| 2354 | |
| 2355 | def load_hardware_map_from_cmdline(self, serial, platform): |
| 2356 | device = { |
| 2357 | "serial": serial, |
| 2358 | "platform": platform, |
| 2359 | "counter": 0, |
| 2360 | "available": True, |
| 2361 | "connected": True |
| 2362 | } |
| 2363 | self.connected_hardware = [device] |
| 2364 | |
| 2365 | def load_hardware_map(self, map_file): |
| 2366 | with open(map_file, 'r') as stream: |
| 2367 | try: |
| 2368 | self.connected_hardware = yaml.safe_load(stream) |
| 2369 | except yaml.YAMLError as exc: |
| 2370 | print(exc) |
| 2371 | for i in self.connected_hardware: |
| 2372 | i['counter'] = 0 |
| 2373 | |
| 2374 | def add_configurations(self): |
| 2375 | |
| 2376 | for board_root in self.board_roots: |
| 2377 | board_root = os.path.abspath(board_root) |
| 2378 | |
| 2379 | debug("Reading platform configuration files under %s..." % |
| 2380 | board_root) |
| 2381 | |
| 2382 | for file in glob.glob(os.path.join(board_root, "*", "*", "*.yaml")): |
| 2383 | verbose("Found plaform configuration " + file) |
| 2384 | try: |
| 2385 | platform = Platform() |
| 2386 | platform.load(file) |
| 2387 | if platform.sanitycheck: |
| 2388 | self.platforms.append(platform) |
| 2389 | if platform.default: |
| 2390 | self.default_platforms.append(platform.name) |
| 2391 | |
| 2392 | except RuntimeError as e: |
| 2393 | error("E: %s: can't load: %s" % (file, e)) |
| 2394 | self.load_errors += 1 |
| 2395 | |
| 2396 | @staticmethod |
| 2397 | def get_toolchain(): |
| 2398 | toolchain = os.environ.get("ZEPHYR_TOOLCHAIN_VARIANT", None) or \ |
| 2399 | os.environ.get("ZEPHYR_GCC_VARIANT", None) |
| 2400 | |
| 2401 | if toolchain == "gccarmemb": |
| 2402 | # Remove this translation when gccarmemb is no longer supported. |
| 2403 | toolchain = "gnuarmemb" |
| 2404 | |
Anas Nashif | b4bdd66 | 2018-08-15 17:12:28 -0500 | [diff] [blame] | 2405 | try: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2406 | if not toolchain: |
| 2407 | raise SanityRuntimeError("E: Variable ZEPHYR_TOOLCHAIN_VARIANT is not defined") |
Anas Nashif | b4bdd66 | 2018-08-15 17:12:28 -0500 | [diff] [blame] | 2408 | except Exception as e: |
| 2409 | print(str(e)) |
| 2410 | sys.exit(2) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2411 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2412 | return toolchain |
| 2413 | |
| 2414 | |
| 2415 | def add_testcases(self): |
| 2416 | for root in self.roots: |
| 2417 | root = os.path.abspath(root) |
| 2418 | |
| 2419 | debug("Reading test case configuration files under %s..." %root) |
| 2420 | |
| 2421 | for dirpath, dirnames, filenames in os.walk(root, topdown=True): |
| 2422 | verbose("scanning %s" % dirpath) |
| 2423 | if 'sample.yaml' in filenames: |
| 2424 | filename = 'sample.yaml' |
| 2425 | elif 'testcase.yaml' in filenames: |
| 2426 | filename = 'testcase.yaml' |
| 2427 | else: |
| 2428 | continue |
| 2429 | |
| 2430 | verbose("Found possible test case in " + dirpath) |
| 2431 | |
| 2432 | dirnames[:] = [] |
| 2433 | tc_path = os.path.join(dirpath, filename) |
| 2434 | self.add_testcase(tc_path, root) |
| 2435 | |
| 2436 | def add_testcase(self, tc_data_file, root): |
| 2437 | try: |
| 2438 | parsed_data = SanityConfigParser(tc_data_file, self.tc_schema) |
| 2439 | parsed_data.load() |
| 2440 | |
| 2441 | tc_path = os.path.dirname(tc_data_file) |
| 2442 | workdir = os.path.relpath(tc_path, root) |
| 2443 | |
| 2444 | for name in parsed_data.tests.keys(): |
| 2445 | tc = TestCase() |
| 2446 | tc.name = tc.get_unique(root, workdir, name) |
| 2447 | |
| 2448 | tc_dict = parsed_data.get_test(name, testcase_valid_keys) |
| 2449 | |
| 2450 | tc.source_dir = tc_path |
| 2451 | tc.yamlfile = tc_data_file |
| 2452 | |
| 2453 | tc.id = name |
| 2454 | tc.type = tc_dict["type"] |
| 2455 | tc.tags = tc_dict["tags"] |
| 2456 | tc.extra_args = tc_dict["extra_args"] |
| 2457 | tc.extra_configs = tc_dict["extra_configs"] |
| 2458 | tc.arch_whitelist = tc_dict["arch_whitelist"] |
| 2459 | tc.arch_exclude = tc_dict["arch_exclude"] |
| 2460 | tc.skip = tc_dict["skip"] |
| 2461 | tc.platform_exclude = tc_dict["platform_exclude"] |
| 2462 | tc.platform_whitelist = tc_dict["platform_whitelist"] |
| 2463 | tc.toolchain_exclude = tc_dict["toolchain_exclude"] |
| 2464 | tc.toolchain_whitelist = tc_dict["toolchain_whitelist"] |
| 2465 | tc.tc_filter = tc_dict["filter"] |
| 2466 | tc.timeout = tc_dict["timeout"] |
| 2467 | tc.harness = tc_dict["harness"] |
| 2468 | tc.harness_config = tc_dict["harness_config"] |
| 2469 | tc.build_only = tc_dict["build_only"] |
| 2470 | tc.build_on_all = tc_dict["build_on_all"] |
| 2471 | tc.slow = tc_dict["slow"] |
| 2472 | tc.min_ram = tc_dict["min_ram"] |
| 2473 | tc.depends_on = tc_dict["depends_on"] |
| 2474 | tc.min_flash = tc_dict["min_flash"] |
| 2475 | tc.extra_sections = tc_dict["extra_sections"] |
| 2476 | |
| 2477 | tc.parse_subcases(tc_path) |
| 2478 | |
| 2479 | if tc.name: |
| 2480 | self.testcases[tc.name] = tc |
| 2481 | |
| 2482 | except Exception as e: |
| 2483 | error("E: %s: can't load (skipping): %s" % (tc_data_file, e)) |
| 2484 | self.load_errors += 1 |
| 2485 | return False |
| 2486 | |
| 2487 | return True |
| 2488 | |
| 2489 | |
| 2490 | def get_platform(self, name): |
| 2491 | selected_platform = None |
| 2492 | for platform in self.platforms: |
| 2493 | if platform.name == name: |
| 2494 | selected_platform = platform |
| 2495 | break |
| 2496 | return selected_platform |
| 2497 | |
| 2498 | def get_last_failed(self): |
| 2499 | last_run = os.path.join(options.outdir, "sanitycheck.csv") |
| 2500 | try: |
| 2501 | if not os.path.exists(last_run): |
| 2502 | raise SanityRuntimeError("Couldn't find last sanitycheck run.: %s" %last_run) |
| 2503 | except Exception as e: |
| 2504 | print(str(e)) |
| 2505 | sys.exit(2) |
| 2506 | |
| 2507 | total_tests = 0 |
| 2508 | with open(last_run, "r") as fp: |
| 2509 | cr = csv.DictReader(fp) |
| 2510 | instance_list = [] |
| 2511 | for row in cr: |
| 2512 | total_tests += 1 |
| 2513 | if row["passed"] == "True": |
| 2514 | continue |
| 2515 | test = row["test"] |
| 2516 | platform = self.get_platform(row["platform"]) |
| 2517 | instance = TestInstance(self.testcases[test], platform, self.outdir) |
| 2518 | instance.create_overlay(platform.name) |
| 2519 | instance_list.append(instance) |
| 2520 | self.add_instances(instance_list) |
| 2521 | |
| 2522 | tests_to_run = len(self.instances) |
| 2523 | info("%d tests passed already, retyring %d tests" %(total_tests - tests_to_run, tests_to_run)) |
| 2524 | |
| 2525 | def load_from_file(self, file): |
| 2526 | try: |
| 2527 | if not os.path.exists(file): |
| 2528 | raise SanityRuntimeError( |
| 2529 | "Couldn't find input file with list of tests.") |
| 2530 | except Exception as e: |
| 2531 | print(str(e)) |
| 2532 | sys.exit(2) |
| 2533 | |
| 2534 | with open(file, "r") as fp: |
| 2535 | cr = csv.DictReader(fp) |
| 2536 | instance_list = [] |
| 2537 | for row in cr: |
| 2538 | if row["arch"] == "arch": |
| 2539 | continue |
| 2540 | test = row["test"] |
| 2541 | platform = self.get_platform(row["platform"]) |
| 2542 | instance = TestInstance(self.testcases[test], platform, self.outdir) |
| 2543 | instance.create_overlay(platform.name) |
| 2544 | instance_list.append(instance) |
| 2545 | self.add_instances(instance_list) |
| 2546 | |
| 2547 | |
| 2548 | def apply_filters(self): |
| 2549 | |
| 2550 | toolchain = self.get_toolchain() |
| 2551 | |
| 2552 | discards = {} |
| 2553 | platform_filter = options.platform |
| 2554 | testcase_filter = run_individual_tests |
| 2555 | arch_filter = options.arch |
| 2556 | tag_filter = options.tag |
| 2557 | exclude_tag = options.exclude_tag |
| 2558 | |
| 2559 | verbose("platform filter: " + str(platform_filter)) |
| 2560 | verbose(" arch_filter: " + str(arch_filter)) |
| 2561 | verbose(" tag_filter: " + str(tag_filter)) |
| 2562 | verbose(" exclude_tag: " + str(exclude_tag)) |
| 2563 | |
| 2564 | default_platforms = False |
| 2565 | |
| 2566 | if platform_filter: |
| 2567 | platforms = list(filter(lambda p: p.name in platform_filter, self.platforms)) |
| 2568 | else: |
| 2569 | platforms = self.platforms |
| 2570 | |
| 2571 | if options.all: |
| 2572 | info("Selecting all possible platforms per test case") |
| 2573 | # When --all used, any --platform arguments ignored |
| 2574 | platform_filter = [] |
| 2575 | elif not platform_filter: |
| 2576 | info("Selecting default platforms per test case") |
| 2577 | default_platforms = True |
| 2578 | |
| 2579 | info("Building initial testcase list...") |
| 2580 | |
| 2581 | for tc_name, tc in self.testcases.items(): |
| 2582 | # list of instances per testcase, aka configurations. |
| 2583 | instance_list = [] |
| 2584 | for plat in platforms: |
| 2585 | instance = TestInstance(tc, plat, self.outdir) |
| 2586 | |
| 2587 | if (plat.arch == "unit") != (tc.type == "unit"): |
| 2588 | # Discard silently |
| 2589 | continue |
| 2590 | |
| 2591 | if options.device_testing and instance.build_only: |
| 2592 | discards[instance] = "Not runnable on device" |
| 2593 | continue |
| 2594 | |
| 2595 | if tc.skip: |
| 2596 | discards[instance] = "Skip filter" |
| 2597 | continue |
| 2598 | |
| 2599 | if tc.build_on_all and not platform_filter: |
| 2600 | platform_filter = [] |
| 2601 | |
| 2602 | if tag_filter and not tc.tags.intersection(tag_filter): |
| 2603 | discards[instance] = "Command line testcase tag filter" |
| 2604 | continue |
| 2605 | |
| 2606 | if exclude_tag and tc.tags.intersection(exclude_tag): |
| 2607 | discards[instance] = "Command line testcase exclude filter" |
| 2608 | continue |
| 2609 | |
| 2610 | if testcase_filter and tc_name not in testcase_filter: |
| 2611 | discards[instance] = "Testcase name filter" |
| 2612 | continue |
| 2613 | |
| 2614 | if arch_filter and plat.arch not in arch_filter: |
| 2615 | discards[instance] = "Command line testcase arch filter" |
| 2616 | continue |
| 2617 | |
| 2618 | if tc.arch_whitelist and plat.arch not in tc.arch_whitelist: |
| 2619 | discards[instance] = "Not in test case arch whitelist" |
| 2620 | continue |
| 2621 | |
| 2622 | if tc.arch_exclude and plat.arch in tc.arch_exclude: |
| 2623 | discards[instance] = "In test case arch exclude" |
| 2624 | continue |
| 2625 | |
| 2626 | if tc.platform_exclude and plat.name in tc.platform_exclude: |
| 2627 | discards[instance] = "In test case platform exclude" |
| 2628 | continue |
| 2629 | |
| 2630 | if tc.toolchain_exclude and toolchain in tc.toolchain_exclude: |
| 2631 | discards[instance] = "In test case toolchain exclude" |
| 2632 | continue |
| 2633 | |
| 2634 | if platform_filter and plat.name not in platform_filter: |
| 2635 | discards[instance] = "Command line platform filter" |
| 2636 | continue |
| 2637 | |
| 2638 | if tc.platform_whitelist and plat.name not in tc.platform_whitelist: |
| 2639 | discards[instance] = "Not in testcase platform whitelist" |
| 2640 | continue |
| 2641 | |
| 2642 | if tc.toolchain_whitelist and toolchain not in tc.toolchain_whitelist: |
| 2643 | discards[instance] = "Not in testcase toolchain whitelist" |
| 2644 | continue |
| 2645 | |
| 2646 | if not plat.env_satisfied: |
| 2647 | discards[instance] = "Environment ({}) not satisfied".format(", ".join(plat.env)) |
| 2648 | continue |
| 2649 | |
| 2650 | if not options.force_toolchain \ |
| 2651 | and toolchain and (toolchain not in plat.supported_toolchains) \ |
| 2652 | and tc.type != 'unit': |
| 2653 | discards[instance] = "Not supported by the toolchain" |
| 2654 | continue |
| 2655 | |
| 2656 | if plat.ram < tc.min_ram: |
| 2657 | discards[instance] = "Not enough RAM" |
| 2658 | continue |
| 2659 | |
| 2660 | if tc.depends_on: |
| 2661 | dep_intersection = tc.depends_on.intersection(set(plat.supported)) |
| 2662 | if dep_intersection != set(tc.depends_on): |
| 2663 | discards[instance] = "No hardware support" |
| 2664 | continue |
| 2665 | |
| 2666 | if plat.flash < tc.min_flash: |
| 2667 | discards[instance] = "Not enough FLASH" |
| 2668 | continue |
| 2669 | |
| 2670 | if set(plat.ignore_tags) & tc.tags: |
| 2671 | discards[instance] = "Excluded tags per platform" |
| 2672 | continue |
| 2673 | |
| 2674 | # if nothing stopped us until now, it means this configuration |
| 2675 | # needs to be added. |
| 2676 | instance_list.append(instance) |
| 2677 | |
| 2678 | # no configurations, so jump to next testcase |
| 2679 | if not instance_list: |
| 2680 | continue |
| 2681 | |
| 2682 | # if sanitycheck was launched with no platform options at all, we |
| 2683 | # take all default platforms |
| 2684 | if default_platforms and not tc.build_on_all: |
| 2685 | if tc.platform_whitelist: |
| 2686 | a = set(self.default_platforms) |
| 2687 | b = set(tc.platform_whitelist) |
| 2688 | c = a.intersection(b) |
| 2689 | if c: |
| 2690 | aa = list( filter( lambda tc: tc.platform.name in c, instance_list)) |
| 2691 | self.add_instances(aa) |
| 2692 | else: |
| 2693 | self.add_instances(instance_list[:1]) |
| 2694 | else: |
| 2695 | instances = list( filter( lambda tc: tc.platform.default, instance_list)) |
| 2696 | self.add_instances(instances) |
| 2697 | |
| 2698 | for instance in list(filter(lambda tc: not tc.platform.default, instance_list)): |
| 2699 | discards[instance] = "Not a default test platform" |
| 2700 | |
| 2701 | else: |
| 2702 | self.add_instances(instance_list) |
| 2703 | |
| 2704 | for _, case in self.instances.items(): |
| 2705 | case.create_overlay(case.platform.name) |
| 2706 | |
| 2707 | self.discards = discards |
| 2708 | |
| 2709 | return discards |
| 2710 | |
| 2711 | def add_instances(self, instance_list): |
| 2712 | for instance in instance_list: |
| 2713 | self.instances[instance.name] = instance |
| 2714 | |
| 2715 | def add_tasks_to_queue(self): |
| 2716 | for instance in self.instances.values(): |
| 2717 | if options.test_only: |
| 2718 | if instance.run: |
| 2719 | pipeline.put({"op": "run", "test": instance, "status": "built"}) |
| 2720 | else: |
| 2721 | if instance.status not in ['passed', 'skipped']: |
| 2722 | instance.status = None |
| 2723 | pipeline.put({"op": "cmake", "test": instance}) |
| 2724 | |
| 2725 | return "DONE FEEDING" |
| 2726 | |
| 2727 | def execute(self): |
| 2728 | def calc_one_elf_size(instance): |
| 2729 | if instance.status not in ["failed", "skipped"]: |
| 2730 | if instance.platform.type != "native": |
| 2731 | size_calc = instance.calculate_sizes() |
| 2732 | instance.metrics["ram_size"] = size_calc.get_ram_size() |
| 2733 | instance.metrics["rom_size"] = size_calc.get_rom_size() |
| 2734 | instance.metrics["unrecognized"] = size_calc.unrecognized_sections() |
| 2735 | else: |
| 2736 | instance.metrics["ram_size"] = 0 |
| 2737 | instance.metrics["rom_size"] = 0 |
| 2738 | instance.metrics["unrecognized"] = [] |
| 2739 | |
| 2740 | instance.metrics["handler_time"] = instance.handler.duration if instance.handler else 0 |
| 2741 | |
| 2742 | info("Adding tasks to the queue...") |
| 2743 | # We can use a with statement to ensure threads are cleaned up promptly |
| 2744 | with BoundedExecutor(bound=self.jobs, max_workers=self.jobs) as executor: |
| 2745 | |
| 2746 | # start a future for a thread which sends work in through the queue |
| 2747 | future_to_test = { |
| 2748 | executor.submit(self.add_tasks_to_queue): 'FEEDER DONE'} |
| 2749 | |
| 2750 | while future_to_test: |
| 2751 | # check for status of the futures which are currently working |
| 2752 | done, _ = concurrent.futures.wait( |
| 2753 | future_to_test, timeout=0.25, |
| 2754 | return_when=concurrent.futures.FIRST_COMPLETED) |
| 2755 | |
| 2756 | # if there is incoming work, start a new future |
| 2757 | while not pipeline.empty(): |
| 2758 | # fetch a url from the queue |
| 2759 | message = pipeline.get() |
| 2760 | test = message['test'] |
| 2761 | |
| 2762 | # Start the load operation and mark the future with its URL |
| 2763 | pb = ProjectBuilder(self, test) |
| 2764 | future_to_test[executor.submit(pb.process, message)] = test.name |
| 2765 | |
| 2766 | # process any completed futures |
| 2767 | for future in done: |
| 2768 | test = future_to_test[future] |
| 2769 | try: |
| 2770 | data = future.result() |
| 2771 | except Exception as exc: |
| 2772 | print('%r generated an exception: %s' % (test, exc)) |
| 2773 | else: |
| 2774 | if data: |
| 2775 | verbose(data) |
| 2776 | |
| 2777 | # remove the now completed future |
| 2778 | del future_to_test[future] |
| 2779 | |
| 2780 | if options.enable_size_report and not options.cmake_only: |
| 2781 | # Parallelize size calculation |
| 2782 | executor = concurrent.futures.ThreadPoolExecutor(self.jobs) |
| 2783 | futures = [executor.submit(calc_one_elf_size, instance) |
| 2784 | for instance in self.instances.values()] |
| 2785 | concurrent.futures.wait(futures) |
| 2786 | else: |
| 2787 | for instance in self.instances.values(): |
| 2788 | instance.metrics["ram_size"] = 0 |
| 2789 | instance.metrics["rom_size"] = 0 |
| 2790 | instance.metrics["handler_time"] = instance.handler.duration if instance.handler else 0 |
| 2791 | instance.metrics["unrecognized"] = [] |
| 2792 | |
| 2793 | |
| 2794 | def discard_report(self, filename): |
| 2795 | |
| 2796 | try: |
| 2797 | if self.discards is None: |
| 2798 | raise SanityRuntimeError("apply_filters() hasn't been run!") |
| 2799 | except Exception as e: |
| 2800 | error(str(e)) |
| 2801 | sys.exit(2) |
| 2802 | |
| 2803 | with open(filename, "wt") as csvfile: |
| 2804 | fieldnames = ["test", "arch", "platform", "reason"] |
| 2805 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 2806 | cw.writeheader() |
| 2807 | for instance, reason in sorted(self.discards.items()): |
| 2808 | rowdict = {"test": instance.testcase.name, |
| 2809 | "arch": instance.platform.arch, |
| 2810 | "platform": instance.platform.name, |
| 2811 | "reason": reason} |
| 2812 | cw.writerow(rowdict) |
| 2813 | |
| 2814 | |
| 2815 | def target_report(self, outdir): |
| 2816 | run = "Sanitycheck" |
| 2817 | eleTestsuite = None |
| 2818 | |
| 2819 | platforms = {inst.platform.name for _,inst in self.instances.items()} |
| 2820 | for platform in platforms: |
| 2821 | errors = 0 |
| 2822 | passes = 0 |
| 2823 | fails = 0 |
| 2824 | duration = 0 |
| 2825 | skips = 0 |
| 2826 | for _, instance in self.instances.items(): |
| 2827 | if instance.platform.name != platform: |
| 2828 | continue |
| 2829 | |
| 2830 | handler_time = instance.metrics.get('handler_time', 0) |
| 2831 | duration += handler_time |
| 2832 | for k in instance.results.keys(): |
| 2833 | if instance.results[k] == 'PASS': |
| 2834 | passes += 1 |
| 2835 | elif instance.results[k] == 'BLOCK': |
| 2836 | errors += 1 |
| 2837 | elif instance.results[k] == 'SKIP': |
| 2838 | skips += 1 |
| 2839 | else: |
| 2840 | fails += 1 |
| 2841 | |
| 2842 | eleTestsuites = ET.Element('testsuites') |
| 2843 | eleTestsuite = ET.SubElement(eleTestsuites, 'testsuite', |
| 2844 | name=run, time="%f" % duration, |
| 2845 | tests="%d" % (errors + passes + fails), |
| 2846 | failures="%d" % fails, |
| 2847 | errors="%d" % errors, skipped="%d" %skips) |
| 2848 | |
| 2849 | handler_time = 0 |
| 2850 | |
| 2851 | # print out test results |
| 2852 | for _, instance in self.instances.items(): |
| 2853 | if instance.platform.name != platform: |
| 2854 | continue |
| 2855 | handler_time = instance.metrics.get('handler_time', 0) |
| 2856 | for k in instance.results.keys(): |
| 2857 | eleTestcase = ET.SubElement( |
| 2858 | eleTestsuite, 'testcase', classname="%s:%s" %(instance.platform.name, os.path.basename(instance.testcase.name)), |
| 2859 | name="%s" % (k), time="%f" %handler_time) |
| 2860 | if instance.results[k] in ['FAIL', 'BLOCK']: |
| 2861 | el = None |
| 2862 | |
| 2863 | if instance.results[k] == 'FAIL': |
| 2864 | el = ET.SubElement( |
| 2865 | eleTestcase, |
| 2866 | 'failure', |
| 2867 | type="failure", |
| 2868 | message="failed") |
| 2869 | elif instance.results[k] == 'BLOCK': |
| 2870 | el = ET.SubElement( |
| 2871 | eleTestcase, |
| 2872 | 'error', |
| 2873 | type="failure", |
| 2874 | message="failed") |
| 2875 | p = os.path.join(options.outdir, instance.platform.name, instance.testcase.name) |
| 2876 | log_file = os.path.join(p, "handler.log") |
| 2877 | |
| 2878 | if os.path.exists(log_file): |
| 2879 | with open(log_file, "rb") as f: |
| 2880 | log = f.read().decode("utf-8") |
| 2881 | filtered_string = ''.join(filter(lambda x: x in string.printable, log)) |
| 2882 | el.text = filtered_string |
| 2883 | |
| 2884 | elif instance.results[k] == 'SKIP': |
| 2885 | el = ET.SubElement( |
| 2886 | eleTestcase, |
| 2887 | 'skipped', |
| 2888 | type="skipped", |
| 2889 | message="Skipped") |
| 2890 | |
| 2891 | |
| 2892 | result = ET.tostring(eleTestsuites) |
| 2893 | with open(os.path.join(outdir, platform + ".xml"), 'wb') as f: |
| 2894 | f.write(result) |
| 2895 | |
| 2896 | |
| 2897 | def xunit_report(self, filename): |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2898 | fails = 0 |
| 2899 | passes = 0 |
| 2900 | errors = 0 |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2901 | skips = 0 |
| 2902 | duration = 0 |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2903 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2904 | for instance in self.instances.values(): |
| 2905 | handler_time = instance.metrics.get('handler_time', 0) |
| 2906 | duration += handler_time |
| 2907 | if instance.status == "failed": |
| 2908 | if instance.reason in ['build_error', 'handler_crash']: |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2909 | errors += 1 |
| 2910 | else: |
| 2911 | fails += 1 |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2912 | elif instance.status == 'skipped': |
| 2913 | skips += 1 |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2914 | else: |
| 2915 | passes += 1 |
| 2916 | |
| 2917 | run = "Sanitycheck" |
| 2918 | eleTestsuite = None |
Anas Nashif | 4f02888 | 2017-12-30 11:48:43 -0500 | [diff] [blame] | 2919 | append = options.only_failed |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2920 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2921 | # When we re-run the tests, we re-use the results and update only with |
| 2922 | # the newly run tests. |
Anas Nashif | 0605fa3 | 2017-05-07 08:51:02 -0400 | [diff] [blame] | 2923 | if os.path.exists(filename) and append: |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2924 | tree = ET.parse(filename) |
| 2925 | eleTestsuites = tree.getroot() |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 2926 | eleTestsuite = tree.findall('testsuite')[0] |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2927 | else: |
| 2928 | eleTestsuites = ET.Element('testsuites') |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 2929 | eleTestsuite = ET.SubElement(eleTestsuites, 'testsuite', |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2930 | name=run, time="%f" % duration, |
| 2931 | tests="%d" % (errors + passes + fails + skips), |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 2932 | failures="%d" % fails, |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2933 | errors="%d" %(errors), skip="%s" %(skips)) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2934 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2935 | for instance in self.instances.values(): |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2936 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2937 | # remove testcases that are a re-run |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2938 | if append: |
| 2939 | for tc in eleTestsuite.findall('testcase'): |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 2940 | if tc.get('classname') == "%s:%s" % ( |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2941 | instance.platform.name, instance.testcase.name): |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2942 | eleTestsuite.remove(tc) |
| 2943 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2944 | handler_time = 0 |
| 2945 | if instance.status != "failed" and instance.handler: |
| 2946 | handler_time = instance.metrics.get("handler_time", 0) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2947 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 2948 | eleTestcase = ET.SubElement( |
| 2949 | eleTestsuite, 'testcase', classname="%s:%s" % |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2950 | (instance.platform.name, instance.testcase.name), name="%s" % |
| 2951 | (instance.testcase.name), time="%f" %handler_time) |
| 2952 | |
| 2953 | if instance.status == "failed": |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 2954 | failure = ET.SubElement( |
| 2955 | eleTestcase, |
| 2956 | 'failure', |
| 2957 | type="failure", |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2958 | message=instance.reason) |
| 2959 | p = ("%s/%s/%s" % (options.outdir, instance.platform.name, instance.testcase.name)) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2960 | bl = os.path.join(p, "build.log") |
Anas Nashif | c96c90a | 2019-02-05 07:38:32 -0500 | [diff] [blame] | 2961 | hl = os.path.join(p, "handler.log") |
| 2962 | log_file = bl |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2963 | if instance.reason != 'Build error': |
Anas Nashif | c96c90a | 2019-02-05 07:38:32 -0500 | [diff] [blame] | 2964 | if os.path.exists(hl): |
| 2965 | log_file = hl |
| 2966 | else: |
| 2967 | log_file = bl |
Anas Nashif | accc8eb | 2017-05-01 16:33:43 -0400 | [diff] [blame] | 2968 | |
Anas Nashif | c96c90a | 2019-02-05 07:38:32 -0500 | [diff] [blame] | 2969 | if os.path.exists(log_file): |
| 2970 | with open(log_file, "rb") as f: |
Anas Nashif | 712d345 | 2017-12-29 22:09:03 -0500 | [diff] [blame] | 2971 | log = f.read().decode("utf-8") |
Anas Nashif | a4c368e | 2018-10-15 09:45:59 -0400 | [diff] [blame] | 2972 | filtered_string = ''.join(filter(lambda x: x in string.printable, log)) |
| 2973 | failure.text = filtered_string |
Anas Nashif | ba4643b | 2018-09-23 09:41:59 -0500 | [diff] [blame] | 2974 | f.close() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2975 | elif instance.status == "skipped": |
| 2976 | ET.SubElement( eleTestcase, 'skipped', type="skipped", message="Skipped") |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2977 | |
| 2978 | result = ET.tostring(eleTestsuites) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2979 | with open(filename, 'wb') as report: |
| 2980 | report.write(result) |
Anas Nashif | b3311ed | 2017-04-13 14:44:48 -0400 | [diff] [blame] | 2981 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2982 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2983 | def csv_report(self, filename): |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 2984 | with open(filename, "wt") as csvfile: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2985 | fieldnames = ["test", "arch", "platform", "passed", "status", |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2986 | "extra_args", "handler", "handler_time", "ram_size", |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2987 | "rom_size"] |
| 2988 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 2989 | cw.writeheader() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2990 | for instance in sorted(self.instances.values()): |
| 2991 | rowdict = {"test": instance.testcase.name, |
| 2992 | "arch": instance.platform.arch, |
| 2993 | "platform": instance.platform.name, |
| 2994 | "extra_args": " ".join(instance.testcase.extra_args), |
| 2995 | "handler": instance.platform.simulation} |
| 2996 | |
| 2997 | if instance.status in ["failed", "timeout"]: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 2998 | rowdict["passed"] = False |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 2999 | rowdict["status"] = instance.reason |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3000 | else: |
| 3001 | rowdict["passed"] = True |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3002 | if instance.handler: |
| 3003 | rowdict["handler_time"] = instance.metrics.get("handler_time", 0) |
| 3004 | ram_size = instance.metrics.get("ram_size", 0) |
| 3005 | rom_size = instance.metrics.get("rom_size", 0) |
| 3006 | rowdict["ram_size"] = ram_size |
| 3007 | rowdict["rom_size"] = rom_size |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3008 | cw.writerow(rowdict) |
| 3009 | |
| 3010 | |
| 3011 | def parse_arguments(): |
| 3012 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3013 | parser = argparse.ArgumentParser( |
| 3014 | description=__doc__, |
| 3015 | formatter_class=argparse.RawDescriptionHelpFormatter) |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 3016 | parser.fromfile_prefix_chars = "+" |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3017 | |
Marc Herbert | 932a33a | 2019-03-12 11:37:53 -0700 | [diff] [blame] | 3018 | case_select = parser.add_argument_group("Test case selection", |
| 3019 | """ |
| 3020 | Artificially long but functional example: |
| 3021 | $ ./scripts/sanitycheck -v \\ |
Marc Herbert | e5cedca | 2019-04-08 14:02:34 -0700 | [diff] [blame] | 3022 | --testcase-root tests/ztest/base \\ |
| 3023 | --testcase-root tests/kernel \\ |
Marc Herbert | 932a33a | 2019-03-12 11:37:53 -0700 | [diff] [blame] | 3024 | --test tests/ztest/base/testing.ztest.verbose_0 \\ |
| 3025 | --test tests/kernel/fifo/fifo_api/kernel.fifo.poll |
| 3026 | |
| 3027 | "kernel.fifo.poll" is one of the test section names in |
| 3028 | __/fifo_api/testcase.yaml |
| 3029 | """) |
Marc Herbert | edf1759 | 2019-03-08 12:39:11 -0800 | [diff] [blame] | 3030 | |
Anas Nashif | 07d54c0 | 2018-07-21 19:29:08 -0500 | [diff] [blame] | 3031 | parser.add_argument("--force-toolchain", action="store_true", |
| 3032 | help="Do not filter based on toolchain, use the set " |
| 3033 | " toolchain unconditionally") |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3034 | parser.add_argument( |
| 3035 | "-p", "--platform", action="append", |
| 3036 | help="Platform filter for testing. This option may be used multiple " |
| 3037 | "times. Testcases will only be built/run on the platforms " |
| 3038 | "specified. If this option is not used, then platforms marked " |
| 3039 | "as default in the platform metadata file will be chosen " |
| 3040 | "to build and test. ") |
| 3041 | parser.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3042 | "-a", "--arch", action="append", |
| 3043 | help="Arch filter for testing. Takes precedence over --platform. " |
| 3044 | "If unspecified, test all arches. Multiple invocations " |
| 3045 | "are treated as a logical 'or' relationship") |
| 3046 | parser.add_argument( |
| 3047 | "-t", "--tag", action="append", |
| 3048 | help="Specify tags to restrict which tests to run by tag value. " |
| 3049 | "Default is to not do any tag filtering. Multiple invocations " |
| 3050 | "are treated as a logical 'or' relationship") |
Anas Nashif | dfa86e2 | 2016-10-24 17:08:56 -0400 | [diff] [blame] | 3051 | parser.add_argument("-e", "--exclude-tag", action="append", |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3052 | help="Specify tags of tests that should not run. " |
| 3053 | "Default is to run all tests with all tags.") |
Marc Herbert | edf1759 | 2019-03-08 12:39:11 -0800 | [diff] [blame] | 3054 | case_select.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3055 | "-f", |
| 3056 | "--only-failed", |
| 3057 | action="store_true", |
| 3058 | help="Run only those tests that failed the previous sanity check " |
| 3059 | "invocation.") |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3060 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3061 | parser.add_argument( |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3062 | "--retry-failed", type=int, default=0, |
| 3063 | help="Retry failing tests again, up to the number of times specified.") |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3064 | |
Marc Herbert | 0c465bb | 2019-03-11 17:28:36 -0700 | [diff] [blame] | 3065 | test_xor_subtest = case_select.add_mutually_exclusive_group() |
| 3066 | |
| 3067 | test_xor_subtest.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3068 | "-s", "--test", action="append", |
| 3069 | help="Run only the specified test cases. These are named by " |
Marc Herbert | e5cedca | 2019-04-08 14:02:34 -0700 | [diff] [blame] | 3070 | "<path/relative/to/Zephyr/base/section.name.in.testcase.yaml>") |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3071 | |
Marc Herbert | 0c465bb | 2019-03-11 17:28:36 -0700 | [diff] [blame] | 3072 | test_xor_subtest.add_argument( |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3073 | "--sub-test", action="append", |
Marc Herbert | 932a33a | 2019-03-12 11:37:53 -0700 | [diff] [blame] | 3074 | help="""Recursively find sub-test functions and run the entire |
| 3075 | test section where they were found, including all sibling test |
| 3076 | functions. Sub-tests are named by: |
| 3077 | section.name.in.testcase.yaml.function_name_without_test_prefix |
| 3078 | Example: kernel.fifo.poll.fifo_loop |
| 3079 | """) |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3080 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3081 | parser.add_argument( |
| 3082 | "-l", "--all", action="store_true", |
| 3083 | help="Build/test on all platforms. Any --platform arguments " |
| 3084 | "ignored.") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3085 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3086 | parser.add_argument( |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3087 | "-o", "--report-dir", |
| 3088 | help="""Output reports containing results of the test run into the |
| 3089 | specified directory. |
| 3090 | The output will be both in CSV and JUNIT format |
| 3091 | (sanitycheck.csv and sanitycheck.xml). |
| 3092 | """) |
| 3093 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3094 | parser.add_argument( |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3095 | "--report-name", |
| 3096 | help="""Create a report with a custom name. |
| 3097 | """) |
| 3098 | |
| 3099 | parser.add_argument("--detailed-report", |
| 3100 | action="store", |
| 3101 | metavar="FILENAME", |
| 3102 | help="""Generate a junit report with detailed testcase results. |
| 3103 | Unlike the CSV file produced by --testcase-report, this XML |
| 3104 | report includes only tests which have run and none which were |
| 3105 | merely built. If an image with multiple tests crashes early then |
| 3106 | later tests are not accounted for either.""") |
| 3107 | |
| 3108 | parser.add_argument("--report-excluded", |
| 3109 | action="store_true", |
| 3110 | help="""List all tests that are never run based on current scope and |
| 3111 | coverage. If you are looking for accurate results, run this with |
| 3112 | --all, but this will take a while...""") |
| 3113 | |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 3114 | parser.add_argument("--compare-report", |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3115 | help="Use this report file for size comparison") |
Daniel Leung | 7f85010 | 2016-04-08 11:07:32 -0700 | [diff] [blame] | 3116 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3117 | parser.add_argument( |
| 3118 | "-B", "--subset", |
| 3119 | help="Only run a subset of the tests, 1/4 for running the first 25%%, " |
| 3120 | "3/5 means run the 3rd fifth of the total. " |
| 3121 | "This option is useful when running a large number of tests on " |
| 3122 | "different hosts to speed up execution time.") |
Anas Nashif | a8a1388 | 2017-12-30 13:01:06 -0500 | [diff] [blame] | 3123 | |
| 3124 | parser.add_argument( |
| 3125 | "-N", "--ninja", action="store_true", |
Anas Nashif | 25f6ab6 | 2018-03-06 07:15:11 -0600 | [diff] [blame] | 3126 | help="Use the Ninja generator with CMake") |
Anas Nashif | a8a1388 | 2017-12-30 13:01:06 -0500 | [diff] [blame] | 3127 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3128 | parser.add_argument( |
| 3129 | "-y", "--dry-run", action="store_true", |
| 3130 | help="Create the filtered list of test cases, but don't actually " |
| 3131 | "run them. Useful if you're just interested in " |
| 3132 | "--discard-report") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3133 | |
Anas Nashif | 75547e2 | 2018-02-24 08:32:14 -0600 | [diff] [blame] | 3134 | parser.add_argument("--list-tags", action="store_true", |
| 3135 | help="list all tags in selected tests") |
| 3136 | |
Marc Herbert | edf1759 | 2019-03-08 12:39:11 -0800 | [diff] [blame] | 3137 | case_select.add_argument("--list-tests", action="store_true", |
Marc Herbert | 932a33a | 2019-03-12 11:37:53 -0700 | [diff] [blame] | 3138 | help="""List of all sub-test functions recursively found in |
| 3139 | all --testcase-root arguments. Note different sub-tests can share |
| 3140 | the same section name and come from different directories. |
| 3141 | The output is flattened and reports --sub-test names only, |
| 3142 | not their directories. For instance net.socket.getaddrinfo_ok |
| 3143 | and net.socket.fd_set belong to different directories. |
| 3144 | """) |
Anas Nashif | c0149cc | 2018-04-14 23:12:58 -0500 | [diff] [blame] | 3145 | |
Anas Nashif | 5d6e7eb | 2018-06-01 09:51:08 -0500 | [diff] [blame] | 3146 | parser.add_argument("--export-tests", action="store", |
| 3147 | metavar="FILENAME", |
| 3148 | help="Export tests case meta-data to a file in CSV format.") |
| 3149 | |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 3150 | |
Anas Nashif | 654ec598 | 2019-04-11 08:38:21 -0400 | [diff] [blame] | 3151 | parser.add_argument("--timestamps", |
| 3152 | action="store_true", |
| 3153 | help="Print all messages with time stamps") |
| 3154 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3155 | parser.add_argument( |
| 3156 | "-r", "--release", action="store_true", |
| 3157 | help="Update the benchmark database with the results of this test " |
| 3158 | "run. Intended to be run by CI when tagging an official " |
| 3159 | "release. This database is used as a basis for comparison " |
| 3160 | "when looking for deltas in metrics such as footprint") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3161 | parser.add_argument("-w", "--warnings-as-errors", action="store_true", |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3162 | help="Treat warning conditions as errors") |
| 3163 | parser.add_argument( |
| 3164 | "-v", |
| 3165 | "--verbose", |
| 3166 | action="count", |
| 3167 | default=0, |
| 3168 | help="Emit debugging information, call multiple times to increase " |
| 3169 | "verbosity") |
| 3170 | parser.add_argument( |
| 3171 | "-i", "--inline-logs", action="store_true", |
| 3172 | help="Upon test failure, print relevant log data to stdout " |
| 3173 | "instead of just a path to it") |
Inaky Perez-Gonzalez | 9a36cb6 | 2016-11-29 10:43:40 -0800 | [diff] [blame] | 3174 | parser.add_argument("--log-file", metavar="FILENAME", action="store", |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3175 | help="log also to file") |
| 3176 | parser.add_argument( |
| 3177 | "-m", "--last-metrics", action="store_true", |
| 3178 | help="Instead of comparing metrics from the last --release, " |
| 3179 | "compare with the results of the previous sanity check " |
| 3180 | "invocation") |
| 3181 | parser.add_argument( |
| 3182 | "-u", |
| 3183 | "--no-update", |
| 3184 | action="store_true", |
| 3185 | help="do not update the results of the last run of the sanity " |
| 3186 | "checks") |
Marc Herbert | edf1759 | 2019-03-08 12:39:11 -0800 | [diff] [blame] | 3187 | case_select.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3188 | "-F", |
| 3189 | "--load-tests", |
| 3190 | metavar="FILENAME", |
| 3191 | action="store", |
Marc Herbert | 932a33a | 2019-03-12 11:37:53 -0700 | [diff] [blame] | 3192 | help="Load list of tests and platforms to be run from file.") |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 3193 | |
Marc Herbert | edf1759 | 2019-03-08 12:39:11 -0800 | [diff] [blame] | 3194 | case_select.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3195 | "-E", |
| 3196 | "--save-tests", |
| 3197 | metavar="FILENAME", |
| 3198 | action="store", |
Marc Herbert | 682961a | 2019-03-20 16:48:49 -0700 | [diff] [blame] | 3199 | help="Append list of tests and platforms to be run to file.") |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 3200 | |
Andy Doan | cbecadd | 2019-02-08 10:19:10 -0600 | [diff] [blame] | 3201 | test_or_build = parser.add_mutually_exclusive_group() |
| 3202 | test_or_build.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3203 | "-b", "--build-only", action="store_true", |
| 3204 | help="Only build the code, do not execute any of it in QEMU") |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3205 | |
Andy Doan | cbecadd | 2019-02-08 10:19:10 -0600 | [diff] [blame] | 3206 | test_or_build.add_argument( |
| 3207 | "--test-only", action="store_true", |
| 3208 | help="""Only run device tests with current artifacts, do not build |
| 3209 | the code""") |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3210 | parser.add_argument( |
Kumar Gala | 84cf9dc | 2019-06-15 09:36:06 -0500 | [diff] [blame] | 3211 | "--cmake-only", action="store_true", |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3212 | help="Only run cmake, do not build or run.") |
Kumar Gala | 84cf9dc | 2019-06-15 09:36:06 -0500 | [diff] [blame] | 3213 | |
| 3214 | parser.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3215 | "-j", "--jobs", type=int, |
Marc Herbert | 9e57338 | 2019-07-03 12:49:42 -0700 | [diff] [blame] | 3216 | help="Number of jobs for building, defaults to number of CPU threads, " |
| 3217 | "overcommited by factor 2 when --build-only") |
Anas Nashif | 73440ea | 2018-02-19 10:57:03 -0600 | [diff] [blame] | 3218 | |
| 3219 | parser.add_argument( |
Anas Nashif | 424a3db | 2018-02-20 08:37:24 -0600 | [diff] [blame] | 3220 | "--show-footprint", action="store_true", |
| 3221 | help="Show footprint statistics and deltas since last release." |
| 3222 | ) |
| 3223 | parser.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3224 | "-H", "--footprint-threshold", type=float, default=5, |
| 3225 | help="When checking test case footprint sizes, warn the user if " |
| 3226 | "the new app size is greater then the specified percentage " |
| 3227 | "from the last release. Default is 5. 0 to warn on any " |
| 3228 | "increase on app size") |
| 3229 | parser.add_argument( |
| 3230 | "-D", "--all-deltas", action="store_true", |
| 3231 | help="Show all footprint deltas, positive or negative. Implies " |
| 3232 | "--footprint-threshold=0") |
Håkon Øye Amundsen | de223cc | 2018-04-25 06:24:25 +0000 | [diff] [blame] | 3233 | parser.add_argument( |
| 3234 | "-O", "--outdir", |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3235 | default=os.path.join(os.getcwd(),"sanity-out"), |
Håkon Øye Amundsen | de223cc | 2018-04-25 06:24:25 +0000 | [diff] [blame] | 3236 | help="Output directory for logs and binaries. " |
Anas Nashif | f114a13 | 2018-11-20 11:51:34 -0500 | [diff] [blame] | 3237 | "Default is 'sanity-out' in the current directory. " |
Håkon Øye Amundsen | de223cc | 2018-04-25 06:24:25 +0000 | [diff] [blame] | 3238 | "This directory will be deleted unless '--no-clean' is set.") |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3239 | parser.add_argument( |
| 3240 | "-n", "--no-clean", action="store_true", |
| 3241 | help="Do not delete the outdir before building. Will result in " |
| 3242 | "faster compilation since builds will be incremental") |
Marc Herbert | edf1759 | 2019-03-08 12:39:11 -0800 | [diff] [blame] | 3243 | case_select.add_argument( |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3244 | "-T", "--testcase-root", action="append", default=[], |
| 3245 | help="Base directory to recursively search for test cases. All " |
| 3246 | "testcase.yaml files under here will be processed. May be " |
Marc Herbert | 932a33a | 2019-03-12 11:37:53 -0700 | [diff] [blame] | 3247 | "called multiple times. Defaults to the 'samples/' and " |
| 3248 | "'tests/' directories at the base of the Zephyr tree.") |
Anas Nashif | 7dd19ea | 2018-11-14 08:46:49 -0500 | [diff] [blame] | 3249 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3250 | board_root_list = ["%s/boards" % ZEPHYR_BASE, |
| 3251 | "%s/scripts/sanity_chk/boards" % ZEPHYR_BASE] |
Anas Nashif | 7dd19ea | 2018-11-14 08:46:49 -0500 | [diff] [blame] | 3252 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3253 | parser.add_argument( |
Thomas Stilwell | 5310510 | 2019-10-23 14:25:09 +0200 | [diff] [blame^] | 3254 | "-A", "--board-root", action="append", default=board_root_list, |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3255 | help="""Directory to search for board configuration files. All .yaml |
| 3256 | files in the directory will be processed. The directory should have the same |
| 3257 | structure in the main Zephyr tree: boards/<arch>/<board_name>/""") |
| 3258 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3259 | parser.add_argument( |
| 3260 | "-z", "--size", action="append", |
| 3261 | help="Don't run sanity checks. Instead, produce a report to " |
| 3262 | "stdout detailing RAM/ROM sizes on the specified filenames. " |
| 3263 | "All other command line arguments ignored.") |
| 3264 | parser.add_argument( |
| 3265 | "-S", "--enable-slow", action="store_true", |
| 3266 | help="Execute time-consuming test cases that have been marked " |
| 3267 | "as 'slow' in testcase.yaml. Normally these are only built.") |
Sebastian Bøe | 03ed095 | 2018-11-13 13:36:19 +0100 | [diff] [blame] | 3268 | parser.add_argument( |
| 3269 | "--disable-unrecognized-section-test", action="store_true", |
| 3270 | default=False, |
| 3271 | help="Skip the 'unrecognized section' test.") |
Andrew Boie | 5512105 | 2016-07-20 11:52:04 -0700 | [diff] [blame] | 3272 | parser.add_argument("-R", "--enable-asserts", action="store_true", |
Kumar Gala | 0d48cbe | 2018-01-26 10:22:20 -0600 | [diff] [blame] | 3273 | default=True, |
Andrew Boie | 29599f6 | 2018-05-24 13:33:09 -0700 | [diff] [blame] | 3274 | help="deprecated, left for compatibility") |
Kumar Gala | 0d48cbe | 2018-01-26 10:22:20 -0600 | [diff] [blame] | 3275 | parser.add_argument("--disable-asserts", action="store_false", |
| 3276 | dest="enable_asserts", |
Andrew Boie | 29599f6 | 2018-05-24 13:33:09 -0700 | [diff] [blame] | 3277 | help="deprecated, left for compatibility") |
Anas Nashif | e3febe9 | 2016-11-30 14:25:44 -0500 | [diff] [blame] | 3278 | parser.add_argument("-Q", "--error-on-deprecations", action="store_false", |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3279 | help="Error on deprecation warnings.") |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3280 | parser.add_argument("--enable-size-report", action="store_true", |
| 3281 | help="Enable expensive computation of RAM/ROM segment sizes.") |
Sebastian Bøe | c218261 | 2017-11-09 12:25:02 +0100 | [diff] [blame] | 3282 | |
| 3283 | parser.add_argument( |
| 3284 | "-x", "--extra-args", action="append", default=[], |
Alberto Escolar Piedras | 25e0636 | 2018-02-10 17:40:33 +0100 | [diff] [blame] | 3285 | help="""Extra CMake cache entries to define when building test cases. |
| 3286 | May be called multiple times. The key-value entries will be |
Sebastian Bøe | c218261 | 2017-11-09 12:25:02 +0100 | [diff] [blame] | 3287 | prefixed with -D before being passed to CMake. |
| 3288 | |
| 3289 | E.g |
| 3290 | "sanitycheck -x=USE_CCACHE=0" |
| 3291 | will translate to |
| 3292 | "cmake -DUSE_CCACHE=0" |
| 3293 | |
| 3294 | which will ultimately disable ccache. |
| 3295 | """ |
| 3296 | ) |
Michael Scott | 421ce46 | 2019-06-18 09:37:46 -0700 | [diff] [blame] | 3297 | |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 3298 | parser.add_argument( |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3299 | "--device-testing", action="store_true", |
| 3300 | help="Test on device directly. Specify the serial device to " |
| 3301 | "use with the --device-serial option.") |
| 3302 | |
| 3303 | parser.add_argument( |
| 3304 | "-X", "--fixture", action="append", default=[], |
| 3305 | help="Specify a fixture that a board might support") |
| 3306 | parser.add_argument( |
| 3307 | "--device-serial", |
| 3308 | help="Serial device for accessing the board (e.g., /dev/ttyACM0)") |
| 3309 | |
| 3310 | parser.add_argument("--generate-hardware-map", |
| 3311 | help="""Probe serial devices connected to this platform |
| 3312 | and create a hardware map file to be used with |
| 3313 | --device-testing |
| 3314 | """) |
| 3315 | |
| 3316 | parser.add_argument("--hardware-map", |
| 3317 | help="""Load hardware map from a file. This will be used |
| 3318 | for testing on hardware that is listed in the file. |
| 3319 | """) |
| 3320 | |
| 3321 | parser.add_argument( |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 3322 | "--west-flash", nargs='?', const=[], |
| 3323 | help="""Uses west instead of ninja or make to flash when running with |
Jorgen Kvalvaag | d50fb31 | 2019-09-02 15:25:20 +0200 | [diff] [blame] | 3324 | --device-testing. Supports comma-separated argument list. |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 3325 | |
Michael Scott | 4ca5439 | 2019-07-09 14:21:30 -0700 | [diff] [blame] | 3326 | E.g "sanitycheck --device-testing --device-serial /dev/ttyACM0 |
Jorgen Kvalvaag | d50fb31 | 2019-09-02 15:25:20 +0200 | [diff] [blame] | 3327 | --west-flash="--board-id=foobar,--erase" |
| 3328 | will translate to "west flash -- --board-id=foobar --erase" |
Michael Scott | 4ca5439 | 2019-07-09 14:21:30 -0700 | [diff] [blame] | 3329 | |
| 3330 | NOTE: device-testing must be enabled to use this option. |
Andy Doan | 79c4884 | 2019-02-08 10:09:04 -0600 | [diff] [blame] | 3331 | """ |
| 3332 | ) |
Michael Scott | 421ce46 | 2019-06-18 09:37:46 -0700 | [diff] [blame] | 3333 | parser.add_argument( |
| 3334 | "--west-runner", |
| 3335 | help="""Uses the specified west runner instead of default when running |
| 3336 | with --west-flash. |
| 3337 | |
| 3338 | E.g "sanitycheck --device-testing --device-serial /dev/ttyACM0 |
| 3339 | --west-flash --west-runner=pyocd" |
| 3340 | will translate to "west flash --runner pyocd" |
| 3341 | |
| 3342 | NOTE: west-flash must be enabled to use this option. |
| 3343 | """ |
| 3344 | ) |
Andrew Boie | 8047a6f | 2019-07-02 15:43:29 -0700 | [diff] [blame] | 3345 | |
Alberto Escolar Piedras | c026c2e | 2018-06-21 09:30:20 +0200 | [diff] [blame] | 3346 | parser.add_argument("--enable-coverage", action="store_true", |
Anas Nashif | 8d72bb9 | 2018-11-07 23:05:42 -0500 | [diff] [blame] | 3347 | help="Enable code coverage using gcov.") |
Alberto Escolar Piedras | c026c2e | 2018-06-21 09:30:20 +0200 | [diff] [blame] | 3348 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3349 | parser.add_argument("-C", "--coverage", action="store_true", |
Andrew Boie | 8047a6f | 2019-07-02 15:43:29 -0700 | [diff] [blame] | 3350 | help="Generate coverage reports. Implies " |
| 3351 | "--enable_coverage and --enable-slow") |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3352 | |
Andrew Boie | 8047a6f | 2019-07-02 15:43:29 -0700 | [diff] [blame] | 3353 | parser.add_argument("--coverage-platform", action="append", default=[], |
Anas Nashif | dbd7649 | 2018-11-23 20:24:19 -0500 | [diff] [blame] | 3354 | help="Plarforms to run coverage reports on. " |
Andrew Boie | 8047a6f | 2019-07-02 15:43:29 -0700 | [diff] [blame] | 3355 | "This option may be used multiple times. " |
| 3356 | "Default to what was selected with --platform.") |
Anas Nashif | dbd7649 | 2018-11-23 20:24:19 -0500 | [diff] [blame] | 3357 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3358 | parser.add_argument("--gcov-tool", default=None, |
| 3359 | help="Path to the gcov tool to use for code coverage " |
| 3360 | "reports") |
| 3361 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3362 | return parser.parse_args() |
| 3363 | |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3364 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3365 | def log_info(filename): |
Mazen NEIFER | 8f1dd3a | 2017-02-04 14:32:04 +0100 | [diff] [blame] | 3366 | filename = os.path.relpath(os.path.realpath(filename)) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3367 | if options.inline_logs: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 3368 | info("{:-^100}".format(filename)) |
Andrew Boie | d01535c | 2017-01-10 13:15:02 -0800 | [diff] [blame] | 3369 | |
| 3370 | try: |
| 3371 | with open(filename) as fp: |
| 3372 | data = fp.read() |
| 3373 | except Exception as e: |
| 3374 | data = "Unable to read log data (%s)\n" % (str(e)) |
| 3375 | |
| 3376 | sys.stdout.write(data) |
| 3377 | if log_file: |
| 3378 | log_file.write(data) |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 3379 | info("{:-^100}".format(filename)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3380 | else: |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 3381 | info("\tsee: " + COLOR_YELLOW + filename + COLOR_NORMAL) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3382 | |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 3383 | def size_report(sc): |
| 3384 | info(sc.filename) |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 3385 | info("SECTION NAME VMA LMA SIZE HEX SZ TYPE") |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 3386 | for i in range(len(sc.sections)): |
| 3387 | v = sc.sections[i] |
| 3388 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 3389 | info("%-17s 0x%08x 0x%08x %8d 0x%05x %-7s" % |
| 3390 | (v["name"], v["virt_addr"], v["load_addr"], v["size"], v["size"], |
| 3391 | v["type"])) |
Andrew Boie | 9882dcd | 2015-10-07 14:25:51 -0700 | [diff] [blame] | 3392 | |
Andrew Boie | 73b4ee6 | 2015-10-07 11:33:22 -0700 | [diff] [blame] | 3393 | info("Totals: %d bytes (ROM), %d bytes (RAM)" % |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3394 | (sc.rom_size, sc.ram_size)) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 3395 | info("") |
| 3396 | |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 3397 | def retrieve_gcov_data(intput_file): |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3398 | if VERBOSE: |
| 3399 | print("Working on %s" %intput_file) |
| 3400 | extracted_coverage_info = {} |
| 3401 | capture_data = False |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 3402 | capture_complete = False |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3403 | with open(intput_file, 'r') as fp: |
| 3404 | for line in fp.readlines(): |
| 3405 | if re.search("GCOV_COVERAGE_DUMP_START", line): |
| 3406 | capture_data = True |
| 3407 | continue |
| 3408 | if re.search("GCOV_COVERAGE_DUMP_END", line): |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 3409 | capture_complete = True |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3410 | break |
| 3411 | # Loop until the coverage data is found. |
| 3412 | if not capture_data: |
| 3413 | continue |
| 3414 | if line.startswith("*"): |
| 3415 | sp = line.split("<") |
| 3416 | if len(sp) > 1: |
| 3417 | # Remove the leading delimiter "*" |
| 3418 | file_name = sp[0][1:] |
| 3419 | # Remove the trailing new line char |
| 3420 | hex_dump = sp[1][:-1] |
| 3421 | else: |
| 3422 | continue |
| 3423 | else: |
| 3424 | continue |
| 3425 | extracted_coverage_info.update({file_name:hex_dump}) |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 3426 | if not capture_data: |
| 3427 | capture_complete = True |
| 3428 | return {'complete': capture_complete, 'data': extracted_coverage_info} |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3429 | |
| 3430 | def create_gcda_files(extracted_coverage_info): |
| 3431 | if VERBOSE: |
| 3432 | print("Generating gcda files") |
| 3433 | for filename, hexdump_val in extracted_coverage_info.items(): |
| 3434 | # if kobject_hash is given for coverage gcovr fails |
| 3435 | # hence skipping it problem only in gcovr v4.1 |
| 3436 | if "kobject_hash" in filename: |
| 3437 | filename = (filename[:-4]) +"gcno" |
| 3438 | try: |
| 3439 | os.remove(filename) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3440 | except Exception: |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3441 | pass |
| 3442 | continue |
| 3443 | |
| 3444 | with open(filename, 'wb') as fp: |
| 3445 | fp.write(bytes.fromhex(hexdump_val)) |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3446 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3447 | def generate_coverage(outdir, ignores): |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3448 | |
| 3449 | for filename in glob.glob("%s/**/handler.log" %outdir, recursive=True): |
Anas Nashif | f29087e | 2019-01-25 09:37:38 -0500 | [diff] [blame] | 3450 | gcov_data = retrieve_gcov_data(filename) |
| 3451 | capture_complete = gcov_data['complete'] |
| 3452 | extracted_coverage_info = gcov_data['data'] |
| 3453 | if capture_complete: |
| 3454 | create_gcda_files(extracted_coverage_info) |
| 3455 | verbose("Gcov data captured: {}".format(filename)) |
| 3456 | else: |
| 3457 | error("Gcov data capture incomplete: {}".format(filename)) |
Anas Nashif | db9592a | 2018-10-08 10:19:41 -0400 | [diff] [blame] | 3458 | |
Anas Nashif | 47cf4bf | 2019-01-24 21:50:59 -0500 | [diff] [blame] | 3459 | gcov_tool = options.gcov_tool |
| 3460 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3461 | with open(os.path.join(outdir, "coverage.log"), "a") as coveragelog: |
| 3462 | coveragefile = os.path.join(outdir, "coverage.info") |
| 3463 | ztestfile = os.path.join(outdir, "ztest.info") |
Anas Nashif | 47cf4bf | 2019-01-24 21:50:59 -0500 | [diff] [blame] | 3464 | subprocess.call(["lcov", "--gcov-tool", gcov_tool, |
| 3465 | "--capture", "--directory", outdir, |
| 3466 | "--rc", "lcov_branch_coverage=1", |
| 3467 | "--output-file", coveragefile], stdout=coveragelog) |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3468 | # We want to remove tests/* and tests/ztest/test/* but save tests/ztest |
Anas Nashif | 47cf4bf | 2019-01-24 21:50:59 -0500 | [diff] [blame] | 3469 | subprocess.call(["lcov", "--gcov-tool", gcov_tool, "--extract", coveragefile, |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3470 | os.path.join(ZEPHYR_BASE, "tests", "ztest", "*"), |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3471 | "--output-file", ztestfile, |
| 3472 | "--rc", "lcov_branch_coverage=1"], stdout=coveragelog) |
| 3473 | |
Anas Nashif | 3cbffef | 2018-11-07 23:50:54 -0500 | [diff] [blame] | 3474 | if os.path.exists(ztestfile) and os.path.getsize(ztestfile) > 0: |
Anas Nashif | 47cf4bf | 2019-01-24 21:50:59 -0500 | [diff] [blame] | 3475 | subprocess.call(["lcov", "--gcov-tool", gcov_tool, "--remove", ztestfile, |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3476 | os.path.join(ZEPHYR_BASE, "tests/ztest/test/*"), |
| 3477 | "--output-file", ztestfile, |
| 3478 | "--rc", "lcov_branch_coverage=1"], |
| 3479 | stdout=coveragelog) |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3480 | files = [coveragefile, ztestfile] |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3481 | else: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3482 | files = [coveragefile] |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3483 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3484 | for i in ignores: |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3485 | subprocess.call( |
Anas Nashif | 47cf4bf | 2019-01-24 21:50:59 -0500 | [diff] [blame] | 3486 | ["lcov", "--gcov-tool", gcov_tool, "--remove", |
| 3487 | coveragefile, i, "--output-file", |
| 3488 | coveragefile, "--rc", "lcov_branch_coverage=1"], |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3489 | stdout=coveragelog) |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3490 | |
Alberto Escolar Piedras | 834b86f | 2019-02-03 15:48:07 +0100 | [diff] [blame] | 3491 | #The --ignore-errors source option is added to avoid it exiting due to |
| 3492 | #samples/application_development/external_lib/ |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3493 | ret = subprocess.call(["genhtml", "--legend", "--branch-coverage", |
Alberto Escolar Piedras | 834b86f | 2019-02-03 15:48:07 +0100 | [diff] [blame] | 3494 | "--ignore-errors", "source", |
Alberto Escolar Piedras | bc101c5 | 2018-02-11 09:33:55 +0100 | [diff] [blame] | 3495 | "-output-directory", |
| 3496 | os.path.join(outdir, "coverage")] + files, |
| 3497 | stdout=coveragelog) |
| 3498 | if ret==0: |
| 3499 | info("HTML report generated: %s"% |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3500 | os.path.join(outdir, "coverage","index.html")) |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3501 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3502 | def get_generator(): |
| 3503 | if options.ninja: |
| 3504 | generator_cmd = "ninja" |
| 3505 | generator = "Ninja" |
| 3506 | else: |
| 3507 | generator_cmd = "make" |
| 3508 | generator = "Unix Makefiles" |
| 3509 | return generator_cmd, generator |
| 3510 | |
| 3511 | |
| 3512 | def export_tests(filename, tests): |
| 3513 | with open(filename, "wt") as csvfile: |
| 3514 | fieldnames = ['section', 'subsection', 'title', 'reference'] |
| 3515 | cw = csv.DictWriter(csvfile, fieldnames, lineterminator=os.linesep) |
| 3516 | for test in tests: |
| 3517 | data = test.split(".") |
| 3518 | subsec = " ".join(data[1].split("_")).title() |
| 3519 | rowdict = { |
| 3520 | "section": data[0].capitalize(), |
| 3521 | "subsection": subsec, |
| 3522 | "title": test, |
| 3523 | "reference": test |
| 3524 | } |
| 3525 | cw.writerow(rowdict) |
| 3526 | |
| 3527 | def get_unique_tests(suite): |
| 3528 | unq = [] |
| 3529 | run_individual_tests = [] |
| 3530 | for _, tc in suite.testcases.items(): |
| 3531 | for c in tc.cases: |
| 3532 | if options.sub_test and c in options.sub_test: |
| 3533 | if tc.name not in run_individual_tests: |
| 3534 | run_individual_tests.append(tc.name) |
| 3535 | unq.append(c) |
| 3536 | |
| 3537 | return unq |
| 3538 | |
| 3539 | |
| 3540 | |
| 3541 | def native_and_unit_first(a, b): |
| 3542 | if a[0].startswith('unit_testing'): |
| 3543 | return -1 |
| 3544 | if b[0].startswith('unit_testing'): |
| 3545 | return 1 |
| 3546 | if a[0].startswith('native_posix'): |
| 3547 | return -1 |
| 3548 | if b[0].startswith('native_posix'): |
| 3549 | return 1 |
| 3550 | if a[0].split("/",1)[0].endswith("_bsim"): |
| 3551 | return -1 |
| 3552 | if b[0].split("/",1)[0].endswith("_bsim"): |
| 3553 | return 1 |
| 3554 | |
| 3555 | return (a > b) - (a < b) |
| 3556 | |
| 3557 | |
| 3558 | run_individual_tests = None |
| 3559 | options = None |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 3560 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3561 | def main(): |
Andrew Boie | 4b18247 | 2015-07-31 12:25:22 -0700 | [diff] [blame] | 3562 | start_time = time.time() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3563 | global VERBOSE, log_file |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3564 | global options |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3565 | global run_individual_tests |
Andrew Boie | 1578ef7 | 2019-07-03 10:19:29 -0700 | [diff] [blame] | 3566 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3567 | options = parse_arguments() |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 3568 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3569 | |
| 3570 | if options.generate_hardware_map: |
| 3571 | from serial.tools import list_ports |
| 3572 | serial_devices = list_ports.comports() |
| 3573 | filtered = [] |
| 3574 | for d in serial_devices: |
| 3575 | if d.manufacturer in ['ARM', 'SEGGER', 'MBED', 'STMicroelectronics', 'Atmel Corp.']: |
| 3576 | s_dev = {} |
| 3577 | s_dev['platform'] = "unknown" |
| 3578 | s_dev['id'] = d.serial_number |
| 3579 | s_dev['serial'] = d.device |
| 3580 | s_dev['product'] = d.product |
| 3581 | if s_dev['product'] in ['DAPLink CMSIS-DAP', 'MBED CMSIS-DAP']: |
| 3582 | s_dev['runner'] = "pyocd" |
| 3583 | else: |
| 3584 | s_dev['runner'] = "unknown" |
| 3585 | s_dev['available'] = True |
| 3586 | s_dev['connected'] = True |
| 3587 | filtered.append(s_dev) |
| 3588 | else: |
| 3589 | print("Unsupported device (%s): %s" %(d.manufacturer, d)) |
| 3590 | |
| 3591 | if os.path.exists(options.generate_hardware_map): |
| 3592 | # use existing map |
| 3593 | |
| 3594 | with open(options.generate_hardware_map, 'r') as yaml_file: |
| 3595 | hwm = yaml.load(yaml_file, Loader=yaml.FullLoader) |
| 3596 | # disconnect everything |
| 3597 | for h in hwm: |
| 3598 | h['connected'] = False |
| 3599 | |
| 3600 | for d in filtered: |
| 3601 | for h in hwm: |
| 3602 | if d['id'] == h['id'] and d['product'] == h['product']: |
| 3603 | print("Already in map: %s (%s)" %(d['product'], d['id'])) |
| 3604 | h['connected'] = True |
| 3605 | h['serial'] = d['serial'] |
| 3606 | d['match'] = True |
| 3607 | |
| 3608 | new = list(filter(lambda n: not n.get('match', False), filtered)) |
| 3609 | hwm = hwm + new |
| 3610 | |
| 3611 | #import pprint |
| 3612 | #pprint.pprint(hwm) |
| 3613 | with open(options.generate_hardware_map, 'w') as yaml_file: |
| 3614 | yaml.dump(hwm, yaml_file, default_flow_style=False) |
| 3615 | |
| 3616 | |
| 3617 | else: |
| 3618 | # create new file |
| 3619 | with open(options.generate_hardware_map, 'w') as yaml_file: |
| 3620 | yaml.dump(filtered, yaml_file, default_flow_style=False) |
| 3621 | |
| 3622 | return |
| 3623 | |
| 3624 | |
Michael Scott | 421ce46 | 2019-06-18 09:37:46 -0700 | [diff] [blame] | 3625 | if options.west_runner and not options.west_flash: |
| 3626 | error("west-runner requires west-flash to be enabled") |
| 3627 | sys.exit(1) |
| 3628 | |
Michael Scott | 4ca5439 | 2019-07-09 14:21:30 -0700 | [diff] [blame] | 3629 | if options.west_flash and not options.device_testing: |
| 3630 | error("west-flash requires device-testing to be enabled") |
| 3631 | sys.exit(1) |
| 3632 | |
Alberto Escolar Piedras | c026c2e | 2018-06-21 09:30:20 +0200 | [diff] [blame] | 3633 | if options.coverage: |
| 3634 | options.enable_coverage = True |
Andrew Boie | 8047a6f | 2019-07-02 15:43:29 -0700 | [diff] [blame] | 3635 | options.enable_slow = True |
| 3636 | if not options.coverage_platform: |
| 3637 | options.coverage_platform = options.platform |
Alberto Escolar Piedras | c026c2e | 2018-06-21 09:30:20 +0200 | [diff] [blame] | 3638 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3639 | if options.size: |
| 3640 | for fn in options.size: |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 3641 | size_report(SizeCalculator(fn, [])) |
Andrew Boie | bbd670c | 2015-08-17 13:16:11 -0700 | [diff] [blame] | 3642 | sys.exit(0) |
| 3643 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3644 | VERBOSE += options.verbose |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3645 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3646 | if options.log_file: |
| 3647 | log_file = open(options.log_file, "w") |
Oleg Zhurakivskyy | c97054c | 2018-08-17 14:56:05 +0300 | [diff] [blame] | 3648 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3649 | if options.subset: |
| 3650 | subset, sets = options.subset.split("/") |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 3651 | if int(subset) > 0 and int(sets) >= int(subset): |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3652 | info("Running only a subset: %s/%s" % (subset, sets)) |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 3653 | else: |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3654 | error("You have provided a wrong subset value: %s." % options.subset) |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 3655 | return |
| 3656 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3657 | # Cleanup |
| 3658 | |
| 3659 | if options.no_clean or options.only_failed or options.test_only: |
| 3660 | if os.path.exists(options.outdir): |
| 3661 | info("Keeping artifacts untouched") |
| 3662 | elif os.path.exists(options.outdir): |
| 3663 | for i in range(1,100): |
| 3664 | new_out = options.outdir + ".{}".format(i) |
| 3665 | if not os.path.exists(new_out): |
| 3666 | info("Renaming output directory to {}".format(new_out)) |
| 3667 | shutil.move(options.outdir, new_out) |
| 3668 | break |
| 3669 | #shutil.rmtree("%s.old" %options.outdir) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3670 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3671 | if not options.testcase_root: |
| 3672 | options.testcase_root = [os.path.join(ZEPHYR_BASE, "tests"), |
Andrew Boie | 3d34871 | 2016-04-08 11:52:13 -0700 | [diff] [blame] | 3673 | os.path.join(ZEPHYR_BASE, "samples")] |
| 3674 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3675 | suite = TestSuite(options.board_root, options.testcase_root, options.outdir) |
| 3676 | suite.add_testcases() |
| 3677 | suite.add_configurations() |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 3678 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3679 | if options.device_testing: |
| 3680 | if options.hardware_map: |
| 3681 | suite.load_hardware_map(options.hardware_map) |
| 3682 | if not options.platform: |
| 3683 | options.platform = [] |
| 3684 | for platform in suite.connected_hardware: |
| 3685 | if platform['connected']: |
| 3686 | options.platform.append(platform['platform']) |
| 3687 | |
| 3688 | elif options.device_serial: #back-ward compatibility |
| 3689 | if options.platform and len(options.platform) == 1: |
| 3690 | suite.load_hardware_map_from_cmdline(options.device_serial, |
| 3691 | options.platform[0]) |
| 3692 | else: |
| 3693 | error("""When --device-testing is used with --device-serial, only one |
| 3694 | platform is allowed""") |
| 3695 | |
| 3696 | |
| 3697 | |
| 3698 | if suite.load_errors: |
Kumar Gala | c84235e | 2018-04-10 13:32:51 -0500 | [diff] [blame] | 3699 | sys.exit(1) |
| 3700 | |
Anas Nashif | 75547e2 | 2018-02-24 08:32:14 -0600 | [diff] [blame] | 3701 | if options.list_tags: |
| 3702 | tags = set() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3703 | for _, tc in suite.testcases.items(): |
Anas Nashif | 75547e2 | 2018-02-24 08:32:14 -0600 | [diff] [blame] | 3704 | tags = tags.union(tc.tags) |
| 3705 | |
| 3706 | for t in tags: |
| 3707 | print("- {}".format(t)) |
| 3708 | |
| 3709 | return |
| 3710 | |
Anas Nashif | 5d6e7eb | 2018-06-01 09:51:08 -0500 | [diff] [blame] | 3711 | if options.export_tests: |
| 3712 | cnt = 0 |
| 3713 | unq = [] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3714 | for _, tc in suite.testcases.items(): |
Anas Nashif | 5d6e7eb | 2018-06-01 09:51:08 -0500 | [diff] [blame] | 3715 | for c in tc.cases: |
| 3716 | unq.append(c) |
| 3717 | |
| 3718 | tests = sorted(set(unq)) |
| 3719 | export_tests(options.export_tests, tests) |
| 3720 | return |
| 3721 | |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3722 | run_individual_tests = [] |
Anas Nashif | 5d6e7eb | 2018-06-01 09:51:08 -0500 | [diff] [blame] | 3723 | |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3724 | if options.test: |
| 3725 | run_individual_tests = options.test |
| 3726 | |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3727 | if options.list_tests or options.sub_test: |
| 3728 | cnt = 0 |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3729 | unq = get_unique_tests(suite) |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3730 | |
Anas Nashif | 1ea5d7b | 2018-07-12 09:25:22 -0500 | [diff] [blame] | 3731 | if options.sub_test: |
| 3732 | if run_individual_tests: |
| 3733 | info("Running the following tests:") |
| 3734 | for t in run_individual_tests: |
| 3735 | print(" - {}".format(t)) |
| 3736 | else: |
| 3737 | info("Tests not found") |
| 3738 | return |
| 3739 | |
| 3740 | elif options.list_tests: |
| 3741 | for u in sorted(set(unq)): |
| 3742 | cnt = cnt + 1 |
| 3743 | print(" - {}".format(u)) |
| 3744 | print("{} total.".format(cnt)) |
| 3745 | return |
Anas Nashif | c0149cc | 2018-04-14 23:12:58 -0500 | [diff] [blame] | 3746 | |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 3747 | discards = [] |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3748 | |
| 3749 | if options.only_failed: |
| 3750 | suite.get_last_failed() |
| 3751 | elif options.load_tests: |
| 3752 | suite.load_from_file(options.load_tests) |
| 3753 | elif options.test_only: |
| 3754 | last_run = os.path.join(options.outdir, "sanitycheck.csv") |
| 3755 | suite.load_from_file(last_run) |
Anas Nashif | bd166f4 | 2017-09-02 12:32:08 -0400 | [diff] [blame] | 3756 | else: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3757 | discards = suite.apply_filters() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3758 | |
Anas Nashif | 30551f4 | 2018-01-12 21:56:59 -0500 | [diff] [blame] | 3759 | if VERBOSE > 1 and discards: |
Anas Nashif | f18e2ab | 2018-01-13 07:57:42 -0500 | [diff] [blame] | 3760 | # if we are using command line platform filter, no need to list every |
| 3761 | # other platform as excluded, we know that already. |
| 3762 | # Show only the discards that apply to the selected platforms on the |
| 3763 | # command line |
| 3764 | |
Andrew Boie | 08ce5a5 | 2016-02-22 13:28:10 -0800 | [diff] [blame] | 3765 | for i, reason in discards.items(): |
Anas Nashif | f18e2ab | 2018-01-13 07:57:42 -0500 | [diff] [blame] | 3766 | if options.platform and i.platform.name not in options.platform: |
| 3767 | continue |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3768 | debug( |
| 3769 | "{:<25} {:<50} {}SKIPPED{}: {}".format( |
| 3770 | i.platform.name, |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3771 | i.testcase.name, |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3772 | COLOR_YELLOW, |
| 3773 | COLOR_NORMAL, |
| 3774 | reason)) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3775 | |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3776 | if options.report_excluded: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3777 | all_tests = set(get_unique_tests(suite)) |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3778 | to_be_run = set() |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3779 | for i,p in suite.instances.items(): |
| 3780 | to_be_run.update(p.testcase.cases) |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3781 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3782 | if all_tests - to_be_run: |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3783 | print("Tests that never build or run:") |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3784 | for not_run in all_tests - to_be_run: |
Anas Nashif | 49b22d4 | 2019-06-14 13:45:34 -0400 | [diff] [blame] | 3785 | print("- {}".format(not_run)) |
| 3786 | |
| 3787 | return |
| 3788 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3789 | if options.subset: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3790 | #suite.instances = OrderedDict(sorted(suite.instances.items(), |
| 3791 | # key=cmp_to_key(native_and_unit_first))) |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3792 | subset, sets = options.subset.split("/") |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3793 | total = len(suite.instances) |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 3794 | per_set = round(total / int(sets)) |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 3795 | start = (int(subset) - 1) * per_set |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 3796 | if subset == sets: |
| 3797 | end = total |
| 3798 | else: |
| 3799 | end = start + per_set |
| 3800 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3801 | sliced_instances = islice(suite.instances.items(), start, end) |
| 3802 | suite.instances = OrderedDict(sliced_instances) |
Anas Nashif | 035799f | 2017-05-13 21:31:53 -0400 | [diff] [blame] | 3803 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3804 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3805 | if options.save_tests: |
| 3806 | suite.csv_report(options.save_tests) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3807 | return |
| 3808 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3809 | info("%d test configurations selected, %d configurations discarded due to filters." % |
| 3810 | (len(suite.instances), len(discards))) |
| 3811 | |
| 3812 | if options.dry_run: |
| 3813 | duration = time.time() - start_time |
| 3814 | info("Completed in %d seconds" % (duration)) |
| 3815 | return |
| 3816 | |
| 3817 | retries = options.retry_failed + 1 |
| 3818 | completed = 0 |
| 3819 | |
| 3820 | suite.update() |
| 3821 | suite.start_time = start_time |
| 3822 | |
| 3823 | while True: |
| 3824 | completed += 1 |
| 3825 | |
| 3826 | if completed > 1: |
| 3827 | info("%d Iteration:" %(completed )) |
| 3828 | time.sleep(60) # waiting for the system to settle down |
| 3829 | suite.total_done = suite.total_tests - suite.total_failed |
| 3830 | suite.total_failed = 0 |
| 3831 | |
| 3832 | suite.execute() |
Anas Nashif | 654ec598 | 2019-04-11 08:38:21 -0400 | [diff] [blame] | 3833 | info("", False) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3834 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3835 | retries = retries - 1 |
| 3836 | if retries == 0 or suite.total_failed == 0: |
| 3837 | break |
Anas Nashif | e0a6a0b | 2018-02-15 20:07:24 -0600 | [diff] [blame] | 3838 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3839 | suite.misc_reports(options.compare_report, options.show_footprint, |
| 3840 | options.all_deltas, options.footprint_threshold, options.last_metrics) |
| 3841 | suite.save_reports() |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3842 | |
Anas Nashif | e10b651 | 2017-12-30 13:01:45 -0500 | [diff] [blame] | 3843 | if options.coverage: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3844 | if options.gcov_tool is None: |
Alberto Escolar Piedras | af30f2b | 2019-09-12 14:44:08 +0200 | [diff] [blame] | 3845 | use_system_gcov = False |
Andrew Boie | 49cf486 | 2019-07-08 12:02:13 -0700 | [diff] [blame] | 3846 | |
| 3847 | for plat in options.coverage_platform: |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3848 | ts_plat = suite.get_platform(plat) |
Alberto Escolar Piedras | af30f2b | 2019-09-12 14:44:08 +0200 | [diff] [blame] | 3849 | if ts_plat and (ts_plat.type in {"native", "unit"}): |
| 3850 | use_system_gcov = True |
Andrew Boie | 49cf486 | 2019-07-08 12:02:13 -0700 | [diff] [blame] | 3851 | |
Alberto Escolar Piedras | af30f2b | 2019-09-12 14:44:08 +0200 | [diff] [blame] | 3852 | if use_system_gcov or "ZEPHYR_SDK_INSTALL_DIR" not in os.environ: |
Andrew Boie | 49cf486 | 2019-07-08 12:02:13 -0700 | [diff] [blame] | 3853 | options.gcov_tool = "gcov" |
| 3854 | else: |
| 3855 | options.gcov_tool = os.path.join(os.environ["ZEPHYR_SDK_INSTALL_DIR"], |
| 3856 | "i586-zephyr-elf/bin/i586-zephyr-elf-gcov") |
| 3857 | |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3858 | info("Generating coverage files...") |
Anas Nashif | 4df6c56 | 2018-11-07 19:29:04 -0500 | [diff] [blame] | 3859 | generate_coverage(options.outdir, ["*generated*", "tests/*", "samples/*"]) |
Jaakko Hannikainen | 54c90bc | 2016-08-31 14:17:03 +0300 | [diff] [blame] | 3860 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3861 | suite.duration = time.time() - start_time |
| 3862 | suite.summary(options.disable_unrecognized_section_test) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3863 | |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 3864 | if options.device_testing: |
| 3865 | print("\nHardware distribution summary:\n") |
| 3866 | for p in suite.connected_hardware: |
| 3867 | if p['connected']: |
| 3868 | print("%s (%s): %d" %(p['platform'], p.get('id', None), p['counter'])) |
| 3869 | |
| 3870 | if suite.total_failed or (suite.warnings and options.warnings_as_errors): |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3871 | sys.exit(1) |
| 3872 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 3873 | if __name__ == "__main__": |
| 3874 | main() |