-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_dockerfile_a38x.py
50 lines (40 loc) · 1.18 KB
/
test_dockerfile_a38x.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
import os
import pytest
import subprocess
import testinfra
DOCKER_TAG = '3.8'
@pytest.fixture(scope='session')
def host(request):
"""Build Docker containers with Testinfra by overloading the host fixture."""
username = os.environ['DOCKER_USERNAME']
image_name = username + '/alpine:' + DOCKER_TAG
# build local ./Dockerfile
subprocess.check_call(
[
'docker',
'build',
'-f',
'Dockerfile',
'-t',
image_name,
'--build-arg',
'TAG=' + DOCKER_TAG,
'.',
]
)
# run a container
docker_id = (
subprocess.check_output(
['docker', 'run', '-d', image_name, 'tail', '-f', '/dev/null']
)
.decode()
.strip()
)
# return a testinfra connection to the container
yield testinfra.get_host("docker://" + docker_id)
# at the end of the test suite, destroy the container
subprocess.check_call(['docker', 'rm', '-f', docker_id])
def test_myimage(host):
"""Test the built Docker container."""
# 'host' now binds to the container
assert host.check_output('cat /etc/alpine-release') == '3.8.2'