setupVar tests passing for debian & centos

This commit is contained in:
diginc 2016-10-10 23:14:39 -05:00
parent aa23fb1d56
commit a03caea549
9 changed files with 178 additions and 1 deletions

4
.gitignore vendored
View File

@ -1 +1,5 @@
.DS_Store
*.pyc
*.swp
__pycache__
.cache

View File

@ -971,4 +971,6 @@ echo "::: The install log is located at: /etc/pihole/install.log"
echo "::: View the web interface at http://pi.hole/admin or http://${IPv4_address%/*}/admin"
}
main "$@"
if [[ -z "$PHTEST" ]] ; then
main "$@"
fi

1
autotest Executable file
View File

@ -0,0 +1 @@
py.test -v -f test/

0
test/__init__.py Normal file
View File

14
test/centos.Dockerfile Normal file
View File

@ -0,0 +1,14 @@
FROM centos:7
ENV GITDIR /etc/.pihole
ENV SCRIPTDIR /opt/pihole
RUN mkdir -p $GITDIR $SCRIPTDIR /etc/pihole
ADD . $GITDIR
RUN cp $GITDIR/advanced/Scripts/*.sh $GITDIR/gravity.sh $GITDIR/pihole $GITDIR/automated\ install/*.sh $SCRIPTDIR/
ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$SCRIPTDIR
RUN true && \
chmod +x $SCRIPTDIR/*
#sed '/# Start the installer/Q' /opt/pihole/basic-install.sh > /opt/pihole/stub_basic-install.sh && \

46
test/conftest.py Normal file
View File

@ -0,0 +1,46 @@
import pytest
import testinfra
check_output = testinfra.get_backend(
"local://"
).get_module("Command").check_output
@pytest.fixture
def Pihole(Docker):
''' used to contain some script stubbing, now pretty much an alias '''
return Docker
@pytest.fixture
def Docker(request, args, image, cmd):
''' combine our fixtures into a docker run command and setup finalizer to cleanup '''
assert 'docker' in check_output('id'), "Are you in the docker group?"
docker_run = "docker run {} {} {}".format(args, image, cmd)
docker_id = check_output(docker_run)
def teardown():
check_output("docker rm -f %s", docker_id)
request.addfinalizer(teardown)
docker_container = testinfra.get_backend("docker://" + docker_id)
docker_container.id = docker_id
return docker_container
@pytest.fixture
def args(request):
''' -t became required when tput began being used '''
return '-t -d'
@pytest.fixture(params=['debian', 'centos'])
def tag(request):
''' consumed by image to make the test matrix '''
return request.param
@pytest.fixture()
def image(request, tag):
''' built by test_000_build_containers.py '''
return 'pytest_pihole:{}'.format(tag)
@pytest.fixture()
def cmd(request):
''' default to doing nothing by tailing null, but don't exit '''
return 'tail -f /dev/null'

15
test/debian.Dockerfile Normal file
View File

@ -0,0 +1,15 @@
FROM debian:jessie
ENV GITDIR /etc/.pihole
ENV SCRIPTDIR /opt/pihole
RUN mkdir -p $GITDIR $SCRIPTDIR /etc/pihole
ADD . $GITDIR
RUN cp $GITDIR/advanced/Scripts/*.sh $GITDIR/gravity.sh $GITDIR/pihole $GITDIR/automated\ install/*.sh $SCRIPTDIR/
ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$SCRIPTDIR
RUN true && \
chmod +x $SCRIPTDIR/*
#sed '/# Start the installer/Q' /opt/pihole/basic-install.sh > /opt/pihole/stub_basic-install.sh && \

View File

@ -0,0 +1,18 @@
''' This file starts with 000 to make it run first '''
import pytest
import testinfra
run_local = testinfra.get_backend(
"local://"
).get_module("Command").run
@pytest.mark.parametrize("image,tag", [
( 'test/debian.Dockerfile', 'pytest_pihole:debian' ),
( 'test/centos.Dockerfile', 'pytest_pihole:centos' ),
])
def test_build_pihole_image(image, tag):
build_cmd = run_local('docker build -f {} -t {} .'.format(image, tag))
if build_cmd.rc != 0:
print build_cmd.stdout
print build_cmd.stderr
assert build_cmd.rc == 0

View File

@ -0,0 +1,77 @@
import pytest
from textwrap import dedent
SETUPVARS = {
'piholeInterface' : 'eth99',
'IPv4_address' : '1.1.1.1',
'IPv6_address' : '2:2:2:2:2:2',
'piholeDNS1' : '4.2.2.1',
'piholeDNS2' : '4.2.2.2'
}
def test_setupVars_are_sourced_to_global_scope(Pihole):
''' currently update_dialogs sources setupVars with a dot,
then various other functions use the variables '''
setup_var_file = 'cat <<EOF> /etc/pihole/setupVars.conf\n'
for k,v in SETUPVARS.iteritems():
setup_var_file += "{}={}\n".format(k, v)
setup_var_file += "EOF\n"
Pihole.run(setup_var_file)
script = dedent('''\
#!/bin/bash -e
printSetupVars() {
# Currently debug test function only
echo "Outputting sourced variables"
echo "piholeInterface=\${piholeInterface}"
echo "IPv4_address=\${IPv4_address}"
echo "IPv6_address=\${IPv6_address}"
echo "piholeDNS1=\${piholeDNS1}"
echo "piholeDNS2=\${piholeDNS2}"
}
update_dialogs() {
. /etc/pihole/setupVars.conf
}
update_dialogs
printSetupVars
''')
output = run_script(Pihole, script).stdout
for k,v in SETUPVARS.iteritems():
assert "{}={}".format(k, v) in output
def test_setupVars_saved_to_file(Pihole):
''' confirm saved settings are written to a file for future updates to re-use '''
set_setup_vars = '\n' # dedent works better with this and padding matching script below
for k,v in SETUPVARS.iteritems():
set_setup_vars += " {}={}\n".format(k, v)
Pihole.run(set_setup_vars).stdout
script = dedent('''\
#!/bin/bash -e
echo start
TERM=xterm
PHTEST=TRUE
source /opt/pihole/basic-install.sh
{}
finalExports
cat /etc/pihole/setupVars.conf
'''.format(set_setup_vars))
output = run_script(Pihole, script).stdout
for k,v in SETUPVARS.iteritems():
assert "{}={}".format(k, v) in output
def run_script(Pihole, script, file="/test.sh"):
_write_test_script(Pihole, script, file=file)
result = Pihole.run(file)
assert result.rc == 0
return result
def _write_test_script(Pihole, script, file):
''' Running the test script blocks directly can behave differently with regard to global vars '''
''' this is a cheap work around to that until all functions no longer rely on global variables '''
Pihole.run('cat <<EOF> {file}\n{script}\nEOF'.format(file=file, script=script))
Pihole.run('chmod +x {}'.format(file))