Merge pull request #1111 from pi-hole/address-sonarr-complaints
Address Sonar complaints
This commit is contained in:
commit
1c369e3dea
|
@ -8,6 +8,7 @@ local_host = testinfra.get_host('local://')
|
|||
check_output = local_host.check_output
|
||||
|
||||
DEBIAN_VERSION = os.environ.get('DEBIAN_VERSION', 'bullseye')
|
||||
TAIL_DEV_NULL='tail -f /dev/null'
|
||||
|
||||
@pytest.fixture()
|
||||
def run_and_stream_command_output():
|
||||
|
@ -44,7 +45,7 @@ def test_args():
|
|||
''' test override fixture to provide arguments separate from our core args '''
|
||||
return ''
|
||||
|
||||
def DockerGeneric(request, _test_args, _args, _image, _cmd, _entrypoint):
|
||||
def docker_generic(request, _test_args, _args, _image, _cmd, _entrypoint):
|
||||
#assert 'docker' in check_output('id'), "Are you in the docker group?"
|
||||
# Always appended PYTEST arg to tell pihole we're testing
|
||||
if 'pihole' in _image and 'PYTEST=1' not in _args:
|
||||
|
@ -52,7 +53,7 @@ def DockerGeneric(request, _test_args, _args, _image, _cmd, _entrypoint):
|
|||
docker_run = 'docker run -d -t {args} {test_args} {entry} {image} {cmd}'\
|
||||
.format(args=_args, test_args=_test_args, entry=_entrypoint, image=_image, cmd=_cmd)
|
||||
# Print a human runable version of the container run command for faster debugging
|
||||
print(docker_run.replace('-d -t', '--rm -it').replace('tail -f /dev/null', 'bash'))
|
||||
print(docker_run.replace('-d -t', '--rm -it').replace(TAIL_DEV_NULL, 'bash'))
|
||||
docker_id = check_output(docker_run)
|
||||
|
||||
def teardown():
|
||||
|
@ -67,17 +68,17 @@ def DockerGeneric(request, _test_args, _args, _image, _cmd, _entrypoint):
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def Docker(request, test_args, args, image, cmd, entrypoint):
|
||||
def docker(request, test_args, args, image, cmd, entrypoint):
|
||||
''' One-off Docker container run '''
|
||||
return DockerGeneric(request, test_args, args, image, cmd, entrypoint)
|
||||
return docker_generic(request, test_args, args, image, cmd, entrypoint)
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def DockerPersist(request, persist_test_args, persist_args, persist_image, persist_cmd, persist_entrypoint, Dig):
|
||||
def docker_persist(request, persist_test_args, persist_args, persist_image, persist_cmd, persist_entrypoint, dig):
|
||||
''' Persistent Docker container for multiple tests, instead of stopping container after one test '''
|
||||
''' Uses DUP'd module scoped fixtures because smaller scoped fixtures won't mix with module scope '''
|
||||
persistent_container = DockerGeneric(request, persist_test_args, persist_args, persist_image, persist_cmd, persist_entrypoint)
|
||||
persistent_container = docker_generic(request, persist_test_args, persist_args, persist_image, persist_cmd, persist_entrypoint)
|
||||
''' attach a dig container for lookups '''
|
||||
persistent_container.dig = Dig(persistent_container.id)
|
||||
persistent_container.dig = dig(persistent_container.id)
|
||||
return persistent_container
|
||||
|
||||
@pytest.fixture
|
||||
|
@ -112,7 +113,7 @@ def image(tag):
|
|||
|
||||
@pytest.fixture()
|
||||
def cmd():
|
||||
return 'tail -f /dev/null'
|
||||
return TAIL_DEV_NULL
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def persist_arch():
|
||||
|
@ -164,19 +165,19 @@ def persist_image(persist_tag):
|
|||
|
||||
@pytest.fixture(scope='module')
|
||||
def persist_cmd():
|
||||
return 'tail -f /dev/null'
|
||||
return TAIL_DEV_NULL
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def persist_entrypoint():
|
||||
return ''
|
||||
|
||||
@pytest.fixture
|
||||
def Slow():
|
||||
def slow():
|
||||
"""
|
||||
Run a slow check, check if the state is correct for `timeout` seconds.
|
||||
"""
|
||||
import time
|
||||
def slow(check, timeout=20):
|
||||
def _slow(check, timeout=20):
|
||||
timeout_at = time.time() + timeout
|
||||
while True:
|
||||
try:
|
||||
|
@ -188,26 +189,27 @@ def Slow():
|
|||
raise e
|
||||
else:
|
||||
return
|
||||
return slow
|
||||
return _slow
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def Dig():
|
||||
def dig():
|
||||
''' 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):
|
||||
def _dig(docker_id):
|
||||
args = '--link {}:test_pihole'.format(docker_id)
|
||||
image = 'azukiapp/dig'
|
||||
cmd = 'tail -f /dev/null'
|
||||
dig_container = DockerGeneric(request, '', args, image, cmd, '')
|
||||
cmd = TAIL_DEV_NULL
|
||||
dig_container = docker_generic(request, '', args, image, cmd, '')
|
||||
return dig_container
|
||||
return dig
|
||||
return _dig
|
||||
|
||||
'''
|
||||
Persistent Docker container for testing service post start.sh
|
||||
'''
|
||||
@pytest.fixture
|
||||
def RunningPiHole(DockerPersist, Slow, persist_webserver):
|
||||
def running_pihole(docker_persist, slow, persist_webserver):
|
||||
''' Persist a fully started docker-pi-hole to help speed up subsequent tests '''
|
||||
Slow(lambda: DockerPersist.run('pgrep pihole-FTL').rc == 0)
|
||||
Slow(lambda: DockerPersist.run('pgrep lighttpd').rc == 0)
|
||||
return DockerPersist
|
||||
slow(lambda: docker_persist.run('pgrep pihole-FTL').rc == 0)
|
||||
slow(lambda: docker_persist.run('pgrep lighttpd').rc == 0)
|
||||
return docker_persist
|
||||
|
||||
|
|
|
@ -3,6 +3,16 @@ import os
|
|||
import pytest
|
||||
import re
|
||||
|
||||
SETUPVARS_LOC='/etc/pihole/setupVars.conf'
|
||||
DNSMASQ_CONFIG_LOC = '/etc/dnsmasq.d/01-pihole.conf'
|
||||
EVAL_SETUP_DNSMASQ='. /bash_functions.sh ; eval `grep "^setup_dnsmasq " /start.sh`'
|
||||
EVAL_SETUP_WEB_PASSWORD='. /bash_functions.sh ; eval `grep setup_web_password /start.sh`'
|
||||
|
||||
def _cat(file):
|
||||
return 'cat {}'.format(file)
|
||||
|
||||
def _grep(string, file):
|
||||
return 'grep -q \'{}\' {}'.format(string,file)
|
||||
|
||||
@pytest.mark.parametrize('test_args,expected_ipv6,expected_stdout', [
|
||||
('', True, 'IPv4 and IPv6'),
|
||||
|
@ -10,32 +20,32 @@ import re
|
|||
('-e "IPv6=False"', False, 'IPv4'),
|
||||
('-e "IPv6=foobar"', False, 'IPv4'),
|
||||
])
|
||||
def test_IPv6_not_True_removes_ipv6(Docker, Slow, test_args, expected_ipv6, expected_stdout):
|
||||
def test_ipv6_not_true_removes_ipv6(docker, slow, test_args, expected_ipv6, expected_stdout):
|
||||
''' When a user overrides IPv6=True they only get IPv4 listening webservers '''
|
||||
IPV6_LINE = 'use-ipv6.pl'
|
||||
WEB_CONFIG = '/etc/lighttpd/lighttpd.conf'
|
||||
|
||||
function = Docker.run('. /bash_functions.sh ; setup_ipv4_ipv6')
|
||||
function = docker.run('. /bash_functions.sh ; setup_ipv4_ipv6')
|
||||
assert "Using {}".format(expected_stdout) in function.stdout
|
||||
if expected_stdout == 'IPv4':
|
||||
assert 'IPv6' not in function.stdout
|
||||
# On overlay2(?) docker sometimes writes to disk are slow enough to break some tests...
|
||||
expected_ipv6_check = lambda: (\
|
||||
IPV6_LINE in Docker.run('grep \'use-ipv6.pl\' {}'.format(WEB_CONFIG)).stdout
|
||||
IPV6_LINE in docker.run('grep \'use-ipv6.pl\' {}'.format(WEB_CONFIG)).stdout
|
||||
) == expected_ipv6
|
||||
Slow(expected_ipv6_check)
|
||||
slow(expected_ipv6_check)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('test_args', ['-e "WEB_PORT=999"'])
|
||||
def test_overrides_default_WEB_PORT(Docker, Slow, test_args):
|
||||
def test_overrides_default_web_port(docker, slow, test_args):
|
||||
''' When a --net=host user sets WEB_PORT to avoid synology's 80 default IPv4 and or IPv6 ports are updated'''
|
||||
CONFIG_LINE = r'server.port\s*=\s*999'
|
||||
WEB_CONFIG = '/etc/lighttpd/lighttpd.conf'
|
||||
|
||||
function = Docker.run('. /bash_functions.sh ; eval `grep setup_web_port /start.sh`')
|
||||
function = docker.run('. /bash_functions.sh ; eval `grep setup_web_port /start.sh`')
|
||||
assert "Custom WEB_PORT set to 999" in function.stdout
|
||||
assert "INFO: Without proper router DNAT forwarding to 127.0.0.1:999, you may not get any blocked websites on ads" in function.stdout
|
||||
Slow(lambda: re.search(CONFIG_LINE, Docker.run('cat {}'.format(WEB_CONFIG)).stdout) != None)
|
||||
slow(lambda: re.search(CONFIG_LINE, docker.run(_cat(WEB_CONFIG)).stdout) != None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('test_args,expected_error', [
|
||||
|
@ -43,42 +53,39 @@ def test_overrides_default_WEB_PORT(Docker, Slow, test_args):
|
|||
('-e WEB_PORT="1,000"', 'WARNING: Custom WEB_PORT not used - 1,000 is not an integer'),
|
||||
('-e WEB_PORT="99999"', 'WARNING: Custom WEB_PORT not used - 99999 is not within valid port range of 1-65535'),
|
||||
])
|
||||
def test_bad_input_to_WEB_PORT(Docker, test_args, expected_error):
|
||||
function = Docker.run('. /bash_functions.sh ; eval `grep setup_web_port /start.sh`')
|
||||
def test_bad_input_to_web_port(docker, test_args, expected_error):
|
||||
function = docker.run('. /bash_functions.sh ; eval `grep setup_web_port /start.sh`')
|
||||
assert expected_error in function.stdout
|
||||
|
||||
|
||||
@pytest.mark.parametrize('test_args,cache_size', [('-e CUSTOM_CACHE_SIZE="0"', '0'), ('-e CUSTOM_CACHE_SIZE="20000"', '20000')])
|
||||
def test_overrides_default_CUSTOM_CACHE_SIZE(Docker, Slow, test_args, cache_size):
|
||||
def test_overrides_default_custom_cache_size(docker, slow, test_args, cache_size):
|
||||
''' Changes the cache_size setting to increase or decrease the cache size for dnsmasq'''
|
||||
CONFIG_LINE = r'cache-size\s*=\s*{}'.format(cache_size)
|
||||
DNSMASQ_CONFIG = '/etc/dnsmasq.d/01-pihole.conf'
|
||||
|
||||
function = Docker.run('echo ${CUSTOM_CACHE_SIZE};. ./bash_functions.sh; echo ${CUSTOM_CACHE_SIZE}; eval `grep setup_dnsmasq /start.sh`')
|
||||
function = docker.run('echo ${CUSTOM_CACHE_SIZE};. ./bash_functions.sh; echo ${CUSTOM_CACHE_SIZE}; eval `grep setup_dnsmasq /start.sh`')
|
||||
assert "Custom CUSTOM_CACHE_SIZE set to {}".format(cache_size) in function.stdout
|
||||
Slow(lambda: re.search(CONFIG_LINE, Docker.run('cat {}'.format(DNSMASQ_CONFIG)).stdout) != None)
|
||||
slow(lambda: re.search(CONFIG_LINE, docker.run(_cat(DNSMASQ_CONFIG_LOC)).stdout) != None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('test_args', [
|
||||
'-e CUSTOM_CACHE_SIZE="-1"',
|
||||
'-e CUSTOM_CACHE_SIZE="1,000"',
|
||||
])
|
||||
def test_bad_input_to_CUSTOM_CACHE_SIZE(Docker, Slow, test_args):
|
||||
def test_bad_input_to_custom_cache_size(docker, slow, test_args):
|
||||
CONFIG_LINE = r'cache-size\s*=\s*10000'
|
||||
DNSMASQ_CONFIG = '/etc/dnsmasq.d/01-pihole.conf'
|
||||
|
||||
Docker.run('. ./bash_functions.sh; eval `grep setup_dnsmasq /start.sh`')
|
||||
Slow(lambda: re.search(CONFIG_LINE, Docker.run('cat {}'.format(DNSMASQ_CONFIG)).stdout) != None)
|
||||
docker.run('. ./bash_functions.sh; eval `grep setup_dnsmasq /start.sh`')
|
||||
slow(lambda: re.search(CONFIG_LINE, docker.run(_cat(DNSMASQ_CONFIG_LOC)).stdout) != None)
|
||||
|
||||
@pytest.mark.parametrize('test_args', [
|
||||
'-e DNSSEC="true" -e CUSTOM_CACHE_SIZE="0"',
|
||||
])
|
||||
def test_dnssec_enabled_with_CUSTOM_CACHE_SIZE(Docker, Slow, test_args):
|
||||
def test_dnssec_enabled_with_custom_cache_size(docker, slow, test_args):
|
||||
CONFIG_LINE = r'cache-size\s*=\s*10000'
|
||||
DNSMASQ_CONFIG = '/etc/dnsmasq.d/01-pihole.conf'
|
||||
|
||||
Docker.run('. ./bash_functions.sh; eval `grep setup_dnsmasq /start.sh`')
|
||||
Slow(lambda: re.search(CONFIG_LINE, Docker.run('cat {}'.format(DNSMASQ_CONFIG)).stdout) != None)
|
||||
docker.run('. ./bash_functions.sh; eval `grep setup_dnsmasq /start.sh`')
|
||||
slow(lambda: re.search(CONFIG_LINE, docker.run(_cat(DNSMASQ_CONFIG_LOC)).stdout) != None)
|
||||
|
||||
|
||||
# DNS Environment Variable behavior in combinations of modified pihole LTE settings
|
||||
|
@ -91,14 +98,14 @@ def test_dnssec_enabled_with_CUSTOM_CACHE_SIZE(Docker, Slow, test_args):
|
|||
('-e DNS1="1.2.3.4" -e DNS2="no"', 'custom DNS', '1.2.3.4', None ),
|
||||
('-e DNS2="no"', 'custom DNS', '8.8.8.8', None ),
|
||||
])
|
||||
def test_override_default_servers_with_DNS_EnvVars(Docker, Slow, args_env, expected_stdout, dns1, dns2):
|
||||
def test_override_default_servers_with_dns_envvars(docker, slow, args_env, expected_stdout, dns1, dns2):
|
||||
''' on first boot when DNS vars are NOT set explain default google DNS settings are used
|
||||
or when DNS vars are set override the pihole DNS settings '''
|
||||
assert Docker.run('test -f /.piholeFirstBoot').rc == 0
|
||||
function = Docker.run('. /bash_functions.sh ; eval `grep "^setup_dnsmasq " /start.sh`')
|
||||
assert docker.run('test -f /.piholeFirstBoot').rc == 0
|
||||
function = docker.run(EVAL_SETUP_DNSMASQ)
|
||||
assert expected_stdout in function.stdout
|
||||
expected_servers = 'server={}\n'.format(dns1) if dns2 == None else 'server={}\nserver={}\n'.format(dns1, dns2)
|
||||
Slow(lambda: expected_servers == Docker.run('grep "^server=[^/]" /etc/dnsmasq.d/01-pihole.conf').stdout)
|
||||
slow(lambda: expected_servers == docker.run('grep "^server=[^/]" /etc/dnsmasq.d/01-pihole.conf').stdout)
|
||||
|
||||
|
||||
#@pytest.mark.skipif(os.environ.get('CI') == 'true',
|
||||
|
@ -115,31 +122,29 @@ def test_override_default_servers_with_DNS_EnvVars(Docker, Slow, args_env, expec
|
|||
('-e DNS1="1.2.3.4" -e DNS2="2.2.3.4"', '1.2.3.4', '2.2.3.4',
|
||||
'Docker DNS variables not used\nExisting DNS servers used (1.2.3.4 & 2.2.3.4'),
|
||||
])
|
||||
def test_DNS_Envs_are_secondary_to_setupvars(Docker, Slow, args_env, expected_stdout, dns1, dns2):
|
||||
def test_dns_envs_are_secondary_to_setupvars(docker, slow, args_env, expected_stdout, dns1, dns2):
|
||||
''' on second boot when DNS vars are set just use pihole DNS settings
|
||||
or when DNS vars and FORCE_DNS var are set override the pihole DNS settings '''
|
||||
# Given we are not booting for the first time
|
||||
assert Docker.run('rm /.piholeFirstBoot').rc == 0
|
||||
assert docker.run('rm /.piholeFirstBoot').rc == 0
|
||||
|
||||
# and a user already has custom pihole dns variables in setup vars
|
||||
dns_count = 1
|
||||
setupVars = '/etc/pihole/setupVars.conf'
|
||||
Docker.run('sed -i "/^PIHOLE_DNS/ d" {}'.format(setupVars))
|
||||
Docker.run('echo "PIHOLE_DNS_1={}" | tee -a {}'.format(dns1, setupVars))
|
||||
# and a user already has custom pihole dns variables in setup vars
|
||||
docker.run('sed -i "/^PIHOLE_DNS/ d" {}'.format(SETUPVARS_LOC))
|
||||
docker.run('echo "PIHOLE_DNS_1={}" | tee -a {}'.format(dns1, SETUPVARS_LOC))
|
||||
if dns2:
|
||||
Docker.run('echo "PIHOLE_DNS_2={}" | tee -a {}'.format(dns2, setupVars))
|
||||
Docker.run('sync {}'.format(setupVars))
|
||||
Slow(lambda: 'PIHOLE_DNS' in Docker.run('cat {}'.format(setupVars)).stdout)
|
||||
docker.run('echo "PIHOLE_DNS_2={}" | tee -a {}'.format(dns2, SETUPVARS_LOC))
|
||||
docker.run('sync {}'.format(SETUPVARS_LOC))
|
||||
slow(lambda: 'PIHOLE_DNS' in docker.run(_cat(SETUPVARS_LOC)).stdout)
|
||||
|
||||
# When we run setup dnsmasq during startup of the container
|
||||
function = Docker.run('. /bash_functions.sh ; eval `grep "^setup_dnsmasq " /start.sh`')
|
||||
function = docker.run(EVAL_SETUP_DNSMASQ)
|
||||
assert expected_stdout in function.stdout
|
||||
|
||||
# Then the servers are still what the user had customized if forced dnsmasq is not set
|
||||
expected_servers = ['server={}'.format(dns1)]
|
||||
if dns2:
|
||||
expected_servers.append('server={}'.format(dns2))
|
||||
Slow(lambda: Docker.run('grep "^server=[^/]" /etc/dnsmasq.d/01-pihole.conf').stdout.strip().split('\n') == \
|
||||
slow(lambda: docker.run('grep "^server=[^/]" /etc/dnsmasq.d/01-pihole.conf').stdout.strip().split('\n') == \
|
||||
expected_servers)
|
||||
|
||||
|
||||
|
@ -147,11 +152,11 @@ def test_DNS_Envs_are_secondary_to_setupvars(Docker, Slow, args_env, expected_st
|
|||
('', 'binding to default interface: eth0', 'PIHOLE_INTERFACE=eth0'),
|
||||
('-e INTERFACE="br0"', 'binding to custom interface: br0', 'PIHOLE_INTERFACE=br0'),
|
||||
])
|
||||
def test_DNS_interface_override_defaults(Docker, Slow, args_env, expected_stdout, expected_config_line):
|
||||
def test_dns_interface_override_defaults(docker, slow, args_env, expected_stdout, expected_config_line):
|
||||
''' When INTERFACE environment var is passed in, overwrite dnsmasq interface '''
|
||||
function = Docker.run('. /bash_functions.sh ; eval `grep "^setup_dnsmasq " /start.sh`')
|
||||
function = docker.run(EVAL_SETUP_DNSMASQ)
|
||||
assert expected_stdout in function.stdout
|
||||
Slow(lambda: expected_config_line + '\n' == Docker.run('grep "^PIHOLE_INTERFACE" /etc/pihole/setupVars.conf').stdout)
|
||||
slow(lambda: expected_config_line + '\n' == docker.run('grep "^PIHOLE_INTERFACE" {}'.format(SETUPVARS_LOC)).stdout)
|
||||
|
||||
|
||||
expected_debian_lines = [
|
||||
|
@ -165,59 +170,56 @@ expected_debian_lines = [
|
|||
(expected_debian_lines, 1),
|
||||
(expected_debian_lines, 2)
|
||||
])
|
||||
def test_debian_setup_php_env(Docker, expected_lines, repeat_function):
|
||||
def test_debian_setup_php_env(docker, expected_lines, repeat_function):
|
||||
''' confirm all expected output is there and nothing else '''
|
||||
stdout = ''
|
||||
for _ in range(repeat_function):
|
||||
stdout = Docker.run('. /bash_functions.sh ; eval `grep setup_php_env /start.sh`').stdout
|
||||
docker.run('. /bash_functions.sh ; eval `grep setup_php_env /start.sh`').stdout
|
||||
for expected_line in expected_lines:
|
||||
search_config_cmd = "grep -c '{}' /etc/lighttpd/conf-enabled/15-fastcgi-php.conf".format(expected_line)
|
||||
search_config_count = Docker.run(search_config_cmd)
|
||||
search_config_count = docker.run(search_config_cmd)
|
||||
found_lines = int(search_config_count.stdout.rstrip('\n'))
|
||||
if found_lines > 1:
|
||||
assert False, f'Found line {expected_line} times (more than once): {found_lines}'
|
||||
|
||||
|
||||
WEBPASSWORD_TEST_FUNCTION_COMMAND='. /bash_functions.sh ; eval `grep setup_web_password /start.sh`'
|
||||
|
||||
|
||||
def test_webPassword_random_generation(Docker):
|
||||
def test_webpassword_random_generation(docker):
|
||||
''' When a user sets webPassword env the admin password gets set to that '''
|
||||
function = Docker.run(WEBPASSWORD_TEST_FUNCTION_COMMAND)
|
||||
function = docker.run(EVAL_SETUP_WEB_PASSWORD)
|
||||
assert 'assigning random password' in function.stdout.lower()
|
||||
|
||||
|
||||
@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')])
|
||||
@pytest.mark.parametrize('args_env,secure,setupVarsHash', [
|
||||
@pytest.mark.parametrize('args_env,secure,setupvars_hash', [
|
||||
('-e ServerIP=1.2.3.4 -e WEBPASSWORD=login', True, 'WEBPASSWORD=6060d59351e8c2f48140f01b2c3f3b61652f396c53a5300ae239ebfbe7d5ff08'),
|
||||
('-e ServerIP=1.2.3.4 -e WEBPASSWORD=""', False, ''),
|
||||
])
|
||||
def test_webPassword_env_assigns_password_to_file_or_removes_if_empty(Docker, args_env, secure, setupVarsHash):
|
||||
def test_webpassword_env_assigns_password_to_file_or_removes_if_empty(docker, args_env, secure, setupvars_hash):
|
||||
''' When a user sets webPassword env the admin password gets set or removed if empty '''
|
||||
function = Docker.run(WEBPASSWORD_TEST_FUNCTION_COMMAND)
|
||||
function = docker.run(EVAL_SETUP_WEB_PASSWORD)
|
||||
|
||||
if secure:
|
||||
assert 'new password set' in function.stdout.lower()
|
||||
assert Docker.run('grep -q \'{}\' {}'.format(setupVarsHash, '/etc/pihole/setupVars.conf')).rc == 0
|
||||
assert docker.run(_grep(setupvars_hash, SETUPVARS_LOC)).rc == 0
|
||||
else:
|
||||
assert 'password removed' in function.stdout.lower()
|
||||
assert Docker.run('grep -q \'^WEBPASSWORD=$\' /etc/pihole/setupVars.conf').rc == 0
|
||||
assert docker.run(_grep('^WEBPASSWORD=$', SETUPVARS_LOC)).rc == 0
|
||||
|
||||
|
||||
@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')])
|
||||
@pytest.mark.parametrize('test_args', ['-e WEBPASSWORD=login', '-e WEBPASSWORD=""'])
|
||||
def test_env_always_updates_password(Docker, args_env, test_args):
|
||||
def test_env_always_updates_password(docker, args_env, test_args):
|
||||
'''When a user sets the WEBPASSWORD environment variable, ensure it always sets the password'''
|
||||
function = Docker.run(WEBPASSWORD_TEST_FUNCTION_COMMAND)
|
||||
function = docker.run(EVAL_SETUP_WEB_PASSWORD)
|
||||
|
||||
assert '::: Assigning password defined by Environment Variable' in function.stdout
|
||||
|
||||
|
||||
@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')])
|
||||
def test_setupvars_trumps_random_password_if_set(Docker, args_env, test_args):
|
||||
'''If a password is already set in setupVars, and no password is set in the environment variable, do not generate a random password'''
|
||||
Docker.run('. /opt/pihole/utils.sh ; addOrEditKeyValPair /etc/pihole/setupVars.conf WEBPASSWORD volumepass')
|
||||
function = Docker.run(WEBPASSWORD_TEST_FUNCTION_COMMAND)
|
||||
def test_setupvars_trumps_random_password_if_set(docker, args_env, test_args):
|
||||
'''If a password is already set in setupvars, and no password is set in the environment variable, do not generate a random password'''
|
||||
docker.run('. /opt/pihole/utils.sh ; addOrEditKeyValPair {} WEBPASSWORD volumepass'.format(SETUPVARS_LOC))
|
||||
function = docker.run(EVAL_SETUP_WEB_PASSWORD)
|
||||
|
||||
assert 'Pre existing WEBPASSWORD found' in function.stdout
|
||||
assert Docker.run('grep -q \'{}\' {}'.format('WEBPASSWORD=volumepass', '/etc/pihole/setupVars.conf')).rc == 0
|
||||
assert docker.run(_grep('WEBPASSWORD=volumepass', SETUPVARS_LOC)).rc == 0
|
||||
|
|
|
@ -8,47 +8,46 @@ def start_cmd():
|
|||
|
||||
|
||||
@pytest.fixture
|
||||
def RunningPiHole(DockerPersist, Slow, persist_webserver, persist_tag, start_cmd):
|
||||
''' Override the RunningPiHole to run and check for success of a
|
||||
def running_pihole(docker_persist, slow, persist_webserver, persist_tag, start_cmd):
|
||||
''' Override the running_pihole to run and check for success of a
|
||||
pihole-FTL start based `pihole` script command
|
||||
|
||||
Individual tests all must override start_cmd'''
|
||||
#print DockerPersist.run('ps -ef').stdout
|
||||
assert DockerPersist.dig.run('ping -c 1 test_pihole').rc == 0
|
||||
Slow(lambda: DockerPersist.run('pgrep pihole-FTL').rc == 0)
|
||||
Slow(lambda: DockerPersist.run('pgrep {}'.format(persist_webserver)).rc == 0)
|
||||
oldpid = DockerPersist.run('pidof pihole-FTL')
|
||||
cmd = DockerPersist.run('pihole {}'.format(start_cmd))
|
||||
Slow(lambda: DockerPersist.run('pgrep pihole-FTL').rc == 0)
|
||||
newpid = DockerPersist.run('pidof pihole-FTL')
|
||||
assert docker_persist.dig.run('ping -c 1 test_pihole').rc == 0
|
||||
slow(lambda: docker_persist.run('pgrep pihole-FTL').rc == 0)
|
||||
slow(lambda: docker_persist.run('pgrep {}'.format(persist_webserver)).rc == 0)
|
||||
oldpid = docker_persist.run('pidof pihole-FTL')
|
||||
cmd = docker_persist.run('pihole {}'.format(start_cmd))
|
||||
slow(lambda: docker_persist.run('pgrep pihole-FTL').rc == 0)
|
||||
newpid = docker_persist.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
|
||||
assert cmd.rc == 0
|
||||
# Save out cmd result to check different stdout of start/enable/disable
|
||||
DockerPersist.cmd = cmd
|
||||
return DockerPersist
|
||||
docker_persist.cmd = cmd
|
||||
return docker_persist
|
||||
|
||||
|
||||
@pytest.mark.parametrize('start_cmd,hostname,expected_ip, expected_messages', [
|
||||
('enable', 'pi.hole', '127.0.0.1', ['Blocking already enabled,','nothing to do']),
|
||||
('disable', 'pi.hole', '127.0.0.1', ['Disabling blocking','Pi-hole Disabled']),
|
||||
])
|
||||
def test_pihole_enable_disable_command(RunningPiHole, Dig, persist_tag, start_cmd, hostname, expected_ip, expected_messages):
|
||||
''' the start_cmd tests are all built into the RunningPiHole fixture in this file '''
|
||||
def test_pihole_enable_disable_command(running_pihole, dig, persist_tag, start_cmd, hostname, expected_ip, expected_messages):
|
||||
''' the start_cmd tests are all built into the running_pihole fixture in this file '''
|
||||
dig_cmd = "dig +time=1 +noall +answer {} @test_pihole".format(hostname)
|
||||
lookup = RunningPiHole.dig.run(dig_cmd)
|
||||
lookup = running_pihole.dig.run(dig_cmd)
|
||||
assert lookup.rc == 0
|
||||
lookup_ip = lookup.stdout.split()[4]
|
||||
assert lookup_ip == expected_ip
|
||||
|
||||
for part_of_output in expected_messages:
|
||||
assert part_of_output in RunningPiHole.cmd.stdout
|
||||
assert part_of_output in running_pihole.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
|
||||
def test_pihole_update_command(running_pihole, start_cmd, expected_message):
|
||||
assert running_pihole.cmd.stdout.strip() == expected_message
|
||||
|
||||
|
|
|
@ -7,9 +7,9 @@ import time
|
|||
|
||||
# If the test runs /start.sh, do not let s6 run it too! Kill entrypoint to avoid race condition/duplicated execution
|
||||
@pytest.mark.parametrize('persist_entrypoint,persist_cmd,persist_args_env', [('--entrypoint=tail','-f /dev/null','')])
|
||||
def test_ServerIP_missing_is_not_required_anymore(RunningPiHole):
|
||||
def test_serverip_missing_is_not_required_anymore(running_pihole):
|
||||
''' When args to docker are empty start.sh exits saying ServerIP is required '''
|
||||
start = Docker.run('/start.sh')
|
||||
start = docker.run('/start.sh')
|
||||
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
|
||||
|
@ -21,9 +21,9 @@ def test_ServerIP_missing_is_not_required_anymore(RunningPiHole):
|
|||
('-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),
|
||||
('-e ServerIP="1.2.3.4" -e ServerIPv6="kernel"', "ERROR: You passed in IPv6 with a value of 'kernel'",1),
|
||||
])
|
||||
def test_ServerIP_invalid_IPs_triggers_exit_error(Docker, error_msg, expect_rc):
|
||||
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')
|
||||
start = docker.run('/start.sh')
|
||||
assert start.rc == expect_rc
|
||||
assert 'ERROR' in start.stdout
|
||||
assert error_msg in start.stdout
|
||||
|
@ -33,15 +33,15 @@ def test_ServerIP_invalid_IPs_triggers_exit_error(Docker, error_msg, expect_rc):
|
|||
('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):
|
||||
def test_dns_responses(running_pihole, hostname, expected_ip):
|
||||
dig_cmd = "dig +time=1 +noall +answer {} @test_pihole | awk '{{ print $5 }}'".format(hostname)
|
||||
lookup = RunningPiHole.dig.run(dig_cmd).stdout.rstrip('\n')
|
||||
lookup = running_pihole.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
|
||||
File('/var/www/html/pihole/index.js').exists
|
||||
def test_indecies_are_present(running_pihole):
|
||||
file = running_pihole.get_module('File')
|
||||
file('/var/www/html/pihole/index.html').exists
|
||||
file('/var/www/html/pihole/index.js').exists
|
||||
|
||||
def validate_curl(http_rc, expected_http_code, page_contents):
|
||||
if int(http_rc.rc) != 0 or int(http_rc.stdout) != expected_http_code:
|
||||
|
@ -53,10 +53,10 @@ def validate_curl(http_rc, expected_http_code, page_contents):
|
|||
|
||||
@pytest.mark.parametrize('addr', [ 'localhost' ] )
|
||||
@pytest.mark.parametrize('url', [ '/admin/', '/admin/index.php' ] )
|
||||
def test_admin_requests_load_as_expected(RunningPiHole, version, addr, url):
|
||||
def test_admin_requests_load_as_expected(running_pihole, version, addr, url):
|
||||
command = 'curl -L -s -o /tmp/curled_file -w "%{{http_code}}" http://{}{}'.format(addr, url)
|
||||
http_rc = RunningPiHole.run(command)
|
||||
page_contents = RunningPiHole.run('cat /tmp/curled_file ').stdout
|
||||
http_rc = running_pihole.run(command)
|
||||
page_contents = running_pihole.run('cat /tmp/curled_file ').stdout
|
||||
expected_http_code = 200
|
||||
|
||||
validate_curl(http_rc, expected_http_code, page_contents)
|
||||
|
|
Loading…
Reference in New Issue