Bootstrap Python venv for tests
authorCleber Rosa <crosa@redhat.com>
Thu, 18 Oct 2018 15:31:32 +0000 (11:31 -0400)
committerEduardo Habkost <ehabkost@redhat.com>
Wed, 31 Oct 2018 00:13:54 +0000 (21:13 -0300)
A number of QEMU tests are written in Python, and may benefit
from an untainted Python venv.

By using make rules, tests that depend on specific Python libs
can set that rule as a requirement, along with rules that require
the presence or installation of specific libraries.

The tests/requirements.txt is supposed to contain the Python
requirements that should be added to the venv created by check-venv.

Signed-off-by: Cleber Rosa <crosa@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
Acked-by: Wainer dos Santos Moschetta <wainersm@redhat.com>
Reviewed-by: Caio Carrara <ccarrara@redhat.com>
Message-Id: <20181018153134.8493-2-crosa@redhat.com>
Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
tests/Makefile.include
tests/requirements.txt [new file with mode: 0644]

index f77a4951098f00edfb51dd9fe7214844d0328edb..eabc1da2f32144d96b8bc8ae044d3c40fda81d50 100644 (file)
@@ -12,6 +12,7 @@ check-help:
        @echo " $(MAKE) check-block          Run block tests"
        @echo " $(MAKE) check-tcg            Run TCG tests"
        @echo " $(MAKE) check-report.html    Generates an HTML test report"
+       @echo " $(MAKE) check-venv           Creates a Python venv for tests"
        @echo " $(MAKE) check-clean          Clean the tests"
        @echo
        @echo "Please note that HTML reports do not regenerate if the unit tests"
@@ -899,6 +900,30 @@ check-decodetree:
           ./check.sh "$(PYTHON)" "$(SRC_PATH)/scripts/decodetree.py", \
           TEST, decodetree.py)
 
+# Python venv for running tests
+
+.PHONY: check-venv
+
+TESTS_VENV_DIR=$(BUILD_DIR)/tests/venv
+TESTS_VENV_REQ=$(SRC_PATH)/tests/requirements.txt
+
+$(shell $(PYTHON) -c 'import sys; assert sys.version_info >= (3,0)' >/dev/null 2>&1)
+ifeq ($(.SHELLSTATUS),0)
+$(TESTS_VENV_DIR): $(TESTS_VENV_REQ)
+       $(call quiet-command, \
+            $(PYTHON) -m venv --system-site-packages $@, \
+            VENV, $@)
+       $(call quiet-command, \
+            $(TESTS_VENV_DIR)/bin/python -m pip -q install -r $(TESTS_VENV_REQ), \
+            PIP, $(TESTS_VENV_REQ))
+       $(call quiet-command, touch $@)
+else
+$(TESTS_VENV_DIR):
+       $(error "venv directory for tests requires Python 3")
+endif
+
+check-venv: $(TESTS_VENV_DIR)
+
 # Consolidated targets
 
 .PHONY: check-qapi-schema check-qtest check-unit check check-clean
@@ -912,6 +937,7 @@ check-clean:
        rm -rf $(check-unit-y) tests/*.o $(QEMU_IOTESTS_HELPERS-y)
        rm -rf $(sort $(foreach target,$(SYSEMU_TARGET_LIST), $(check-qtest-$(target)-y)) $(check-qtest-generic-y))
        rm -f tests/test-qapi-gen-timestamp
+       rm -rf $(TESTS_VENV_DIR)
 
 clean: check-clean
 
diff --git a/tests/requirements.txt b/tests/requirements.txt
new file mode 100644 (file)
index 0000000..d39f9d1
--- /dev/null
@@ -0,0 +1,3 @@
+# Add Python module requirements, one per line, to be installed
+# in the tests/venv Python virtual environment. For more info,
+# refer to: https://pip.pypa.io/en/stable/user_guide/#id1