2016-07-16 23:00:20 +02:00
|
|
|
import pytest
|
|
|
|
import testinfra
|
|
|
|
|
2016-10-21 05:36:55 +02:00
|
|
|
WEB_SERVER = { 'alpine': 'nginx', 'debian': 'lighttpd' }
|
2016-08-28 07:43:21 +02:00
|
|
|
|
|
|
|
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?"
|
2016-09-08 06:07:38 +02:00
|
|
|
if 'diginc/pi-hole' in image:
|
|
|
|
args += " -e PYTEST=\"True\""
|
|
|
|
docker_run = "docker run -d {} {} {}".format(args, image, cmd)
|
2016-08-28 07:43:21 +02:00
|
|
|
docker_id = check_output(docker_run)
|
|
|
|
|
|
|
|
def teardown():
|
2016-09-08 06:07:38 +02:00
|
|
|
check_output("docker rm -f %s", docker_id)
|
2016-08-28 07:43:21 +02:00
|
|
|
request.addfinalizer(teardown)
|
|
|
|
|
2016-09-08 06:07:38 +02:00
|
|
|
docker_container = testinfra.get_backend("docker://" + docker_id)
|
|
|
|
docker_container.id = docker_id
|
|
|
|
return docker_container
|
2016-08-28 07:43:21 +02:00
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def Docker(request, args, image, cmd):
|
|
|
|
''' One-off Docker container run '''
|
|
|
|
return DockerGeneric(request, args, image, cmd)
|
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
2016-09-08 06:07:38 +02:00
|
|
|
def DockerPersist(request, persist_args, persist_image, persist_cmd, Dig):
|
2016-08-28 07:43:21 +02:00
|
|
|
''' Persistent Docker container for multiple tests '''
|
2016-09-08 06:07:38 +02:00
|
|
|
persistent_container = DockerGeneric(request, persist_args, persist_image, persist_cmd)
|
|
|
|
''' attach a dig conatiner for lookups '''
|
|
|
|
persistent_container.dig = Dig(persistent_container.id)
|
|
|
|
return persistent_container
|
2016-08-28 07:43:21 +02:00
|
|
|
|
2016-08-03 05:29:52 +02:00
|
|
|
@pytest.fixture()
|
2016-08-28 07:43:21 +02:00
|
|
|
def args(request):
|
2016-08-03 05:29:52 +02:00
|
|
|
return '-e ServerIP="192.168.100.2"'
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-08-03 05:29:52 +02:00
|
|
|
@pytest.fixture(params=['alpine', 'debian'])
|
|
|
|
def tag(request):
|
|
|
|
return request.param
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-08-28 07:43:21 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def webserver(request, tag):
|
2016-10-21 05:36:55 +02:00
|
|
|
return WEB_SERVER[tag]
|
2016-08-28 07:43:21 +02:00
|
|
|
|
2016-08-03 05:29:52 +02:00
|
|
|
@pytest.fixture()
|
2016-08-28 07:43:21 +02:00
|
|
|
def image(request, tag):
|
2016-08-03 05:29:52 +02:00
|
|
|
return 'diginc/pi-hole:{}'.format(tag)
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2016-08-28 07:43:21 +02:00
|
|
|
def cmd(request):
|
2016-08-03 05:29:52 +02:00
|
|
|
return '/start.sh'
|
|
|
|
|
2016-08-28 07:43:21 +02:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def persist_args(request):
|
|
|
|
return '-e ServerIP="192.168.100.2"'
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-08-28 07:43:21 +02:00
|
|
|
@pytest.fixture(scope='session', params=['alpine', 'debian'])
|
|
|
|
def persist_tag(request):
|
|
|
|
return request.param
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-08-28 07:43:21 +02:00
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def persist_webserver(request, persist_tag):
|
2016-10-21 05:36:55 +02:00
|
|
|
return WEB_SERVER[persist_tag]
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-08-28 07:43:21 +02:00
|
|
|
@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'
|
2016-07-16 23:00:20 +02:00
|
|
|
|
2016-08-03 05:29:52 +02:00
|
|
|
@pytest.fixture
|
|
|
|
def Slow():
|
|
|
|
"""
|
|
|
|
Run a slow check, check if the state is correct for `timeout` seconds.
|
|
|
|
"""
|
|
|
|
import time
|
2016-08-28 08:11:55 +02:00
|
|
|
def slow(check, timeout=5):
|
2016-08-03 05:29:52 +02:00
|
|
|
timeout_at = time.time() + timeout
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
assert check()
|
|
|
|
except AssertionError, e:
|
2016-08-28 07:43:21 +02:00
|
|
|
if time.time() < timeout_at:
|
2016-08-03 05:29:52 +02:00
|
|
|
time.sleep(1)
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
else:
|
|
|
|
return
|
|
|
|
return slow
|
2016-09-08 06:07:38 +02:00
|
|
|
|
|
|
|
@pytest.fixture(scope='session')
|
|
|
|
def Dig(request):
|
|
|
|
''' separate container to link to pi-hole and perform lookups '''
|
|
|
|
''' a docker pull is faster than running an install of dnsutils '''
|
|
|
|
def dig(docker_id):
|
2016-10-21 05:36:55 +02:00
|
|
|
args = '--link {}:test_pihole'.format(docker_id)
|
2016-09-08 06:07:38 +02:00
|
|
|
image = 'azukiapp/dig'
|
|
|
|
cmd = 'tail -f /dev/null'
|
|
|
|
dig_container = DockerGeneric(request, args, image, cmd)
|
|
|
|
return dig_container
|
|
|
|
return dig
|
|
|
|
|