forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_plan.py
executable file
·357 lines (289 loc) · 11.5 KB
/
test_plan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/env python3
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2021 Intel Corporation
# A script to generate twister options based on modified files.
import re, os
import argparse
import yaml
import json
import fnmatch
import subprocess
import csv
import logging
import sys
from git import Repo
if "ZEPHYR_BASE" not in os.environ:
exit("$ZEPHYR_BASE environment variable undefined.")
repository_path = os.environ['ZEPHYR_BASE']
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
sys.path.append(os.path.join(repository_path, 'scripts'))
import list_boards
def _get_match_fn(globs, regexes):
# Constructs a single regex that tests for matches against the globs in
# 'globs' and the regexes in 'regexes'. Parts are joined with '|' (OR).
# Returns the search() method of the compiled regex.
#
# Returns None if there are neither globs nor regexes, which should be
# interpreted as no match.
if not (globs or regexes):
return None
regex = ""
if globs:
glob_regexes = []
for glob in globs:
# Construct a regex equivalent to the glob
glob_regex = glob.replace(".", "\\.").replace("*", "[^/]*") \
.replace("?", "[^/]")
if not glob.endswith("/"):
# Require a full match for globs that don't end in /
glob_regex += "$"
glob_regexes.append(glob_regex)
# The glob regexes must anchor to the beginning of the path, since we
# return search(). (?:) is a non-capturing group.
regex += "^(?:{})".format("|".join(glob_regexes))
if regexes:
if regex:
regex += "|"
regex += "|".join(regexes)
return re.compile(regex).search
class Tag:
"""
Represents an entry for a tag in tags.yaml.
These attributes are available:
name:
List of GitHub labels for the area. Empty if the area has no 'labels'
key.
description:
Text from 'description' key, or None if the area has no 'description'
key
"""
def _contains(self, path):
# Returns True if the area contains 'path', and False otherwise
return self._match_fn and self._match_fn(path) and not \
(self._exclude_match_fn and self._exclude_match_fn(path))
def __repr__(self):
return "<Tag {}>".format(self.name)
class Filters:
def __init__(self, modified_files, pull_request=False, platforms=[]):
self.modified_files = modified_files
self.twister_options = []
self.full_twister = False
self.all_tests = []
self.tag_options = []
self.pull_request = pull_request
self.platforms = platforms
def process(self):
self.find_tags()
self.find_excludes()
self.find_tests()
if not self.platforms:
self.find_archs()
self.find_boards()
def get_plan(self, options, integration=False):
fname = "_test_plan_partial.csv"
cmd = ["scripts/twister", "-c"] + options + ["--save-tests", fname ]
if integration:
cmd.append("--integration")
logging.info(" ".join(cmd))
_ = subprocess.call(cmd)
with open(fname, newline='') as csvfile:
csv_reader = csv.reader(csvfile, delimiter=',')
_ = next(csv_reader)
for e in csv_reader:
self.all_tests.append(e)
if os.path.exists(fname):
os.remove(fname)
def find_archs(self):
# we match both arch/<arch>/* and include/arch/<arch> and skip common.
# Some architectures like riscv require special handling, i.e. riscv
# directory covers 2 architectures known to twister: riscv32 and riscv64.
archs = set()
for f in self.modified_files:
p = re.match(r"^arch\/([^/]+)\/", f)
if not p:
p = re.match(r"^include\/arch\/([^/]+)\/", f)
if p:
if p.group(1) != 'common':
if p.group(1) == 'riscv':
archs.add('riscv32')
archs.add('riscv64')
else:
archs.add(p.group(1))
_options = []
for arch in archs:
_options.extend(["-a", arch ])
if _options:
logging.info(f'Potential architecture filters...')
if self.platforms:
for platform in self.platforms:
_options.extend(["-p", platform])
self.get_plan(_options, True)
else:
self.get_plan(_options, False)
def find_boards(self):
boards = set()
all_boards = set()
for f in self.modified_files:
if f.endswith(".rst") or f.endswith(".png") or f.endswith(".jpg"):
continue
p = re.match(r"^boards\/[^/]+\/([^/]+)\/", f)
if p and p.groups():
boards.add(p.group(1))
# Limit search to $ZEPHYR_BASE since this is where the changed files are
lb_args = argparse.Namespace(**{ 'arch_roots': [], 'board_roots': [] })
known_boards = list_boards.find_boards(lb_args)
for b in boards:
name_re = re.compile(b)
for kb in known_boards:
if name_re.search(kb.name):
all_boards.add(kb.name)
_options = []
for board in all_boards:
_options.extend(["-p", board ])
if _options:
logging.info(f'Potential board filters...')
self.get_plan(_options)
def find_tests(self):
tests = set()
for f in self.modified_files:
if f.endswith(".rst"):
continue
d = os.path.dirname(f)
while d:
if os.path.exists(os.path.join(d, "testcase.yaml")) or \
os.path.exists(os.path.join(d, "sample.yaml")):
tests.add(d)
break
else:
d = os.path.dirname(d)
_options = []
for t in tests:
_options.extend(["-T", t ])
if _options:
logging.info(f'Potential test filters...')
if self.platforms:
for platform in self.platforms:
_options.extend(["-p", platform])
else:
_options.append("--all")
self.get_plan(_options)
def find_tags(self):
tag_cfg_file = os.path.join(repository_path, 'scripts', 'ci', 'tags.yaml')
with open(tag_cfg_file, 'r') as ymlfile:
tags_config = yaml.safe_load(ymlfile)
tags = {}
for t,x in tags_config.items():
tag = Tag()
tag.exclude = True
tag.name = t
# tag._match_fn(path) tests if the path matches files and/or
# files-regex
tag._match_fn = _get_match_fn(x.get("files"), x.get("files-regex"))
# Like tag._match_fn(path), but for files-exclude and
# files-regex-exclude
tag._exclude_match_fn = \
_get_match_fn(x.get("files-exclude"), x.get("files-regex-exclude"))
tags[tag.name] = tag
for f in self.modified_files:
for t in tags.values():
if t._contains(f):
t.exclude = False
exclude_tags = set()
for t in tags.values():
if t.exclude:
exclude_tags.add(t.name)
for tag in exclude_tags:
self.tag_options.extend(["-e", tag ])
if exclude_tags:
logging.info(f'Potential tag based filters...')
def find_excludes(self):
with open("scripts/ci/twister_ignore.txt", "r") as twister_ignore:
ignores = twister_ignore.read().splitlines()
ignores = filter(lambda x: not x.startswith("#"), ignores)
found = set()
files = list(filter(lambda x: x, self.modified_files))
for pattern in ignores:
if pattern:
found.update(fnmatch.filter(files, pattern))
logging.debug(found)
logging.debug(files)
if sorted(files) != sorted(found):
_options = []
logging.info(f'Need to run full or partial twister...')
self.full_twister = True
if self.platforms:
for platform in self.platforms:
_options.extend(["-p", platform])
_options.extend(self.tag_options)
self.get_plan(_options)
else:
_options.extend(self.tag_options)
self.get_plan(_options, True)
else:
logging.info(f'No twister needed or partial twister run only...')
def parse_args():
parser = argparse.ArgumentParser(
description="Generate twister argument files based on modified file")
parser.add_argument('-c', '--commits', default=None,
help="Commit range in the form: a..b")
parser.add_argument('-m', '--modified-files', default=None,
help="File with information about changed/deleted/added files.")
parser.add_argument('-o', '--output-file', default="testplan.csv",
help="CSV file with the test plan to be passed to twister")
parser.add_argument('-P', '--pull-request', action="store_true",
help="This is a pull request")
parser.add_argument('-p', '--platform', action="append",
help="Limit this for a platform or a list of platforms.")
parser.add_argument('-t', '--tests_per_builder', default=700, type=int,
help="Number of tests per builder")
parser.add_argument('-n', '--default-matrix', default=10, type=int,
help="Number of tests per builder")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
files = []
if args.commits:
repo = Repo(repository_path)
commit = repo.git.diff("--name-only", args.commits)
files = commit.split("\n")
elif args.modified_files:
with open(args.modified_files, "r") as fp:
files = json.load(fp)
if files:
print("Changed files:\n=========")
print("\n".join(files))
print("=========")
f = Filters(files, args.pull_request, args.platform)
f.process()
# remove dupes and filtered cases
dup_free = []
dup_free_set = set()
for x in f.all_tests:
if x[3] == 'skipped':
continue
if tuple(x) not in dup_free_set:
dup_free.append(x)
dup_free_set.add(tuple(x))
logging.info(f'Total tests to be run: {len(dup_free)}')
with open(".testplan", "w") as tp:
total_tests = len(dup_free)
if total_tests and total_tests < args.tests_per_builder:
nodes = 1
else:
nodes = round(total_tests / args.tests_per_builder)
if total_tests % args.tests_per_builder != total_tests:
nodes = nodes + 1
if args.default_matrix > nodes > 5:
nodes = args.default_matrix
tp.write(f"TWISTER_TESTS={total_tests}\n")
tp.write(f"TWISTER_NODES={nodes}\n")
tp.write(f"TWISTER_FULL={f.full_twister}\n")
logging.info(f'Total nodes to launch: {nodes}')
header = ['test', 'arch', 'platform', 'status', 'extra_args', 'handler',
'handler_time', 'ram_size', 'rom_size']
# write plan
if dup_free:
with open(args.output_file, 'w', newline='') as csv_file:
writer = csv.writer(csv_file)
writer.writerow(header)
writer.writerows(dup_free)