2016-07-16 23:00:20 +02:00
|
|
|
import pytest
|
2016-08-28 07:43:21 +02:00
|
|
|
import time
|
2016-08-03 05:29:52 +02:00
|
|
|
''' conftest.py provides the defaults through fixtures '''
|
2016-08-28 07:43:21 +02:00
|
|
|
''' Note, testinfra builtins don't seem fully compatible with
|
2016-08-03 05:29:52 +02:00
|
|
|
docker containers (esp. alpine) stripped down nature '''
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-11-06 20:40:24 +01:00
|
|
|
def test_pihole_default_run_command(Docker, tag):
|
2016-08-03 05:29:52 +02:00
|
|
|
expected_proc = '/sbin/tini -- /start.sh'
|
|
|
|
pgrep = 'pgrep -f "{}" | wc -l || echo 0'.format(expected_proc)
|
|
|
|
find_proc = Docker.run(pgrep).stdout
|
|
|
|
if int(find_proc) < 1:
|
|
|
|
print Docker.run('ps -ef')
|
|
|
|
print "{} : {}".format(pgrep, find_proc)
|
|
|
|
assert False, '{}: Couldn\'t find proc {}'.format(tag, expected_proc)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('args', [ '' ])
|
|
|
|
@pytest.mark.parametrize('cmd', [ 'tail -f /dev/null' ])
|
|
|
|
def test_ServerIP_missing_triggers_start_error(Docker):
|
|
|
|
''' When args to docker are empty start.sh exits saying ServerIP is required '''
|
|
|
|
start = Docker.run('/start.sh')
|
2016-07-16 23:00:20 +02:00
|
|
|
error_msg = "ERROR: To function correctly you must pass an environment variables of 'ServerIP' into the docker container"
|
|
|
|
assert start.rc == 1
|
|
|
|
assert error_msg in start.stdout
|
|
|
|
|
2016-09-08 06:07:38 +02:00
|
|
|
@pytest.mark.parametrize('hostname,expected_ip', [
|
2017-05-14 00:03:24 +02:00
|
|
|
('pi.hole', '127.0.0.1'),
|
2016-09-08 06:07:38 +02:00
|
|
|
('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):
|
2016-10-21 05:36:55 +02:00
|
|
|
dig_cmd = "dig +time=1 +noall +answer {} @test_pihole | awk '{{ print $5 }}'".format(hostname)
|
2016-09-08 06:07:38 +02:00
|
|
|
lookup = RunningPiHole.dig.run(dig_cmd).stdout.rstrip('\n')
|
|
|
|
assert lookup == expected_ip
|
|
|
|
|
2016-08-28 07:43:21 +02:00
|
|
|
def test_indecies_are_present(RunningPiHole):
|
|
|
|
File = RunningPiHole.get_module('File')
|
|
|
|
File('/var/www/html/pihole/index.html').exists
|
|
|
|
File('/var/www/html/pihole/index.js').exists
|
|
|
|
|
2017-05-14 00:03:24 +02:00
|
|
|
@pytest.mark.parametrize('addr', [ 'testblock.pi-hole.local' ])
|
2016-08-28 08:11:55 +02:00
|
|
|
@pytest.mark.parametrize('url', [ '/', '/index.html', '/any.html' ] )
|
2017-05-14 00:03:24 +02:00
|
|
|
def test_html_index_requests_load_as_expected(RunningPiHole, Slow, addr, url):
|
|
|
|
command = 'curl -s -o /tmp/curled_file -w "%{{http_code}}" http://{}{}'.format(addr, url)
|
2016-08-28 07:43:21 +02:00
|
|
|
http_rc = RunningPiHole.run(command)
|
2017-05-14 00:03:24 +02:00
|
|
|
assert http_rc.rc == 0
|
2016-08-28 07:43:21 +02:00
|
|
|
assert int(http_rc.stdout) == 200
|
2017-05-14 00:03:24 +02:00
|
|
|
page_contents = RunningPiHole.run('cat /tmp/curled_file ').stdout
|
|
|
|
assert 'blocked' in page_contents
|
2016-08-28 07:43:21 +02:00
|
|
|
|
2017-05-14 00:03:24 +02:00
|
|
|
@pytest.mark.parametrize('addr', [ 'testblock.pi-hole.local' ])
|
2016-08-28 08:11:55 +02:00
|
|
|
@pytest.mark.parametrize('url', [ '/index.js', '/any.js'] )
|
2017-05-14 00:03:24 +02:00
|
|
|
def test_javascript_requests_load_as_expected(RunningPiHole, addr, url):
|
|
|
|
command = 'curl -s -o /tmp/curled_file -w "%{{http_code}}" http://{}{}'.format(addr, url)
|
2016-08-28 07:43:21 +02:00
|
|
|
http_rc = RunningPiHole.run(command)
|
2017-05-14 00:03:24 +02:00
|
|
|
assert http_rc.rc == 0
|
2016-08-28 07:43:21 +02:00
|
|
|
assert int(http_rc.stdout) == 200
|
2017-05-14 00:03:24 +02:00
|
|
|
assert RunningPiHole.run('md5sum /tmp/curled_file /var/www/html/pihole/index.js').rc == 0
|
2016-08-28 09:01:48 +02:00
|
|
|
|
2017-01-12 03:12:56 +01:00
|
|
|
# IPv6 checks aren't passing CORS, removed :(
|
2017-05-14 00:03:24 +02:00
|
|
|
@pytest.mark.parametrize('addr', [ 'localhost' ] )
|
2016-08-28 09:01:48 +02:00
|
|
|
@pytest.mark.parametrize('url', [ '/admin/', '/admin/index.php' ] )
|
2017-05-14 00:03:24 +02:00
|
|
|
def test_admin_requests_load_as_expected(RunningPiHole, addr, url):
|
|
|
|
command = 'curl -s -o /tmp/curled_file -w "%{{http_code}}" http://{}{}'.format(addr, url)
|
2016-08-28 09:01:48 +02:00
|
|
|
http_rc = RunningPiHole.run(command)
|
2017-05-14 00:03:24 +02:00
|
|
|
assert http_rc.rc == 0
|
2016-08-28 09:01:48 +02:00
|
|
|
assert int(http_rc.stdout) == 200
|
|
|
|
assert RunningPiHole.run('wc -l /tmp/curled_file ') > 10
|
|
|
|
assert RunningPiHole.run('grep -q "Content-Security-Policy" /tmp/curled_file ').rc == 0
|
2017-01-09 06:43:12 +01:00
|
|
|
assert RunningPiHole.run('grep -q "scripts/pi-hole/js/footer.js" /tmp/curled_file ').rc == 0
|
2016-11-01 05:42:08 +01:00
|
|
|
|