diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 69f2cc3..102482d 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -1,7 +1,12 @@ name: Tests on: - pull_request: push: + branches: + - master + - dev + - v* + - beta-v* + pull_request: jobs: build: @@ -23,4 +28,4 @@ jobs: - name: Run Circle Test run: | echo "Building ${ARCH}" - ./circle-test.sh \ No newline at end of file + ./circle-test.sh diff --git a/test/conftest.py b/test/conftest.py index 161fd3b..bb82273 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -2,6 +2,7 @@ import functools import os import pytest +import subprocess import testinfra import types @@ -14,6 +15,24 @@ with open('{}/VERSION'.format(dotdot), 'r') as v: raw_version = v.read().strip() __version__ = raw_version.replace('release/', 'release-') +@pytest.fixture() +def run_and_stream_command_output(): + def run_and_stream_command_output_inner(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) + return run_and_stream_command_output_inner + @pytest.fixture() def args_volumes(): return '-v /dev/null:/etc/pihole/adlists.list' diff --git a/test/test_volume_data.sh b/test/test_volume_data.sh new file mode 100755 index 0000000..1261ed0 --- /dev/null +++ b/test/test_volume_data.sh @@ -0,0 +1,103 @@ +#!/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 +if [ $(id -u) != 0 ] ; then + sudo=sudo # do not need if root (in docker) +fi +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() { + local container="$1" + # Must quote for complex commands + docker exec $TTY $container bash -c "$2" +} +EXEC $CONTAINER /start.sh # run all the startup scripts + +# 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 $CONTAINER 'pihole -a setdns 127.1.1.1,127.2.2.2,127.3.3.3,127.4.4.4' +EXEC $CONTAINER '. /opt/pihole/webpage.sh ; change_setting IPV4_ADDRESS 10.0.0.0' +EXEC $CONTAINER '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 + +# When removing/re-creating the container +docker rm -f $CONTAINER +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 + +# 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..1207f27 --- /dev/null +++ b/test/test_volumes.py @@ -0,0 +1,4 @@ +def test_volume_shell_script(arch, run_and_stream_command_output): + # only one arch should be necessary + if arch == 'amd64': + run_and_stream_command_output('./test/test_volume_data.sh')