diff --git a/bash_functions.sh b/bash_functions.sh index 1aa0499..7da4922 100644 --- a/bash_functions.sh +++ b/bash_functions.sh @@ -231,18 +231,22 @@ setup_web_port() { } -setup_web_password() { +generate_password() { if [ -z "${WEBPASSWORD+x}" ] ; then # Not set at all, give the user a random pass WEBPASSWORD=$(tr -dc _A-Z-a-z-0-9 < /dev/urandom | head -c 8) echo "Assigning random password: $WEBPASSWORD" fi; +} + +setup_web_password() { + PASS="$1" # Turn bash debug on while setting up password (to print it) set -x - if [[ "$WEBPASSWORD" == "" ]] ; then + if [[ "$PASS" == "" ]] ; then echo "" | pihole -a -p else - pihole -a -p "$WEBPASSWORD" "$WEBPASSWORD" + pihole -a -p "$PASS" "$PASS" fi if [ "${PH_VERBOSE:-0}" -gt 0 ] ; then # Turn bash debug back off after print password setup @@ -262,10 +266,8 @@ setup_ipv4_ipv6() { test_configs() { set -e - echo -n '::: Testing pihole-FTL configs: ' + echo -n '::: Testing pihole-FTL DNS: ' pihole-FTL test || exit 1 - echo -n '::: Testing pihole-dnsmasq configs: ' - pihole-FTL dnsmasq-test || exit 1 echo -n '::: Testing lighttpd config: ' lighttpd -t -f /etc/lighttpd/lighttpd.conf || exit 1 set +e diff --git a/start.sh b/start.sh index fb630c1..ab5c1be 100755 --- a/start.sh +++ b/start.sh @@ -13,7 +13,6 @@ export DNS2 export INTERFACE export IPv6 export WEB_PORT -export PLAINWEBPASSWORD="$WEBPASSWORD" export adlistFile='/etc/pihole/adlists.list' @@ -26,12 +25,13 @@ export adlistFile='/etc/pihole/adlists.list' PH_TEST=true . $PIHOLE_INSTALL echo " ::: Starting docker specific setup for docker pihole/pihole" +generate_password validate_env || exit 1 prepare_configs change_setting "IPV4_ADDRESS" "$ServerIP" change_setting "IPV6_ADDRESS" "$ServerIPv6" setup_web_port "$WEB_PORT" -setup_web_password "$PLAINWEBPASSWORD" +setup_web_password "$WEBPASSWORD" setup_dnsmasq "$DNS1" "$DNS2" "$INTERFACE" setup_php_env setup_dnsmasq_hostnames "$ServerIP" "$ServerIPv6" "$HOSTNAME" diff --git a/test/conftest.py b/test/conftest.py index 9a0ed98..38fc11f 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -60,7 +60,7 @@ def entrypoint(): def args(request): return '-e ServerIP="127.0.0.1" -e ServerIPv6="::1"' -@pytest.fixture(params=['amd64', 'armel', 'armhf', 'aarch64']) +@pytest.fixture(params=['amd64', 'armhf', 'aarch64']) def arch(request): return request.param diff --git a/test/test_bash_functions.py b/test/test_bash_functions.py index 4b4bbe4..6136c20 100644 --- a/test/test_bash_functions.py +++ b/test/test_bash_functions.py @@ -1,8 +1,10 @@ import pytest import re + DEFAULTARGS = '-e ServerIP="127.0.0.1" ' + @pytest.mark.parametrize('args,expected_ipv6,expected_stdout', [ (DEFAULTARGS, True, 'IPv4 and IPv6'), (DEFAULTARGS + '-e "IPv6=True"', True, 'IPv4 and IPv6'), @@ -19,6 +21,7 @@ def test_IPv6_not_True_removes_ipv6(Docker, args, expected_ipv6, expected_stdout config = Docker.run('cat {}'.format(WEB_CONFIG)).stdout assert (IPV6_LINE in config) == expected_ipv6 + @pytest.mark.parametrize('args', [DEFAULTARGS + '-e "WEB_PORT=999"']) def test_overrides_default_WEB_PORT(Docker, args): ''' When a --net=host user sets WEB_PORT to avoid synology's 80 default IPv4 and or IPv6 ports are updated''' @@ -38,6 +41,7 @@ def test_overrides_default_WEB_PORT(Docker, args): assert int(Docker.run('grep -rl "://127.0.0.1:999/" /var/www/html/ | wc -l').stdout) >= 1 assert int(Docker.run('grep -rl "://pi.hole:999/" /var/www/html/ | wc -l').stdout) >= 1 + @pytest.mark.parametrize('args,expected_error', [ (DEFAULTARGS + '-e WEB_PORT="LXXX"', 'WARNING: Custom WEB_PORT not used - LXXX is not an integer'), (DEFAULTARGS + '-e WEB_PORT="1,000"', 'WARNING: Custom WEB_PORT not used - 1,000 is not an integer'), @@ -68,6 +72,7 @@ def test_override_default_servers_with_DNS_EnvVars(Docker, args, expected_stdout expected_servers = 'server={}\n'.format(dns1) if dns2 == None else 'server={}\nserver={}\n'.format(dns1, dns2) assert expected_servers == docker_dns_servers + @pytest.mark.parametrize('args, dns1, dns2, expected_stdout', [ ('-e ServerIP="1.2.3.4"', '9.9.9.1', '9.9.9.2', 'Existing DNS servers used'), @@ -102,6 +107,7 @@ def test_DNS_Envs_are_secondary_to_setupvars(Docker, args, expected_stdout, dns1 assert 'server={}'.format(dns1) == searchDns1 assert 'server={}'.format(dns2) == searchDns2 + @pytest.mark.parametrize('args, expected_stdout, expected_config_line', [ ('-e ServerIP="1.2.3.4"', 'binding to default interface: eth0', 'interface=eth0' ), ('-e ServerIP="1.2.3.4" -e INTERFACE="eth0"', 'binding to default interface: eth0', 'interface=eth0' ), @@ -115,11 +121,14 @@ def test_DNS_interface_override_defaults(Docker, args, expected_stdout, expected docker_dns_interface = Docker.run('grep "^interface" /etc/dnsmasq.d/01-pihole.conf').stdout assert expected_config_line + '\n' == docker_dns_interface + expected_debian_lines = [ '"VIRTUAL_HOST" => "127.0.0.1"', '"ServerIP" => "127.0.0.1"', '"PHP_ERROR_LOG" => "/var/log/lighttpd/error.log"' ] + + @pytest.mark.parametrize('expected_lines,repeat_function', [ (expected_debian_lines, 1), (expected_debian_lines, 2) @@ -136,18 +145,24 @@ def test_debian_setup_php_env(Docker, expected_lines, repeat_function): if found_lines > 1: assert False, "Found line {} times (more than once): {}".format(expected_line) + +# Overwrite entrypoint / cmd with noop, just run our method for this unit +@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')]) +@pytest.mark.parametrize('args', [('-e ServerIP=1.2.3.4')]) +def test_webPassword_random_generation(Docker, args): + ''' When a user sets webPassword env the admin password gets set to that ''' + function = Docker.run('. /bash_functions.sh ; eval `grep generate_password /start.sh`') + assert 'assigning random password' in function.stdout.lower() + + +@pytest.mark.parametrize('entrypoint,cmd', [('--entrypoint=tail','-f /dev/null')]) @pytest.mark.parametrize('args,secure,setupVarsHash', [ ('-e ServerIP=1.2.3.4 -e WEBPASSWORD=login', True, 'WEBPASSWORD=6060d59351e8c2f48140f01b2c3f3b61652f396c53a5300ae239ebfbe7d5ff08'), ('-e ServerIP=1.2.3.4 -e WEBPASSWORD=""', False, ''), - ('-e ServerIP=1.2.3.4', True, 'WEBPASSWORD='), ]) -def test_webPassword_env_assigns_password_to_file(Docker, args, secure, setupVarsHash): - ''' When a user sets webPassword env the admin password gets set to that ''' +def test_webPassword_env_assigns_password_to_file_or_removes_if_empty(Docker, args, secure, setupVarsHash): + ''' When a user sets webPassword env the admin password gets set or removed if empty ''' function = Docker.run('. /bash_functions.sh ; eval `grep setup_web_password /start.sh`') - if secure and 'WEBPASSWORD' not in args: - assert 'assigning random password' in function.stdout.lower() - else: - assert 'assigning random password' not in function.stdout.lower() if secure: assert 'new password set' in function.stdout.lower() diff --git a/tox.ini b/tox.ini index ad9cca1..c719ebd 100644 --- a/tox.ini +++ b/tox.ini @@ -4,10 +4,10 @@ envlist = py27 [testenv] whitelist_externals = docker deps = -rrequirements.txt -# 2 parallel max b/c race condition with docker fixture (I think?) +# auto parallel max b/c race condition with docker fixture (I think?) commands = docker run --rm --privileged multiarch/qemu-user-static:register --reset ./Dockerfile.py -v --arch amd64 - pytest -vv -n 2 -k amd64 ./test/ + pytest -vv -n auto -k amd64 ./test/ ./Dockerfile.py -v --arch armhf --arch aarch64 - pytest -vv -n 2 -k armhf ./test/ - pytest -vv -n 2 -k aarch64 ./test/ + pytest -vv -n auto -k armhf ./test/ + pytest -vv -n auto -k aarch64 ./test/