Tests working again, along with persistent tests with an actual running pi-hole container

This commit is contained in:
diginc 2016-08-28 00:43:21 -05:00
parent 782fb27559
commit 3b74e8d3e3
13 changed files with 107 additions and 43 deletions

2
.gitmodules vendored
View File

@ -3,4 +3,4 @@
url = https://github.com/pi-hole/pi-hole.git
[submodule "AdminLTE"]
path = AdminLTE
url = https://github.com/diginc/AdminLTE.git
url = https://github.com/pi-hole/AdminLTE.git

View File

@ -7,4 +7,4 @@ python:
install:
- pip install -r requirements.txt
script: py.test
script: py.test -v

@ -1 +1 @@
Subproject commit 2e02fc412ab95a55b835aa5777283ad59a4e3458
Subproject commit fa5f2fcaf000a9343985dffc9e7382ae1c99b85b

View File

@ -1 +1 @@
vDev
v1.4

View File

@ -20,7 +20,7 @@ COPY ./pi-hole/advanced/Scripts/* /usr/local/bin/
RUN mkdir -p /opt/ && ln -s /usr/local/bin /opt/pihole
COPY ./pi-hole/advanced/dnsmasq.conf.original /etc/dnsmasq.conf
COPY ./pi-hole/advanced/01-pihole.conf /etc/dnsmasq.d/
COPY ./pi-hole/advanced/index.html /var/www/html/pihole/index.html
COPY ./pi-hole/advanced/index* /var/www/html/pihole/
COPY ./pi-hole/advanced/pihole.sudo /etc/sudoers.d/pihole
COPY ./AdminLTE /var/www/html/admin
COPY ./AdminLTE_version.txt /etc/

View File

@ -19,7 +19,7 @@ else
echo "::: sudo will be used."
# Check if it is actually installed
# If it isn't, exit because the install cannot complete
if [[ $(dpkg-query -s sudo) ]];then
if [ -x "$(command -v sudo)" ];then
export SUDO="sudo"
else
echo "::: Please install sudo or run this script as root."
@ -152,7 +152,7 @@ function gravity_transport() {
fi
# Silently curl url
curl -s $cmd_ext $heisenbergCompensator -A "$agent" $url > $patternBuffer
curl -s -L $cmd_ext $heisenbergCompensator -A "$agent" $url > $patternBuffer
# Check for list updates
gravity_patternCheck "$patternBuffer"
# Cleanup
@ -181,7 +181,7 @@ function gravity_spinup() {
# to complete properly and reset the user agent when required
case "$domain" in
"adblock.mahakala.is")
agent='Mozilla/5.0 (X11; Linux x86_64; rv:30.0) Gecko/20100101 Firefox/30.0'
agent='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
cmd_ext="-e http://forum.xda-developers.com/"
;;

View File

@ -14,7 +14,7 @@ http {
server {
listen 80;
listen [::]:80;
root /var/www/html;
root /var/www/html/pihole;
index index.php index.html index.nginx-debian.html;
error_page 404 /pihole/index.html;

View File

@ -34,7 +34,9 @@ sed -i "s/@DNS2@/$DNS2/" /etc/dnsmasq.d/01-pihole.conf && \
dnsmasq --test -7 /etc/dnsmasq.d || exit 1
php-fpm -t || exit 1
nginx -t || exit 1
echo " :: All config checks passed, starting ..."
if [ -n "$PYTEST" ] ; then sed -i 's/^gravity_spinup/#donotcurl/g' `which gravity.sh`; fi;
gravity.sh
dnsmasq -7 /etc/dnsmasq.d
php-fpm

View File

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

View File

@ -26,7 +26,8 @@ RUN mkdir -p /opt/ && ln -s /usr/local/bin /opt/pihole
COPY ./pi-hole/advanced/lighttpd.conf.debian /etc/lighttpd/lighttpd.conf
COPY ./pi-hole/advanced/dnsmasq.conf.original /etc/dnsmasq.conf
COPY ./pi-hole/advanced/01-pihole.conf /etc/dnsmasq.d/
COPY ./pi-hole/advanced/index.html /var/www/html/pihole/index.html
COPY ./pi-hole/advanced/index* /var/www/html/pihole/
RUN rm /var/www/html/index.lighttpd.html
COPY ./pi-hole/advanced/pihole.sudo /etc/sudoers.d/pihole
COPY ./AdminLTE /var/www/html/admin
COPY ./AdminLTE_version.txt /etc/

2
debian/start.sh vendored
View File

@ -31,7 +31,9 @@ sed -i "s/@DNS2@/$DNS2/" /etc/dnsmasq.d/01-pihole.conf && \
dnsmasq --test -7 /etc/dnsmasq.d || exit 1
lighttpd -t -f /etc/lighttpd/lighttpd.conf || exit 1
echo " :: All config checks passed, starting ..."
if [ -n "$PYTEST" ] ; then sed -i 's/^gravity_spinup/#donotcurl/g' `which gravity.sh`; fi;
gravity.sh # dnsmasq start included
service lighttpd start

View File

@ -1,6 +1,34 @@
import pytest
import testinfra
DEBUG = []
check_output = testinfra.get_backend(
"local://"
).get_module("Command").check_output
def DockerGeneric(request, args, image, cmd):
assert 'docker' in check_output('id'), "Are you in the docker group?"
docker_run = "docker run -d -e PYTEST=\"True\" {} {} {}".format(args, image, cmd)
docker_id = check_output(docker_run)
def teardown():
check_output("docker stop %s", docker_id)
check_output("docker rm %s", docker_id)
request.addfinalizer(teardown)
return testinfra.get_backend("docker://" + docker_id)
@pytest.fixture
def Docker(request, args, image, cmd):
''' One-off Docker container run '''
return DockerGeneric(request, args, image, cmd)
@pytest.fixture(scope='session')
def DockerPersist(request, persist_args, persist_image, persist_cmd):
''' Persistent Docker container for multiple tests '''
return DockerGeneric(request, persist_args, persist_image, persist_cmd)
@pytest.fixture()
def args(request):
return '-e ServerIP="192.168.100.2"'
@ -9,6 +37,11 @@ def args(request):
def tag(request):
return request.param
@pytest.fixture
@pytest.mark.parametrize('tag,webserver', [ ( 'alpine', 'nginx' ), ( 'debian', 'lighttpd' ) ])
def webserver(request, tag):
return webserver
@pytest.fixture()
def image(request, tag):
return 'diginc/pi-hole:{}'.format(tag)
@ -17,22 +50,26 @@ def image(request, tag):
def cmd(request):
return '/start.sh'
DEBUG = []
@pytest.fixture(scope='session')
def persist_args(request):
return '-e ServerIP="192.168.100.2"'
@pytest.fixture()
def Docker(request, LocalCommand, args, image, cmd):
assert 'docker' in LocalCommand.check_output('id'), "Are you in the docker group?"
docker_run = "docker run -d {} {} {}".format(args, image, cmd)
if 'run' in DEBUG:
assert docker_run == 'docker run -d -e ServerIP="192.168.100.2" diginc/pi-hole:alpine /start.sh'
docker_id = LocalCommand.check_output(docker_run)
LocalCommand.check_output("docker exec %s sed -i 's/^gravity_spinup/#donotcurl/g' /usr/local/bin/gravity.sh", docker_id)
@pytest.fixture(scope='session', params=['alpine', 'debian'])
def persist_tag(request):
return request.param
def teardown():
LocalCommand.check_output("docker rm -f %s", docker_id)
request.addfinalizer(teardown)
@pytest.fixture(scope='session')
def persist_webserver(request, persist_tag):
web_dict = { 'alpine': 'nginx', 'debian': 'lighttpd' }
return web_dict[persist_tag]
return testinfra.get_backend("docker://" + docker_id)
@pytest.fixture(scope='session')
def persist_image(request, persist_tag):
return 'diginc/pi-hole:{}'.format(persist_tag)
@pytest.fixture(scope='session')
def persist_cmd(request):
return '/start.sh'
@pytest.fixture
def Slow():
@ -40,13 +77,13 @@ def Slow():
Run a slow check, check if the state is correct for `timeout` seconds.
"""
import time
def slow(check, timeout=30):
def slow(check, timeout=15):
timeout_at = time.time() + timeout
while True:
try:
assert check()
except AssertionError, e:
if timeout_at < time.time():
if time.time() < timeout_at:
time.sleep(1)
else:
raise e

View File

@ -1,4 +1,5 @@
import pytest
import time
''' conftest.py provides the defaults through fixtures '''
''' Note, testinfra builtins don't seem fully compatible with
docker containers (esp. alpine) stripped down nature '''
@ -21,12 +22,33 @@ def test_ServerIP_missing_triggers_start_error(Docker):
assert start.rc == 1
assert error_msg in start.stdout
@pytest.mark.parametrize('tag,webserver', [
( 'alpine', 'nginx' ),
( 'debian', 'lighttpd' )
])
def test_start_launches_dns_and_a_webserver(Docker, webserver, Slow):
''' after we wait for start to finish '''
import time
Socket = Docker.get_module("Socket")
Slow(lambda: Docker.run( 'ps -ef | grep -q "{}"'.format(webserver) ).rc == 0)
@pytest.fixture
def RunningPiHole(DockerPersist, Slow, persist_webserver):
''' Persist a docker and provide some parameterized data for re-use '''
Slow(lambda: DockerPersist.run( 'pgrep {}'.format(persist_webserver) ).rc == 0)
return DockerPersist
def test_indecies_are_present(RunningPiHole):
File = RunningPiHole.get_module('File')
File('/var/www/html/pihole/index.html').exists
File('/var/www/html/pihole/index.js').exists
@pytest.mark.parametrize('url', [ '/' ] )
#@pytest.mark.parametrize('url', [ '/', '/index.html', 'any.html' ] )
def test_html_index_requests_load_as_expected(RunningPiHole, url):
command = 'curl -s -o /tmp/curled_file -w "%{{http_code}}" http://127.0.0.1{}'.format(url)
print command
http_rc = RunningPiHole.run(command)
print RunningPiHole.run('ls -lat /tmp/curled_file').stdout
print RunningPiHole.run('cat /tmp/curled_file').stdout
assert RunningPiHole.run('md5sum /tmp/curled_file /var/www/html/pihole/index.html').rc == 0
assert int(http_rc.stdout) == 200
@pytest.mark.parametrize('url', [ '/index.js' ] )
#@pytest.mark.parametrize('url', [ '/index.js', '/any.js'] )
def test_javascript_requests_load_as_expected(RunningPiHole, url):
command = 'curl -s -o /tmp/curled_file -w "%{{http_code}}" http://127.0.0.1{}'.format(url)
print command
http_rc = RunningPiHole.run(command)
assert RunningPiHole.run('md5sum /tmp/curled_file /var/www/html/pihole/index.js').rc == 0
assert int(http_rc.stdout) == 200