4e5dbe2
From 2eb842a0b42c1ce5214a9ffd5005391762386161 Mon Sep 17 00:00:00 2001
4e5dbe2
From: Adam Williamson <awilliam@redhat.com>
4e5dbe2
Date: Wed, 24 Jan 2024 17:25:41 -0800
4e5dbe2
Subject: [PATCH 18/21] Add ability to run the unit tests in Fedora and EL7
4e5dbe2
 containers
4e5dbe2
4e5dbe2
This adds a convenient way to run the unit tests in containers -
4e5dbe2
a "latest Fedora" container to check things work with the latest
4e5dbe2
shiny bits, and an EL 7 (CentOS 7 plus EPEL) container to check
4e5dbe2
things work with antique bits. This should make it possible to
4e5dbe2
conveniently set up CI with a github action. It also should
4e5dbe2
make it easy to run the tests on non-Fedora/RHEL-ish platforms.
4e5dbe2
4e5dbe2
`make container-unittests` will build both containers and run
4e5dbe2
the tests. `make container-clean` cleans up the containers and
4e5dbe2
images.
4e5dbe2
4e5dbe2
Signed-off-by: Adam Williamson <awilliam@redhat.com>
4e5dbe2
---
4e5dbe2
 Containerfile.tests.el7    | 16 ++++++++++++++++
4e5dbe2
 Containerfile.tests.fedora | 14 ++++++++++++++
4e5dbe2
 Makefile                   | 19 ++++++++++++++++++-
4e5dbe2
 README                     | 22 ++++++++++++++++++++--
4e5dbe2
 4 files changed, 68 insertions(+), 3 deletions(-)
4e5dbe2
 create mode 100644 Containerfile.tests.el7
4e5dbe2
 create mode 100644 Containerfile.tests.fedora
