datasette icon indicating copy to clipboard operation
datasette copied to clipboard

Unit tests for the Dockerfile

Open simonw opened this issue 3 years ago • 3 comments

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.

simonw avatar Mar 23 '21 01:03 simonw

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"

simonw avatar Mar 27 '21 04:03 simonw

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.

simonw avatar Mar 27 '21 04:03 simonw

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 ?

xmichele avatar Jul 29 '22 10:07 xmichele