Manage test DAV servers as fixtures

Rather than require manually starting them before tests, manage them as
fixtures an initialise/destroy them as needed.
This commit is contained in:
Hugo Osvaldo Barrera 2021-06-12 14:32:11 +02:00
parent 8cd4a44d02
commit 299c699cb9
9 changed files with 92 additions and 22 deletions

View file

@ -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:

View file

@ -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 \

View file

@ -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

View file

@ -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 = {

View file

@ -1,3 +0,0 @@
#!/bin/sh
docker run -d -p 8002:80 whynothugo/vdirsyncer-devkit-baikal

View file

@ -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 = {

View file

@ -1,3 +0,0 @@
#!/bin/sh
docker run -d -p 8001:8001 whynothugo/vdirsyncer-devkit-radicale

View file

@ -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}

View file

@ -1,3 +0,0 @@
#!/bin/sh
docker run -d -p 8000:8000 whynothugo/vdirsyncer-devkit-xandikos