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