Skip to content

Commit 87f3a59

Browse files
author
Hugo Osvaldo Barrera
authored
Merge pull request pimutils#897 from pimutils/optimise-test-servers
Run test servers as fixtures
2 parents b0f08e0 + d95a826 commit 87f3a59

File tree

15 files changed

+123
-74
lines changed

15 files changed

+123
-74
lines changed

.builds/archlinux.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,16 @@ environment:
2424
BUILD: test
2525
CI: true
2626
CODECOV_TOKEN: b834a3c5-28fa-4808-9bdb-182210069c79
27+
DAV_SERVER: radicale xandikos
2728
REQUIREMENTS: release
2829
# TODO: ETESYNC_TESTS
2930
tasks:
3031
- setup: |
32+
sudo systemctl start docker
3133
cd vdirsyncer
3234
python setup.py build
3335
sudo pip install --no-index .
34-
sudo systemctl start docker
35-
DAV_SERVER="radicale xandikos" make -e install-servers
3636
- test: |
3737
cd vdirsyncer
38-
# Non-system python is used for packages:
39-
export PATH=$PATH:~/.local/bin/
4038
make -e ci-test
41-
DAV_SERVER=radicale make -e ci-test-storage
42-
DAV_SERVER=xandikos make -e ci-test-storage
39+
make -e ci-test-storage

.builds/tests-minimal.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,17 @@ environment:
1414
BUILD: test
1515
CI: true
1616
CODECOV_TOKEN: b834a3c5-28fa-4808-9bdb-182210069c79
17+
DAV_SERVER: radicale xandikos
1718
REQUIREMENTS: minimal
1819
# TODO: ETESYNC_TESTS
1920
tasks:
2021
- setup: |
21-
cd vdirsyncer
2222
sudo systemctl start docker
23-
DAV_SERVER="radicale xandikos" make -e install-test
23+
cd vdirsyncer
24+
make -e install-dev
2425
- test: |
2526
cd vdirsyncer
2627
# Non-system python is used for packages:
2728
export PATH=$PATH:~/.local/bin/
2829
make -e ci-test
29-
DAV_SERVER=radicale make -e ci-test-storage
30-
DAV_SERVER=xandikos make -e ci-test-storage
30+
make -e ci-test-storage

.builds/tests-release.yaml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,20 @@ environment:
1111
BUILD: test
1212
CI: true
1313
CODECOV_TOKEN: b834a3c5-28fa-4808-9bdb-182210069c79
14+
DAV_SERVER: baikal radicale xandikos
1415
REQUIREMENTS: release
1516
# TODO: ETESYNC_TESTS
1617
tasks:
1718
- setup: |
18-
cd vdirsyncer
1919
sudo systemctl start docker
20-
DAV_SERVER="radicale xandikos baikal" make -e install-test
21-
make -e install-style
20+
cd vdirsyncer
21+
make -e install-dev -e install-docs
2222
- test: |
2323
cd vdirsyncer
2424
# Non-system python is used for packages:
2525
export PATH=$PATH:~/.local/bin/
2626
make -e ci-test
27-
DAV_SERVER=baikal make -e ci-test-storage
28-
DAV_SERVER=radicale make -e ci-test-storage
29-
DAV_SERVER=xandikos make -e ci-test-storage
27+
make -e ci-test-storage
3028
- style: |
3129
cd vdirsyncer
3230
# Non-system python is used for packages:

Makefile

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ ifeq ($(ETESYNC_TESTS), true)
3636
endif
3737

3838
PYTEST = py.test $(PYTEST_ARGS)
39-
40-
export TESTSERVER_BASE := ./tests/storage/servers/
4139
CODECOV_PATH = /tmp/codecov.sh
4240

