d8bbf4d5c69831dd6ba2aeb7fc03d69407a28ac2
[vpp.git] / test / Makefile
1 .PHONY: verify-test-dir
2
3 FAILED_DIR=/tmp/vpp-failed-unittests/
4
5 verify-test-dir:
6 ifndef TEST_DIR
7         $(error TEST_DIR is not set)
8 endif
9
10 .PHONY: verify-no-running-vpp
11
12 ifdef VPP_ZOMBIE_NOCHECK
13 VPP_PIDS=
14 else
15 VPP_PIDS=$(shell pgrep -d, -x vpp_main)
16 endif
17
18 ifeq ($(DEBUG),gdb)
19 FORCE_FOREGROUND=1
20 else ifeq ($(DEBUG),gdbserver)
21 FORCE_FOREGROUND=1
22 else ifeq ($(DEBUG),core)
23 FORCE_FOREGROUND=1
24 else ifeq ($(STEP),yes)
25 FORCE_FOREGROUND=1
26 else ifeq ($(STEP),y)
27 FORCE_FOREGROUND=1
28 else ifeq ($(STEP),1)
29 FORCE_FOREGROUND=1
30 else
31 FORCE_FOREGROUND=0
32 endif
33
34 ifdef PROFILE_OUTPUT
35 PROFILE_OUTPUT_OPTS=-o $(PROFILE_OUTPUT)
36 endif
37
38 ifndef PROFILE_SORT_BY
39 PROFILE_SORT_BY=cumtime
40 endif
41
42 ifeq ($(PROFILE),1)
43 PYTHON_PROFILE_OPTS=-m cProfile $(PROFILE_OUTPUT_OPTS) -s $(PROFILE_SORT_BY)
44 FORCE_FOREGROUND=1
45 endif
46
47 verify-no-running-vpp:
48         @if [ "$(VPP_PIDS)" != "" ]; then \
49                 echo; \
50                 echo "*** Existing vpp processes detected (PID(s): $(VPP_PIDS)). Running tests under these conditions is not supported. ***"; \
51                 echo; \
52                 ps -fp $(VPP_PIDS);\
53                 echo; \
54                 false; \
55         fi
56
57 UNITTEST_EXTRA_OPTS=
58 UNITTEST_FAILFAST_OPTS=
59
60 ifeq ($(FAILFAST),1)
61 UNITTEST_EXTRA_OPTS=-f
62 endif
63
64 ifneq ($(EXTERN_TESTS),)
65 UNITTEST_EXTRA_OPTS=$(UNITTEST_FAILFAST_OPTS) -d $(EXTERN_TESTS)
66 endif
67
68 VENV_PATH=$(TEST_DIR)/run/venv
69
70 ifeq ($(TEST_DEBUG),1)
71 TEST_RUN_DIR:=$(VENV_PATH)/run-debug
72 else
73 TEST_RUN_DIR:=$(VENV_PATH)/run
74 endif
75
76 ifeq ($(PYTHON),)
77 PYTHON_INTERP=python2.7
78 else
79 PYTHON_INTERP=$(PYTHON)
80 endif
81
82 PYTHON_VERSION=$(shell $(PYTHON_INTERP) -c 'import sys; print(sys.version_info.major)')
83 PIP_VERSION=19.1.1
84 PIP_TOOLS_VERSION=3.8.0   # Keep in sync with requirements.txt
85 PYTHON_DEPENDS=requirements-$(PYTHON_VERSION).txt
86 SCAPY_SOURCE=$(shell find $(VENV_PATH)/lib/$(PYTHON_INTERP) -name site-packages)
87 BUILD_COV_DIR=$(TEST_DIR)/coverage
88
89 PIP_TOOLS_INSTALL_DONE=$(TEST_RUN_DIR)/pip-tools-install-$(PYTHON_VERSION).done
90 PIP_INSTALL_DONE=$(TEST_RUN_DIR)/pip-install-$(PYTHON_VERSION).done
91 PIP_PATCH_DONE=$(TEST_RUN_DIR)/pip-patch-$(PYTHON_VERSION).done
92 PAPI_INSTALL_DONE=$(TEST_RUN_DIR)/papi-install-$(PYTHON_VERSION).done
93
94 PAPI_INSTALL_FLAGS=$(PIP_INSTALL_DONE) $(PIP_PATCH_DONE) $(PAPI_INSTALL_DONE)
95
96 $(PIP_TOOLS_INSTALL_DONE):
97         @rm -rf $(VENV_PATH)
98         @mkdir -p $(TEST_RUN_DIR)
99         @virtualenv $(VENV_PATH) -p $(PYTHON_INTERP)
100         # pip version pinning
101         @bash -c "source $(VENV_PATH)/bin/activate && \
102                   $(PYTHON_INTERP) -m pip install pip===$(PIP_VERSION)"
103         @bash -c "source $(VENV_PATH)/bin/activate && \
104                   $(PYTHON_INTERP) -m pip install pip-tools===$(PIP_TOOLS_VERSION)"
105         @touch $@
106
107 $(PYTHON_DEPENDS): $(PIP_TOOLS_INSTALL_DONE) requirements.txt
108         @bash -c "source $(VENV_PATH)/bin/activate && \
109                   CUSTOM_COMPILE_COMMAND='make test-refresh-deps (or update requirements.txt)' \
110                   $(PYTHON_INTERP) -m piptools compile -q --generate-hashes requirements.txt --output-file $@"
111
112 $(PIP_INSTALL_DONE): $(PYTHON_DEPENDS)
113         @bash -c "source $(VENV_PATH)/bin/activate && \
114                   $(PYTHON_INTERP) -m piptools sync $(PYTHON_DEPENDS)"
115         @touch $@
116
117 $(PIP_PATCH_DONE): $(PIP_INSTALL_DONE)
118         @echo --- patching ---
119         @sleep 1 # Ensure python recompiles patched *.py files -> *.pyc
120         for f in $(CURDIR)/patches/scapy-2.4/*.patch ; do \
121                 echo Applying patch: $$(basename $$f) ; \
122                 patch --forward -p1 -d $(SCAPY_SOURCE) < $$f ; \
123                 retCode=$$?; \
124                 [ $$retCode -gt 1 ] && exit $$retCode; \
125         done; \
126         touch $@
127
128 $(PAPI_INSTALL_DONE): $(PIP_PATCH_DONE)
129         @bash -c "source $(VENV_PATH)/bin/activate && $(PYTHON_INTERP) -m pip install -e $(WS_ROOT)/src/vpp-api/python"
130         @touch $@
131
132 .PHONY: update-deps clear-deps
133 refresh-deps: clean-deps $(PYTHON_DEPENDS)
134
135 clean-deps:
136         @rm -f $(PYTHON_DEPENDS)
137
138 ifneq ($(EXTERN_PLUGIN_SRC_DIR),)
139 PLUGIN_SRC_DIR=$(EXTERN_PLUGIN_SRC_DIR)
140 else
141 PLUGIN_SRC_DIR=$(WS_ROOT)/src/plugins
142 endif
143 PLUGIN_TEST_DIRS=$(shell find $(PLUGIN_SRC_DIR) -type d -name test -exec echo -n " -d {}" \;)
144
145 define retest-func
146 @env FORCE_FOREGROUND=$(FORCE_FOREGROUND) FAILED_DIR=$(FAILED_DIR) VENV_PATH=$(VENV_PATH) scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(VENV_PATH)/bin/activate $(PYTHON_INTERP) $(PYTHON_PROFILE_OPTS) run_tests.py -d $(TEST_DIR)$(PLUGIN_TEST_DIRS) $(UNITTEST_EXTRA_OPTS) || env FAILED_DIR=$(FAILED_DIR) COMPRESS_FAILED_TEST_LOGS=$(COMPRESS_FAILED_TEST_LOGS) scripts/compress_failed.sh
147 endef
148
149 .PHONY: sanity
150
151 ifeq ($(SANITY),no)
152 SANITY_IMPORT_VPP_PAPI_CMD=true
153 SANITY_RUN_VPP_CMD=true
154 else
155 SANITY_IMPORT_VPP_PAPI_CMD=source $(VENV_PATH)/bin/activate && $(PYTHON_INTERP) sanity_import_vpp_papi.py
156 SANITY_RUN_VPP_CMD=source $(VENV_PATH)/bin/activate && $(PYTHON_INTERP) sanity_run_vpp.py
157 endif
158
159 ifndef TEST_JOBS
160 PARALLEL_ILLEGAL=0
161 else ifeq ($(FORCE_FOREGROUND),0)
162 PARALLEL_ILLEGAL=0
163 else ifeq ($(TEST_JOBS),auto)
164 PARALLEL_ILLEGAL=0
165 else ifeq ($(TEST_JOBS),1)
166 PARALLEL_ILLEGAL=0
167 else
168 PARALLEL_ILLEGAL=1
169 endif
170
171 sanity: verify-no-running-vpp
172         @sys_req/dev_shm_size.sh
173         @bash -c "test $(PARALLEL_ILLEGAL) -eq 0 ||\
174             (echo \"*******************************************************************\" &&\
175                  echo \"* Sanity check failed, TEST_JOBS is not 1 or 'auto' and DEBUG, STEP or PROFILE is set\" &&\
176                  echo \"*******************************************************************\" &&\
177                  false)"
178         @bash -c "$(SANITY_IMPORT_VPP_PAPI_CMD) ||\
179                 (echo \"*******************************************************************\" &&\
180                  echo \"* Sanity check failed, cannot import vpp_papi\" &&\
181                  echo \"* to debug: \" &&\
182                  echo \"* 1. enter test shell:   make test-shell\" &&\
183                  echo \"* 2. execute debugger:   gdb python -ex 'run sanity_import_vpp_papi.py'\" &&\
184                  echo \"*******************************************************************\" &&\
185                  false)"
186         @bash -c "$(SANITY_RUN_VPP_CMD) ||\
187                 (echo \"*******************************************************************\" &&\
188                  echo \"* Sanity check failed, cannot run vpp\" &&\
189                  echo \"*******************************************************************\" &&\
190                  false)"
191
192 .PHONY: ext
193 ext:
194         make -C ext
195
196 test-dep: verify-test-dir $(PAPI_INSTALL_DONE)
197
198 test: verify-test-dir $(PAPI_INSTALL_DONE) ext sanity reset
199         $(call retest-func)
200
201 retest: verify-test-dir sanity reset
202         $(call retest-func)
203
204 shell: verify-test-dir $(PAPI_INSTALL_DONE)
205         @echo "source $(VENV_PATH)/bin/activate;\
206                 echo '***';\
207                 echo VPP_BUILD_DIR=$(VPP_BUILD_DIR);\
208                 echo VPP_BIN=$(VPP_BIN);\
209                 echo VPP_PLUGIN_PATH=$(VPP_PLUGIN_PATH);\
210                 echo VPP_TEST_PLUGIN_PATH=$(VPP_TEST_PLUGIN_PATH);\
211                 echo VPP_INSTALL_PATH=$(VPP_INSTALL_PATH);\
212                 echo EXTERN_TESTS=$(EXTERN_TESTS);\
213                 echo EXTERN_PLUGINS=$(EXTERN_PLUGINS);\
214                 echo EXTERN_COV_DIR=$(EXTERN_COV_DIR);\
215                 echo LD_LIBRARY_PATH=$(LD_LIBRARY_PATH);\
216                 echo '***';\
217                 exec </dev/tty" | bash -i
218
219 .PHONY: wipe doc
220
221 reset:
222         @rm -f /dev/shm/vpp-unittest-*
223         @rm -rf /tmp/vpp-unittest-*
224         @rm -rf $(FAILED_DIR)
225         @mkdir $(FAILED_DIR)
226
227 wipe: reset
228         @make -C ext clean
229         @rm -rf $(VENV_PATH)
230         @rm -f $(PAPI_INSTALL_FLAGS)
231
232 doc: verify-test-dir $(PIP_PATCH_DONE)
233         @virtualenv $(VENV_PATH) -p python3
234         @bash -c "source $(VENV_PATH)/bin/activate && python3 -m pip install sphinx sphinx-rtd-theme"
235         @bash -c "source $(VENV_PATH)/bin/activate && make -C doc WS_ROOT=$(WS_ROOT) BR=$(BR) html"
236
237 .PHONY: wipe-doc
238
239 wipe-doc:
240         @make -C doc wipe BR=$(BR)
241
242 cov: wipe-cov reset ext verify-test-dir $(PAPI_INSTALL_DONE)
243         @lcov --zerocounters --directory $(VPP_BUILD_DIR)
244         @test -z "$(EXTERN_COV_DIR)" || lcov --zerocounters --directory $(EXTERN_COV_DIR)
245         $(call retest-func)
246         @mkdir $(BUILD_COV_DIR)
247         @lcov --capture --directory $(VPP_BUILD_DIR) --output-file $(BUILD_COV_DIR)/coverage.info
248         @test -z "$(EXTERN_COV_DIR)" || lcov --capture --directory $(EXTERN_COV_DIR) --output-file $(BUILD_COV_DIR)/extern-coverage.info
249         @genhtml $(BUILD_COV_DIR)/coverage.info --output-directory $(BUILD_COV_DIR)/html
250         @test -z "$(EXTERN_COV_DIR)" || genhtml $(BUILD_COV_DIR)/extern-coverage.info --output-directory $(BUILD_COV_DIR)/extern-html
251         @echo
252         @echo "Build finished. Code coverage report is in $(BUILD_COV_DIR)/html/index.html"
253         @test -z "$(EXTERN_COV_DIR)" || echo "Code coverage report for out-of-tree objects is in $(BUILD_COV_DIR)/extern-html/index.html"
254
255 .PHONY: wipe-cov
256
257 wipe-cov: wipe
258         @rm -rf $(BUILD_COV_DIR)
259
260 .PHONY: papi-wipe
261
262 papi-wipe:
263         @rm -rf $(PAPI_INSTALL_DONE)
264
265 .PHONY: checkstyle
266 checkstyle: verify-test-dir
267         @virtualenv $(VENV_PATH) -p python3
268         @bash -c "source $(VENV_PATH)/bin/activate && python3 -m pip install pycodestyle"
269         @bash -c "source $(VENV_PATH)/bin/activate &&\
270                 pycodestyle --show-source --ignore=W504,E126,E241,E226,E305,E704,E741,E722 --exclude=$(WS_ROOT)/test/_*.py -v $(WS_ROOT)/test/*.py $(PLUGIN_SRC_DIR)/*/test/*.py ||\
271                 (echo \"*******************************************************************\" &&\
272                  echo \"* Test framework PEP8 compliance check FAILED \" &&\
273                  echo \"*******************************************************************\" &&\
274                  false)"
275         @echo "*******************************************************************"
276         @echo "* Test framework PEP8 compliance check passed"
277         @echo "*******************************************************************"
278
279 help:
280         @echo "Running tests:"
281         @echo ""
282         @echo " test                - build and run (basic) functional tests"
283         @echo " test-debug          - build and run (basic) functional tests (debug build)"
284         @echo " test-all            - build and run (all) functional tests"
285         @echo " test-all-debug      - build and run (all) functional tests (debug build)"
286         @echo " retest              - run functional tests"
287         @echo " retest-debug        - run functional tests (debug build)"
288         @echo " papi-wipe           - rebuild vpp_papi sources"
289         @echo " test-wipe           - wipe (temporary) files generated by unit tests"
290         @echo " test-shell          - enter shell with test environment"
291         @echo " test-shell-debug    - enter shell with test environment (debug build)"
292         @echo ""
293         @echo "Arguments controlling test runs:"
294         @echo " V=[0|1|2]            - set test verbosity level"
295         @echo "                        0=ERROR, 1=INFO, 2=DEBUG"
296         @echo " TEST_JOBS=[<n>|auto] - use <n> parallel processes for test execution or automatic discovery of maximum acceptable processes (default: 1)"
297         @echo " CACHE_OUTPUT=[0|1]   - cache VPP stdout/stderr and log as one block after test finishes (default: 1)"
298         @echo " FAILFAST=[0|1]       - fail fast if 1, complete all tests if 0"
299         @echo " TIMEOUT=<timeout>    - fail test suite if any single test takes longer than <timeout> (in seconds) to finish (default: 600)"
300         @echo " RETRIES=<n>          - retry failed tests <n> times"
301         @echo " DEBUG=<type>         - set VPP debugging kind"
302         @echo "    DEBUG=core        - detect coredump and load it in gdb on crash"
303         @echo "    DEBUG=gdb         - allow easy debugging by printing VPP PID"
304         @echo "                        and waiting for user input before running"
305         @echo "                        and tearing down a testcase"
306         @echo "    DEBUG=gdbserver   - run gdb inside a gdb server, otherwise"
307         @echo "                        same as above"
308         @echo " STEP=[yes|no]        - ease debugging by stepping through a testcase"
309         @echo " SANITY=[yes|no]      - perform sanity import of vpp-api/sanity vpp run before running tests (default: yes)"
310         @echo " EXTENDED_TESTS=[1|y] - run extended tests"
311         @echo " TEST=<filter>        - filter the set of tests:"
312         @echo "    by file-name      - only run tests from specified file, e.g. TEST=test_bfd selects all tests from test_bfd.py"
313         @echo "    by file-suffix    - same as file-name, but 'test_' is omitted e.g. TEST=bfd selects all tests from test_bfd.py"
314         @echo "    by wildcard       - wildcard filter is <file>.<class>.<test function>, each can be replaced by '*'"
315         @echo "                        e.g. TEST='test_bfd.*.*' is equivalent to above example of filter by file-name"
316         @echo "                             TEST='bfd.*.*' is equivalent to above example of filter by file-suffix"
317         @echo "                             TEST='bfd.BFDAPITestCase.*' selects all tests from test_bfd.py which are part of BFDAPITestCase class"
318         @echo "                             TEST='bfd.BFDAPITestCase.test_add_bfd' selects a single test named test_add_bfd from test_bfd.py/BFDAPITestCase"
319         @echo "                             TEST='*.*.test_add_bfd' selects all test functions named test_add_bfd from all files/classes"
320         @echo ""
321         @echo " VPP_ZOMBIE_NOCHECK=1 - skip checking for vpp (zombie) processes (CAUTION)"
322         @echo " COREDUMP_SIZE=<size> - pass <size> as unix { coredump-size <size> } argument to vpp"
323         @echo "                        e.g. COREDUMP_SIZE=4g"
324         @echo "                             COREDUMP_SIZE=unlimited"
325         @echo " COREDUMP_COMPRESS=1  - compress core files if not debugging them"
326         @echo " EXTERN_TESTS=<path>  - path to out-of-tree test_<name>.py files containing test cases"
327         @echo " EXTERN_PLUGINS=<path>- path to out-of-tree plugins to be loaded by vpp under test"
328         @echo " EXTERN_COV_DIR=<path>- path to out-of-tree prefix, where source, object and .gcda files can be found for coverage report"
329         @echo ""
330         @echo " PROFILE=1            - enable profiling of test framework via cProfile module"
331         @echo " PROFILE_SORT_BY=opt  - sort profiling report by opt - consult cProfile documentation for possible values (default: cumtime)"
332         @echo " PROFILE_OUTPUT=file  - output profiling info to file - use absolute path (default: stdout)"
333         @echo ""
334         @echo " TEST_DEBUG=1         - turn on debugging of the test framework itself (expert)"
335         @echo ""
336         @echo " SKIP_AARCH64=1       - skip tests that are failing on the ARM platorm in FD.io CI"
337         @echo ""
338         @echo " SOCKET=1             - Communicate with VPP over Unix domain socket instead of SHM"
339         @echo ""
340         @echo "Creating test documentation"
341         @echo " test-doc            - generate documentation for test framework"
342         @echo " test-wipe-doc       - wipe documentation for test framework"
343         @echo ""
344         @echo "Creating test code coverage report"
345         @echo " test-cov            - generate code coverage report for test framework"
346         @echo " test-wipe-cov       - wipe code coverage report for test framework"
347         @echo ""
348         @echo "Verifying code-style"
349         @echo " test-checkstyle     - check PEP8 compliance"
350         @echo ""