-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
55 lines (45 loc) · 1.33 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
50
51
52
53
54
55
import pytest
import subprocess
import os
import testinfra
import logging
import docker
import json
DOCKER_IMAGE_NAME = 'eeacms/hello:latest'
logger = logging.getLogger('test_configuration')
logger.setLevel(logging.DEBUG)
# scope='session' uses the same container for all the tests;
# scope='function' uses a new container per test function.
# @pytest.fixture(scope='class')
def host(request):
# run a container
print("Starting Docker container...")
docker_id = subprocess.check_output(
[
'docker',
'run',
'-d',
DOCKER_IMAGE_NAME,
]
).decode().strip()
# return a testinfra connection to the container
print(f"Docker ID: ${docker_id}")
host = testinfra.get_host("docker://" + docker_id)
request.cls.host = host
yield host
# at the end of the test suite, destroy the container
print(f"Removing container: ${docker_id}")
subprocess.check_call(['docker', 'rm', '-f', docker_id])
@pytest.fixture(scope='class')
def host(request):
client = docker.from_env()
container = client.containers.run(
DOCKER_IMAGE_NAME,
detach=True,
remove=True,
)
print(f"container.id: ${container.id}")
host = testinfra.get_host("docker://" + container.id)
request.cls.host = host
yield host
container.stop()