41+
all:
42+
$(error Take a look at https://vdirsyncer.pimutils.org/en/stable/tutorial.html#installation)
43+
4344
ci-test:
4445
curl -s https://codecov.io/bash > $(CODECOV_PATH)
4546
$(PYTEST) tests/unit/
@@ -50,35 +51,14 @@ ci-test:
5051

5152
ci-test-storage:
5253
curl -s https://codecov.io/bash > $(CODECOV_PATH)
53-
$(PYTEST) tests/storage/
54-
bash $(CODECOV_PATH) -c -F storage
55-
56-
test:
57-
$(PYTEST)
58-
59-
all:
60-
$(error Take a look at https://vdirsyncer.pimutils.org/en/stable/tutorial.html#installation)
61-
62-
install-servers:
6354
set -ex; \
6455
for server in $(DAV_SERVER); do \
65-
if [ -f $(TESTSERVER_BASE)$$server/install.sh ]; then \
66-
(cd $(TESTSERVER_BASE)$$server && sh install.sh); \
67-
fi \
56+
DAV_SERVER=$$server $(PYTEST) --cov-append tests/storage; \
6857
done
58+
bash $(CODECOV_PATH) -c -F storage
6959

70-
install-test: install-servers install-dev
71-
pip install -Ur test-requirements.txt
72-
set -xe && if [ "$$REQUIREMENTS" = "devel" ]; then \
73-
pip install -U --force-reinstall \
74-
git+https://github.com/DRMacIver/hypothesis \
75-
git+https://github.com/kennethreitz/requests \
76-
git+https://github.com/pytest-dev/pytest; \
77-
fi
78-
[ -z "$(TEST_EXTRA_PACKAGES)" ] || pip install $(TEST_EXTRA_PACKAGES)
79-
80-
install-style: install-docs install-dev
81-
pip install pre-commit
60+
test:
61+
$(PYTEST)
8262

8363
style:
8464
pre-commit run --all
@@ -91,8 +71,6 @@ install-docs:
9171

9272
docs:
9373
cd docs && make html
94-
95-
linkcheck:
9674
sphinx-build -W -b linkcheck ./docs/ ./docs/_build/linkcheck/
9775

9876
release-deb:
@@ -105,12 +83,10 @@ release-deb:
10583
install-dev:
10684
pip install -U pip setuptools wheel
10785
pip install -e .
86+
pip install -Ur test-requirements.txt $(TEST_EXTRA_PACKAGES)
87+
pip install pre-commit
10888
[ "$(ETESYNC_TESTS)" = "false" ] || pip install -Ue .[etesync]
109-
set -xe && if [ "$(REQUIREMENTS)" = "devel" ]; then \
110-
pip install -U --force-reinstall \
111-
git+https://github.com/mitsuhiko/click \
112-
git+https://github.com/kennethreitz/requests; \
113-
elif [ "$(REQUIREMENTS)" = "minimal" ]; then \
89+
set -xe && if [ "$(REQUIREMENTS)" = "minimal" ]; then \
11490
pip install -U --force-reinstall $$(python setup.py --quiet minimal_requirements); \
11591
fi
11692

docs/contributing.rst

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ virtualenv_ and run this inside of it::
8787
# Install git commit hook for some extra linting and checking
8888
pre-commit install
8989

90-
# install test dependencies
91-
make install-test
90+
# Install development dependencies
91+
make install-dev
9292

9393
Then you can run::
9494

@@ -100,14 +100,13 @@ The ``Makefile`` has a lot of options that allow you to control which tests are
100100
run, and which servers are tested. Take a look at its code where they are all
101101
initialized and documented.
102102

103-
For example, to test xandikos, first run the server itself::
104-
105-
docker run -p 8000:8000 whynothugo/vdirsyncer-devkit-xandikos
106-
107-
Then run the tests specifying this ``DAV_SERVER``, run::
103+
To tests against a specific DAV server, use ``DAV_SERVER``::
108104

109105
make DAV_SERVER=xandikos test
110106

107+
The server will be initialised in a docker container and terminated at the end
108+
of the test suite.
109+
111110
If you have any questions, feel free to open issues about it.
112111

113112
Structure of the testsuite

docs/packaging.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ repository or PyPI package. Trying to e.g. run ``pytest`` directly will
4141
require a lot of environment variables to be set (for configuration) and you
4242
probably don't want to deal with that.
4343

44-
You can install the testing dependencies with::
44+
You can install the all development dependencies with::
4545

46-
make install-test
46+
make install-dev
4747

4848
You probably don't want this since it will use pip to download the
4949
dependencies. Alternatively you can find the testing dependencies in

docs/when.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ program chosen:
5050

5151
* Such a setup doesn't work at all with smartphones. Vdirsyncer, on the other
5252
hand, synchronizes with CardDAV/CalDAV servers, which can be accessed with
53-
e.g. DAVDroid_ or the apps by dmfs_.
53+
e.g. DAVx⁵_ or the apps by dmfs_.
5454

55-
.. _DAVDroid: http://davdroid.bitfire.at/
55+
.. _DAVx⁵: https://www.davx5.com/
5656
.. _dmfs: https://dmfs.org/

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
requirements = [
1313
# https://github.com/mitsuhiko/click/issues/200
14-
"click>=5.0",
14+
"click>=5.0,<9.0",
1515
"click-log>=0.3.0, <0.4.0",
1616
# https://github.com/pimutils/vdirsyncer/issues/478
1717
"click-threading>=0.5",

tests/storage/conftest.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,82 @@
1+
import contextlib
2+
import subprocess
3+
import time
14
import uuid
25

36
import pytest
7+
import requests
8+
9+
10+
def wait_for_container(url):
11+
"""Wait for a container to initialise.
12+
13+
Polls a URL every 100ms until the server responds.
14+
"""
15+
# give the server 5 seconds to settle
16+
for _ in range(50):
17+
print(_)
18+
19+
try:
20+
response = requests.get(url)
21+
response.raise_for_status()
22+
except requests.ConnectionError:
23+
pass
24+
else:
25+
return
26+
27+
time.sleep(0.1)
28+
29+
pytest.exit(
30+
"Server did not initialise in 5 seconds.\n"
31+
"WARNING: There may be a stale docker container still running."
32+
)
33+
34+
35+
@contextlib.contextmanager
36+
def dockerised_server(name, container_port, exposed_port):
37+
"""Run a dockerised DAV server as a contenxt manager."""
38+
container_id = None
39+
url = f"http://127.0.0.1:{exposed_port}/"
40+
41+
try:
42+
# Hint: This will block while the pull happends, and only return once
43+
# the container has actually started.
44+
output = subprocess.check_output(
45+
[
46+
"docker",
47+
"run",
48+
"--detach",
49+
"--publish",
50+
f"{exposed_port}:{container_port}",
51+
f"whynothugo/vdirsyncer-devkit-{name}",
52+
]
53+
)
54+
55+
container_id = output.decode().strip()
56+
wait_for_container(url)
57+
58+
yield url
59+
finally:
60+
if container_id:
61+
subprocess.check_output(["docker", "kill", container_id])
62+
63+
64+
@pytest.fixture(scope="session")
65+
def baikal_server():
66+
with dockerised_server("baikal", "80", "8002"):
67+
yield
68+
69+
70+
@pytest.fixture(scope="session")
71+
def radicale_server():
72+
with dockerised_server("radicale", "8001", "8001"):
73+
yield
74+
75+
76+
@pytest.fixture(scope="session")
77+
def xandikos_server():
78+
with dockerised_server("xandikos", "8000", "8000"):
79+
yield
480

581

682
@pytest.fixture

tests/storage/servers/baikal/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class ServerMixin:
55
@pytest.fixture
6-
def get_storage_args(self, request, tmpdir, slow_create_collection):
6+
def get_storage_args(self, request, tmpdir, slow_create_collection, baikal_server):
77
def inner(collection="test"):
88
base_url = "http://127.0.0.1:8002/"
99
args = {

0 commit comments

Comments
 (0)