From 299c699cb9de2d99aec46c3a950be287d71f6866 Mon Sep 17 00:00:00 2001 From: Hugo Osvaldo Barrera Date: Sat, 12 Jun 2021 14:32:11 +0200 Subject: [PATCH] Manage test DAV servers as fixtures Rather than require manually starting them before tests, manage them as fixtures an initialise/destroy them as needed. --- .builds/archlinux.yaml | 1 - Makefile | 10 +-- tests/storage/conftest.py | 76 ++++++++++++++++++++++ tests/storage/servers/baikal/__init__.py | 2 +- tests/storage/servers/baikal/install.sh | 3 - tests/storage/servers/radicale/__init__.py | 8 ++- tests/storage/servers/radicale/install.sh | 3 - tests/storage/servers/xandikos/__init__.py | 8 ++- tests/storage/servers/xandikos/install.sh | 3 - 9 files changed, 92 insertions(+), 22 deletions(-) delete mode 100644 tests/storage/servers/baikal/install.sh delete mode 100644 tests/storage/servers/radicale/install.sh delete mode 100644 tests/storage/servers/xandikos/install.sh diff --git a/.builds/archlinux.yaml b/.builds/archlinux.yaml index e9b9740..9e50858 100644 --- a/.builds/archlinux.yaml +++ b/.builds/archlinux.yaml @@ -33,7 +33,6 @@ tasks: python setup.py build sudo pip install --no-index . sudo systemctl start docker - make -e install-servers - test: | cd vdirsyncer # Non-system python is used for packages: diff --git a/Makefile b/Makefile index 159e4b1..35c042d 100644 --- a/Makefile +++ b/Makefile @@ -62,15 +62,7 @@ test: all: $(error Take a look at https://vdirsyncer.pimutils.org/en/stable/tutorial.html#installation) -install-servers: - set -ex; \ - for server in $(DAV_SERVER); do \ - if [ -f $(TESTSERVER_BASE)$$server/install.sh ]; then \ - (cd $(TESTSERVER_BASE)$$server && sh install.sh); \ - fi \ - done - -install-test: install-servers install-dev +install-test: install-dev pip install -Ur test-requirements.txt set -xe && if [ "$$REQUIREMENTS" = "devel" ]; then \ pip install -U --force-reinstall \ diff --git a/tests/storage/conftest.py b/tests/storage/conftest.py index 96f0414..74b8d04 100644 --- a/tests/storage/conftest.py +++ b/tests/storage/conftest.py @@ -1,6 +1,82 @@ +import contextlib +import subprocess +import time import uuid import pytest +import requests + + +def wait_for_container(url): + """Wait for a container to initialise. + + Polls a URL every 100ms until the server responds. + """ + # give the server 5 seconds to settle + for _ in range(50): + print(_) + + try: + response = requests.get(url) + response.raise_for_status() + except requests.ConnectionError: + pass + else: + return + + time.sleep(0.1) + + pytest.exit( + "Server did not initialise in 5 seconds.\n" + "WARNING: There may be a stale docker container still running." + ) + + +@contextlib.contextmanager +def dockerised_server(name, container_port, exposed_port): + """Run a dockerised DAV server as a contenxt manager.""" + container_id = None + url = f"http://127.0.0.1:{exposed_port}/" + + try: + # Hint: This will block while the pull happends, and only return once + # the container has actually started. + output = subprocess.check_output( + [ + "docker", + "run", + "--detach", + "--publish", + f"{exposed_port}:{container_port}", + f"whynothugo/vdirsyncer-devkit-{name}", + ] + ) + + container_id = output.decode().strip() + wait_for_container(url) + + yield url + finally: + if container_id: + subprocess.check_output(["docker", "kill", container_id]) + + +@pytest.fixture(scope="session") +def baikal_server(): + with dockerised_server("baikal", "80", "8002"): + yield + + +@pytest.fixture(scope="session") +def radicale_server(): + with dockerised_server("radicale", "8001", "8001"): + yield + + +@pytest.fixture(scope="session") +def xandikos_server(): + with dockerised_server("xandikos", "8000", "8000"): + yield @pytest.fixture diff --git a/tests/storage/servers/baikal/__init__.py b/tests/storage/servers/baikal/__init__.py index 88c3e90..7547e07 100644 --- a/tests/storage/servers/baikal/__init__.py +++ b/tests/storage/servers/baikal/__init__.py @@ -3,7 +3,7 @@ import pytest class ServerMixin: @pytest.fixture - def get_storage_args(self, request, tmpdir, slow_create_collection): + def get_storage_args(self, request, tmpdir, slow_create_collection, baikal_server): def inner(collection="test"): base_url = "http://127.0.0.1:8002/" args = { diff --git a/tests/storage/servers/baikal/install.sh b/tests/storage/servers/baikal/install.sh deleted file mode 100644 index 44f3c95..0000000 --- a/tests/storage/servers/baikal/install.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -docker run -d -p 8002:80 whynothugo/vdirsyncer-devkit-baikal diff --git a/tests/storage/servers/radicale/__init__.py b/tests/storage/servers/radicale/__init__.py index b3b59bb..fb97ab2 100644 --- a/tests/storage/servers/radicale/__init__.py +++ b/tests/storage/servers/radicale/__init__.py @@ -3,7 +3,13 @@ import pytest class ServerMixin: @pytest.fixture - def get_storage_args(self, request, tmpdir, slow_create_collection): + def get_storage_args( + self, + request, + tmpdir, + slow_create_collection, + radicale_server, + ): def inner(collection="test"): url = "http://127.0.0.1:8001/" args = { diff --git a/tests/storage/servers/radicale/install.sh b/tests/storage/servers/radicale/install.sh deleted file mode 100644 index fdeaff9..0000000 --- a/tests/storage/servers/radicale/install.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -docker run -d -p 8001:8001 whynothugo/vdirsyncer-devkit-radicale diff --git a/tests/storage/servers/xandikos/__init__.py b/tests/storage/servers/xandikos/__init__.py index b0e6451..ece5de0 100644 --- a/tests/storage/servers/xandikos/__init__.py +++ b/tests/storage/servers/xandikos/__init__.py @@ -3,7 +3,13 @@ import pytest class ServerMixin: @pytest.fixture - def get_storage_args(self, request, tmpdir, slow_create_collection): + def get_storage_args( + self, + request, + tmpdir, + slow_create_collection, + xandikos_server, + ): def inner(collection="test"): url = "http://127.0.0.1:8000/" args = {"url": url} diff --git a/tests/storage/servers/xandikos/install.sh b/tests/storage/servers/xandikos/install.sh deleted file mode 100644 index aa7b089..0000000 --- a/tests/storage/servers/xandikos/install.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh - -docker run -d -p 8000:8000 whynothugo/vdirsyncer-devkit-xandikos