-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathtest_container.py
More file actions
121 lines (91 loc) · 4.21 KB
/
test_container.py
File metadata and controls
121 lines (91 loc) · 4.21 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
import pytest
from testcontainers.core.container import DockerContainer
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.config import ConnectionMode
from testcontainers.core.config import testcontainers_config
FAKE_ID = "ABC123"
class FakeContainer:
@property
def id(self) -> str:
return FAKE_ID
@pytest.fixture
def container(monkeypatch: pytest.MonkeyPatch) -> DockerContainer:
"""
Fake initialized container
"""
client = DockerClient()
container = DockerContainer("foobar")
monkeypatch.setattr(container, "_docker", client)
monkeypatch.setattr(container, "_container", FakeContainer())
return container
@pytest.mark.parametrize("mode", ["docker_host", "gateway_ip", "bridge_ip"])
def test_get_container_host_ip(container: DockerContainer, monkeypatch: pytest.MonkeyPatch, mode: str) -> None:
"""
Depending on the connection mode the correct function is executed to the host_ip
"""
connection_mode = ConnectionMode(mode)
def result_fake(result: str, require_container_id):
def fake_for_mode(*container_id: str):
if require_container_id:
assert len(container_id) == 1
assert container_id[0] == FAKE_ID
else:
assert len(container_id) == 0
return result
return fake_for_mode
client = container._docker
monkeypatch.setattr(client, "get_connection_mode", lambda: connection_mode)
monkeypatch.setattr(client, "gateway_ip", result_fake("gateway_ip", True))
monkeypatch.setattr(client, "bridge_ip", result_fake("bridge_ip", True))
monkeypatch.setattr(client, "host", result_fake("docker_host", False))
assert container.get_container_host_ip() == mode
@pytest.mark.parametrize("mode", [ConnectionMode.gateway_ip, ConnectionMode.docker_host])
def test_get_exposed_port_mapped(
container: DockerContainer, monkeypatch: pytest.MonkeyPatch, mode: ConnectionMode
) -> None:
def fake_mapped(container_id: str, port: int) -> int:
assert container_id == FAKE_ID
assert port == 8080
return 45678
client = container._docker
monkeypatch.setattr(client, "port", fake_mapped)
monkeypatch.setattr(client, "get_connection_mode", lambda: mode)
assert container._get_exposed_port(8080) == 45678
def test_get_exposed_port_original(container: DockerContainer, monkeypatch: pytest.MonkeyPatch) -> None:
client = container._docker
monkeypatch.setattr(client, "get_connection_mode", lambda: ConnectionMode.bridge_ip)
assert container._get_exposed_port(8080) == 8080
@pytest.mark.parametrize(
"init_attr,init_value,class_attr,stored_value",
[
("command", "ps", "_command", "ps"),
("env", {"e1": "v1"}, "env", {"e1": "v1"}),
("name", "foo-bar", "_name", "foo-bar"),
("ports", [22, 80], "ports", {22: None, 80: None}),
(
"volumes",
[("/tmp", "/tmp2", "ro")],
"volumes",
{"/tmp": {"bind": "/tmp2", "mode": "ro"}},
),
],
)
def test_attribute(init_attr, init_value, class_attr, stored_value):
"""Test that the attributes set through the __init__ function are properly stored."""
with DockerContainer("ubuntu", **{init_attr: init_value}) as container:
assert getattr(container, class_attr) == stored_value
def test_image_prefix_applied(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that the hub_image_name_prefix is properly applied to the image name."""
# Set a prefix
test_prefix = "myregistry.example.com/"
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", test_prefix)
# Create a container and verify the prefix is applied
container = DockerContainer("nginx:latest")
assert container.image == "myregistry.example.com/nginx:latest"
def test_image_no_prefix_applied_when_empty(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test that when hub_image_name_prefix is empty, no prefix is applied."""
# Set an empty prefix
monkeypatch.setattr(testcontainers_config, "hub_image_name_prefix", "")
# Create a container and verify no prefix is applied
container = DockerContainer("nginx:latest")
assert container.image == "nginx:latest"