-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathdocker_utils.py
More file actions
43 lines (35 loc) · 1.48 KB
/
docker_utils.py
File metadata and controls
43 lines (35 loc) · 1.48 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
"""Provide utility functions related to the use of Docker during integration tests."""
from tests.utils.classes import ExternalAction
from tests.utils.logging_utils import format_action_failure_msg
from tests.utils.utils import get_binary_path
def list_running_services_in_compose_project(project_name: str) -> list[str]:
"""
Lists running Docker services that belong to the given Docker Compose project.
:param project_name:
:return: List of the running services that belong to the specified Docker Compose project.
:raise RuntimeError: if `docker compose ps` returns a non-zero exit code.
"""
docker_bin = get_binary_path("docker")
# fmt: off
compose_ps_cmd = [
docker_bin,
"compose",
"--project-name", project_name,
"ps",
"--format", "{{.Service}}",
]
# fmt: on
compose_ps_action = ExternalAction.from_cmd(compose_ps_cmd)
if compose_ps_action.completed_proc.returncode != 0:
err_msg = format_action_failure_msg(
"`docker compose ps` failed with exit code"
f" {compose_ps_action.completed_proc.returncode} for project `{project_name}`.",
compose_ps_action,
)
raise RuntimeError(err_msg)
service_names: list[str] = []
for line in compose_ps_action.completed_proc.stdout.splitlines():
service_name_candidate = line.strip()
if service_name_candidate:
service_names.append(service_name_candidate)
return service_names