mirror of
https://github.com/pi-hole/docker-pi-hole.git
synced 2025-12-24 20:35:42 +00:00
Major overhaul of the test suite
Signed-off-by: yubiuser <github@yubiuser.dev>
This commit is contained in:
@@ -1,131 +1,64 @@
|
||||
import os
|
||||
import pytest
|
||||
import subprocess
|
||||
import testinfra
|
||||
|
||||
local_host = testinfra.get_host("local://")
|
||||
check_output = local_host.check_output
|
||||
|
||||
TAIL_DEV_NULL = "tail -f /dev/null"
|
||||
import testinfra.backend.docker
|
||||
import os
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def run_and_stream_command_output():
|
||||
def run_and_stream_command_output_inner(command, verbose=False):
|
||||
print("Running", command)
|
||||
build_env = os.environ.copy()
|
||||
build_env["PIHOLE_DOCKER_TAG"] = version
|
||||
build_result = subprocess.Popen(
|
||||
command.split(),
|
||||
env=build_env,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
bufsize=1,
|
||||
universal_newlines=True,
|
||||
# Monkeypatch sh to bash, if they ever support non hard code /bin/sh this can go away
|
||||
# https://github.com/pytest-dev/pytest-testinfra/blob/master/testinfra/backend/docker.py
|
||||
def run_bash(self, command, *args, **kwargs):
|
||||
cmd = self.get_command(command, *args)
|
||||
if self.user is not None:
|
||||
out = self.run_local(
|
||||
"docker exec -u %s %s /bin/bash -c %s", self.user, self.name, cmd
|
||||
)
|
||||
if verbose:
|
||||
while build_result.poll() is None:
|
||||
for line in build_result.stdout:
|
||||
print(line, end="")
|
||||
build_result.wait()
|
||||
if build_result.returncode != 0:
|
||||
print(f" [i] Error running: {command}")
|
||||
print(build_result.stderr)
|
||||
|
||||
return run_and_stream_command_output_inner
|
||||
else:
|
||||
out = self.run_local("docker exec %s /bin/bash -c %s", self.name, cmd)
|
||||
out.command = self.encode(cmd)
|
||||
return out
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def args_env():
|
||||
return '-e TZ="Europe/London" -e FTLCONF_dns_upstreams="8.8.8.8"'
|
||||
testinfra.backend.docker.DockerBackend.run = run_bash
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def args(args_env):
|
||||
return "{}".format(args_env)
|
||||
# scope='session' uses the same container for all the tests;
|
||||
# scope='function' uses a new container per test function.
|
||||
@pytest.fixture(scope="function")
|
||||
def docker(request):
|
||||
# Get platform from environment variable, default to None if not set
|
||||
platform = os.environ.get("CIPLATFORM")
|
||||
|
||||
# build the docker run command with args
|
||||
cmd = ["docker", "run", "-d", "-t"]
|
||||
|
||||
@pytest.fixture()
|
||||
def test_args():
|
||||
"""test override fixture to provide arguments separate from our core args"""
|
||||
return ""
|
||||
# Only add platform flag if CIPLATFORM is set
|
||||
if platform:
|
||||
cmd.extend(["--platform", platform])
|
||||
|
||||
# Get env vars from parameterization
|
||||
env_vars = getattr(request, "param", [])
|
||||
if isinstance(env_vars, str):
|
||||
env_vars = [env_vars]
|
||||
|
||||
def docker_generic(request, _test_args, _args, _image, _cmd, _entrypoint):
|
||||
# assert 'docker' in check_output('id'), "Are you in the docker group?"
|
||||
# Always appended PYTEST arg to tell pihole we're testing
|
||||
if "pihole" in _image and "PYTEST=1" not in _args:
|
||||
_args = "{} -e PYTEST=1".format(_args)
|
||||
docker_run = "docker run -d -t {args} {test_args} {entry} {image} {cmd}".format(
|
||||
args=_args, test_args=_test_args, entry=_entrypoint, image=_image, cmd=_cmd
|
||||
)
|
||||
# Print a human runable version of the container run command for faster debugging
|
||||
print(docker_run.replace("-d -t", "--rm -it").replace(TAIL_DEV_NULL, "bash"))
|
||||
docker_id = check_output(docker_run)
|
||||
# add parameterized environment variables
|
||||
for env_var in env_vars:
|
||||
cmd.extend(["-e", env_var])
|
||||
|
||||
def teardown():
|
||||
check_output("docker logs {}".format(docker_id))
|
||||
check_output("docker rm -f {}".format(docker_id))
|
||||
# ensure PYTEST=1 is set
|
||||
if not any("PYTEST=1" in arg for arg in cmd):
|
||||
cmd.extend(["-e", "PYTEST=1"])
|
||||
|
||||
request.addfinalizer(teardown)
|
||||
docker_container = testinfra.backend.get_backend(
|
||||
"docker://" + docker_id, sudo=False
|
||||
)
|
||||
docker_container.id = docker_id
|
||||
# add default TZ if not already set
|
||||
if not any("TZ=" in arg for arg in cmd):
|
||||
cmd.extend(["-e", 'TZ="Europe/London"'])
|
||||
|
||||
return docker_container
|
||||
# add the image name
|
||||
cmd.append("pihole:CI_container")
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def docker(request, test_args, args, image, cmd, entrypoint):
|
||||
"""One-off Docker container run"""
|
||||
return docker_generic(request, test_args, args, image, cmd, entrypoint)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def entrypoint():
|
||||
return ""
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def version():
|
||||
return os.environ.get("GIT_TAG", None)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def tag(version):
|
||||
return "{}".format(version)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def image(tag):
|
||||
image = "pihole"
|
||||
return "{}:{}".format(image, tag)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def cmd():
|
||||
return TAIL_DEV_NULL
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def slow():
|
||||
"""
|
||||
Run a slow check, check if the state is correct for `timeout` seconds.
|
||||
"""
|
||||
import time
|
||||
|
||||
def _slow(check, timeout=20):
|
||||
timeout_at = time.time() + timeout
|
||||
while True:
|
||||
try:
|
||||
assert check()
|
||||
except AssertionError as e:
|
||||
if time.time() < timeout_at:
|
||||
time.sleep(1)
|
||||
else:
|
||||
raise e
|
||||
else:
|
||||
return
|
||||
|
||||
return _slow
|
||||
# run a container
|
||||
docker_id = subprocess.check_output(cmd).decode().strip()
|
||||
# return a testinfra connection to the container
|
||||
yield testinfra.get_host("docker://" + docker_id)
|
||||
# at the end of the test suite, destroy the container
|
||||
subprocess.check_call(["docker", "rm", "-f", docker_id])
|
||||
|
||||
Reference in New Issue
Block a user