-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_utils.py
91 lines (74 loc) · 2.79 KB
/
test_utils.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import _thread
import docker
import os
import socket
from http.server import HTTPServer, SimpleHTTPRequestHandler
class TestFixtureServer(object):
def __init__(self):
self.port = 9999
self.ip = self.get_python_server_ip()
def get_python_server_ip(self):
# https://stackoverflow.com/a/166589
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
host_ip = s.getsockname()[0]
s.close()
return host_ip
def _start_server(self):
server = HTTPServer((self.ip, self.port), SimpleHTTPRequestHandler)
server.serve_forever()
def start_server_in_background(self):
# start the server in a background thread
_thread.start_new_thread(self._start_server, ())
class TestContainerRunner(object):
def __init__(self):
self.client = docker.from_env()
self.image_name = "docker_igv_js"
self.repository = "gehlenborglab/docker_igv_js"
self.containers = []
self.test_fixture_server = TestFixtureServer()
self.test_fixture_server.start_server_in_background()
self._pull_image()
self._build_image()
def __enter__(self):
self.run()
def __exit__(self, *args):
if not os.environ.get("CONTINUOUS_INTEGRATION"):
self.cleanup_containers()
def _pull_image(self):
self.client.images.pull(self.repository + ":latest")
def _build_image(self):
print("Building image: {}".format(self.image_name))
self.client.images.build(
path="context",
tag=self.image_name,
rm=True,
forcerm=True,
cache_from=[self.repository]
)
def run(self):
print("Starting TestContainerRunner...")
for test_fixture in os.listdir("input_fixtures"):
print("Creating container: {}".format(test_fixture))
container = self.client.containers.run(
self.image_name,
detach=True,
name=test_fixture,
environment={
"INPUT_JSON_URL":
"http://{}:{}/input_fixtures/{}/input.json".format(
self.test_fixture_server.ip,
self.test_fixture_server.port,
test_fixture
)
},
ports={"80/tcp": None},
publish_all_ports=True,
extra_hosts={socket.gethostname(): self.test_fixture_server.ip}
)
self.containers.append(container)
def cleanup_containers(self):
print("Cleaning up TestContainerRunner containers...")
print(self.containers)
for container in self.containers:
container.remove(force=True, v=True)