aboutsummaryrefslogtreecommitdiffstats
path: root/test/config.py
blob: d2f14c82e190f455a6f0fe950bed7b494cc2e0fe (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import argparse
import os
import psutil
import time


def positive_int_or_default(default):
    def positive_integer(v):
        if v is None or v == "":
            return default
        if int(v) <= 0:
            raise ValueError("value must be positive")
        return int(v)

    return positive_integer


def positive_float_or_default(default):
    def positive_float(v):
        if v is None or v == "":
            return default
        if float(v) <= 0:
            raise ValueError("value must be positive")
        return float(v)

    return positive_float


def positive_int_or_auto(v):
    if v is None or v in ("", "auto"):
        return "auto"
    if int(v) <= 0:
        raise ValueError("value must be positive or auto")
    return int(v)


def int_or_auto(v):
    if v is None or v in ("", "auto"):
        return "auto"
    if int(v) < 0:
        raise ValueError("value must be positive or auto")
    return int(v)


def int_choice_or_default(options, default):
    assert default in options

    def choice(v):
        if v is None or v == "":
            return default
        if int(v) in options:
            return int(v)
        raise ValueError("invalid choice")

    return choice


def worker_config(v):
    if v is None or v == "":
        return 0
    if v.startswith("workers "):
        return int(v.split(" ")[1])
    return int(v)


def directory(v):
    if not os.path.isdir(v):
        raise ValueError(f"provided path '{v}' doesn't exist or is not a directory")
    return v


def directory_verify_or_create(v):
    if not os.path.isdir(v):
        os.mkdir(v)
    return v


parser = argparse.ArgumentParser(
    description="VPP unit tests", formatter_class=argparse.RawTextHelpFormatter
)

parser.add_argument(
    "--failfast", action="store_true", help="stop running tests on first failure"
)

parser.add_argument(
    "--test-src-dir",
    action="append",
    type=directory,
    help="directory containing test files "
    "(may be specified multiple times) "
    "(VPP_WS_DIR/test is added automatically to the set)",
)

default_verbose = 0

parser.add_argument(
    "--verbose",
    action="store",
    default=default_verbose,
    type=int_choice_or_default((0, 1, 2), default_verbose),
    help="verbosity setting - 0 - least verbose, 2 - most verbose (default: 0)",
)

default_test_run_timeout = 600

parser.add_argument(
    "--timeout",
    action="store",
    type=positive_int_or_default(default_test_run_timeout),
    default=default_test_run_timeout,
    metavar="TEST_RUN_TIMEOUT",
    help="test run timeout in seconds - per test "
    f"(default: {default_test_run_timeout})",
)

parser.add_argument(
    "--failed-dir",
    action="store",
    type=directory,
    help="directory containing failed tests (default: --tmp-dir)",
)

filter_help_string = """\
expression consists of 3 string selectors separated by '.' separators:

    <file>.<class>.<function>

- selectors restrict which files/classes/functions are run
- selector can be replaced with '*' or omitted entirely if not needed
- <file> selector is automatically prepended with 'test_' if required
- '.' separators are required only if selector(s) follow(s)

examples:

1. all of the following expressions are equivalent and will select
   all test classes and functions from test_bfd.py:
   'test_bfd' 'bfd' 'test_bfd..' 'bfd.' 'bfd.*.*' 'test_bfd.*.*'
2. 'bfd.BFDAPITestCase' selects all tests from test_bfd.py,
   which are part of BFDAPITestCase class
3. 'bfd.BFDAPITestCase.test_add_bfd' selects a single test named
   test_add_bfd from test_bfd.py/BFDAPITestCase
4. '.*.test_add_bfd' selects all test functions named test_add_bfd
   from all files/classes
"""
parser.add_argument(
    "--filter", action="store", metavar="FILTER_EXPRESSION", help=filter_help_string
)

default_retries = 0

parser.add_argument(
    "--retries",
    action="store",
    default=default_retries,
    type=positive_int_or_default(default_retries),
    help="retry failed tests RETRIES times",
)

parser.add_argument(
    "--step", action="store_true", default=False, help="enable stepping through tests"
)

debug_help_string = """\
attach     - attach to already running vpp
core       - detect coredump and load core in gdb on crash
gdb        - print VPP PID and pause allowing attaching gdb
gdbserver  - same as above, but run gdb in gdbserver
"""

parser.add_argument(
    "--debug",
    action="store",
    choices=["attach", "core", "gdb", "gdbserver"],
    help=debug_help_string,
)

parser.add_argument(
    "--debug-framework",
    action="store_true",
    help="enable internal test framework debugging",
)

parser.add_argument(
    "--compress-core",
    action="store_true",
    help="compress core files if not debugging them",
)

parser.add_argument("--extended", action="store_true", help="run extended tests")

parser.add_argument(
    "--sanity", action="store_true", help="perform sanity vpp run before running tests"
)

parser.add_argument(
    "--force-foreground",
    action="store_true",
    help="force running in foreground - don't fork",
)

parser.add_argument(
    "--jobs",
    action="store",
    type=positive_int_or_auto,
    default="auto",
    help="maximum concurrent test jobs",
)

parser.add_argument(
    "--venv-dir", action="store", type=directory, help="path to virtual environment"
)

default_rnd_seed = time.time()
parser.add_argument(
    "--rnd-seed",
    action="store",
    default=default_rnd_seed,
    type=positive_float_or_default(default_rnd_seed),
    help="random generator seed (default: current time)",
)

parser.add_argument(
    "--vpp-worker-count",
    action="store",
    type=worker_config,
    default=0,
    help="number of vpp workers",
)

parser.add_argument(
    "--gcov", action="store_true", default=False, help="running gcov tests"
)

parser.add_argument(
    "--cache-vpp-output",
    action="store_true",
    default=False,
    help="cache VPP stdout/stderr and log as one block after test finishes",
)

parser.add_argument(
    "--vpp-ws-dir",
    action="store",
    required=True,
    type=directory,
    help="vpp workspace directory",
)

parser.add_argument(
    "--vpp-tag",
    action="store",
    default="vpp_debug",
    metavar="VPP_TAG",
    required=True,
    help="vpp tag (e.g. vpp, vpp_debug, vpp_gcov)",
)

parser.add_argument(
    "--vpp",
    action="store",
    help="path to vpp binary (default: derive from VPP_WS_DIR and VPP_TAG)",
)

parser.add_argument(
    "--vpp-install-dir",
    type=directory,
    action="store",
    help="path to vpp install directory"
    "(default: derive from VPP_WS_DIR and VPP_TAG)",
)

parser.add_argument(
    "--vpp-build-dir",
    action="store",
    type=directory,
    help="vpp build directory (default: derive from VPP_WS_DIR and VPP_TAG)",
)

parser.add_argument(
    "--vpp-plugin-dir",
    action="append",
    type=directory,
    help="directory containing vpp plugins"
    "(default: derive from VPP_WS_DIR and VPP_TAG)",
)

parser.add_argument(
    "--vpp-test-plugin-dir",
    action="append",
    type=directory,
    help="directory containing vpp api test plugins"
    "(default: derive from VPP_WS_DIR and VPP_TAG)",
)

parser.add_argument(
    "--extern-plugin-dir",
    action="append",
    type=directory,
    default=[],
    help="directory containing external plugins",
)

parser.add_argument(
    "--extern-apidir",
    action="append",
    type=directory,
    default=[],
    help="directory to look for API JSON files",
)

parser.add_argument(
    "--coredump-size",
    action="store",
    default="unlimited",
    help="specify vpp coredump size",
)

parser.add_argument(
    "--max-vpp-cpus",
    action="store",
    type=int_or_auto,
    default=0,
    help="max cpus used by vpp",
)

variant_help_string = """\
specify which march node variant to unit test
  e.g. --variant=skx - test the skx march variants
  e.g. --variant=icl - test the icl march variants
"""

parser.add_argument("--variant", action="store", help=variant_help_string)

parser.add_argument(
    "--api-fuzz", action="store", default=None, help="specify api fuzzing parameters"
)

parser.add_argument(
    "--wipe-tmp-dir",
    action="store_true",
    default=True,
    help="remove test tmp directory before running test",
)

parser.add_argument(
    "--tmp-dir",
    action="store",
    default="/tmp",
    type=directory_verify_or_create,
    help="directory where to store test temporary directories",
)

parser.add_argument(
    "--log-dir",
    action="store",
    type=directory_verify_or_create,
    help="directory where to store directories "
    "containing log files (default: --tmp-dir)",
)

default_keep_pcaps = False
parser.add_argument(
    "--keep-pcaps",
    action="store_true",
    default=default_keep_pcaps,
    help=f"if set, keep all pcap files from a test run (default: {default_keep_pcaps})",
)

parser.add_argument(
    "-r",
    "--use-running-vpp",
    dest="running_vpp",
    required=False,
    action="store_true",
    default=False,
    help="Runs tests against a running VPP.",
)

parser.add_argument(
    "-d",
    "--socket-dir",
    dest="socket_dir",
    required=False,
    action="store",
    default="",
    help="Relative or absolute path to running VPP's socket directory.\n"
    "The directory must contain VPP's socket files:api.sock & stats.sock.\n"
    "Default: /var/run/vpp if VPP is started as the root user, else "
    "/var/run/user/${uid}/vpp.",
)

config = parser.parse_args()

ws = config.vpp_ws_dir
br = f"{ws}/build-root"
tag = config.vpp_tag

if config.vpp_install_dir is None:
    config.vpp_install_dir = f"{br}/install-{tag}-native"

if config.vpp is None:
    config.vpp = f"{config.vpp_install_dir}/vpp/bin/vpp"

if config.vpp_build_dir is None:
    config.vpp_build_dir = f"{br}/build-{tag}-native"

libs = ["lib", "lib64"]

if config.vpp_plugin_dir is None:
    config.vpp_plugin_dir = [
        f"{config.vpp_install_dir}/vpp/{lib}/vpp_plugins" for lib in libs
    ]

if config.vpp_test_plugin_dir is None:
    config.vpp_test_plugin_dir = [
        f"{config.vpp_install_dir}/vpp/{lib}/vpp_api_test_plugins" for lib in libs
    ]

test_dirs = [f"{ws}/test"]

if config.test_src_dir is not None:
    test_dirs.extend(config.test_src_dir)

config.test_src_dir = test_dirs


if config.venv_dir is None:
    config.venv_dir = f"{ws}/build-root/test/venv"

if config.failed_dir is None:
    config.failed_dir = f"{config.tmp_dir}"

available_cpus = psutil.Process().cpu_affinity()
num_cpus = len(available_cpus)

if config.max_vpp_cpus == "auto":
    max_vpp_cpus = num_cpus
elif config.max_vpp_cpus > 0:
    max_vpp_cpus = min(config.max_vpp_cpus, num_cpus)
else:
    max_vpp_cpus = num_cpus

if __name__ == "__main__":
    print("Provided arguments:")
    for i in config.__dict__:
        print(f"  {i} is {config.__dict__[i]}")