4e5dbe2
4e5dbe2
diff --git a/Containerfile.tests.el7 b/Containerfile.tests.el7
4e5dbe2
new file mode 100644
4e5dbe2
index 0000000..06a9b78
4e5dbe2
--- /dev/null
4e5dbe2
+++ b/Containerfile.tests.el7
4e5dbe2
@@ -0,0 +1,16 @@
4e5dbe2
+# this container definition is intended *only* for running the oz test suite, it is not
4e5dbe2
+# a general-purpose oz container definition!
4e5dbe2
+
4e5dbe2
+FROM quay.io/centos/centos:7
4e5dbe2
+RUN set -exo pipefail \
4e5dbe2
+    && yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
4e5dbe2
+    && yum install -y python-requests m2crypto libvirt-python python-lxml python-libguestfs pytest python-monotonic libvirt \
4e5dbe2
+    && yum clean all \
4e5dbe2
+    && rm -rf /var/cache/* /var/log/yum*
4e5dbe2
+
4e5dbe2
+COPY ./ /oz
4e5dbe2
+# the XML generation tests are inherently unreliable before Python 3.8,
4e5dbe2
+# as there was no consistent ordering of XML element attributes. See
4e5dbe2
+# https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring
4e5dbe2
+RUN printf "#!/bin/sh\n/usr/sbin/libvirtd -d\ncd /oz\npy.test -vv -k 'not test_xml_generation and not modify_libvirt_xml_for_serial' tests/" > /usr/local/bin/runtests.sh && chmod ugo+x /usr/local/bin/runtests.sh
4e5dbe2
+CMD /usr/local/bin/runtests.sh
4e5dbe2
diff --git a/Containerfile.tests.fedora b/Containerfile.tests.fedora
4e5dbe2
new file mode 100644
4e5dbe2
index 0000000..7dd3e41
4e5dbe2
--- /dev/null
4e5dbe2
+++ b/Containerfile.tests.fedora
4e5dbe2
@@ -0,0 +1,14 @@
4e5dbe2
+# this container definition is intended *only* for running the oz test suite, it is not
4e5dbe2
+# a general-purpose oz container definition!
4e5dbe2
+
4e5dbe2
+FROM quay.io/fedora/fedora:latest
4e5dbe2
+RUN set -exo pipefail \
4e5dbe2
+    && dnf install -y --setopt install_weak_deps=false --nodocs \
4e5dbe2
+    python3-requests python3-m2crypto python3-setuptools python3-libvirt python3-lxml python3-libguestfs python3-pytest python3-monotonic \
4e5dbe2
+    libvirt-daemon libvirt-daemon-kvm libvirt-daemon-qemu libvirt-daemon-config-network systemd \
4e5dbe2
+    && dnf clean all \
4e5dbe2
+    && rm -rf /var/cache/* /var/log/dnf*
4e5dbe2
+
4e5dbe2
+COPY ./ /oz
4e5dbe2
+RUN printf "#!/bin/sh\n/usr/sbin/libvirtd -d\ncd /oz\npy.test -vv tests/" > /usr/local/bin/runtests.sh && chmod ugo+x /usr/local/bin/runtests.sh
4e5dbe2
+CMD /usr/local/bin/runtests.sh
4e5dbe2
diff --git a/Makefile b/Makefile
4e5dbe2
index c8d94dd..74d3d99 100644
4e5dbe2
--- a/Makefile
4e5dbe2
+++ b/Makefile
4e5dbe2
@@ -46,6 +46,18 @@ unittests:
4e5dbe2
 
4e5dbe2
 tests: unittests
4e5dbe2
 
4e5dbe2
+container-unittests-fedora:
4e5dbe2
+	docker rm -f oz-tests-fedora
4e5dbe2
+	docker build -f Containerfile.tests.fedora -t oz-tests-fedora-image .
4e5dbe2
+	docker run --name oz-tests-fedora oz-tests-fedora-image
4e5dbe2
+
4e5dbe2
+container-unittests-el7:
4e5dbe2
+	docker rm -f oz-tests-el7
4e5dbe2
+	docker build -f Containerfile.tests.el7 -t oz-tests-el7-image .
4e5dbe2
+	docker run --name oz-tests-el7 oz-tests-el7-image
4e5dbe2
+
4e5dbe2
+container-unittests: container-unittests-fedora container-unittests-el7
4e5dbe2
+
4e5dbe2
 test-coverage:
4e5dbe2
 	python-coverage run --source oz /usr/bin/py.test --verbose tests
4e5dbe2
 	python-coverage html
4e5dbe2
@@ -57,7 +69,12 @@ pylint:
4e5dbe2
 flake8:
4e5dbe2
 	flake8 --ignore=E501 oz
4e5dbe2
 
4e5dbe2
+container-clean:
4e5dbe2
+	docker rm -f oz-tests-fedora
4e5dbe2
+	docker rm -f oz-tests-el7
4e5dbe2
+	docker image rm -f -i oz-tests-fedora-image oz-tests-el7-image
4e5dbe2
+
4e5dbe2
 clean:
4e5dbe2
 	rm -rf MANIFEST build dist usr *~ oz.spec *.pyc oz/*~ oz/*.pyc examples/*~ oz/auto/*~ man/*~ docs/*~ man/*.html $(VENV_DIR) tests/tdl/*~ tests/factory/*~ tests/results.xml htmlcov
4e5dbe2
 
4e5dbe2
-.PHONY: sdist oz.spec signed-tarball signed-rpm rpm srpm deb release man2html virtualenv unittests tests test-coverage pylint clean
4e5dbe2
+.PHONY: sdist oz.spec signed-tarball signed-rpm rpm srpm deb release man2html virtualenv unittests container-unittests-fedora container-unittests-el7 container-unittests tests test-coverage pylint clean container-clean
4e5dbe2
diff --git a/README b/README
4e5dbe2
index 9c78c59..5f2c983 100644
4e5dbe2
--- a/README
4e5dbe2
+++ b/README
4e5dbe2
@@ -10,9 +10,19 @@ Once you have the RPM installed, you will need to build up TDL files to feed
4e5dbe2
 as input into the building process.  Please see the examples directory to get
4e5dbe2
 an idea of what a TDL file looks like.
4e5dbe2
 
4e5dbe2
-Running the tests is also most easily done from a Fedora host. oz uses the
4e5dbe2
+You can run the oz unit tests in containers by installing make and docker
4e5dbe2
+(or podman-docker), then running:
4e5dbe2
+
4e5dbe2
+make container-unittests
4e5dbe2
+
4e5dbe2
+You can clean up the containers and images by running:
4e5dbe2
+
4e5dbe2
+make container-clean
4e5dbe2
+
4e5dbe2
+Otherwise, you can try running the tests directly on your host, but oz uses the
4e5dbe2
 guestfs Python module, which is not available from pypi, and needs a running
4e5dbe2
-libvirtd for most of the tests to run. Install all the test requirements:
4e5dbe2
+libvirtd for most of the tests to run. To install all the test requirements on
4e5dbe2
+Fedora:
4e5dbe2
 
4e5dbe2
 dnf install python3-requests python3-m2crypto python3-libvirt python3-lxml python3-libguestfs python3-pytest python3-monotonic
4e5dbe2
 
4e5dbe2
@@ -23,3 +33,11 @@ yum install python-requests m2crypto libvirt-python python-lxml python-libguestf
4e5dbe2
 then run the tests:
4e5dbe2
 
4e5dbe2
 py.test tests/
4e5dbe2
+
4e5dbe2
+You can try `make virtualenv` then `make unittests` to run the tests in a
4e5dbe2
+virtualenv if you like, but this still requires at least the libguestfs library
4e5dbe2
+installed on the host, and a running libvirtd. You may also want to install
4e5dbe2
+m2crypto and libvirt libraries on the host, as otherwise pip will have to
4e5dbe2
+compile them, and you'll need their build dependencies.
4e5dbe2
+
4e5dbe2
+You can use `make pylint` and `make flake8` to run lint checks.
4e5dbe2
-- 
4e5dbe2
2.43.0
4e5dbe2