Merge pull request #33 from diginc/dev

fixed problem with nginx admin, added test!
This commit is contained in:
Adam Hill 2016-08-28 02:06:53 -05:00 committed by GitHub
commit b4a929f050
6 changed files with 132 additions and 48 deletions

@ -1 +1 @@
Subproject commit 246599a0ba46f2f63659ba231341b03472ecf9ea
Subproject commit fa5f2fcaf000a9343985dffc9e7382ae1c99b85b

View File

@ -1 +1 @@
vDev
v1.4

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

@ -15,9 +15,8 @@ http {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.nginx-debian.html;
error_page 404 /pihole/index.html;
index index.php pihole/index.html;
error_page 404 =200 /pihole/index.html;
location ~ ^/admin/ {
add_header X-Pi-hole "The Pi-hole Web interface is working!";

View File

@ -1,53 +1,92 @@
import pytest
import testinfra
# Use testinfra to get a handy function to run commands locally
DEBUG = []
check_output = testinfra.get_backend(
"local://"
).get_module("Command").check_output
@pytest.fixture
def TestinfraBackend(request):
docker_run = "docker run -d {}".format(request.param)
print docker_run
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)
check_output("docker exec %s sed -i 's/^gravity_spinup/#donotcurl/g' /usr/local/bin/gravity.sh", docker_id)
def teardown():
check_output("docker rm -f %s", docker_id)
request.addfinalizer(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)
def pytest_generate_tests(metafunc):
if "TestinfraBackend" in metafunc.fixturenames:
@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)
mark_args = getattr(metafunc.function, "docker_args", None)
docker_args = []
if mark_args is not None:
docker_args = docker_args + list(mark_args.args)
@pytest.fixture()
def args(request):
return '-e ServerIP="192.168.100.2"'
mark_images = getattr(metafunc.function, "docker_images", None)
images = ['diginc/pi-hole:alpine', 'diginc/pi-hole:debian']
if mark_images is not None:
images = mark_images.args
@pytest.fixture(params=['alpine', 'debian'])
def tag(request):
return request.param
mark_cmd = getattr(metafunc.function, "docker_cmd", None)
command = 'tail -f /dev/null'
if mark_cmd is not None:
command = " ".join(mark_cmd.args)
@pytest.fixture
@pytest.mark.parametrize('tag,webserver', [ ( 'alpine', 'nginx' ), ( 'debian', 'lighttpd' ) ])
def webserver(request, tag):
return webserver
docker_run_args = []
for img in images:
docker_run_args.append('{} {} {}'.format(" ".join(docker_args),
img, command))
if getattr(metafunc.function, "persistent", None) is not None:
scope = "session"
else:
scope = "function"
@pytest.fixture()
def image(request, tag):
return 'diginc/pi-hole:{}'.format(tag)
metafunc.parametrize(
"TestinfraBackend", docker_run_args, indirect=True, scope=scope)
@pytest.fixture()
def cmd(request):
return '/start.sh'
@pytest.fixture(scope='session')
def persist_args(request):
return '-e ServerIP="192.168.100.2"'
@pytest.fixture(scope='session', params=['alpine', 'debian'])
def persist_tag(request):
return request.param
@pytest.fixture(scope='session')
def persist_webserver(request, persist_tag):
web_dict = { 'alpine': 'nginx', 'debian': 'lighttpd' }
return web_dict[persist_tag]
@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():
"""
Run a slow check, check if the state is correct for `timeout` seconds.
"""
import time
def slow(check, timeout=5):
timeout_at = time.time() + timeout
while True:
try:
assert check()
except AssertionError, e:
if time.time() < timeout_at:
time.sleep(1)
else:
raise e
else:
return
return slow

View File

@ -1,12 +1,58 @@
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 '''
def test_ServerIP_missing_env_triggers_error(Command):
start = Command.run('/start.sh')
def test_pihole_default_run_command(Docker):
expected_proc = '/sbin/tini -- /start.sh'
pgrep = 'pgrep -f "{}" | wc -l || echo 0'.format(expected_proc)
find_proc = Docker.run(pgrep).stdout
if int(find_proc) < 1:
print Docker.run('ps -ef')
print "{} : {}".format(pgrep, find_proc)
assert False, '{}: Couldn\'t find proc {}'.format(tag, expected_proc)
@pytest.mark.parametrize('args', [ '' ])
@pytest.mark.parametrize('cmd', [ 'tail -f /dev/null' ])
def test_ServerIP_missing_triggers_start_error(Docker):
''' When args to docker are empty start.sh exits saying ServerIP is required '''
start = Docker.run('/start.sh')
error_msg = "ERROR: To function correctly you must pass an environment variables of 'ServerIP' into the docker container"
assert start.rc == 1
assert error_msg in start.stdout
@pytest.mark.docker_args('-e ServerIP="192.168.1.2"')
@pytest.mark.docker_cmd('/start.sh')
def test_ServerIP_allows_normal_startup(Command):
assert Command.run('pgrep -f /start.sh | wc') != 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', [ '/', '/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)
http_rc = RunningPiHole.run(command)
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', '/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
@pytest.mark.parametrize('url', [ '/admin/', '/admin/index.php' ] )
def test_admin_requests_load_as_expected(RunningPiHole, url):
command = 'curl -s -o /tmp/curled_file -w "%{{http_code}}" http://127.0.0.1{}'.format(url)
http_rc = RunningPiHole.run(command)
assert int(http_rc.stdout) == 200
assert RunningPiHole.run('wc -l /tmp/curled_file ') > 10
assert RunningPiHole.run('grep -q "Content-Security-Policy" /tmp/curled_file ').rc == 0
assert RunningPiHole.run('grep -q "js/pihole/footer.js" /tmp/curled_file ').rc == 0