blob: d2f14c82e190f455a6f0fe950bed7b494cc2e0fe [file] [log] [blame]
Klement Sekerab23ffd72021-05-31 16:08:53 +02001import argparse
2import os
3import psutil
Klement Sekerab23ffd72021-05-31 16:08:53 +02004import time
5
6
7def positive_int_or_default(default):
8 def positive_integer(v):
9 if v is None or v == "":
10 return default
Andrew Yourtchenkof56b0072022-03-18 17:05:53 +000011 if int(v) <= 0:
12 raise ValueError("value must be positive")
Klement Sekerab23ffd72021-05-31 16:08:53 +020013 return int(v)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020014
Klement Sekerab23ffd72021-05-31 16:08:53 +020015 return positive_integer
16
17
Andrew Yourtchenkof56b0072022-03-18 17:05:53 +000018def positive_float_or_default(default):
19 def positive_float(v):
20 if v is None or v == "":
21 return default
22 if float(v) <= 0:
23 raise ValueError("value must be positive")
24 return float(v)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025
Andrew Yourtchenkof56b0072022-03-18 17:05:53 +000026 return positive_float
27
28
Klement Sekerab23ffd72021-05-31 16:08:53 +020029def positive_int_or_auto(v):
30 if v is None or v in ("", "auto"):
31 return "auto"
32 if int(v) <= 0:
33 raise ValueError("value must be positive or auto")
34 return int(v)
35
36
37def int_or_auto(v):
38 if v is None or v in ("", "auto"):
39 return "auto"
40 if int(v) < 0:
41 raise ValueError("value must be positive or auto")
42 return int(v)
43
44
45def int_choice_or_default(options, default):
46 assert default in options
47
48 def choice(v):
49 if v is None or v == "":
50 return default
51 if int(v) in options:
52 return int(v)
53 raise ValueError("invalid choice")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020054
Klement Sekerab23ffd72021-05-31 16:08:53 +020055 return choice
56
57
58def worker_config(v):
59 if v is None or v == "":
60 return 0
61 if v.startswith("workers "):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020062 return int(v.split(" ")[1])
Klement Sekerab23ffd72021-05-31 16:08:53 +020063 return int(v)
64
65
66def directory(v):
67 if not os.path.isdir(v):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020068 raise ValueError(f"provided path '{v}' doesn't exist or is not a directory")
Klement Sekerab23ffd72021-05-31 16:08:53 +020069 return v
70
71
72def directory_verify_or_create(v):
73 if not os.path.isdir(v):
74 os.mkdir(v)
75 return v
76
77
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020078parser = argparse.ArgumentParser(
79 description="VPP unit tests", formatter_class=argparse.RawTextHelpFormatter
80)
Klement Sekerab23ffd72021-05-31 16:08:53 +020081
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020082parser.add_argument(
83 "--failfast", action="store_true", help="stop running tests on first failure"
84)
Klement Sekerab23ffd72021-05-31 16:08:53 +020085
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020086parser.add_argument(
87 "--test-src-dir",
88 action="append",
89 type=directory,
90 help="directory containing test files "
91 "(may be specified multiple times) "
92 "(VPP_WS_DIR/test is added automatically to the set)",
93)
Klement Sekerab23ffd72021-05-31 16:08:53 +020094
95default_verbose = 0
96
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097parser.add_argument(
98 "--verbose",
99 action="store",
100 default=default_verbose,
101 type=int_choice_or_default((0, 1, 2), default_verbose),
102 help="verbosity setting - 0 - least verbose, 2 - most verbose (default: 0)",
103)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200104
105default_test_run_timeout = 600
106
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200107parser.add_argument(
108 "--timeout",
109 action="store",
110 type=positive_int_or_default(default_test_run_timeout),
111 default=default_test_run_timeout,
112 metavar="TEST_RUN_TIMEOUT",
113 help="test run timeout in seconds - per test "
114 f"(default: {default_test_run_timeout})",
115)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200116
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200117parser.add_argument(
118 "--failed-dir",
119 action="store",
120 type=directory,
Klement Sekera152a9b62022-05-13 18:01:36 +0200121 help="directory containing failed tests (default: --tmp-dir)",
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200122)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200123
124filter_help_string = """\
125expression consists of 3 string selectors separated by '.' separators:
126
127 <file>.<class>.<function>
128
129- selectors restrict which files/classes/functions are run
130- selector can be replaced with '*' or omitted entirely if not needed
131- <file> selector is automatically prepended with 'test_' if required
132- '.' separators are required only if selector(s) follow(s)
133
134examples:
135
1361. all of the following expressions are equivalent and will select
137 all test classes and functions from test_bfd.py:
138 'test_bfd' 'bfd' 'test_bfd..' 'bfd.' 'bfd.*.*' 'test_bfd.*.*'
1392. 'bfd.BFDAPITestCase' selects all tests from test_bfd.py,
140 which are part of BFDAPITestCase class
1413. 'bfd.BFDAPITestCase.test_add_bfd' selects a single test named
142 test_add_bfd from test_bfd.py/BFDAPITestCase
1434. '.*.test_add_bfd' selects all test functions named test_add_bfd
144 from all files/classes
145"""
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200146parser.add_argument(
147 "--filter", action="store", metavar="FILTER_EXPRESSION", help=filter_help_string
148)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200149
150default_retries = 0
151
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200152parser.add_argument(
153 "--retries",
154 action="store",
155 default=default_retries,
156 type=positive_int_or_default(default_retries),
157 help="retry failed tests RETRIES times",
158)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200159
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200160parser.add_argument(
161 "--step", action="store_true", default=False, help="enable stepping through tests"
162)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200163
164debug_help_string = """\
165attach - attach to already running vpp
166core - detect coredump and load core in gdb on crash
167gdb - print VPP PID and pause allowing attaching gdb
168gdbserver - same as above, but run gdb in gdbserver
169"""
170
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200171parser.add_argument(
172 "--debug",
173 action="store",
174 choices=["attach", "core", "gdb", "gdbserver"],
175 help=debug_help_string,
176)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200177
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200178parser.add_argument(
179 "--debug-framework",
180 action="store_true",
181 help="enable internal test framework debugging",
182)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200183
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200184parser.add_argument(
185 "--compress-core",
186 action="store_true",
187 help="compress core files if not debugging them",
188)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200189
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200190parser.add_argument("--extended", action="store_true", help="run extended tests")
Klement Sekerab23ffd72021-05-31 16:08:53 +0200191
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200192parser.add_argument(
193 "--sanity", action="store_true", help="perform sanity vpp run before running tests"
194)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200195
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200196parser.add_argument(
197 "--force-foreground",
198 action="store_true",
199 help="force running in foreground - don't fork",
200)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200201
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200202parser.add_argument(
203 "--jobs",
204 action="store",
205 type=positive_int_or_auto,
206 default="auto",
207 help="maximum concurrent test jobs",
208)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200209
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200210parser.add_argument(
211 "--venv-dir", action="store", type=directory, help="path to virtual environment"
212)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200213
214default_rnd_seed = time.time()
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200215parser.add_argument(
216 "--rnd-seed",
217 action="store",
218 default=default_rnd_seed,
219 type=positive_float_or_default(default_rnd_seed),
220 help="random generator seed (default: current time)",
221)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200222
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200223parser.add_argument(
224 "--vpp-worker-count",
225 action="store",
226 type=worker_config,
227 default=0,
228 help="number of vpp workers",
229)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200230
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200231parser.add_argument(
232 "--gcov", action="store_true", default=False, help="running gcov tests"
233)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200234
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200235parser.add_argument(
236 "--cache-vpp-output",
237 action="store_true",
238 default=False,
239 help="cache VPP stdout/stderr and log as one block after test finishes",
240)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200241
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200242parser.add_argument(
243 "--vpp-ws-dir",
244 action="store",
245 required=True,
246 type=directory,
247 help="vpp workspace directory",
248)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200249
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200250parser.add_argument(
251 "--vpp-tag",
252 action="store",
253 default="vpp_debug",
254 metavar="VPP_TAG",
255 required=True,
256 help="vpp tag (e.g. vpp, vpp_debug, vpp_gcov)",
257)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200258
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200259parser.add_argument(
260 "--vpp",
261 action="store",
262 help="path to vpp binary (default: derive from VPP_WS_DIR and VPP_TAG)",
263)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200264
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200265parser.add_argument(
266 "--vpp-install-dir",
267 type=directory,
268 action="store",
269 help="path to vpp install directory"
270 "(default: derive from VPP_WS_DIR and VPP_TAG)",
271)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200272
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200273parser.add_argument(
274 "--vpp-build-dir",
275 action="store",
276 type=directory,
277 help="vpp build directory (default: derive from VPP_WS_DIR and VPP_TAG)",
278)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200279
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200280parser.add_argument(
281 "--vpp-plugin-dir",
282 action="append",
283 type=directory,
284 help="directory containing vpp plugins"
285 "(default: derive from VPP_WS_DIR and VPP_TAG)",
286)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200287
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200288parser.add_argument(
289 "--vpp-test-plugin-dir",
290 action="append",
291 type=directory,
292 help="directory containing vpp api test plugins"
293 "(default: derive from VPP_WS_DIR and VPP_TAG)",
294)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200295
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200296parser.add_argument(
297 "--extern-plugin-dir",
298 action="append",
299 type=directory,
300 default=[],
301 help="directory containing external plugins",
302)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200303
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200304parser.add_argument(
Ole Troan37157da2022-12-01 11:22:06 +0100305 "--extern-apidir",
306 action="append",
307 type=directory,
308 default=[],
309 help="directory to look for API JSON files",
310)
311
312parser.add_argument(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200313 "--coredump-size",
314 action="store",
315 default="unlimited",
316 help="specify vpp coredump size",
317)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200318
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200319parser.add_argument(
320 "--max-vpp-cpus",
321 action="store",
322 type=int_or_auto,
323 default=0,
324 help="max cpus used by vpp",
325)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200326
327variant_help_string = """\
328specify which march node variant to unit test
329 e.g. --variant=skx - test the skx march variants
330 e.g. --variant=icl - test the icl march variants
331"""
332
333parser.add_argument("--variant", action="store", help=variant_help_string)
334
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200335parser.add_argument(
336 "--api-fuzz", action="store", default=None, help="specify api fuzzing parameters"
337)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200338
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200339parser.add_argument(
340 "--wipe-tmp-dir",
341 action="store_true",
342 default=True,
343 help="remove test tmp directory before running test",
344)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200345
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200346parser.add_argument(
347 "--tmp-dir",
348 action="store",
349 default="/tmp",
350 type=directory_verify_or_create,
351 help="directory where to store test temporary directories",
352)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200353
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200354parser.add_argument(
355 "--log-dir",
356 action="store",
357 type=directory_verify_or_create,
358 help="directory where to store directories "
359 "containing log files (default: --tmp-dir)",
360)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200361
362default_keep_pcaps = False
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200363parser.add_argument(
364 "--keep-pcaps",
365 action="store_true",
366 default=default_keep_pcaps,
Klement Sekera152a9b62022-05-13 18:01:36 +0200367 help=f"if set, keep all pcap files from a test run (default: {default_keep_pcaps})",
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200368)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200369
Naveen Joyc872cec2022-08-30 13:59:03 -0700370parser.add_argument(
371 "-r",
372 "--use-running-vpp",
373 dest="running_vpp",
374 required=False,
375 action="store_true",
376 default=False,
377 help="Runs tests against a running VPP.",
378)
379
380parser.add_argument(
381 "-d",
382 "--socket-dir",
383 dest="socket_dir",
384 required=False,
385 action="store",
386 default="",
387 help="Relative or absolute path to running VPP's socket directory.\n"
388 "The directory must contain VPP's socket files:api.sock & stats.sock.\n"
389 "Default: /var/run/vpp if VPP is started as the root user, else "
390 "/var/run/user/${uid}/vpp.",
391)
392
Klement Sekerab23ffd72021-05-31 16:08:53 +0200393config = parser.parse_args()
394
395ws = config.vpp_ws_dir
396br = f"{ws}/build-root"
397tag = config.vpp_tag
398
399if config.vpp_install_dir is None:
400 config.vpp_install_dir = f"{br}/install-{tag}-native"
401
402if config.vpp is None:
403 config.vpp = f"{config.vpp_install_dir}/vpp/bin/vpp"
404
405if config.vpp_build_dir is None:
406 config.vpp_build_dir = f"{br}/build-{tag}-native"
407
408libs = ["lib", "lib64"]
409
410if config.vpp_plugin_dir is None:
411 config.vpp_plugin_dir = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200412 f"{config.vpp_install_dir}/vpp/{lib}/vpp_plugins" for lib in libs
413 ]
Klement Sekerab23ffd72021-05-31 16:08:53 +0200414
415if config.vpp_test_plugin_dir is None:
416 config.vpp_test_plugin_dir = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200417 f"{config.vpp_install_dir}/vpp/{lib}/vpp_api_test_plugins" for lib in libs
418 ]
Klement Sekerab23ffd72021-05-31 16:08:53 +0200419
420test_dirs = [f"{ws}/test"]
421
422if config.test_src_dir is not None:
423 test_dirs.extend(config.test_src_dir)
424
425config.test_src_dir = test_dirs
426
427
428if config.venv_dir is None:
Saima Yunusc7f93b32022-08-10 03:25:31 -0400429 config.venv_dir = f"{ws}/build-root/test/venv"
Klement Sekerab23ffd72021-05-31 16:08:53 +0200430
Klement Sekera152a9b62022-05-13 18:01:36 +0200431if config.failed_dir is None:
432 config.failed_dir = f"{config.tmp_dir}"
433
Klement Sekerab23ffd72021-05-31 16:08:53 +0200434available_cpus = psutil.Process().cpu_affinity()
435num_cpus = len(available_cpus)
436
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200437if config.max_vpp_cpus == "auto":
Klement Sekerab23ffd72021-05-31 16:08:53 +0200438 max_vpp_cpus = num_cpus
439elif config.max_vpp_cpus > 0:
440 max_vpp_cpus = min(config.max_vpp_cpus, num_cpus)
441else:
442 max_vpp_cpus = num_cpus
443
444if __name__ == "__main__":
445 print("Provided arguments:")
446 for i in config.__dict__:
447 print(f" {i} is {config.__dict__[i]}")