+ifndef BR
+ $(error BR is not set)
+endif
+ifndef TEST_DIR
+ $(error TEST_DIR is not set)
+endif
+
+export TEST_BR = $(BR)/build-test
+export TEST_DOC_BR = $(TEST_BR)/doc
+export BUILD_TEST_SRC = $(TEST_BR)/src
+FAILED_DIR=/tmp/vpp-failed-unittests/
+PLUGIN_TEST_DIRS=$(shell find $(PLUGIN_SRC_DIR) -type d -name test -exec echo -n " -d {}" \;)
+CORE_TEST_DIRS=$(shell find $(WS_ROOT)/src -not \( -path $(INTERN_PLUGIN_SRC_DIR) -prune \) -type d -name test -exec echo -n " -d {}" \;)
+VPP_TEST_DIRS=$(shell ls -d $(TEST_DIR)$(PLUGIN_TEST_DIRS)$(CORE_TEST_DIRS) $(EXTERN_TESTS))
+VPP_TEST_SRC=$(shell for dir in $(VPP_TEST_DIRS) ; do ls $$dir/*.py; done)
+
+.PHONY: verify-no-running-vpp
+
+ifdef VPP_ZOMBIE_NOCHECK
+VPP_PIDS=
+else
+VPP_PIDS=$(shell pgrep -d, -x vpp_main)
+endif
+
+ifeq ($(DEBUG),gdb)
+FORCE_FOREGROUND=1
+else ifeq ($(DEBUG),gdbserver)
+FORCE_FOREGROUND=1
+else ifeq ($(DEBUG),gdb-all)
+FORCE_FOREGROUND=1
+else ifeq ($(DEBUG),gdbserver-all)
+FORCE_FOREGROUND=1
+else ifeq ($(DEBUG),core)
+FORCE_FOREGROUND=1
+else ifeq ($(STEP),yes)
+FORCE_FOREGROUND=1
+else ifeq ($(STEP),y)
+FORCE_FOREGROUND=1
+else ifeq ($(STEP),1)
+FORCE_FOREGROUND=1
+else
+FORCE_FOREGROUND=0
+endif
+
+ifdef PROFILE_OUTPUT
+PROFILE_OUTPUT_OPTS=-o $(PROFILE_OUTPUT)
+endif
+
+ifndef PROFILE_SORT_BY
+PROFILE_SORT_BY=cumtime
+endif
+
+ifeq ($(PROFILE),1)
+PYTHON_PROFILE_OPTS=-m cProfile $(PROFILE_OUTPUT_OPTS) -s $(PROFILE_SORT_BY)
+FORCE_FOREGROUND=1
+endif
+
+verify-no-running-vpp:
+ @if [ "$(VPP_PIDS)" != "" ]; then \
+ echo; \
+ echo "*** Existing vpp processes detected (PID(s): $(VPP_PIDS)). Running tests under these conditions is not supported. ***"; \
+ echo; \
+ ps -fp $(VPP_PIDS);\
+ echo; \
+ false; \
+ fi
+
+UNITTEST_EXTRA_OPTS=
+UNITTEST_FAILFAST_OPTS=
+
+ifeq ($(FAILFAST),1)
+UNITTEST_EXTRA_OPTS=-f
+endif
+
+ifneq ($(EXTERN_TESTS),)
+UNITTEST_EXTRA_OPTS=$(UNITTEST_FAILFAST_OPTS) -d $(EXTERN_TESTS)
+endif
+
+VENV_PATH=$(TEST_BR)/venv
+
+ifeq ($(TEST_DEBUG),1)
+VENV_RUN_DIR:=$(VENV_PATH)/run-debug
+else
+VENV_RUN_DIR:=$(VENV_PATH)/run
+endif
+
+ifeq ($(PYTHON),)
+PYTHON_INTERP=python3
+else
+PYTHON_INTERP=$(PYTHON)
+endif
+
+PYTHON_VERSION=$(shell $(PYTHON_INTERP) -c 'import sys; print(sys.version_info.major)')
+PIP_VERSION=19.1.1
+PIP_TOOLS_VERSION=3.8.0 # Keep in sync with requirements.txt
+PYTHON_DEPENDS=requirements-$(PYTHON_VERSION).txt
+SCAPY_SOURCE=$(shell find $(VENV_PATH)/lib/python* -name site-packages)
+BUILD_COV_DIR=$(TEST_BR)/coverage
+
+PIP_TOOLS_INSTALL_DONE=$(VENV_RUN_DIR)/pip-tools-install-$(PYTHON_VERSION).done
+PIP_INSTALL_DONE=$(VENV_RUN_DIR)/pip-install-$(PYTHON_VERSION).done
+PIP_PATCH_DONE=$(VENV_RUN_DIR)/pip-patch-$(PYTHON_VERSION).done
+PAPI_INSTALL_DONE=$(VENV_RUN_DIR)/papi-install-$(PYTHON_VERSION).done
+PAPI_PYTHON_SRC_DIR=$(WS_ROOT)/src/vpp-api/python
+PAPI_WIPE_DIST=$(WS_ROOT)/src/vpp-api/vapi/__pycache__ \
+ $(PAPI_PYTHON_SRC_DIR)/build \
+ $(PAPI_PYTHON_SRC_DIR)/vpp_papi.egg-info \
+ $(PAPI_PYTHON_SRC_DIR)/vpp_papi/__pycache__
+
+PAPI_INSTALL_FLAGS=$(PIP_INSTALL_DONE) $(PIP_PATCH_DONE) $(PAPI_INSTALL_DONE)
+
+$(PIP_TOOLS_INSTALL_DONE):
+ @rm -rf $(VENV_PATH)
+ @mkdir -p $(VENV_RUN_DIR)
+ @virtualenv $(VENV_PATH) -p $(PYTHON_INTERP)
+ # pip version pinning
+ @bash -c "source $(VENV_PATH)/bin/activate && \
+ $(PYTHON_INTERP) -m pip install pip===$(PIP_VERSION)"
+ @bash -c "source $(VENV_PATH)/bin/activate && \
+ $(PYTHON_INTERP) -m pip install pip-tools===$(PIP_TOOLS_VERSION)"
+ @touch $@
+
+$(PYTHON_DEPENDS): $(PIP_TOOLS_INSTALL_DONE) requirements.txt
+ @bash -c "source $(VENV_PATH)/bin/activate && \
+ CUSTOM_COMPILE_COMMAND='make test-refresh-deps (or update requirements.txt)' \
+ $(PYTHON_INTERP) -m piptools compile -q --generate-hashes requirements.txt --output-file $@"
+
+$(PIP_INSTALL_DONE): $(PYTHON_DEPENDS)
+ @bash -c "source $(VENV_PATH)/bin/activate && \
+ $(PYTHON_INTERP) -m piptools sync $(PYTHON_DEPENDS)"
+ @touch $@
+
+$(PIP_PATCH_DONE): $(PIP_INSTALL_DONE)
+ @echo --- patching ---
+ @sleep 1 # Ensure python recompiles patched *.py files -> *.pyc
+ for f in $(CURDIR)/patches/scapy-2.4.3/*.patch ; do \
+ echo Applying patch: $$(basename $$f) ; \
+ patch --forward -p1 -d $(SCAPY_SOURCE) < $$f ; \
+ retCode=$$?; \
+ [ $$retCode -gt 0 ] && exit $$retCode; \
+ done; \
+ touch $@