2020-01-14 15:37:44 +01:00
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
2018-01-04 03:09:47 +01:00
docker containers ( esp . musl based OSs ) stripped down nature '''
2016-07-16 23:00:20 +02:00
2018-08-06 01:20:55 +02:00
# If the test runs /start.sh, do not let s6 run it too! Kill entrypoint to avoid race condition/duplicated execution
2019-02-08 02:51:15 +01:00
@pytest.mark.parametrize ( ' persist_entrypoint,persist_cmd,persist_args_env ' , [ ( ' --entrypoint=tail ' , ' -f /dev/null ' , ' ' ) ] )
def test_ServerIP_missing_is_not_required_anymore ( RunningPiHole ) :
2016-08-03 05:29:52 +02:00
''' 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
2018-08-06 01:20:55 +02:00
# If the test runs /start.sh, do not let s6 run it too! Kill entrypoint to avoid race condition/duplicated execution
@pytest.mark.parametrize ( ' entrypoint,cmd ' , [ ( ' --entrypoint=tail ' , ' -f /dev/null ' ) ] )
2017-05-19 07:36:53 +02:00
@pytest.mark.parametrize ( ' args,error_msg,expect_rc ' , [
( ' -e ServerIP= " 1.2.3.z " ' , " ServerIP Environment variable (1.2.3.z) doesn ' t appear to be a valid IPv4 address " , 1 ) ,
( ' -e ServerIP= " 1.2.3.4 " -e ServerIPv6= " 1234:1234:1234:ZZZZ " ' , " Environment variable (1234:1234:1234:ZZZZ) doesn ' t appear to be a valid IPv6 address " , 1 ) ,
2018-01-05 05:30:40 +01:00
( ' -e ServerIP= " 1.2.3.4 " -e ServerIPv6= " kernel " ' , " ERROR: You passed in IPv6 with a value of ' kernel ' " , 1 ) ,
2017-05-19 07:36:53 +02:00
] )
def test_ServerIP_invalid_IPs_triggers_exit_error ( Docker , error_msg , expect_rc ) :
''' When args to docker are empty start.sh exits saying ServerIP is required '''
start = Docker . run ( ' /start.sh ' )
assert start . rc == expect_rc
assert ' ERROR ' in start . stdout
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
2018-01-04 04:35:53 +01:00
def validate_curl ( http_rc , expected_http_code , page_contents ) :
if int ( http_rc . rc ) != 0 or int ( http_rc . stdout ) != expected_http_code :
2019-08-31 22:07:13 +02:00
print ( ' CURL return code: {} ' . format ( http_rc . rc ) )
print ( ' CURL stdout: {} ' . format ( http_rc . stdout ) )
print ( ' CURL stderr: {} ' . format ( http_rc . stderr ) )
print ( ' CURL file: \n {} \n ' . format ( page_contents . encode ( ' utf-8 ' ) ) )
2018-01-04 04:35:53 +01:00
2016-08-28 09:01:48 +02:00
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 ' ] )
2019-01-22 05:43:38 +01:00
def test_admin_requests_load_as_expected ( RunningPiHole , version , addr , url ) :
2019-02-03 18:45:40 +01:00
command = ' curl -L -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-12-11 17:28:05 +01:00
page_contents = RunningPiHole . run ( ' cat /tmp/curled_file ' ) . stdout
2018-01-04 04:35:53 +01:00
expected_http_code = 200
2017-12-11 17:28:05 +01:00
2018-01-04 04:35:53 +01:00
validate_curl ( http_rc , expected_http_code , page_contents )
2017-05-14 00:03:24 +02:00
assert http_rc . rc == 0
2017-12-11 17:28:05 +01:00
assert int ( http_rc . stdout ) == expected_http_code
2019-01-22 05:43:38 +01:00
for html_text in [ ' dns_queries_today ' , ' Content-Security-Policy ' ,
2019-02-03 18:45:40 +01:00
' scripts/pi-hole/js/footer.js ' ] :
# version removed, not showing up in footer of test env (fix me)
2017-12-11 17:28:05 +01:00
assert html_text in page_contents
2016-11-01 05:42:08 +01:00