From 706e459d04a60b5cb725f9fa17e9007983e4d27a Mon Sep 17 00:00:00 2001 From: diginc Date: Tue, 13 Sep 2016 08:12:38 -0500 Subject: [PATCH] Adding test to ensure upgrade vars are set, fixed Modified upgrade variable import to export vars to ensure they stay set in all script scopes and environments --- .gitignore | 4 ++++ .travis.yml | 10 +++++++++ automated install/basic-install.sh | 10 ++++++--- autotest | 1 + pytest.Dockerfile | 13 +++++++++++ requirements.txt | 5 +++++ test/__init__.py | 0 test/conftest.py | 34 ++++++++++++++++++++++++++++ test/test_000_build_containers.py | 17 ++++++++++++++ test/test_automated_install.py | 36 ++++++++++++++++++++++++++++++ 10 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 .travis.yml create mode 100755 autotest create mode 100644 pytest.Dockerfile create mode 100644 requirements.txt create mode 100644 test/__init__.py create mode 100644 test/conftest.py create mode 100644 test/test_000_build_containers.py create mode 100644 test/test_automated_install.py diff --git a/.gitignore b/.gitignore index e43b0f98..0e0d4b99 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ .DS_Store +*.pyc +*.swp +__pycache__ +.cache diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..655f84e3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,10 @@ +sudo: required +services: + - docker +language: python +python: + - "2.7" +install: + - pip install -r requirements.txt + +script: py.test -v diff --git a/automated install/basic-install.sh b/automated install/basic-install.sh index d5f070a4..292517b2 100755 --- a/automated install/basic-install.sh +++ b/automated install/basic-install.sh @@ -803,6 +803,7 @@ finalExports() { ${SUDO} echo "piholeIPv6=${piholeIPv6}" >> ${setupVars} ${SUDO} echo "piholeDNS1=${piholeDNS1}" >> ${setupVars} ${SUDO} echo "piholeDNS2=${piholeDNS2}" >> ${setupVars} + export IPv4addr } @@ -923,15 +924,18 @@ updateDialogs(){ } ######## SCRIPT ############ -if [[ -f ${setupVars} ]];then - . ${setupVars} +if [ -f ${setupVars} ];then + echo "::: Importing previous variables for upgrade" + while read line; do + echo " ::: ${line}" + eval "export $line" + done < ${setupVars} if [ "$1" == "pihole" ]; then useUpdateVars=true else updateDialogs fi - fi # Start the installer diff --git a/autotest b/autotest new file mode 100755 index 00000000..3747cc0b --- /dev/null +++ b/autotest @@ -0,0 +1 @@ +py.test -v -f test/ diff --git a/pytest.Dockerfile b/pytest.Dockerfile new file mode 100644 index 00000000..d22fdbb6 --- /dev/null +++ b/pytest.Dockerfile @@ -0,0 +1,13 @@ +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/ && \ + rmdir /usr/local/bin && ln -s $SCRIPTDIR/ /usr/local/bin + +RUN true && \ + sed '/# Start the installer/Q' /opt/pihole/basic-install.sh > /opt/pihole/stub_basic-install.sh && \ + chmod +x $SCRIPTDIR/* diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..53737ca5 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +docker-compose +pytest +pytest-xdist +pytest-cov +testinfra diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/test/conftest.py b/test/conftest.py new file mode 100644 index 00000000..52d54083 --- /dev/null +++ b/test/conftest.py @@ -0,0 +1,34 @@ +import pytest +import testinfra + +DEBUG = [] + +check_output = testinfra.get_backend( + "local://" +).get_module("Command").check_output + +@pytest.fixture +def Docker(request, args, image, cmd): + 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): + return '-d' + +@pytest.fixture() +def image(request): + return 'pytest_pihole' + +@pytest.fixture() +def cmd(request): + return 'tail -f /dev/null' diff --git a/test/test_000_build_containers.py b/test/test_000_build_containers.py new file mode 100644 index 00000000..3516ff4e --- /dev/null +++ b/test/test_000_build_containers.py @@ -0,0 +1,17 @@ +''' 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", [ + ( 'pytest.Dockerfile', 'pytest_pihole' ), +]) +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 diff --git a/test/test_automated_install.py b/test/test_automated_install.py new file mode 100644 index 00000000..2fdedd1b --- /dev/null +++ b/test/test_automated_install.py @@ -0,0 +1,36 @@ +import pytest + +@pytest.fixture +def Pihole(Docker): + ''' generates stubs of files that normally execute code ''' + return Docker + +def test_setupVars_get_read_into_main_scope_level(Pihole): + vars = { + 'piholeInterface' : 'eth99', + 'IPv4addr' : '192.168.100.2', + 'piholeIPv6' : 'True', + 'piholeDNS1' : '4.2.2.1', + 'piholeDNS2' : '4.2.2.2' + } + setup_fake_vars = 'cat < /etc/pihole/setupVars.conf\n' + for k,v in vars.iteritems(): + setup_fake_vars += "{}={}\n".format(k, v) + setup_fake_vars += "EOF\n" + Pihole.run(setup_fake_vars).stdout + #print Pihole.run('cat /etc/pihole/setupVars.conf').stdout + + script = '/opt/pihole/stub_basic-install.sh pihole' + + cmd = 'bash -c "\n' + cmd += 'source {};\n'.format(script) + cmd += 'env > /tmp/saved_env"\n' + cmd += '"\n' + stubbed_script = Pihole.run(cmd) + env = Pihole.run('cat /tmp/saved_env').stdout + print stubbed_script.stdout + + assert "::: Importing previous variables for upgrade" in stubbed_script.stdout + for k,v in vars.iteritems(): + assert "{}={}".format(k, v) in stubbed_script.stdout + assert "{}={}".format(k, v) in env