forked from Josephrp/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_docker_client.py
More file actions
118 lines (87 loc) · 4.35 KB
/
test_docker_client.py
File metadata and controls
118 lines (87 loc) · 4.35 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
import os
import json
from collections import namedtuple
from unittest import mock
from unittest.mock import MagicMock, patch
import docker
from testcontainers.core.config import testcontainers_config as c
from testcontainers.core.container import DockerContainer
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.auth import parse_docker_auth_config
from testcontainers.core.image import DockerImage
from pytest import mark
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(**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
TestAuth = namedtuple("Auth", "value")
mock_parse_docker_auth_config.return_value = [TestAuth("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(**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(**test_kwargs)