1
0
mirror of https://github.com/pi-hole/docker-pi-hole.git synced 2024-06-26 00:59:07 +02:00

Fix tests

This commit is contained in:
Matthew Kennedy 2017-01-07 22:36:31 -07:00
parent 0d87eaeb58
commit 0477dea085

View File

@ -1,22 +1,22 @@
import pytest import pytest
@pytest.fixture @pytest.fixture
def restart_cmd(): def start_cmd():
''' broken by default, required override ''' ''' broken by default, required override '''
return None return None
RESTART_DNS_STDOUT = { START_DNS_STDOUT = {
'alpine': '', 'alpine': '',
'debian': 'Restarting DNS forwarder and DHCP server: dnsmasq.\n' 'debian': 'Starting DNS forwarder and DHCP server: dnsmasq.\n'
} }
@pytest.fixture @pytest.fixture
def RunningPiHole(DockerPersist, Slow, persist_webserver, persist_tag, restart_cmd): def RunningPiHole(DockerPersist, Slow, persist_webserver, persist_tag, start_cmd):
''' Override the RunningPiHole to run and check for success of a ''' Override the RunningPiHole to run and check for success of a
dnsmasq restart based `pihole` script command ''' dnsmasq start based `pihole` script command '''
Slow(lambda: DockerPersist.run('pgrep {}'.format(persist_webserver) ).rc == 0) Slow(lambda: DockerPersist.run('pgrep {}'.format(persist_webserver) ).rc == 0)
Slow(lambda: DockerPersist.run('pgrep dnsmasq').rc == 0) Slow(lambda: DockerPersist.run('pgrep dnsmasq').rc == 0)
oldpid = DockerPersist.run('pidof dnsmasq') oldpid = DockerPersist.run('pidof dnsmasq')
cmd = DockerPersist.run('pihole {}'.format(restart_cmd)) cmd = DockerPersist.run('pihole {}'.format(start_cmd))
Slow(lambda: DockerPersist.run('pgrep dnsmasq').rc == 0) Slow(lambda: DockerPersist.run('pgrep dnsmasq').rc == 0)
newpid = DockerPersist.run('pidof dnsmasq') newpid = DockerPersist.run('pidof dnsmasq')
for pid in [oldpid, newpid]: for pid in [oldpid, newpid]:
@ -24,24 +24,24 @@ def RunningPiHole(DockerPersist, Slow, persist_webserver, persist_tag, restart_c
# ensure a new pid for dnsmasq appeared # ensure a new pid for dnsmasq appeared
assert oldpid != newpid assert oldpid != newpid
assert cmd.rc == 0 assert cmd.rc == 0
# Save out cmd result to check different stdout of restart/enable/disable # Save out cmd result to check different stdout of start/enable/disable
DockerPersist.cmd = cmd DockerPersist.cmd = cmd
return DockerPersist return DockerPersist
@pytest.mark.parametrize('restart_cmd', ['restart_cmd']) @pytest.mark.parametrize('start_cmd', ['start_cmd'])
def test_pihole_restart_cmd(RunningPiHole, restart_cmd, persist_tag): def test_pihole_start_cmd(RunningPiHole, start_cmd, persist_tag):
''' the restart_cmd tests are all built into the RunningPiHole fixture in this file ''' ''' the start_cmd tests are all built into the RunningPiHole fixture in this file '''
assert RunningPiHole.cmd.stdout == RESTART_DNS_STDOUT[persist_tag] assert RunningPiHole.cmd.stdout == START_DNS_STDOUT[persist_tag]
@pytest.mark.parametrize('restart_cmd,hostname,expected_ip', [ @pytest.mark.parametrize('start_cmd,hostname,expected_ip', [
('enable', 'pi.hole', '192.168.100.2'), ('enable', 'pi.hole', '192.168.100.2'),
('disable', 'pi.hole', '192.168.100.2'), ('disable', 'pi.hole', '192.168.100.2'),
]) ])
def test_pihole_restart_cmd(RunningPiHole, Dig, persist_tag, restart_cmd, hostname, expected_ip): def test_pihole_start_cmd(RunningPiHole, Dig, persist_tag, start_cmd, hostname, expected_ip):
''' the restart_cmd tests are all built into the RunningPiHole fixture in this file ''' ''' the start_cmd tests are all built into the RunningPiHole fixture in this file '''
dig_cmd = "dig +time=1 +noall +answer {} @test_pihole | awk '{{ print $5 }}'".format(hostname) dig_cmd = "dig +time=1 +noall +answer {} @test_pihole | awk '{{ print $5 }}'".format(hostname)
lookup = RunningPiHole.dig.run(dig_cmd).stdout.rstrip('\n') lookup = RunningPiHole.dig.run(dig_cmd).stdout.rstrip('\n')
assert lookup == expected_ip assert lookup == expected_ip
stdout = "::: Blocking has been {}d!\n{}".format(restart_cmd, RESTART_DNS_STDOUT[persist_tag]) stdout = "::: Blocking has been {}d!\n{}".format(start_cmd, START_DNS_STDOUT[persist_tag])
assert RunningPiHole.cmd.stdout == stdout assert RunningPiHole.cmd.stdout == stdout