Skip to content

Commit a03caea

Browse files
committed
setupVar tests passing for debian & centos
1 parent aa23fb1 commit a03caea

9 files changed

+178
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
.DS_Store
2+
*.pyc
3+
*.swp
4+
__pycache__
5+
.cache

automated install/basic-install.sh

+3-1
Original file line numberDiff line numberDiff line change
@@ -971,4 +971,6 @@ echo "::: The install log is located at: /etc/pihole/install.log"
971971
echo "::: View the web interface at http://pi.hole/admin or http://${IPv4_address%/*}/admin"
972972
}
973973

974-
main "$@"
974+
if [[ -z "$PHTEST" ]] ; then
975+
main "$@"
976+
fi

autotest

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
py.test -v -f test/

test/__init__.py

Whitespace-only changes.

test/centos.Dockerfile

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM centos:7
2+
3+
ENV GITDIR /etc/.pihole
4+
ENV SCRIPTDIR /opt/pihole
5+
6+
RUN mkdir -p $GITDIR $SCRIPTDIR /etc/pihole
7+
ADD . $GITDIR
8+
RUN cp $GITDIR/advanced/Scripts/*.sh $GITDIR/gravity.sh $GITDIR/pihole $GITDIR/automated\ install/*.sh $SCRIPTDIR/
9+
ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$SCRIPTDIR
10+
11+
RUN true && \
12+
chmod +x $SCRIPTDIR/*
13+
14+
#sed '/# Start the installer/Q' /opt/pihole/basic-install.sh > /opt/pihole/stub_basic-install.sh && \

test/conftest.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import pytest
2+
import testinfra
3+
4+
check_output = testinfra.get_backend(
5+
"local://"
6+
).get_module("Command").check_output
7+
8+
@pytest.fixture
9+
def Pihole(Docker):
10+
''' used to contain some script stubbing, now pretty much an alias '''
11+
return Docker
12+
13+
@pytest.fixture
14+
def Docker(request, args, image, cmd):
15+
''' combine our fixtures into a docker run command and setup finalizer to cleanup '''
16+
assert 'docker' in check_output('id'), "Are you in the docker group?"
17+
docker_run = "docker run {} {} {}".format(args, image, cmd)
18+
docker_id = check_output(docker_run)
19+
20+
def teardown():
21+
check_output("docker rm -f %s", docker_id)
22+
request.addfinalizer(teardown)
23+
24+
docker_container = testinfra.get_backend("docker://" + docker_id)
25+
docker_container.id = docker_id
26+
return docker_container
27+
28+
@pytest.fixture
29+
def args(request):
30+
''' -t became required when tput began being used '''
31+
return '-t -d'
32+
33+
@pytest.fixture(params=['debian', 'centos'])
34+
def tag(request):
35+
''' consumed by image to make the test matrix '''
36+
return request.param
37+
38+
@pytest.fixture()
39+
def image(request, tag):
40+
''' built by test_000_build_containers.py '''
41+
return 'pytest_pihole:{}'.format(tag)
42+
43+
@pytest.fixture()
44+
def cmd(request):
45+
''' default to doing nothing by tailing null, but don't exit '''
46+
return 'tail -f /dev/null'

test/debian.Dockerfile

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM debian:jessie
2+
3+
ENV GITDIR /etc/.pihole
4+
ENV SCRIPTDIR /opt/pihole
5+
6+
RUN mkdir -p $GITDIR $SCRIPTDIR /etc/pihole
7+
ADD . $GITDIR
8+
RUN cp $GITDIR/advanced/Scripts/*.sh $GITDIR/gravity.sh $GITDIR/pihole $GITDIR/automated\ install/*.sh $SCRIPTDIR/
9+
ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:$SCRIPTDIR
10+
11+
12+
RUN true && \
13+
chmod +x $SCRIPTDIR/*
14+
15+
#sed '/# Start the installer/Q' /opt/pihole/basic-install.sh > /opt/pihole/stub_basic-install.sh && \

test/test_000_build_containers.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
''' This file starts with 000 to make it run first '''
2+
import pytest
3+
import testinfra
4+
5+
run_local = testinfra.get_backend(
6+
"local://"
7+
).get_module("Command").run
8+
9+
@pytest.mark.parametrize("image,tag", [
10+
( 'test/debian.Dockerfile', 'pytest_pihole:debian' ),
11+
( 'test/centos.Dockerfile', 'pytest_pihole:centos' ),
12+
])
13+
def test_build_pihole_image(image, tag):
14+
build_cmd = run_local('docker build -f {} -t {} .'.format(image, tag))
15+
if build_cmd.rc != 0:
16+
print build_cmd.stdout
17+
print build_cmd.stderr
18+
assert build_cmd.rc == 0

test/test_automated_install.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import pytest
2+
from textwrap import dedent
3+
4+
SETUPVARS = {
5+
'piholeInterface' : 'eth99',
6+
'IPv4_address' : '1.1.1.1',
7+
'IPv6_address' : '2:2:2:2:2:2',
8+
'piholeDNS1' : '4.2.2.1',
9+
'piholeDNS2' : '4.2.2.2'
10+
}
11+
12+
def test_setupVars_are_sourced_to_global_scope(Pihole):
13+
''' currently update_dialogs sources setupVars with a dot,
14+
then various other functions use the variables '''
15+
setup_var_file = 'cat <<EOF> /etc/pihole/setupVars.conf\n'
16+
for k,v in SETUPVARS.iteritems():
17+
setup_var_file += "{}={}\n".format(k, v)
18+
setup_var_file += "EOF\n"
19+
Pihole.run(setup_var_file)
20+
21+
script = dedent('''\
22+
#!/bin/bash -e
23+
printSetupVars() {
24+
# Currently debug test function only
25+
echo "Outputting sourced variables"
26+
echo "piholeInterface=\${piholeInterface}"
27+
echo "IPv4_address=\${IPv4_address}"
28+
echo "IPv6_address=\${IPv6_address}"
29+
echo "piholeDNS1=\${piholeDNS1}"
30+
echo "piholeDNS2=\${piholeDNS2}"
31+
}
32+
update_dialogs() {
33+
. /etc/pihole/setupVars.conf
34+
}
35+
update_dialogs
36+
printSetupVars
37+
''')
38+
39+
output = run_script(Pihole, script).stdout
40+
41+
for k,v in SETUPVARS.iteritems():
42+
assert "{}={}".format(k, v) in output
43+
44+
def test_setupVars_saved_to_file(Pihole):
45+
''' confirm saved settings are written to a file for future updates to re-use '''
46+
set_setup_vars = '\n' # dedent works better with this and padding matching script below
47+
for k,v in SETUPVARS.iteritems():
48+
set_setup_vars += " {}={}\n".format(k, v)
49+
Pihole.run(set_setup_vars).stdout
50+
51+
script = dedent('''\
52+
#!/bin/bash -e
53+
echo start
54+
TERM=xterm
55+
PHTEST=TRUE
56+
source /opt/pihole/basic-install.sh
57+
{}
58+
finalExports
59+
cat /etc/pihole/setupVars.conf
60+
'''.format(set_setup_vars))
61+
62+
output = run_script(Pihole, script).stdout
63+
64+
for k,v in SETUPVARS.iteritems():
65+
assert "{}={}".format(k, v) in output
66+
67+
def run_script(Pihole, script, file="/test.sh"):
68+
_write_test_script(Pihole, script, file=file)
69+
result = Pihole.run(file)
70+
assert result.rc == 0
71+
return result
72+
73+
def _write_test_script(Pihole, script, file):
74+
''' Running the test script blocks directly can behave differently with regard to global vars '''
75+
''' this is a cheap work around to that until all functions no longer rely on global variables '''
76+
Pihole.run('cat <<EOF> {file}\n{script}\nEOF'.format(file=file, script=script))
77+
Pihole.run('chmod +x {}'.format(file))

0 commit comments

Comments
 (0)