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