Skip to content

Commit 0294d0c

Browse files
committed
docker-compose added
1 parent dae61ea commit 0294d0c

File tree

6 files changed

+246
-69
lines changed

6 files changed

+246
-69
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.

docs/compose.rst

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Docker compose support
2+
======================
3+
4+
Allows to spin up services configured via docker-compose.yml
5+
6+
Example docker-compose.yml for grid
7+
-----------------------------------
8+
9+
::
10+
11+
hub:
12+
image: selenium/hub
13+
ports:
14+
- "4444:4444"
15+
firefox:
16+
image: selenium/node-firefox
17+
links:
18+
- hub
19+
expose:
20+
- "5555"
21+
chrome:
22+
image: selenium/node-chrome
23+
links:
24+
- hub
25+
expose:
26+
- "5555"
27+
28+
Code
29+
----
30+
31+
::
32+
33+
compose = DockerCompose("/home/project")
34+
with compose:
35+
host = compose.get_service_host("hub", 4444)
36+
port = compose.get_service_port("hub", 4444)
37+
driver = webdriver.Remote(
38+
command_executor=("http://{}:{}/wd/hub".format(host,port)),
39+
desired_capabilities=CHROME)
40+
driver.get("http://automation-remarks.com")
41+

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ Usage modes
4444
Database containers <database>
4545
Selenium containers <selenium>
4646
Generic containers <generic>
47-
47+
Docker Compose <compose>

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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import os
2+
3+
import pytest
4+
5+
from testcontainers.compose import DockerCompose
6+
from testcontainers.core.exceptions import NoSuchPortExposed
7+
8+
9+
def test_can_spawn_service_via_compose():
10+
compose = DockerCompose(os.path.dirname(__file__))
11+
12+
try:
13+
compose.start()
14+
host = compose.get_service_host("hub", 4444)
15+
port = compose.get_service_port("hub", 4444)
16+
assert host == "0.0.0.0"
17+
assert port == "4444"
18+
finally:
19+
compose.stop()
20+
21+
22+
def test_can_throw_exception_if_no_port_exposed():
23+
compose = DockerCompose(os.path.dirname(__file__))
24+
25+
compose.start()
26+
with pytest.raises(NoSuchPortExposed):
27+
compose.get_service_host("hub", 5555)
28+
29+
compose.stop()

0 commit comments

Comments
 (0)