Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to filter testsuite roots #84225

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions scripts/ci/test_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,28 @@ def __repr__(self):

class Filters:
def __init__(self, modified_files, ignore_path, alt_tags, testsuite_root,
pull_request=False, platforms=[], detailed_test_id=True, quarantine_list=None, tc_roots_th=20):
pull_request=False, platforms=[], detailed_test_id=True, quarantine_list=None, tc_roots_th=20, testsuite_excludes_file=None):
self.modified_files = modified_files
self.testsuite_root = testsuite_root
self.resolved_files = []
self.twister_options = []
self.full_twister = False
self.all_tests = []
self.tag_options = []
self.testsuite_excludes_options = []
self.pull_request = pull_request
self.platforms = platforms
self.detailed_test_id = detailed_test_id
self.ignore_path = ignore_path
self.tag_cfg_file = alt_tags
self.quarantine_list = quarantine_list
self.tc_roots_th = tc_roots_th
self.testsuite_excludes_file = testsuite_excludes_file

def process(self):
self.find_modules()
self.find_tags()
self.find_testsuite_excludes()
self.find_tests()
if not self.platforms:
# disable for now, this is generating lots of churn when changing
Expand Down Expand Up @@ -326,9 +329,8 @@ def find_tests(self):
_options.extend(["-p", platform])
self.get_plan(_options, use_testsuite_root=False)

def find_tags(self):

with open(self.tag_cfg_file, 'r') as ymlfile:
def _get_tags(self, yml_path):
with open(yml_path, 'r') as ymlfile:
tags_config = yaml.safe_load(ymlfile)

tags = {}
Expand Down Expand Up @@ -358,12 +360,29 @@ def find_tags(self):
if t.exclude:
exclude_tags.add(t.name)

return exclude_tags

def find_tags(self):
exclude_tags = self._get_tags(self.tag_cfg_file)

for tag in exclude_tags:
self.tag_options.extend(["-e", tag ])

if exclude_tags:
logging.info(f'Potential tag based filters: {exclude_tags}')

def find_testsuite_excludes(self):
if self.testsuite_excludes_file is None:
return

exclude_testsuites = self._get_tags(self.testsuite_excludes_file)

for tag in exclude_testsuites:
self.testsuite_excludes_options.extend(["--testsuite-exclude-path", tag ])

if exclude_testsuites:
logging.info(f'Testsuite exclude filters: {exclude_testsuites}')

def find_excludes(self, skip=[]):
with open(self.ignore_path, "r") as twister_ignore:
ignores = twister_ignore.read().splitlines()
Expand Down Expand Up @@ -391,9 +410,11 @@ def find_excludes(self, skip=[]):
_options.extend(["-p", platform])

_options.extend(self.tag_options)
_options.extend(self.testsuite_excludes_options)
self.get_plan(_options)
else:
_options.extend(self.tag_options)
_options.extend(self.testsuite_excludes_options)
self.get_plan(_options, True)
else:
logging.info(f'No twister needed or partial twister run only...')
Expand Down Expand Up @@ -443,6 +464,9 @@ def parse_args():
"the file need to correspond to the test scenarios names as in "
"corresponding tests .yaml files. These scenarios "
"will be skipped with quarantine as the reason.")
parser.add_argument('--testsuite-excludes-file',
default=None,
help="Path to a file describing relations between directories/paths (modified files) and testsuites filters.")

# Include paths in names by default.
parser.set_defaults(detailed_test_id=True)
Expand Down Expand Up @@ -472,7 +496,7 @@ def parse_args():

f = Filters(files, args.ignore_path, args.alt_tags, args.testsuite_root,
args.pull_request, args.platform, args.detailed_test_id, args.quarantine_list,
args.testcase_roots_threshold)
args.testcase_roots_threshold, args.testsuite_excludes_file)
f.process()

# remove dupes and filtered cases
Expand Down
17 changes: 17 additions & 0 deletions scripts/ci/testsuite_excludes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file contains information on what files are associated with which
# twister testsuite excludes patterns.
#
# File format is the same as for tags.yaml - please refer to its description.

# zephyr-keep-sorted-start
"*tests/kernel*":
files:
- kernel/
- arch/
- tests/kernel/

"*tests/posix*":
files:
- lib/posix/
- tests/posix/
# zephyr-keep-sorted-stop
10 changes: 10 additions & 0 deletions scripts/pylib/twister/twisterlib/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ def add_parse_arguments(parser = None) -> argparse.ArgumentParser:
"called multiple times. Defaults to the 'samples/' and "
"'tests/' directories at the base of the Zephyr tree.")

case_select.add_argument(
"--testsuite-exclude-path", action="append", default=[], type = norm_path,
help="Directory to exclude from searching for test cases. "
"This is a filter pattern for paths provided with the testsuite-root option. "
"Supports Unix shell-style wildcards, e.g. *samples/sub* (fnmatch). "
"The exclude pattern is matched against an absolute path for the test suite. "
"This option can be used multiple times, multiple invocations "
"are treated as a logical 'or' relationship.")

case_select.add_argument(
"-f",
"--only-failed",
Expand Down Expand Up @@ -1024,6 +1033,7 @@ def __init__(self, options : argparse.Namespace, default_options=None) -> None:
logger.info(f"Using {self.generator}..")

self.test_roots = options.testsuite_root
self.test_exclude_paths = options.testsuite_exclude_path
hakehuang marked this conversation as resolved.
Show resolved Hide resolved

if not isinstance(options.board_root, list):
self.board_roots = [options.board_root]
Expand Down
10 changes: 10 additions & 0 deletions scripts/pylib/twister/twisterlib/testplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# SPDX-License-Identifier: Apache-2.0
import collections
import copy
import fnmatch
import glob
import itertools
import json
Expand Down Expand Up @@ -588,6 +589,15 @@ def add_testsuites(self, testsuite_filter=None):

logger.debug("Found possible testsuite in " + dirpath)

skip_path = False
for test_exclude_path in self.env.test_exclude_paths:
if fnmatch.fnmatch(dirpath, test_exclude_path):
skip_path = True
break
if skip_path:
logger.debug(f"Skipping {dirpath} due to excludes")
continue

suite_yaml_path = os.path.join(dirpath, filename)
suite_path = os.path.dirname(suite_yaml_path)

Expand Down
3 changes: 2 additions & 1 deletion scripts/tests/twister/test_testplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,8 @@ def test_testplan_add_testsuites(tmp_path, testsuite_filter, use_alt_root, detai
env = mock.Mock(
test_roots=[tmp_test_root_dir],
options=mock.Mock(detailed_test_id=detailed_id),
alt_config_root=[tmp_alt_test_root_dir] if use_alt_root else []
alt_config_root=[tmp_alt_test_root_dir] if use_alt_root else [],
test_exclude_paths=[]
)

testplan = TestPlan(env=env)
Expand Down
Loading