e5e997583dbf90792fe5b3357a0a3a70e0c44675
[vpp.git] / test / Makefile
1 ASAN_OPTIONS?=verify_asan_link_order=0:detect_leaks=0:abort_on_error=1:unmap_shadow_on_exit=1:disable_coredump=0
2 export ASAN_OPTIONS
3
4 .PHONY: verify-env
5 verify-env:
6 ifndef WS_ROOT
7         $(error WS_ROOT is not set)
8 endif
9 ifndef BR
10         $(error BR is not set)
11 endif
12 ifndef TEST_DIR
13         $(error TEST_DIR is not set)
14 endif
15
16 export TEST_BR = $(TEST_DIR)
17 FAILED_DIR=/tmp/vpp-failed-unittests/
18 VPP_TEST_DIRS=$(shell ls -d $(TEST_DIR) $(EXTERN_TESTS))
19
20 FORCE_NO_WIPE=0
21 ifeq ($(DEBUG),gdb)
22 FORCE_FOREGROUND=1
23 else ifeq ($(DEBUG),gdbserver)
24 FORCE_FOREGROUND=1
25 else ifeq ($(DEBUG),gdb-all)
26 FORCE_FOREGROUND=1
27 else ifeq ($(DEBUG),gdbserver-all)
28 FORCE_FOREGROUND=1
29 else ifeq ($(DEBUG),core)
30 FORCE_FOREGROUND=1
31 else ifeq ($(DEBUG),attach)
32 FORCE_FOREGROUND=1
33 FORCE_NO_WIPE=1
34 else ifeq ($(STEP),yes)
35 FORCE_FOREGROUND=1
36 else ifeq ($(STEP),y)
37 FORCE_FOREGROUND=1
38 else ifeq ($(STEP),1)
39 FORCE_FOREGROUND=1
40 else
41 FORCE_FOREGROUND=0
42 endif
43
44 ifdef PROFILE_OUTPUT
45 PROFILE_OUTPUT_OPTS=-o $(PROFILE_OUTPUT)
46 endif
47
48 ifndef PROFILE_SORT_BY
49 PROFILE_SORT_BY=cumtime
50 endif
51
52 ifeq ($(PROFILE),1)
53 PYTHON_OPTS="-m cProfile $(PROFILE_OUTPUT_OPTS) -s $(PROFILE_SORT_BY)"
54 FORCE_FOREGROUND=1
55 endif
56
57 VENV_BR_DIR=$(BR)/test
58 VENV_PATH=$(VENV_BR_DIR)/venv
59
60 ifeq ($(TEST_DEBUG),1)
61 VENV_RUN_DIR:=$(VENV_PATH)/run-debug
62 else
63 VENV_RUN_DIR:=$(VENV_PATH)/run
64 endif
65
66 ifeq ($(PYTHON),)
67 PYTHON_INTERP=python3
68 else
69 PYTHON_INTERP=$(PYTHON)
70 endif
71
72 ifeq ($(V),)
73 V=0
74 endif
75
76 PYTHON_VERSION=$(shell $(PYTHON_INTERP) -c 'import sys; print(sys.version_info.major)')
77 PIP_VERSION=22.0.4
78 # Keep in sync with requirements.txt
79 PIP_TOOLS_VERSION=6.6.0
80 PIP_SETUPTOOLS_VERSION=62.1.0
81 PYTHON_DEPENDS=requirements-$(PYTHON_VERSION).txt
82 SCAPY_SOURCE=$(shell find $(VENV_PATH)/lib/python* -name site-packages)
83 BUILD_COV_DIR=$(TEST_BR)/coverage
84
85 PIP_TOOLS_INSTALL_DONE=$(VENV_RUN_DIR)/pip-tools-install-$(PYTHON_VERSION)-$(PIP_TOOLS_VERSION).done
86 PIP_INSTALL_DONE=$(VENV_RUN_DIR)/pip-install-$(PYTHON_VERSION)-$(PIP_VERSION).done
87 PIP_PATCH_DONE=$(VENV_RUN_DIR)/pip-patch-$(PYTHON_VERSION).done
88 PAPI_INSTALL_DONE=$(VENV_RUN_DIR)/papi-install-$(PYTHON_VERSION).done
89 PAPI_PYTHON_SRC_DIR=$(WS_ROOT)/src/vpp-api/python
90 PAPI_WIPE_DIST=$(WS_ROOT)/src/vpp-api/vapi/__pycache__ \
91         $(PAPI_PYTHON_SRC_DIR)/build \
92         $(PAPI_PYTHON_SRC_DIR)/vpp_papi.egg-info \
93         $(PAPI_PYTHON_SRC_DIR)/vpp_papi/__pycache__
94
95 $(PIP_TOOLS_INSTALL_DONE):
96         @rm -rf $(VENV_PATH)
97         @mkdir -p $(VENV_RUN_DIR)
98         @$(PYTHON_INTERP) -m venv $(VENV_PATH)
99         # pip version pinning
100         @bash -c "source $(VENV_PATH)/bin/activate && \
101                   python3 -m pip install pip===$(PIP_VERSION)"
102         @bash -c "source $(VENV_PATH)/bin/activate && \
103                   python3 -m pip install pip-tools===$(PIP_TOOLS_VERSION)"
104         @bash -c "source $(VENV_PATH)/bin/activate && \
105                   python3 -m pip install setuptools===$(PIP_SETUPTOOLS_VERSION)"
106         @touch $@
107
108 $(PYTHON_DEPENDS): requirements.txt
109         @bash -c "source $(VENV_PATH)/bin/activate && \
110                   CUSTOM_COMPILE_COMMAND='make test-refresh-deps (or update requirements.txt)' \
111                   python3 -m piptools compile -q --generate-hashes requirements.txt --output-file $@"
112
113 $(PIP_INSTALL_DONE): $(PIP_TOOLS_INSTALL_DONE) $(PYTHON_DEPENDS)
114         @bash -c "source $(VENV_PATH)/bin/activate && \
115                   python3 -m piptools sync $(PYTHON_DEPENDS)"
116         @touch $@
117
118 $(PIP_PATCH_DONE): $(PIP_INSTALL_DONE)
119         @echo --- patching ---
120         @sleep 1 # Ensure python recompiles patched *.py files -> *.pyc
121         for f in $(CURDIR)/patches/scapy-2.4.3/*.patch ; do \
122                 echo Applying patch: $$(basename $$f) ; \
123                 patch --forward -p1 -d $(SCAPY_SOURCE) < $$f ; \
124                 retCode=$$?; \
125                 [ $$retCode -gt 1 ] && exit $$retCode; \
126         done; \
127         touch $@
128
129 $(PAPI_INSTALL_DONE): $(PIP_PATCH_DONE)
130         @bash -c "source $(VENV_PATH)/bin/activate && python3 -m pip install -e $(PAPI_PYTHON_SRC_DIR)"
131         @touch $@
132
133 .PHONY: refresh-deps
134 refresh-deps: clean-deps $(PYTHON_DEPENDS)
135
136 .PHONY: clean-deps
137 clean-deps:
138         @rm -f $(PYTHON_DEPENDS)
139
140 INTERN_PLUGIN_SRC_DIR=$(WS_ROOT)/src/plugins
141 ifneq ($(EXTERN_PLUGIN_SRC_DIR),)
142 PLUGIN_SRC_DIR=$(EXTERN_PLUGIN_SRC_DIR)
143 else
144 PLUGIN_SRC_DIR=$(INTERN_PLUGIN_SRC_DIR)
145 endif
146
147 .PHONY: sanity
148
149 ifndef TEST_JOBS
150 PARALLEL_ILLEGAL=0
151 TEST_JOBS=1
152 else ifeq ($(FORCE_FOREGROUND),0)
153 PARALLEL_ILLEGAL=0
154 else ifneq ($(findstring $(TEST_JOBS),1 auto),)
155 PARALLEL_ILLEGAL=0
156 else
157 PARALLEL_ILLEGAL=1
158 endif
159
160 ifneq ($(DEBUG),)
161 SANITY=no
162 endif
163
164 ifneq ($(findstring $(SANITY),0 n no),)
165 SANITY_IMPORT_VPP_PAPI_CMD=true
166 ARG0=
167 else
168 SANITY_IMPORT_VPP_PAPI_CMD=source $(VENV_PATH)/bin/activate && $(PYTHON_INTERP) sanity_import_vpp_papi.py
169 ARG0=--sanity
170 endif
171
172 ARG1=
173 ifneq ($(findstring $(FAILFAST),1 y yes),)
174 ARG1=--failfast
175 endif
176
177 ARG2=
178 ifneq ($(findstring $(EXTENDED_TESTS),1 y yes),)
179 ARG2=--extended
180 endif
181
182 ARG3=
183 ifneq ($(EXTERN_TESTS),)
184 ARG3=--test-src-dir $(EXTERN_TESTS)
185 endif
186
187 ARG4=
188 ifneq ($(findstring $(FORCE_FOREGROUND),1 y yes),)
189 ARG4=--force-foreground
190 endif
191
192 ARG5=
193 ifneq ($(findstring $(COREDUMP_COMPRESS),1 y yes),)
194 ARG5=--compress-core
195 endif
196
197 ARG6=
198 ifneq ($(findstring $(STEP),1 y yes),)
199 ARG6=--step
200 endif
201
202 ARG7=
203 ifneq ($(findstring $(TESTS_GCOV),1 y yes),)
204 ARG7=--gcov
205 endif
206
207 ARG8=
208 ifneq ($(EXTERN_PLUGINS),)
209 ARG8=--extern-plugin-dir=$(EXTERN_PLUGINS)
210 endif
211
212 ARG9=
213 ifneq ($(DEBUG),)
214 ARG9=--debug=$(DEBUG)
215 endif
216
217 ARG10=
218 ifneq ($(COREDUMP_SIZE),)
219 ARG10=--coredump-size=$(COREDUMP_SIZE)
220 endif
221
222 ARG11=
223 ifneq ($(VARIANT),)
224 ARG11=--variant=$(VARIANT)
225 endif
226
227 ARG12=--cache-vpp-output
228 ifneq ($(findstring $(CACHE_OUTPUT),0 n no),)
229 ARG12=
230 endif
231
232 ARG13=
233 ifneq ($(MAX_VPP_CPUS),)
234 ARG13=--max-vpp-cpus=$(MAX_VPP_CPUS)
235 endif
236
237 ARG14=
238 ifneq ($(TIMEOUT),)
239 ARG14=--timeout=$(TIMEOUT)
240 endif
241
242 ARG15=
243 ifneq ($(findstring $(TEST_DEBUG),1 y yes),)
244 ARG15=--debug-framework
245 endif
246
247 ARG16=
248 ifneq ($(findstring $(API_FUZZ),1 y yes),)
249 ARG16=--api-fuzz=on
250 endif
251
252 ARG17=
253 ifneq ($(EXTERN_APIDIR),)
254 ARG17=--extern-apidir=$(EXTERN_APIDIR)
255 endif
256
257 EXTRA_ARGS=$(ARG0) $(ARG1) $(ARG2) $(ARG3) $(ARG4) $(ARG5) $(ARG6) $(ARG7) $(ARG8) $(ARG9) $(ARG10) $(ARG11) $(ARG12) $(ARG13) $(ARG14) $(ARG15) $(ARG16) $(ARG17)
258
259 RUN_TESTS_ARGS=--failed-dir=$(FAILED_DIR) --verbose=$(V) --jobs=$(TEST_JOBS) --filter=$(TEST) --retries=$(RETRIES) --venv-dir=$(VENV_PATH) --vpp-ws-dir=$(WS_ROOT) --vpp-tag=$(TAG) --rnd-seed=$(RND_SEED) --vpp-worker-count="$(VPP_WORKER_COUNT)" --keep-pcaps $(PLUGIN_PATH_ARGS) $(TEST_PLUGIN_PATH_ARGS) $(EXTRA_ARGS)
260 RUN_SCRIPT_ARGS=--python-opts=$(PYTHON_OPTS)
261
262 define retest-func
263 @scripts/run.sh $(RUN_SCRIPT_ARGS) $(RUN_TESTS_ARGS) || env FAILED_DIR=$(FAILED_DIR) COMPRESS_FAILED_TEST_LOGS=$(COMPRESS_FAILED_TEST_LOGS) scripts/compress_failed.sh
264 endef
265
266 sanity: test-dep
267         @bash -c "test $(PARALLEL_ILLEGAL) -eq 0 ||\
268             (echo \"*******************************************************************\" &&\
269                  echo \"* Sanity check failed, TEST_JOBS is not 1 or 'auto' and DEBUG, STEP or PROFILE is set\" &&\
270                  echo \"*******************************************************************\" &&\
271                  false)"
272         @bash -c "$(SANITY_IMPORT_VPP_PAPI_CMD) ||\
273                 (echo \"*******************************************************************\" &&\
274                  echo \"* Sanity check failed, cannot import vpp_papi\" &&\
275                  echo \"* to debug: \" &&\
276                  echo \"* 1. enter test shell:   make test-shell\" &&\
277                  echo \"* 2. execute debugger:   gdb python -ex 'run sanity_import_vpp_papi.py'\" &&\
278                  echo \"*******************************************************************\" &&\
279                  false)"
280
281 $(FAILED_DIR): reset
282         @mkdir -p $@
283
284 .PHONY: test-dep
285 test-dep: $(PAPI_INSTALL_DONE) $(FAILED_DIR)
286
287 .PHONY: test
288 test: test-dep sanity
289         $(call retest-func)
290
291 .PHONY: retest
292 retest: verify-env sanity $(FAILED_DIR)
293         $(call retest-func)
294
295 .PHONY: shell
296 shell: test-dep
297         @echo "source $(VENV_PATH)/bin/activate;\
298                 export RND_SEED=$(RND_SEED);\
299                 echo '***';\
300                 echo PYTHONPATH=$(PYTHONPATH);\
301                 echo RND_SEED=$(RND_SEED);\
302                 echo VPP_BUILD_DIR=$(VPP_BUILD_DIR);\
303                 echo VPP_PLUGIN_PATH=$(VPP_PLUGIN_PATH);\
304                 echo VPP_TEST_PLUGIN_PATH=$(VPP_TEST_PLUGIN_PATH);\
305                 echo VPP_INSTALL_PATH=$(VPP_INSTALL_PATH);\
306                 echo EXTERN_TESTS=$(EXTERN_TESTS);\
307                 echo EXTERN_PLUGINS=$(EXTERN_PLUGINS);\
308         echo EXTERN_COV_DIR=$(EXTERN_COV_DIR);\
309                 echo LD_LIBRARY_PATH=$(LD_LIBRARY_PATH);\
310                 echo '***';\
311                 exec </dev/tty" | bash -i
312
313 .PHONY: reset
314 reset:
315         @rm -f /dev/shm/vpp-unittest-*
316         @if [ $(FORCE_NO_WIPE) -eq "0" ] ; then rm -rf /tmp/vpp-unittest-*;  fi
317         @rm -f /tmp/api_post_mortem.*
318         @rm -rf $(FAILED_DIR)
319         @rm -rf /tmp/vpp-vm-tests
320
321 .PHONY: wipe
322 wipe: reset
323         @rm -rf $(VENV_BR_DIR)
324         @rm -rf $(patsubst %,%/__pycache__, $(VPP_TEST_DIRS))
325
326 $(BUILD_COV_DIR):
327         @mkdir -p $@
328
329 .PHONY: cov
330 cov: wipe-cov test-dep ext $(BUILD_COV_DIR)
331         @lcov --zerocounters --directory $(VPP_BUILD_DIR)
332         @test -z "$(EXTERN_COV_DIR)" || lcov --zerocounters --directory $(EXTERN_COV_DIR)
333         $(call retest-func)
334         @lcov --capture --directory $(VPP_BUILD_DIR) --output-file $(BUILD_COV_DIR)/coverage.info
335         @test -z "$(EXTERN_COV_DIR)" || lcov --capture --directory $(EXTERN_COV_DIR) --output-file $(BUILD_COV_DIR)/extern-coverage.info
336         @genhtml $(BUILD_COV_DIR)/coverage.info --output-directory $(BUILD_COV_DIR)/html
337         @test -z "$(EXTERN_COV_DIR)" || genhtml $(BUILD_COV_DIR)/extern-coverage.info --output-directory $(BUILD_COV_DIR)/extern-html
338         @echo
339         @echo "Build finished. Code coverage report is in $(BUILD_COV_DIR)/html/index.html"
340         @test -z "$(EXTERN_COV_DIR)" || echo "Code coverage report for out-of-tree objects is in $(BUILD_COV_DIR)/extern-html/index.html"
341
342 .PHONY: wipe-cov
343 wipe-cov: wipe
344         @rm -rf $(BUILD_COV_DIR)
345
346 .PHONY: wipe-papi
347 wipe-papi:
348         @rm -rf $(PAPI_INSTALL_DONE) $(PAPI_WIPE_DIST)
349
350 .PHONY: wipe-all
351 wipe-all: wipe wipe-papi wipe-cov
352         @rm -rf $(TEST_BR)
353
354 .PHONY: start-gdb
355 start-gdb: sanity
356         @bash -c "source $(VENV_PATH)/bin/activate && python3 -c 'from debug import start_vpp_in_gdb; start_vpp_in_gdb()' $(RUN_TESTS_ARGS)"
357
358 .PHONY: checkstyle-python-all
359 checkstyle-python-all: $(PIP_INSTALL_DONE)
360         @bash -c "source $(VENV_PATH)/bin/activate &&\
361                 black -t py39 --check --diff $(WS_ROOT) ||\
362                 (echo \"*************************************************************************\" &&\
363                 echo \"* Test framework PEP8 compliance check FAILED (maybe: make fixstyle-python)\" &&\
364                 echo \"*************************************************************************\" &&\
365                 false)"
366         @echo "*******************************************************************"
367         @echo "* Test framework PEP8 compliance check passed"
368         @echo "*******************************************************************"
369
370 .PHONY: checkstyle
371 checkstyle: checkstyle-python-all
372
373 .PHONY: fixstyle-python-all
374 fixstyle-python-all: $(PIP_INSTALL_DONE)
375         @bash -c "source $(VENV_PATH)/bin/activate &&\
376                 black -t py39 $(WS_ROOT) ||\
377                 (echo \"*************************************************************************\" &&\
378                 echo \"* Test framework PEP8 compliance check FAILED (maybe: make fixstyle-python)\" &&\
379                 echo \"*************************************************************************\" &&\
380                 false)"
381         @echo "*******************************************************************"
382         @echo "* Test framework PEP8 compliance check passed"
383         @echo "*******************************************************************"
384
385 .PHONY: help
386 help:
387         @echo "Running tests:"
388         @echo ""
389         @echo " test                   - build and run (basic) functional tests"
390         @echo " test-debug             - build and run (basic) functional tests (debug build)"
391         @echo " test-all               - build and run functional and extended tests"
392         @echo " test-all-debug         - build and run functional and extended tests (debug build)"
393         @echo " retest                 - run functional tests"
394         @echo " retest-debug           - run functional tests (debug build)"
395         @echo " retest-all             - run functional and extended tests"
396         @echo " retest-all-debug       - run functional and extended tests (debug build)"
397         @echo " test-cov               - generate code coverage report for test framework"
398         @echo " test-gcov                      - build and run functional tests (gcov build)"
399         @echo " test-wipe              - wipe (temporary) files generated by unit tests"
400         @echo " test-wipe-cov          - wipe code coverage report for test framework"
401         @echo " test-wipe-papi         - rebuild vpp_papi sources"
402         @echo " test-wipe-all          - wipe (temporary) files generated by unit tests, and coverage"
403         @echo " test-shell             - enter shell with test environment"
404         @echo " test-shell-debug       - enter shell with test environment (debug build)"
405         @echo " test-refresh-deps      - refresh the Python dependencies for the tests"
406         @echo ""
407         @echo "Arguments controlling test runs:"
408         @echo ""
409         @echo " V=[0|1|2]              - set test verbosity level"
410         @echo "                          0=ERROR, 1=INFO, 2=DEBUG"
411         @echo " TEST_JOBS=[<n>|auto]   - use at most <n> parallel python processes for test execution, if auto, set to number of available cpus (default: 1)"
412         @echo " MAX_VPP_CPUS=[<n>|auto]- use at most <n> cpus for running vpp main and worker threads, if auto, set to number of available cpus (default: auto)"
413         @echo " CACHE_OUTPUT=[0|n|no]  - disable cache VPP stdout/stderr and log as one block after test finishes (default: yes)"
414         @echo " FAILFAST=[1|y|yes]     - fail fast if 1, otherwise complete all tests"
415         @echo " TIMEOUT=<timeout>      - fail test suite if any single test takes longer than <timeout> (in seconds) to finish (default: 600)"
416         @echo " RETRIES=<n>            - retry failed tests <n> times"
417         @echo " DEBUG=<type>           - set VPP debugging kind"
418         @echo "    DEBUG=core          - detect coredump and load it in gdb on crash"
419         @echo "    DEBUG=gdb           - allow easy debugging by printing VPP PID"
420         @echo "                          and waiting for user input before running"
421         @echo "                          and tearing down a testcase"
422         @echo "    DEBUG=gdbserver     - run gdb inside a gdb server, otherwise"
423         @echo "                          same as above"
424         @echo "    DEBUG=attach        - attach test case to already running vpp in gdb (see test-start-vpp-in-gdb)"
425         @echo " STEP=[1|y|yes]         - enable stepping through a testcase (for testcase debugging)"
426         @echo " SANITY=[0|n|no]        - disable sanity import of vpp-api/sanity vpp run before running tests"
427         @echo " EXTENDED_TESTS=[1|y|yes] - run extended tests"
428         @echo " TEST=<filter>          - filter the set of tests:"
429         @echo "    by file-name        - only run tests from specified file, e.g. TEST=test_bfd selects all tests from test_bfd.py"
430         @echo "    by file-suffix      - same as file-name, but 'test_' is omitted e.g. TEST=bfd selects all tests from test_bfd.py"
431         @echo "    by wildcard         - wildcard filter is <file>.<class>.<test function>, each can be replaced by '*'"
432         @echo "                          e.g. TEST='test_bfd.*.*' is equivalent to above example of filter by file-name"
433         @echo "                               TEST='bfd.*.*' is equivalent to above example of filter by file-suffix"
434         @echo "                               TEST='bfd.BFDAPITestCase.*' selects all tests from test_bfd.py which are part of BFDAPITestCase class"
435         @echo "                               TEST='bfd.BFDAPITestCase.test_add_bfd' selects a single test named test_add_bfd from test_bfd.py/BFDAPITestCase"
436         @echo "                               TEST='*.*.test_add_bfd' selects all test functions named test_add_bfd from all files/classes"
437         @echo " VARIANT=<variant>      - specify which march node variant to unit test"
438         @echo "                          e.g. VARIANT=skx test the skx march variants"
439         @echo "                          e.g. VARIANT=icl test the icl march variants"
440         @echo " COREDUMP_SIZE=<size>   - pass <size> as unix { coredump-size <size> } argument to vpp"
441         @echo "                          e.g. COREDUMP_SIZE=4g"
442         @echo "                               COREDUMP_SIZE=unlimited"
443         @echo " COREDUMP_COMPRESS=[1|y|yes] - compress core files if not debugging them"
444         @echo " EXTERN_TESTS=<path>    - path to out-of-tree test_<name>.py files containing test cases"
445         @echo " EXTERN_PLUGINS=<path>  - path to out-of-tree plugins to be loaded by vpp under test"
446         @echo " EXTERN_COV_DIR=<path>  - path to out-of-tree prefix, where source, object and .gcda files can be found for coverage report"
447         @echo " PROFILE=[1|y|yes]      - enable profiling of test framework via cProfile module"
448         @echo " PROFILE_SORT_BY=opt    - sort profiling report by opt - consult cProfile documentation for possible values (default: cumtime)"
449         @echo " PROFILE_OUTPUT=file    - output profiling info to file - use absolute path (default: stdout)"
450         @echo " TEST_DEBUG=[1|y|yes]   - enable debugging of the test framework itself (expert)"
451         @echo " API_FUZZ=[1|y|yes]     - enable VPP api fuzz testing"
452         @echo " RND_SEED=<seed>        - Seed RND with given seed"
453         @echo ""
454         @echo "Starting VPP in GDB for use with DEBUG=attach:"
455         @echo ""
456         @echo " test-start-vpp-in-gdb       - start VPP in gdb (release)"
457         @echo " test-start-vpp-debug-in-gdb - start VPP in gdb (debug)"
458         @echo ""
459         @echo "Creating test code coverage report:"
460         @echo ""
461         @echo " test-cov               - generate code coverage report for test framework"
462         @echo " test-wipe-cov          - wipe code coverage report for test framework"