-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathtest_docker_services.py
177 lines (144 loc) · 5.54 KB
/
test_docker_services.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import subprocess
from unittest import mock
import pytest
from pytest_docker.plugin import (
DockerComposeExecutor,
Services,
ServiceTimeoutError,
get_docker_services,
get_cleanup_command,
get_setup_command,
)
def test_docker_services():
"""Automatic teardown of all services."""
with mock.patch("subprocess.check_output") as check_output:
check_output.side_effect = [b"", b"0.0.0.0:32770", b""]
check_output.returncode = 0
assert check_output.call_count == 0
# The fixture is a context-manager.
with get_docker_services(
"docker-compose",
"docker-compose.yml",
docker_compose_project_name="pytest123",
docker_setup=get_setup_command(),
docker_cleanup=get_cleanup_command(),
) as services:
assert isinstance(services, Services)
assert check_output.call_count == 1
# Can request port for services.
port = services.port_for("abc", 123)
assert port == 32770
assert check_output.call_count == 2
# 2nd request for same service should hit the cache.
port = services.port_for("abc", 123)
assert port == 32770
assert check_output.call_count == 2
assert check_output.call_count == 3
# Both should have been called.
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" up --build -d',
stderr=subprocess.STDOUT,
shell=True,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" port abc 123',
stderr=subprocess.STDOUT,
shell=True,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" down -v',
stderr=subprocess.STDOUT,
shell=True,
),
]
def test_docker_services_unused_port():
"""Complain loudly when the requested port is not used by the service."""
with mock.patch("subprocess.check_output") as check_output:
check_output.side_effect = [b"", b"", b""]
check_output.returncode = 0
assert check_output.call_count == 0
# The fixture is a context-manager.
with get_docker_services(
"docker-compose",
"docker-compose.yml",
docker_compose_project_name="pytest123",
docker_setup=get_setup_command(),
docker_cleanup=get_cleanup_command(),
) as services:
assert isinstance(services, Services)
assert check_output.call_count == 1
# Can request port for services.
with pytest.raises(ValueError) as exc:
print(services.port_for("abc", 123))
assert str(exc.value) == (
'Could not detect port for "%s:%d".' % ("abc", 123)
)
assert check_output.call_count == 2
assert check_output.call_count == 3
# Both should have been called.
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" ' "up --build -d",
shell=True,
stderr=subprocess.STDOUT,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" ' "port abc 123",
shell=True,
stderr=subprocess.STDOUT,
),
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" down -v',
shell=True,
stderr=subprocess.STDOUT,
),
]
def test_docker_services_failure():
"""Propagate failure to start service."""
with mock.patch("subprocess.check_output") as check_output:
check_output.side_effect = [
subprocess.CalledProcessError(1, "the command", b"the output")
]
check_output.returncode = 1
# The fixture is a context-manager.
with pytest.raises(Exception) as exc:
with get_docker_services(
"docker-compose",
"docker-compose.yml",
docker_compose_project_name="pytest123",
docker_setup=get_setup_command(),
docker_cleanup=get_cleanup_command(),
):
pass
# Failure propagates with improved diagnoatics.
assert str(exc.value) == (
'Command {} returned {}: """{}""".'.format("the command", 1, "the output")
)
assert check_output.call_count == 1
# Tear down code should not be called.
assert check_output.call_args_list == [
mock.call(
'docker-compose -f "docker-compose.yml" -p "pytest123" ' "up --build -d",
shell=True,
stderr=subprocess.STDOUT,
)
]
def test_wait_until_responsive_timeout():
clock = mock.MagicMock()
clock.side_effect = [0.0, 1.0, 2.0, 3.0]
with mock.patch("time.sleep") as sleep:
docker_compose = DockerComposeExecutor(
compose_command="docker-compose",
compose_files="docker-compose.yml",
compose_project_name="pytest123",
)
services = Services(docker_compose)
with pytest.raises(ServiceTimeoutError) as exc:
print(
services.wait_until_responsive(
check=lambda: False, timeout=3.0, pause=1.0, clock=clock
)
)
assert sleep.call_args_list == [mock.call(1.0), mock.call(1.0), mock.call(1.0)]
assert str(exc.value) == ("Timeout reached while waiting on service!")