docker-pi-hole/test/test_pihole_scripts.py

55 lines
2.2 KiB
Python
Raw Normal View History

import pytest
@pytest.fixture(scope='module')
2017-01-08 06:36:31 +01:00
def start_cmd():
2016-11-06 22:12:11 +01:00
''' broken by default, required override '''
return None
2016-11-06 22:12:11 +01:00
@pytest.fixture
2017-01-08 06:36:31 +01:00
def RunningPiHole(DockerPersist, Slow, persist_webserver, persist_tag, start_cmd):
2016-11-06 22:12:11 +01:00
''' Override the RunningPiHole to run and check for success of a
pihole-FTL start based `pihole` script command
Individual tests all must override start_cmd'''
2017-05-05 05:37:26 +02:00
#print DockerPersist.run('ps -ef').stdout
2017-12-08 03:27:06 +01:00
assert DockerPersist.dig.run('ping -c 1 test_pihole').rc == 0
Slow(lambda: DockerPersist.run('pgrep pihole-FTL').rc == 0)
2017-05-05 05:37:26 +02:00
Slow(lambda: DockerPersist.run('pgrep {}'.format(persist_webserver)).rc == 0)
oldpid = DockerPersist.run('pidof pihole-FTL')
2017-01-08 06:36:31 +01:00
cmd = DockerPersist.run('pihole {}'.format(start_cmd))
Slow(lambda: DockerPersist.run('pgrep pihole-FTL').rc == 0)
newpid = DockerPersist.run('pidof pihole-FTL')
for pid in [oldpid, newpid]:
assert pid != ''
# ensure a new pid for pihole-FTL appeared due to service restart
assert oldpid != newpid
2016-11-06 22:12:11 +01:00
assert cmd.rc == 0
2017-01-08 06:36:31 +01:00
# Save out cmd result to check different stdout of start/enable/disable
2016-11-06 22:12:11 +01:00
DockerPersist.cmd = cmd
return DockerPersist
2017-12-08 03:27:06 +01:00
@pytest.mark.parametrize('start_cmd,hostname,expected_ip, expected_messages', [
('enable', 'pi.hole', '127.0.0.1', ['Blocking already enabled,','nothing to do']),
2017-12-08 03:27:06 +01:00
('disable', 'pi.hole', '127.0.0.1', ['Disabling blocking','Pi-hole Disabled']),
2016-11-06 22:12:11 +01:00
])
def test_pihole_enable_disable_command(RunningPiHole, Dig, persist_tag, start_cmd, hostname, expected_ip, expected_messages):
2017-01-08 06:36:31 +01:00
''' the start_cmd tests are all built into the RunningPiHole fixture in this file '''
2017-12-08 03:27:06 +01:00
dig_cmd = "dig +time=1 +noall +answer {} @test_pihole".format(hostname)
lookup = RunningPiHole.dig.run(dig_cmd)
assert lookup.rc == 0
lookup_ip = lookup.stdout.split()[4]
assert lookup_ip == expected_ip
2016-11-06 22:12:11 +01:00
2017-12-08 03:27:06 +01:00
for part_of_output in expected_messages:
assert part_of_output in RunningPiHole.cmd.stdout
@pytest.mark.parametrize('start_cmd,expected_message', [
('-up', 'Function not supported in Docker images')
])
def test_pihole_update_command(RunningPiHole, start_cmd, expected_message):
assert RunningPiHole.cmd.stdout.strip() == expected_message