diff --git a/test/test_volume_data.sh b/test/test_volume_data.sh new file mode 100755 index 0000000..97a71ce --- /dev/null +++ b/test/test_volume_data.sh @@ -0,0 +1,85 @@ +#!/bin/bash +set -ex +# Trying something different from the python test, this is a big integration test in bash +# Tests multiple volume settings and how they are impacted by the complete startup scripts + restart/re-creation of container +# Maybe a bit easier to read the workflow/debug in bash than python for others? +# This workflow is VERY similar to python's tests, but in bash so not object-oriented/pytest fixture based + +# Debug can be added anywhere to check current state mid-test +RED='\033[0;31m' +NC='\033[0m' # No Color +debug() { + sudo grep -r . "$VOL_PH" + sudo grep -r . "$VOL_DM" +} +# Cleanup at the end, print debug on fail +cleanup() { + retcode=$? + { set +x; } 2>/dev/null + if [ $retcode != 0 ] ; then + printf "${RED}ERROR / FAILURE${NC} - printing all volume info" + debug + fi + docker rm -f $CONTAINER + sudo rm -rf $VOLUMES + exit $retcode +} +trap "cleanup" INT TERM EXIT + + +# VOLUME TESTS + +# Given... +IMAGE="pihole:v5.0-amd64" # The latest build test image (generic, non release/branch tag) +VOLUMES="$(mktemp -d)" # A fresh volume directory +VOL_PH="$VOLUMES/pihole" +VOL_DM="$VOLUMES/dnsmasq.d" +tty -s && TTY='-t' || TTY='' + +echo "Testing $IMAGE with volumes base path $VOLUMES" + +# When +# Running stock+empty volumes (no ports to avoid conflicts) +CONTAINER="$( + docker run -d \ + -v "$VOL_PH:/etc/pihole/" \ + -v "$VOL_DM:/etc/dnsmasq.d/" \ + -v "/dev/null:/etc/pihole/adlists.list" \ + --entrypoint='' \ + $IMAGE \ + tail -f /dev/null +)" # container backgrounded for multipiple operations over time + +EXEC() { + # Must quote for complex commands + docker exec $TTY $CONTAINER bash -c "$@" +} +EXEC /start.sh + +# Then default are present +grep "PIHOLE_DNS_1=8.8.8.8" "$VOL_PH/setupVars.conf" +grep "PIHOLE_DNS_2=8.8.4.4" "$VOL_PH/setupVars.conf" +grep "IPV4_ADDRESS=0.0.0.0" "$VOL_PH/setupVars.conf" +grep -E "WEBPASSWORD=.+" "$VOL_PH/setupVars.conf" + +# Given the settings are manually changed (not good settings, just for testing changes) +EXEC 'pihole -a setdns 127.1.1.1,127.2.2.2,127.3.3.3,127.4.4.4' +EXEC '. /opt/pihole/webpage.sh ; change_setting IPV4_ADDRESS 10.0.0.0' +EXEC 'pihole -a -p login' +assert_new_settings() { + grep "PIHOLE_DNS_1=127.1.1.1" "$VOL_PH/setupVars.conf" + grep "PIHOLE_DNS_2=127.2.2.2" "$VOL_PH/setupVars.conf" + grep "PIHOLE_DNS_3=127.3.3.3" "$VOL_PH/setupVars.conf" + grep "PIHOLE_DNS_4=127.4.4.4" "$VOL_PH/setupVars.conf" + grep "IPV4_ADDRESS=10.0.0.0" "$VOL_PH/setupVars.conf" + grep "WEBPASSWORD=6060d59351e8c2f48140f01b2c3f3b61652f396c53a5300ae239ebfbe7d5ff08" "$VOL_PH/setupVars.conf" + grep "server=127.1.1.1" $VOL_DM/01-pihole.conf + grep "server=127.2.2.2" $VOL_DM/01-pihole.conf +} +assert_new_settings + +# When Restarting +docker restart $CONTAINER + +# Then settings are still manual changed values +assert_new_settings diff --git a/test/test_volumes.py b/test/test_volumes.py new file mode 100644 index 0000000..d703bf1 --- /dev/null +++ b/test/test_volumes.py @@ -0,0 +1,25 @@ +import subprocess + +def run_and_stream_command_output(command, verbose=False): + print("Running", command) + build_env = os.environ.copy() + build_env['PIHOLE_VERSION'] = __version__ + build_result = subprocess.Popen(command.split(), env=build_env, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, + bufsize=1, universal_newlines=True) + if verbose: + while build_result.poll() is None: + for line in build_result.stdout: + print(line, end='') + build_result.wait() + if build_result.returncode != 0: + print(" ::: Error running".format(command)) + print(build_result.stderr) + +def test_volume_shell_script(arch): + # only one arch should be necessary + if arch == 'amd64': + run_and_stream_command_output('./test/test_volume_data.sh') + + +def test_fail(): + assert 1 == 2