Persistent dig container running dns tests

* Sidekick to pi-hole links and queries DNS
* Lookup the default pi.hole against what was fed in as ServerIP
* Lookup a couple outside popular static DNS servers hostnames too
* a little pre-optimized to persist the dig container since docker start adds half a second (no virtually nothing for digs)
This commit is contained in:
diginc 2016-09-07 23:07:38 -05:00
parent e8cb52b23a
commit 1e6eda27eb
2 changed files with 37 additions and 8 deletions

View File

@ -9,15 +9,18 @@ check_output = testinfra.get_backend(
def DockerGeneric(request, args, image, cmd):
assert 'docker' in check_output('id'), "Are you in the docker group?"
docker_run = "docker run -d -e PYTEST=\"True\" {} {} {}".format(args, image, cmd)
if 'diginc/pi-hole' in image:
args += " -e PYTEST=\"True\""
docker_run = "docker run -d {} {} {}".format(args, image, cmd)
docker_id = check_output(docker_run)
def teardown():
check_output("docker stop %s", docker_id)
check_output("docker rm %s", docker_id)
check_output("docker rm -f %s", docker_id)
request.addfinalizer(teardown)
return testinfra.get_backend("docker://" + docker_id)
docker_container = testinfra.get_backend("docker://" + docker_id)
docker_container.id = docker_id
return docker_container
@pytest.fixture
def Docker(request, args, image, cmd):
@ -25,9 +28,12 @@ def Docker(request, args, image, cmd):
return DockerGeneric(request, args, image, cmd)
@pytest.fixture(scope='session')
def DockerPersist(request, persist_args, persist_image, persist_cmd):
def DockerPersist(request, persist_args, persist_image, persist_cmd, Dig):
''' Persistent Docker container for multiple tests '''
return DockerGeneric(request, persist_args, persist_image, persist_cmd)
persistent_container = DockerGeneric(request, persist_args, persist_image, persist_cmd)
''' attach a dig conatiner for lookups '''
persistent_container.dig = Dig(persistent_container.id)
return persistent_container
@pytest.fixture()
def args(request):
@ -90,3 +96,16 @@ def Slow():
else:
return
return slow
@pytest.fixture(scope='session')
def Dig(request):
''' separate container to link to pi-hole and perform lookups '''
''' a docker pull is faster than running an install of dnsutils '''
def dig(docker_id):
args = '--link {}:pihole'.format(docker_id)
image = 'azukiapp/dig'
cmd = 'tail -f /dev/null'
dig_container = DockerGeneric(request, args, image, cmd)
return dig_container
return dig

View File

@ -24,10 +24,20 @@ def test_ServerIP_missing_triggers_start_error(Docker):
@pytest.fixture
def RunningPiHole(DockerPersist, Slow, persist_webserver):
''' Persist a docker and provide some parameterized data for re-use '''
Slow(lambda: DockerPersist.run( 'pgrep {}'.format(persist_webserver) ).rc == 0)
''' Persist a working docker-pi-hole to help speed up subsequent tests '''
Slow(lambda: DockerPersist.run('pgrep {}'.format(persist_webserver) ).rc == 0)
return DockerPersist
@pytest.mark.parametrize('hostname,expected_ip', [
('pi.hole', '192.168.100.2'),
('google-public-dns-a.google.com', '8.8.8.8'),
('b.resolvers.Level3.net', '4.2.2.2')
])
def test_dns_responses(RunningPiHole, hostname, expected_ip):
dig_cmd = "dig +noall +answer {} @pihole | awk '{{ print $5 }}'".format(hostname)
lookup = RunningPiHole.dig.run(dig_cmd).stdout.rstrip('\n')
assert lookup == expected_ip
def test_indecies_are_present(RunningPiHole):
File = RunningPiHole.get_module('File')
File('/var/www/html/pihole/index.html').exists