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