-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathtest_docker_client.py
More file actions
357 lines (275 loc) · 13.5 KB
/
test_docker_client.py
File metadata and controls
357 lines (275 loc) · 13.5 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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import os
import json
from collections import namedtuple
from typing import Any
from unittest import mock
from unittest.mock import MagicMock, patch
import docker
import pytest
from testcontainers.core.config import testcontainers_config as c, ConnectionMode
from testcontainers.core.container import DockerContainer
from testcontainers.core.docker_client import DockerClient, is_ssh_docker_host
from testcontainers.core.auth import parse_docker_auth_config
from testcontainers.core.image import DockerImage
from testcontainers.core import utils
from pytest import mark
from docker.models.networks import Network
def _expected_from_env_kwargs(**kwargs: Any) -> dict[str, Any]:
"""Build the kwargs we expect ``docker.from_env`` to be called with.
When DOCKER_HOST is SSH-based, ``use_ssh_client=True`` is added automatically.
"""
if is_ssh_docker_host():
kwargs.setdefault("use_ssh_client", True)
return kwargs
def test_docker_client_from_env():
test_kwargs = {"test_kw": "test_value"}
mock_docker = MagicMock(spec=docker)
with patch("testcontainers.core.docker_client.docker", mock_docker):
DockerClient(**test_kwargs)
mock_docker.from_env.assert_called_with(**_expected_from_env_kwargs(**test_kwargs))
def test_docker_client_login_no_login():
with patch.dict(os.environ, {}, clear=True):
mock_docker = MagicMock(spec=docker)
with patch("testcontainers.core.docker_client.docker", mock_docker):
DockerClient()
mock_docker.from_env.return_value.login.assert_not_called()
def test_docker_client_login():
mock_docker = MagicMock(spec=docker)
mock_parse_docker_auth_config = MagicMock(spec=parse_docker_auth_config)
mock_utils = MagicMock()
mock_utils.parse_docker_auth_config = mock_parse_docker_auth_config
Auth = namedtuple("Auth", "value")
mock_parse_docker_auth_config.return_value = [Auth("test")]
with (
mock.patch.object(c, "_docker_auth_config", "test"),
patch("testcontainers.core.docker_client.docker", mock_docker),
patch("testcontainers.core.docker_client.parse_docker_auth_config", mock_parse_docker_auth_config),
):
DockerClient()
mock_docker.from_env.return_value.login.assert_called_with(**{"value": "test"})
def test_docker_client_login_empty_get_docker_auth_config():
mock_docker = MagicMock(spec=docker)
mock_get_docker_auth_config = MagicMock()
mock_get_docker_auth_config.return_value = None
with (
mock.patch.object(c, "_docker_auth_config", "test"),
patch("testcontainers.core.docker_client.docker", mock_docker),
patch("testcontainers.core.docker_client.get_docker_auth_config", mock_get_docker_auth_config),
):
DockerClient()
mock_docker.from_env.return_value.login.assert_not_called()
def test_docker_client_login_empty_parse_docker_auth_config():
mock_docker = MagicMock(spec=docker)
mock_parse_docker_auth_config = MagicMock(spec=parse_docker_auth_config)
mock_utils = MagicMock()
mock_utils.parse_docker_auth_config = mock_parse_docker_auth_config
mock_parse_docker_auth_config.return_value = None
with (
mock.patch.object(c, "_docker_auth_config", "test"),
patch("testcontainers.core.docker_client.docker", mock_docker),
patch("testcontainers.core.docker_client.parse_docker_auth_config", mock_parse_docker_auth_config),
):
DockerClient()
mock_docker.from_env.return_value.login.assert_not_called()
# This is used to make sure we don't fail (nor try to login) when we have unsupported auth config
@mark.parametrize("auth_config_sample", [{"credHelpers": {"test": "login"}}, {"credsStore": "login"}])
def test_docker_client_login_unsupported_auth_config(auth_config_sample):
mock_docker = MagicMock(spec=docker)
mock_get_docker_auth_config = MagicMock()
mock_get_docker_auth_config.return_value = json.dumps(auth_config_sample)
with (
mock.patch.object(c, "_docker_auth_config", "test"),
patch("testcontainers.core.docker_client.docker", mock_docker),
patch("testcontainers.core.docker_client.get_docker_auth_config", mock_get_docker_auth_config),
):
DockerClient()
mock_docker.from_env.return_value.login.assert_not_called()
def test_container_docker_client_kw():
test_kwargs = {"test_kw": "test_value"}
mock_docker = MagicMock(spec=docker)
with patch("testcontainers.core.docker_client.docker", mock_docker):
DockerContainer(image="", docker_client_kw=test_kwargs)
mock_docker.from_env.assert_called_with(**_expected_from_env_kwargs(**test_kwargs))
def test_image_docker_client_kw():
test_kwargs = {"test_kw": "test_value"}
mock_docker = MagicMock(spec=docker)
with patch("testcontainers.core.docker_client.docker", mock_docker):
DockerImage(name="", path="", docker_client_kw=test_kwargs)
mock_docker.from_env.assert_called_with(**_expected_from_env_kwargs(**test_kwargs))
def test_host_prefer_host_override(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(c, "tc_host_override", "my_docker_host")
assert DockerClient().host() == "my_docker_host"
@pytest.mark.parametrize(
"base_url, expected",
[
pytest.param("http://[-", "localhost", id="invalid_url"),
pytest.param("http+docker://localhost", "localhost", id="docker_socket"),
pytest.param("http://localnpipe", "localhost", id="docker_socket_windows"),
pytest.param("http://some_host", "some_host", id="other_host"),
pytest.param("unix://something", "1.2.3.4", id="inside_container_socket"),
],
)
def test_host(monkeypatch: pytest.MonkeyPatch, base_url: str, expected: str) -> None:
if is_ssh_docker_host():
pytest.skip("base_url parsing is not exercised under SSH (host() returns SSH hostname)")
client = DockerClient()
monkeypatch.setattr(client.client.api, "base_url", base_url)
monkeypatch.setattr(c, "tc_host_override", None)
# overwrite some utils in order to test all branches of host
monkeypatch.setattr(utils, "is_windows", lambda: True)
monkeypatch.setattr(utils, "inside_container", lambda: True)
monkeypatch.setattr(utils, "default_gateway_ip", lambda: "1.2.3.4")
assert client.host() == expected
def test_get_connection_mode_overwritten(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(c, "connection_mode_override", ConnectionMode.gateway_ip)
assert DockerClient().get_connection_mode() == ConnectionMode.gateway_ip
@pytest.mark.parametrize("host", ["localhost", "127.0.0.1", "::1"])
def test_get_connection_mode_localhost_inside_container(monkeypatch: pytest.MonkeyPatch, host: str) -> None:
"""
If docker host is localhost and we are inside a container prefer gateway_ip
"""
client = DockerClient()
monkeypatch.setattr(c, "connection_mode_override", None)
monkeypatch.setattr(client, "host", lambda: host)
monkeypatch.setattr(client, "find_host_network", lambda: None)
monkeypatch.setattr(utils, "inside_container", lambda: True)
assert client.get_connection_mode() == ConnectionMode.gateway_ip
def test_get_connection_mode_remote_docker_host(monkeypatch: pytest.MonkeyPatch) -> None:
"""
Use docker_host inside container if remote docker host is given
"""
client = DockerClient()
monkeypatch.setattr(c, "connection_mode_override", None)
monkeypatch.setattr(client, "host", lambda: "remote.docker.host")
monkeypatch.setattr(client, "find_host_network", lambda: None)
monkeypatch.setattr(utils, "inside_container", lambda: True)
assert client.get_connection_mode() == ConnectionMode.docker_host
def test_get_connection_mode_dood(monkeypatch: pytest.MonkeyPatch) -> None:
"""
For docker out of docker (docker socket mount), we expect to be able
to find a host network.
In this case we should use the bridge ip as we can't expect
that either docker_host nor gateway_ip of the container are actually
reachable from within this network.
This is the case for instance if using Gitlab CIs `FF_NETWORK_PER_BUILD` flag
"""
client = DockerClient()
monkeypatch.setattr(c, "connection_mode_override", None)
monkeypatch.setattr(client, "host", lambda: "localhost")
monkeypatch.setattr(client, "find_host_network", lambda: "new_bridge_network")
monkeypatch.setattr(utils, "inside_container", lambda: True)
assert client.get_connection_mode() == ConnectionMode.bridge_ip
def test_find_host_network_invalid_url(monkeypatch: pytest.MonkeyPatch) -> None:
"""
If the hostname can't be resolved just return None
"""
client = DockerClient()
monkeypatch.setattr(client, "host", lambda: "this does not exists")
assert client.find_host_network() is None
def test_find_host_network_found_by_docker_host(monkeypatch: pytest.MonkeyPatch) -> None:
client = DockerClient()
monkeypatch.setattr(client, "host", lambda: "172.22.0.1")
networks = [
# a network without IPAM
{"Name": "host"},
# network with invalid subnet
{
"Name": "invalid",
"IPAM": {"Config": [{"Gateway": "172.22.0.1", "Subnet": "invalid subnet"}]},
},
{
"Attachable": False,
"ConfigFrom": {"Network": ""},
"ConfigOnly": False,
"Containers": {},
"Created": "2024-10-11T16:08:36.005642863Z",
"Driver": "bridge",
"EnableIPv6": False,
"Name": "runner-346da30e-2641-1-8365005",
"IPAM": {
"Config": [{"Gateway": "172.22.0.1", "Subnet": "172.22.0.0/16"}],
"Driver": "default",
"Options": None,
},
},
]
class FakeNetworks:
def list(self, filters: dict[str, str]) -> list[Network]:
assert filters == {"type": "custom"}
return [Network(network) for network in networks]
class FakeClient:
@property
def networks(self):
return FakeNetworks()
monkeypatch.setattr(client, "client", FakeClient())
assert client.find_host_network() == "runner-346da30e-2641-1-8365005"
def test_find_host_network_found_by_running_id(monkeypatch: pytest.MonkeyPatch) -> None:
client = DockerClient()
fake_id = "abcde1234"
def network_name(container_id: str) -> str:
assert container_id == fake_id
return "FAKE_NETWORK"
monkeypatch.setattr(utils, "get_running_in_container_id", lambda: fake_id)
monkeypatch.setattr(client, "network_name", network_name)
assert client.find_host_network() == "FAKE_NETWORK"
def test_run_uses_found_network(monkeypatch: pytest.MonkeyPatch) -> None:
"""
If a host network is found, use it
"""
if is_ssh_docker_host():
pytest.skip("Host network discovery is skipped when DOCKER_HOST is set")
client = DockerClient()
class ContainerRunFake:
def __init__(self) -> None:
self.calls: list[dict[str, Any]] = []
def run(self, image: str, **kwargs: Any) -> str:
self.calls.append(kwargs)
return "CONTAINER"
class FakeClient:
def __init__(self) -> None:
self.containers = ContainerRunFake()
fake_client = FakeClient()
monkeypatch.setattr(client, "find_host_network", lambda: "new_bridge_network")
monkeypatch.setattr(client, "client", fake_client)
assert client.run("test") == "CONTAINER"
assert fake_client.containers.calls[0]["network"] == "new_bridge_network"
@pytest.mark.parametrize(
"docker_host, expected",
[
pytest.param("ssh://user@192.168.1.42", "ssh://user@192.168.1.42", id="no_path"),
pytest.param("ssh://user@host/", "ssh://user@host", id="trailing_slash"),
pytest.param("ssh://user@host/some/path", "ssh://user@host", id="strips_path"),
pytest.param("tcp://localhost:2375", "tcp://localhost:2375", id="tcp_unchanged"),
pytest.param("unix:///var/run/docker.sock", "unix:///var/run/docker.sock", id="unix_unchanged"),
],
)
def test_sanitize_docker_host(docker_host: str, expected: str) -> None:
from testcontainers.core.docker_client import _sanitize_docker_host
assert _sanitize_docker_host(docker_host) == expected
@pytest.mark.parametrize(
"docker_host, expected_hostname",
[
pytest.param("ssh://user@192.168.1.42", "192.168.1.42", id="ssh_ip"),
pytest.param("ssh://user@myhost.example.com", "myhost.example.com", id="ssh_fqdn"),
pytest.param("tcp://localhost:2375", None, id="tcp_returns_none"),
pytest.param(None, None, id="unset_returns_none"),
],
)
def test_get_docker_host_hostname(monkeypatch: pytest.MonkeyPatch, docker_host: str, expected_hostname) -> None:
from testcontainers.core.docker_client import get_docker_host_hostname
monkeypatch.setattr(c, "tc_properties_get_tc_host", lambda: None)
if docker_host:
monkeypatch.setenv("DOCKER_HOST", docker_host)
else:
monkeypatch.delenv("DOCKER_HOST", raising=False)
assert get_docker_host_hostname() == expected_hostname
def test_ssh_docker_host(monkeypatch: pytest.MonkeyPatch) -> None:
"""Verify SSH DOCKER_HOST sets use_ssh_client and host() returns the remote hostname."""
monkeypatch.setenv("DOCKER_HOST", "ssh://user@10.0.0.1")
monkeypatch.setattr(c, "tc_properties_get_tc_host", lambda: None)
monkeypatch.setattr(c, "tc_host_override", None)
mock_docker = MagicMock(spec=docker)
with patch("testcontainers.core.docker_client.docker", mock_docker):
client = DockerClient()
mock_docker.from_env.assert_called_once_with(use_ssh_client=True)
assert client.host() == "10.0.0.1"