datasette
datasette copied to clipboard
Unit tests for the Dockerfile
Working on the Dockerfile in #1249 made me wish for automated tests - to confirm that it boots up correctly, can run SpatiaLite and doesn't have weird bugs like the /db
page hanging.
These could run in CI too, but maybe only if the Dockerfile
is updated.
Part of the challenge here is only running if a Docker daemon is available. I think this pattern works, in tests/test_dockerfile.py
:
import httpx
import pathlib
import pytest
import subprocess
root = pathlib.Path(__file__).parent.parent
def docker_is_available():
try:
client = httpx.Client(
transport=httpx.HTTPTransport(uds="/var/run/docker.sock")
)
client.get("http://docker/info")
return True
except httpx.ConnectError:
return False
@pytest.fixture
def build_container():
assert (root / "Dockerfile").exists()
subprocess.check_call([
"docker", "build", str(root), "-t", "datasette-dockerfile-test"
])
@pytest.mark.skipif(not docker_is_available(),
reason="Docker is not available"
)
def test_dockerfile(build_container):
output = subprocess.check_output([
"docker", "run", "datasette-dockerfile-test", "datasette", "--get", "/_memory?sql=select+1&shape=_array"
])
assert False, "Implement better assertion here"
I'm skipping this for the moment because the new Dockerfile shape introduced in https://github.com/simonw/datasette/issues/1249#issuecomment-804404544 isn't compatible with this technique, since it installs Datasette from PyPI rather than directly from the repo.
Will need to change that if I want to do this unit tests thing.
test_dockerfile.py
I'm skipping this for the moment because the new Dockerfile shape introduced in #1249 (comment) isn't compatible with this technique, since it installs Datasette from PyPI rather than directly from the repo.
Will need to change that if I want to do this unit tests thing.
Hello, can't you copy in a later step directly in the output directory, e.g. COPY test_dockerfile.py /usr/lib/python*/.. or something like that ?