mirror of
https://github.com/samsonjs/vdirsyncer.git
synced 2026-04-26 14:47:44 +00:00
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:
parent
8cd4a44d02
commit
299c699cb9
9 changed files with 92 additions and 22 deletions
|
|
@ -33,7 +33,6 @@ tasks:
|
||||||
python setup.py build
|
python setup.py build
|
||||||
sudo pip install --no-index .
|
sudo pip install --no-index .
|
||||||
sudo systemctl start docker
|
sudo systemctl start docker
|
||||||
make -e install-servers
|
|
||||||
- test: |
|
- test: |
|
||||||
cd vdirsyncer
|
cd vdirsyncer
|
||||||
# Non-system python is used for packages:
|
# Non-system python is used for packages:
|
||||||
|
|
|
||||||
10
Makefile
10
Makefile
|
|
@ -62,15 +62,7 @@ test:
|
||||||
all:
|
all:
|
||||||
$(error Take a look at https://vdirsyncer.pimutils.org/en/stable/tutorial.html#installation)
|
$(error Take a look at https://vdirsyncer.pimutils.org/en/stable/tutorial.html#installation)
|
||||||
|
|
||||||
install-servers:
|
install-test: install-dev
|
||||||
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
|
|
||||||
pip install -Ur test-requirements.txt
|
pip install -Ur test-requirements.txt
|
||||||
set -xe && if [ "$$REQUIREMENTS" = "devel" ]; then \
|
set -xe && if [ "$$REQUIREMENTS" = "devel" ]; then \
|
||||||
pip install -U --force-reinstall \
|
pip install -U --force-reinstall \
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,82 @@
|
||||||
|
import contextlib
|
||||||
|
import subprocess
|
||||||
|
import time
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
import pytest
|
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
|
@pytest.fixture
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import pytest
|
||||||
|
|
||||||
class ServerMixin:
|
class ServerMixin:
|
||||||
@pytest.fixture
|
@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"):
|
def inner(collection="test"):
|
||||||
base_url = "http://127.0.0.1:8002/"
|
base_url = "http://127.0.0.1:8002/"
|
||||||
args = {
|
args = {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
docker run -d -p 8002:80 whynothugo/vdirsyncer-devkit-baikal
|
|
||||||
|
|
@ -3,7 +3,13 @@ import pytest
|
||||||
|
|
||||||
class ServerMixin:
|
class ServerMixin:
|
||||||
@pytest.fixture
|
@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"):
|
def inner(collection="test"):
|
||||||
url = "http://127.0.0.1:8001/"
|
url = "http://127.0.0.1:8001/"
|
||||||
args = {
|
args = {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
docker run -d -p 8001:8001 whynothugo/vdirsyncer-devkit-radicale
|
|
||||||
|
|
@ -3,7 +3,13 @@ import pytest
|
||||||
|
|
||||||
class ServerMixin:
|
class ServerMixin:
|
||||||
@pytest.fixture
|
@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"):
|
def inner(collection="test"):
|
||||||
url = "http://127.0.0.1:8000/"
|
url = "http://127.0.0.1:8000/"
|
||||||
args = {"url": url}
|
args = {"url": url}
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
docker run -d -p 8000:8000 whynothugo/vdirsyncer-devkit-xandikos
|
|
||||||
Loading…
Reference in a new issue