10
10
11
11
12
12
def test_docker_container_reuse_default ():
13
- with DockerContainer ("hello-world" ) as container :
14
- assert container ._reuse == False
15
- id = container ._container .id
16
- wait_for_logs (container , "Hello from Docker!" )
13
+ # Make sure Ryuk cleanup is not active from previous test runs
14
+ Reaper .delete_instance ()
15
+
16
+ container = DockerContainer ("hello-world" ).start ()
17
+ wait_for_logs (container , "Hello from Docker!" )
18
+
19
+ assert container ._reuse == False
20
+ assert testcontainers_config .tc_properties_testcontainers_reuse_enable == False
21
+ assert Reaper ._socket is not None
22
+
23
+ container .stop ()
17
24
containers = DockerClient ().client .containers .list (all = True )
18
- assert id not in [container .id for container in containers ]
25
+ assert container . _container . id not in [container .id for container in containers ]
19
26
20
27
21
- def test_docker_container_with_reuse_reuse_disabled ():
22
- with DockerContainer ("hello-world" ).with_reuse () as container :
23
- assert container ._reuse == True
24
- assert testcontainers_config .tc_properties_testcontainers_reuse_enable == False
25
- id = container ._container .id
26
- wait_for_logs (container , "Hello from Docker!" )
28
+ def test_docker_container_with_reuse_reuse_disabled (caplog ):
29
+ # Make sure Ryuk cleanup is not active from previous test runs
30
+ Reaper .delete_instance ()
31
+
32
+ container = DockerContainer ("hello-world" ).with_reuse ().start ()
33
+ wait_for_logs (container , "Hello from Docker!" )
34
+
35
+ assert container ._reuse == True
36
+ assert testcontainers_config .tc_properties_testcontainers_reuse_enable == False
37
+ assert (
38
+ "Reuse was requested (`with_reuse`) but the environment does not support the "
39
+ + "reuse of containers. To enable container reuse, add "
40
+ + "'testcontainers.reuse.enable=true' to '~/.testcontainers.properties'."
41
+ ) in caplog .text
42
+ assert Reaper ._socket is not None
43
+
44
+ container .stop ()
27
45
containers = DockerClient ().client .containers .list (all = True )
28
- assert id not in [container .id for container in containers ]
46
+ assert container . _container . id not in [container .id for container in containers ]
29
47
30
48
31
- def test_docker_container_with_reuse_reuse_enabled_ryuk_enabled (monkeypatch ):
49
+ def test_docker_container_without_reuse_reuse_enabled (monkeypatch ):
32
50
# Make sure Ryuk cleanup is not active from previous test runs
33
51
Reaper .delete_instance ()
34
52
35
53
tc_properties_mock = testcontainers_config .tc_properties | {"testcontainers.reuse.enable" : "true" }
36
54
monkeypatch .setattr (testcontainers_config , "tc_properties" , tc_properties_mock )
37
- monkeypatch .setattr (testcontainers_config , "ryuk_reconnection_timeout" , "0.1s" )
38
55
39
- container = DockerContainer ("hello-world" ).with_reuse ().start ()
40
- id = container ._container .id
56
+ container = DockerContainer ("hello-world" ).start ()
41
57
wait_for_logs (container , "Hello from Docker!" )
42
58
43
- Reaper . _socket . close ()
44
- # Sleep until Ryuk reaps all dangling containers
45
- sleep ( 0.6 )
59
+ assert container . _reuse == False
60
+ assert testcontainers_config . tc_properties_testcontainers_reuse_enable == True
61
+ assert Reaper . _socket is not None
46
62
63
+ container .stop ()
47
64
containers = DockerClient ().client .containers .list (all = True )
48
- assert id not in [container .id for container in containers ]
49
-
50
- # Cleanup Ryuk class fields after manual Ryuk shutdown
51
- Reaper .delete_instance ()
65
+ assert container ._container .id not in [container .id for container in containers ]
52
66
53
67
54
- def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled (monkeypatch ):
68
+ def test_docker_container_with_reuse_reuse_enabled (monkeypatch ):
55
69
# Make sure Ryuk cleanup is not active from previous test runs
56
70
Reaper .delete_instance ()
57
71
58
72
tc_properties_mock = testcontainers_config .tc_properties | {"testcontainers.reuse.enable" : "true" }
59
73
monkeypatch .setattr (testcontainers_config , "tc_properties" , tc_properties_mock )
60
- monkeypatch .setattr (testcontainers_config , "ryuk_disabled" , True )
61
74
62
75
container = DockerContainer ("hello-world" ).with_reuse ().start ()
63
- id = container ._container .id
64
76
wait_for_logs (container , "Hello from Docker!" )
65
77
66
- containers = DockerClient ().client .containers .list (all = True )
67
- assert id in [container .id for container in containers ]
78
+ assert Reaper ._socket is None
68
79
80
+ containers = DockerClient ().client .containers .list (all = True )
81
+ assert container ._container .id in [container .id for container in containers ]
69
82
# Cleanup after keeping container alive (with_reuse)
70
83
container .stop ()
71
84
@@ -82,8 +95,8 @@ def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled_same_id(monkeyp
82
95
id_1 = container_1 ._container .id
83
96
container_2 = DockerContainer ("hello-world" ).with_reuse ().start ()
84
97
id_2 = container_2 ._container .id
98
+ assert Reaper ._socket is None
85
99
assert id_1 == id_2
86
-
87
100
# Cleanup after keeping container alive (with_reuse)
88
101
container_1 .stop ()
89
102
# container_2.stop() is not needed since it is the same as container_1
0 commit comments