Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the tests #4427

Merged
merged 3 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
name: Checkout repository
uses: actions/checkout@v2
-
name: Set up Python 3.7
name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.7
python-version: 3.8
-
name: Install dependencies
run: pip install -r test/requirements.txt
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ __pycache__
*.egg-info
.idea/
*.iml
.vscode/
1 change: 1 addition & 0 deletions test/_centos_7.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM centos:7
RUN yum install -y git

ENV GITDIR /etc/.pihole
ENV SCRIPTDIR /opt/pihole
Expand Down
1 change: 1 addition & 0 deletions test/_centos_8.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM centos:8
RUN yum install -y git

ENV GITDIR /etc/.pihole
ENV SCRIPTDIR /opt/pihole
Expand Down
1 change: 1 addition & 0 deletions test/_fedora_33.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM fedora:33
RUN dnf install -y git

ENV GITDIR /etc/.pihole
ENV SCRIPTDIR /opt/pihole
Expand Down
1 change: 1 addition & 0 deletions test/_fedora_34.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM fedora:34
RUN dnf install -y git

ENV GITDIR /etc/.pihole
ENV SCRIPTDIR /opt/pihole
Expand Down
99 changes: 28 additions & 71 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,52 @@
import pytest
import testinfra
import testinfra.backend.docker
import subprocess
from textwrap import dedent

check_output = testinfra.get_backend(
"local://"
).get_module("Command").check_output

SETUPVARS = {
'PIHOLE_INTERFACE': 'eth99',
'PIHOLE_DNS_1': '4.2.2.1',
'PIHOLE_DNS_2': '4.2.2.2'
}

IMAGE = 'pytest_pihole:test_container'

tick_box = "[\x1b[1;32m\u2713\x1b[0m]"
cross_box = "[\x1b[1;31m\u2717\x1b[0m]"
info_box = "[i]"


@pytest.fixture
def Pihole(Docker):
'''
used to contain some script stubbing, now pretty much an alias.
Also provides bash as the default run function shell
'''
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)
else:
out = self.run_local(
"docker exec %s /bin/bash -c %s", self.name, cmd)
out.command = self.encode(cmd)
return out

funcType = type(Docker.run)
Docker.run = funcType(run_bash, Docker)
return Docker
# 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
)
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 Docker(request, args, image, cmd):
'''
combine our fixtures into a docker run command and setup finalizer to
cleanup
'''
assert 'docker' in check_output('id'), "Are you in the docker group?"
docker_run = "docker run {} {} {}".format(args, image, cmd)
docker_id = check_output(docker_run)

def teardown():
check_output("docker rm -f %s", docker_id)
request.addfinalizer(teardown)

docker_container = testinfra.get_backend("docker://" + docker_id)
docker_container.id = docker_id
return docker_container
testinfra.backend.docker.DockerBackend.run = run_bash


@pytest.fixture
def args(request):
'''
-t became required when tput began being used
'''
return '-t -d --cap-add=ALL'
def host():
# run a container
docker_id = subprocess.check_output(
['docker', 'run', '-t', '-d', '--cap-add=ALL', IMAGE]).decode().strip()

# return a testinfra connection to the container
docker_host = testinfra.get_host("docker://" + docker_id)

@pytest.fixture(params=[
'test_container'
])
def tag(request):
'''
consumed by image to make the test matrix
'''
return request.param


@pytest.fixture()
def image(request, tag):
'''
built by test_000_build_containers.py
'''
return 'pytest_pihole:{}'.format(tag)


@pytest.fixture()
def cmd(request):
'''
default to doing nothing by tailing null, but don't exit
'''
return 'tail -f /dev/null'
yield docker_host
# at the end of the test suite, destroy the container
subprocess.check_call(['docker', 'rm', '-f', docker_id])


# Helper functions
Expand Down Expand Up @@ -129,7 +85,7 @@ def mock_command_passthrough(script, args, container):

Example use-case: mocking `git pull` but still allowing `git clone` to work as intended
'''
orig_script_path = check_output('which {}'.format(script))
orig_script_path = container.check_output('command -v {}'.format(script))
full_script_path = '/usr/local/bin/{}'.format(script)
mock_script = dedent(r'''\
#!/bin/bash -e
Expand Down Expand Up @@ -209,6 +165,7 @@ def mock_command_2(script, args, container):
content=mock_script,
scriptlog=script))


def run_script(Pihole, script):
result = Pihole.run(script)
assert result.rc == 0
Expand Down
12 changes: 6 additions & 6 deletions test/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
docker-compose==1.23.2
pytest==4.3.0
pytest-xdist==1.26.1
pytest-cov==2.6.1
testinfra==1.19.0
tox==3.7.0
docker-compose
pytest
pytest-xdist
pytest-cov
pytest-testinfra
tox