forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompose.py
More file actions
59 lines (47 loc) · 1.93 KB
/
compose.py
File metadata and controls
59 lines (47 loc) · 1.93 KB
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
import subprocess
import blindspin
import requests
from testcontainers.core.waiting_utils import wait_container_is_ready
from testcontainers.core.exceptions import NoSuchPortExposed
class DockerCompose(object):
def __init__(
self,
filepath,
compose_file_name="docker-compose.yml",
pull=False):
self.filepath = filepath
self.compose_file_name = compose_file_name
self.pull = pull
def __enter__(self):
self.start()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()
def start(self):
with blindspin.spinner():
if self.pull:
subprocess.call(["docker-compose", "-f", self.compose_file_name, "pull"],
cwd=self.filepath)
subprocess.call(["docker-compose", "-f", self.compose_file_name, "up", "-d"],
cwd=self.filepath)
def stop(self):
with blindspin.spinner():
subprocess.call(["docker-compose", "-f", self.compose_file_name, "down", "-v"],
cwd=self.filepath)
def get_service_port(self, service_name, port):
return self._get_service_info(service_name, port)[1]
def get_service_host(self, service_name, port):
return self._get_service_info(service_name, port)[0]
def _get_service_info(self, service, port):
cmd_as_list = ["docker-compose", "port", service, str(port)]
output = subprocess.check_output(cmd_as_list,
cwd=self.filepath).decode("utf-8")
result = str(output).rstrip().split(":")
if len(result) == 1:
raise NoSuchPortExposed("Port {} was not exposed for service {}"
.format(port, service))
return result
@wait_container_is_ready()
def wait_for(self, url):
requests.get(url)
return self