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 | ce2b418 | 2020-03-24 14:40:28 -0400 | [diff] [blame] | 3 | # Copyright (c) 2020 Intel Corporation |
Anas Nashif | 3ae5262 | 2019-04-06 09:08:09 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: Apache-2.0 |
Anas Nashif | b18f311 | 2020-12-07 11:40:19 -0500 | [diff] [blame] | 5 | """Zephyr Test Runner (twister) |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 6 | |
Marc Herbert | e5cedca | 2019-04-08 14:02:34 -0700 | [diff] [blame] | 7 | Also check the "User and Developer Guides" at https://docs.zephyrproject.org/ |
| 8 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 9 | This script scans for the set of unit test applications in the git |
| 10 | repository and attempts to execute them. By default, it tries to |
| 11 | build each test case on one platform per architecture, using a precedence |
Paul Sokolovsky | ff70add | 2017-06-16 01:31:54 +0300 | [diff] [blame] | 12 | list defined in an architecture configuration file, and if possible |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 13 | run the tests in any available emulators or simulators on the system. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 14 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 15 | Test cases are detected by the presence of a 'testcase.yaml' or a sample.yaml |
| 16 | files in the application's project directory. This file may contain one or more |
| 17 | blocks, each identifying a test scenario. The title of the block is a name for |
| 18 | the test case, which only needs to be unique for the test cases specified in |
Anas Nashif | 15bc98e | 2022-03-23 14:07:54 -0400 | [diff] [blame] | 19 | that testsuite meta-data. The full canonical name for each test case is <path to |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 20 | test case>/<block>. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 21 | |
Anas Nashif | 15bc98e | 2022-03-23 14:07:54 -0400 | [diff] [blame] | 22 | Each test block in the testsuite meta data can define the following key/value |
Anas Nashif | 3ba1d43 | 2017-12-05 15:28:44 -0500 | [diff] [blame] | 23 | pairs: |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 24 | |
Anas Nashif | fa695d2 | 2017-10-04 16:14:27 -0400 | [diff] [blame] | 25 | tags: <list of tags> (required) |
Anas Nashif | 15bc98e | 2022-03-23 14:07:54 -0400 | [diff] [blame] | 26 | A set of string tags for the testsuite. Usually pertains to |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 27 | functional domains but can be anything. Command line invocations |
| 28 | of this script can filter the set of tests to run based on tag. |
| 29 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 30 | skip: <True|False> (default False) |
Anas Nashif | 15bc98e | 2022-03-23 14:07:54 -0400 | [diff] [blame] | 31 | skip testsuite unconditionally. This can be used for broken tests. |
Anas Nashif | 2bd99bc | 2015-10-12 13:10:57 -0400 | [diff] [blame] | 32 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 33 | slow: <True|False> (default False) |
Andy Ross | dc4151f | 2019-01-03 14:17:43 -0800 | [diff] [blame] | 34 | Don't build or run this test case unless --enable-slow was passed |
| 35 | in on the command line. Intended for time-consuming test cases |
| 36 | that are only run under certain circumstances, like daily |
| 37 | builds. |
Andrew Boie | 6bb087c | 2016-02-10 13:39:00 -0800 | [diff] [blame] | 38 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 39 | extra_args: <list of extra arguments> |
Sebastian Bøe | c218261 | 2017-11-09 12:25:02 +0100 | [diff] [blame] | 40 | Extra cache entries to pass to CMake when building or running the |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 41 | test case. |
| 42 | |
Anas Nashif | ebc329d | 2017-10-17 09:00:33 -0400 | [diff] [blame] | 43 | extra_configs: <list of extra configurations> |
| 44 | Extra configuration options to be merged with a master prj.conf |
| 45 | when building or running the test case. |
| 46 | |
Jamie McCrae | bc97d8f | 2023-08-03 10:56:41 +0100 | [diff] [blame] | 47 | required_snippets: <list of snippets> |
| 48 | Snippets that must be applied for the test case to run. |
| 49 | |
Daniel DeGrasse | 602b8b4 | 2022-08-24 18:37:06 -0500 | [diff] [blame] | 50 | sysbuild: <True|False> (default False) |
| 51 | If true, build the sample using the sysbuild infrastructure. Filtering |
| 52 | will only be enabled for the main project, and is not supported for |
| 53 | other projects included by sysbuild. |
| 54 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 55 | build_only: <True|False> (default False) |
Marc Herbert | e5cedca | 2019-04-08 14:02:34 -0700 | [diff] [blame] | 56 | If true, don't try to run the test even if the selected platform |
| 57 | supports it. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 58 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 59 | build_on_all: <True|False> (default False) |
| 60 | If true, attempt to build test on all available platforms. |
| 61 | |
| 62 | depends_on: <list of features> |
| 63 | A board or platform can announce what features it supports, this option |
| 64 | will enable the test only those platforms that provide this feature. |
| 65 | |
| 66 | min_ram: <integer> |
| 67 | minimum amount of RAM needed for this test to build and run. This is |
| 68 | compared with information provided by the board metadata. |
| 69 | |
| 70 | min_flash: <integer> |
| 71 | minimum amount of ROM needed for this test to build and run. This is |
| 72 | compared with information provided by the board metadata. |
| 73 | |
Anas Nashif | f429f33 | 2022-04-18 22:34:39 -0400 | [diff] [blame] | 74 | modules: <list of modules> |
| 75 | Add list of modules needed for this sample to build and run. |
| 76 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 77 | timeout: <number of seconds> |
Anas Nashif | 83fc06a | 2019-06-22 11:04:10 -0400 | [diff] [blame] | 78 | Length of time to run test in emulator before automatically killing it. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 79 | Default to 60 seconds. |
| 80 | |
Anas Nashif | dca317c | 2020-08-26 11:28:25 -0400 | [diff] [blame] | 81 | arch_allow: <list of arches, such as x86, arm, arc> |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 82 | Set of architectures that this test case should only be run for. |
| 83 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 84 | arch_exclude: <list of arches, such as x86, arm, arc> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 85 | Set of architectures that this test case should not run on. |
| 86 | |
Anas Nashif | dca317c | 2020-08-26 11:28:25 -0400 | [diff] [blame] | 87 | platform_allow: <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 88 | Set of platforms that this test case should only be run for. |
| 89 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 90 | platform_exclude: <list of platforms> |
Anas Nashif | 30d1387 | 2015-10-05 10:02:45 -0400 | [diff] [blame] | 91 | Set of platforms that this test case should not run on. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 92 | |
Yong Cong Sin | 9d314ff | 2023-12-12 15:40:49 +0800 | [diff] [blame] | 93 | simulation_exclude: <list of simulators> |
| 94 | Set of simulators that this test case should not run on. |
| 95 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 96 | extra_sections: <list of extra binary sections> |
Anas Nashif | b18f311 | 2020-12-07 11:40:19 -0500 | [diff] [blame] | 97 | When computing sizes, twister will report errors if it finds |
Andrew Boie | 52fef67 | 2016-11-29 12:21:59 -0800 | [diff] [blame] | 98 | extra, unexpected sections in the Zephyr binary unless they are named |
| 99 | here. They will not be included in the size calculation. |
| 100 | |
Anas Nashif | a792a3d | 2017-04-04 18:47:49 -0400 | [diff] [blame] | 101 | filter: <expression> |
Anas Nashif | 15bc98e | 2022-03-23 14:07:54 -0400 | [diff] [blame] | 102 | Filter whether the testsuite should be run by evaluating an expression |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 103 | against an environment containing the following values: |
| 104 | |
| 105 | { ARCH : <architecture>, |
| 106 | PLATFORM : <platform>, |
Javier B Perez | 7941454 | 2016-08-08 12:24:59 -0500 | [diff] [blame] | 107 | <all CONFIG_* key/value pairs in the test's generated defconfig>, |
Anas Nashif | 45a9786 | 2019-01-09 08:46:42 -0500 | [diff] [blame] | 108 | <all DT_* key/value pairs in the test's generated device tree file>, |
| 109 | <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] | 110 | *<env>: any environment variable available |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | The grammar for the expression language is as follows: |
| 114 | |
| 115 | expression ::= expression "and" expression |
| 116 | | expression "or" expression |
| 117 | | "not" expression |
| 118 | | "(" expression ")" |
| 119 | | symbol "==" constant |
| 120 | | symbol "!=" constant |
| 121 | | symbol "<" number |
| 122 | | symbol ">" number |
| 123 | | symbol ">=" number |
| 124 | | symbol "<=" number |
| 125 | | symbol "in" list |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 126 | | symbol ":" string |
Andrew Boie | 3ea7892 | 2016-03-24 14:46:00 -0700 | [diff] [blame] | 127 | | symbol |
| 128 | |
| 129 | list ::= "[" list_contents "]" |
| 130 | |
| 131 | list_contents ::= constant |
| 132 | | list_contents "," constant |
| 133 | |
| 134 | constant ::= number |
| 135 | | string |
| 136 | |
| 137 | |
| 138 | For the case where expression ::= symbol, it evaluates to true |
| 139 | if the symbol is defined to a non-empty string. |
| 140 | |
| 141 | Operator precedence, starting from lowest to highest: |
| 142 | |
| 143 | or (left associative) |
| 144 | and (left associative) |
| 145 | not (right associative) |
| 146 | all comparison operators (non-associative) |
| 147 | |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 148 | The ':' operator compiles the string argument as a regular expression, |
| 149 | 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] | 150 | matches. For example, if CONFIG_SOC="stm32f107xc" then |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 151 | |
Anas Nashif | 578ae40 | 2019-07-12 07:54:35 -0700 | [diff] [blame] | 152 | filter = CONFIG_SOC : "stm.*" |
Andrew Boie | 7a87f9f | 2016-06-02 12:27:54 -0700 | [diff] [blame] | 153 | |
| 154 | Would match it. |
| 155 | |
Alberto Escolar Piedras | ec273d1 | 2023-10-17 14:00:08 +0200 | [diff] [blame] | 156 | Note that arch_allow, arch_exclude, platform_allow, platform_exclude |
| 157 | are not just syntactic sugar for filter expressions. For instance |
| 158 | |
| 159 | arch_exclude = x86 arc |
| 160 | |
| 161 | Can appear at first glance to have a similar effect to |
| 162 | |
| 163 | filter = not ARCH in ["x86", "arc"] |
| 164 | |
| 165 | but unlike "filter", these cause platforms to be filtered already during the testplan |
| 166 | generation. While "filter" does not exclue platforms at the testplan generation, and instead |
| 167 | relies on the result of running the build configuration stage. That is, to evaluate the filter |
| 168 | expression, cmake is run for that target, and then the filter evaluated as a gate for the |
| 169 | build and run steps. |
| 170 | Therefore filtering by using {platform|arch}_{exclude|allow} is much faster. |
| 171 | |
Anas Nashif | 15bc98e | 2022-03-23 14:07:54 -0400 | [diff] [blame] | 172 | The set of test cases that actually run depends on directives in the testsuite |
Anas Nashif | 3dcc038 | 2022-03-19 08:08:44 -0400 | [diff] [blame] | 173 | files and options passed in on the command line. If there is any confusion, |
| 174 | running with -v or examining the test plan report (testplan.json) |
Anas Nashif | 12d8cce | 2019-11-20 03:47:27 -0800 | [diff] [blame] | 175 | can help show why particular test cases were skipped. |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 176 | |
Genaro Saucedo Tejada | 28bba92 | 2016-10-24 18:00:58 -0500 | [diff] [blame] | 177 | To load arguments from a file, write '+' before the file name, e.g., |
| 178 | +file_name. File content must be one or more valid arguments separated by |
| 179 | line break instead of white spaces. |
| 180 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 181 | Most everyday users will run with no arguments. |
Andy Ross | dc4151f | 2019-01-03 14:17:43 -0800 | [diff] [blame] | 182 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 183 | """ |
| 184 | |
Sebastian Bøe | 56d7471 | 2019-01-21 15:48:46 +0100 | [diff] [blame] | 185 | import os |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 186 | import sys |
Martí Bolívar | 4fad535 | 2020-04-13 16:05:44 -0700 | [diff] [blame] | 187 | from pathlib import Path |
Anas Nashif | facc685 | 2022-06-10 07:27:28 -0400 | [diff] [blame] | 188 | |
Anas Nashif | bb28035 | 2020-05-07 12:02:48 -0400 | [diff] [blame] | 189 | |
Anas Nashif | ce2b418 | 2020-03-24 14:40:28 -0400 | [diff] [blame] | 190 | ZEPHYR_BASE = os.getenv("ZEPHYR_BASE") |
| 191 | if not ZEPHYR_BASE: |
Anas Nashif | b18f311 | 2020-12-07 11:40:19 -0500 | [diff] [blame] | 192 | # This file has been zephyr/scripts/twister for years, |
Martí Bolívar | 4fad535 | 2020-04-13 16:05:44 -0700 | [diff] [blame] | 193 | # and that is not going to change anytime soon. Let the user |
Anas Nashif | b18f311 | 2020-12-07 11:40:19 -0500 | [diff] [blame] | 194 | # run this script as ./scripts/twister without making them |
Martí Bolívar | 4fad535 | 2020-04-13 16:05:44 -0700 | [diff] [blame] | 195 | # set ZEPHYR_BASE. |
| 196 | ZEPHYR_BASE = str(Path(__file__).resolve().parents[1]) |
| 197 | |
| 198 | # Propagate this decision to child processes. |
| 199 | os.environ['ZEPHYR_BASE'] = ZEPHYR_BASE |
| 200 | |
| 201 | print(f'ZEPHYR_BASE unset, using "{ZEPHYR_BASE}"') |
Anas Nashif | d988238 | 2019-12-12 09:58:28 -0500 | [diff] [blame] | 202 | |
Anas Nashif | 087f1e4 | 2022-06-23 17:40:57 -0400 | [diff] [blame] | 203 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/twister/")) |
Daniel DeGrasse | b579a44 | 2022-10-05 16:06:29 -0500 | [diff] [blame] | 204 | sys.path.insert(0, os.path.join(ZEPHYR_BASE, "scripts/pylib/build_helpers")) |
Anas Nashif | 434995c | 2019-12-01 13:55:11 -0500 | [diff] [blame] | 205 | |
Al Semjonovs | d09fd22 | 2022-11-17 14:05:12 -0700 | [diff] [blame] | 206 | from twisterlib.environment import add_parse_arguments, parse_arguments |
| 207 | from twisterlib.twister_main import main |
Anas Nashif | d988238 | 2019-12-12 09:58:28 -0500 | [diff] [blame] | 208 | |
Andrew Boie | 6acbe63 | 2015-07-17 12:03:52 -0700 | [diff] [blame] | 209 | if __name__ == "__main__": |
Anas Nashif | 76d5543 | 2022-06-13 15:50:32 -0400 | [diff] [blame] | 210 | ret = 0 |
Andrew Boie | 1fe1f3a | 2020-01-02 19:27:40 -0800 | [diff] [blame] | 211 | try: |
Al Semjonovs | 067ba65 | 2022-09-26 15:38:00 -0600 | [diff] [blame] | 212 | parser = add_parse_arguments() |
| 213 | options = parse_arguments(parser, sys.argv[1:]) |
Dmitrii Golovanov | d45dbc2 | 2024-05-07 09:11:47 +0200 | [diff] [blame] | 214 | default_options = parse_arguments(parser, [], on_init=False) |
| 215 | ret = main(options, default_options) |
Andrew Boie | 1fe1f3a | 2020-01-02 19:27:40 -0800 | [diff] [blame] | 216 | finally: |
Piotr Golyzniak | 8b77348 | 2021-11-02 16:13:26 +0100 | [diff] [blame] | 217 | if (os.name != "nt") and os.isatty(1): |
| 218 | # (OS is not Windows) and (stdout is interactive) |
Jeremy Bettis | 562b291 | 2022-08-24 12:44:05 -0600 | [diff] [blame] | 219 | os.system("stty sane <&1") |
Anas Nashif | 76d5543 | 2022-06-13 15:50:32 -0400 | [diff] [blame] | 220 | |
| 221 | sys.exit(ret) |