Skip to content

Commit 3c0ca82

Browse files
committed
docker-compose support added
1 parent dae61ea commit 3c0ca82

File tree

4 files changed

+191
-68
lines changed

4 files changed

+191
-68
lines changed

Pipfile.lock

Lines changed: 133 additions & 68 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

testcontainers/compose.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import subprocess
2+
3+
import blindspin
4+
5+
from testcontainers.core.exceptions import NoSuchPortExposed
6+
7+
8+
class DockerCompose(object):
9+
def __init__(self, filepath):
10+
self.filepath = filepath
11+
12+
def __enter__(self):
13+
return self.start()
14+
15+
def __exit__(self, exc_type, exc_val, exc_tb):
16+
self.stop()
17+
18+
def start(self):
19+
with blindspin.spinner():
20+
subprocess.call(["docker-compose", "up", "-d"], cwd=self.filepath)
21+
22+
def stop(self):
23+
with blindspin.spinner():
24+
subprocess.call(["docker-compose", "down"], cwd=self.filepath)
25+
26+
def get_service_port(self, service_name, port):
27+
return self._get_service_info(service_name, port)[1]
28+
29+
def get_service_host(self, service_name, port):
30+
return self._get_service_info(service_name, port)[0]
31+
32+
def _get_service_info(self, service, port):
33+
output = subprocess.check_output(["docker-compose", "port", service, str(port)],
34+
cwd=self.filepath).decode("utf-8")
35+
result = str(output).rstrip().split(":")
36+
if len(result) == 1:
37+
raise NoSuchPortExposed("Port {} was not exposed for service {}".format(port, service))
38+
return result

testcontainers/core/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,7 @@ class TimeoutException(Exception):
2222

2323
class NoSuchBrowserException(Exception):
2424
pass
25+
26+
27+
class NoSuchPortExposed(Exception):
28+
pass

tests/test_docker_compose.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import os
2+
3+
from testcontainers.compose import DockerCompose
4+
5+
6+
def test_can_spawn_service_via_compose():
7+
compose = DockerCompose(os.path.dirname(__file__))
8+
9+
try:
10+
compose.start()
11+
host = compose.get_service_host("hub", 4444)
12+
port = compose.get_service_port("hub", 4444)
13+
assert host == "0.0.0.0"
14+
assert port == "4444"
15+
finally:
16+
compose.stop()

0 commit comments

Comments
 (0)