blob: e73555723ffa7ab209e4abb9a0d97c47fade0329 [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(
305 "--coredump-size",
306 action="store",
307 default="unlimited",
308 help="specify vpp coredump size",
309)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200310
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200311parser.add_argument(
312 "--max-vpp-cpus",
313 action="store",
314 type=int_or_auto,
315 default=0,
316 help="max cpus used by vpp",
317)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200318
319variant_help_string = """\
320specify which march node variant to unit test
321 e.g. --variant=skx - test the skx march variants
322 e.g. --variant=icl - test the icl march variants
323"""
324
325parser.add_argument("--variant", action="store", help=variant_help_string)
326
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200327parser.add_argument(
328 "--api-fuzz", action="store", default=None, help="specify api fuzzing parameters"
329)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200330
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200331parser.add_argument(
332 "--wipe-tmp-dir",
333 action="store_true",
334 default=True,
335 help="remove test tmp directory before running test",
336)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200337
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200338parser.add_argument(
339 "--tmp-dir",
340 action="store",
341 default="/tmp",
342 type=directory_verify_or_create,
343 help="directory where to store test temporary directories",
344)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200345
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200346parser.add_argument(
347 "--log-dir",
348 action="store",
349 type=directory_verify_or_create,
350 help="directory where to store directories "
351 "containing log files (default: --tmp-dir)",
352)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200353
354default_keep_pcaps = False
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200355parser.add_argument(
356 "--keep-pcaps",
357 action="store_true",
358 default=default_keep_pcaps,
Klement Sekera152a9b62022-05-13 18:01:36 +0200359 help=f"if set, keep all pcap files from a test run (default: {default_keep_pcaps})",
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200360)
Klement Sekerab23ffd72021-05-31 16:08:53 +0200361
Naveen Joyc872cec2022-08-30 13:59:03 -0700362parser.add_argument(
363 "-r",
364 "--use-running-vpp",
365 dest="running_vpp",
366 required=False,
367 action="store_true",
368 default=False,
369 help="Runs tests against a running VPP.",
370)
371
372parser.add_argument(
373 "-d",
374 "--socket-dir",
375 dest="socket_dir",
376 required=False,
377 action="store",
378 default="",
379 help="Relative or absolute path to running VPP's socket directory.\n"
380 "The directory must contain VPP's socket files:api.sock & stats.sock.\n"
381 "Default: /var/run/vpp if VPP is started as the root user, else "
382 "/var/run/user/${uid}/vpp.",
383)
384
Klement Sekerab23ffd72021-05-31 16:08:53 +0200385config = parser.parse_args()
386
387ws = config.vpp_ws_dir
388br = f"{ws}/build-root"
389tag = config.vpp_tag
390
391if config.vpp_install_dir is None:
392 config.vpp_install_dir = f"{br}/install-{tag}-native"
393
394if config.vpp is None:
395 config.vpp = f"{config.vpp_install_dir}/vpp/bin/vpp"
396
397if config.vpp_build_dir is None:
398 config.vpp_build_dir = f"{br}/build-{tag}-native"
399
400libs = ["lib", "lib64"]
401
402if config.vpp_plugin_dir is None:
403 config.vpp_plugin_dir = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200404 f"{config.vpp_install_dir}/vpp/{lib}/vpp_plugins" for lib in libs
405 ]
Klement Sekerab23ffd72021-05-31 16:08:53 +0200406
407if config.vpp_test_plugin_dir is None:
408 config.vpp_test_plugin_dir = [
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200409 f"{config.vpp_install_dir}/vpp/{lib}/vpp_api_test_plugins" for lib in libs
410 ]
Klement Sekerab23ffd72021-05-31 16:08:53 +0200411
412test_dirs = [f"{ws}/test"]
413
414if config.test_src_dir is not None:
415 test_dirs.extend(config.test_src_dir)
416
417config.test_src_dir = test_dirs
418
419
420if config.venv_dir is None:
Saima Yunusc7f93b32022-08-10 03:25:31 -0400421 config.venv_dir = f"{ws}/build-root/test/venv"
Klement Sekerab23ffd72021-05-31 16:08:53 +0200422
Klement Sekera152a9b62022-05-13 18:01:36 +0200423if config.failed_dir is None:
424 config.failed_dir = f"{config.tmp_dir}"
425
Klement Sekerab23ffd72021-05-31 16:08:53 +0200426available_cpus = psutil.Process().cpu_affinity()
427num_cpus = len(available_cpus)
428
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200429if config.max_vpp_cpus == "auto":
Klement Sekerab23ffd72021-05-31 16:08:53 +0200430 max_vpp_cpus = num_cpus
431elif config.max_vpp_cpus > 0:
432 max_vpp_cpus = min(config.max_vpp_cpus, num_cpus)
433else:
434 max_vpp_cpus = num_cpus
435
436if __name__ == "__main__":
437 print("Provided arguments:")
438 for i in config.__dict__:
439 print(f" {i} is {config.__dict__[i]}")