-
Notifications
You must be signed in to change notification settings - Fork 493
/
conftest.py
49 lines (37 loc) · 1.39 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import os, time
import pytest, testinfra
conftest_path = os.path.dirname(os.path.realpath(__file__))
test_path = os.path.join(conftest_path, 'tests')
test_conf_path = os.path.join(test_path, 'conf')
test_conf_app_path = os.path.join(test_conf_path, 'app')
# Use testinfra to get a handy function to run commands locally
check_output = testinfra.get_backend(
"local://"
).get_module("Command").check_output
@pytest.fixture
def TestinfraBackend(request):
# Override the TestinfraBackend fixture,
# all testinfra fixtures (i.e. modules) depend on it.
docker_command = ''
docker_image = request.param
docker_sleeptime = 5
# Check for custom command in docker image name
if "#" in docker_image:
docker_image, docker_command = docker_image.split("#", 2)
if docker_command == "loop":
docker_command = 'tail -f /dev/null'
docker_sleeptime = 0
docker_id = check_output(
"docker run -d -v \"%s:/app:ro\" %s " + docker_command,
test_conf_app_path,
docker_image
)
def teardown():
check_output("docker rm -f %s", docker_id)
# Destroy the container at the end of the fixture life
request.addfinalizer(teardown)
# wait for getting the image up
if docker_sleeptime:
time.sleep(docker_sleeptime)
# Return a dynamic created backend
return testinfra.get_backend("docker://" + docker_id)