1
+ from time import sleep
2
+
1
3
from docker .models .containers import Container
4
+ from docker import DockerClient as DockerDockerClient
5
+
6
+ from testcontainers .core .config import testcontainers_config
2
7
from testcontainers .core .container import DockerContainer
3
8
from testcontainers .core .docker_client import DockerClient
4
9
from testcontainers .core .waiting_utils import wait_for_logs
10
+ from testcontainers .core .container import Reaper
5
11
6
12
7
13
def test_docker_container_reuse_default ():
8
14
with DockerContainer ("hello-world" ) as container :
9
15
assert container ._reuse == False
10
16
id = container ._container .id
17
+ wait_for_logs (container , "Hello from Docker!" )
11
18
containers = DockerClient ().client .containers .list (all = True )
12
19
assert id not in [container .id for container in containers ]
13
20
@@ -16,34 +23,51 @@ def test_docker_container_with_reuse_reuse_disabled():
16
23
with DockerContainer ("hello-world" ).with_reuse () as container :
17
24
assert container ._reuse == True
18
25
id = container ._container .id
26
+ wait_for_logs (container , "Hello from Docker!" )
19
27
containers = DockerClient ().client .containers .list (all = True )
20
28
assert id not in [container .id for container in containers ]
21
29
22
30
23
31
def test_docker_container_with_reuse_reuse_enabled_ryuk_enabled (monkeypatch ):
24
- monkeypatch .setattr ("testcontainers.core.config.testcontainers_config.reuse_enabled" , True )
32
+ # Make sure Ryuk cleanup is not active from previous test runs
33
+ Reaper .delete_instance ()
34
+ monkeypatch .setattr (testcontainers_config , "reuse_enabled" , True )
35
+ monkeypatch .setattr (testcontainers_config , "ryuk_reconnection_timeout" , "0.1s" )
36
+
25
37
with DockerContainer ("hello-world" ).with_reuse () as container :
26
- assert container ._reuse == True
27
38
id = container ._container .id
39
+ wait_for_logs (container , "Hello from Docker!" )
40
+
41
+ Reaper ._socket .close ()
42
+ # Sleep until Ryuk reaps all dangling containers
43
+ sleep (0.6 )
44
+
28
45
containers = DockerClient ().client .containers .list (all = True )
29
- assert id in [container .id for container in containers ]
46
+ assert id not in [container .id for container in containers ]
47
+
48
+ # Cleanup Ryuk class fields after manual Ryuk shutdown
49
+ Reaper .delete_instance ()
30
50
31
51
32
- # TODO: do not run test while ryuk is running
33
- # TODO: clean up container after test run
34
- def test_docker_container_with_reuse_reuse_enabled_ryuk_diabled ( monkeypatch ):
35
- monkeypatch .setattr ("testcontainers.core.config. testcontainers_config. reuse_enabled" , True )
36
- monkeypatch .setattr ("testcontainers.core.config. testcontainers_config. ryuk_disabled" , True )
52
+ def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled ( monkeypatch ):
53
+ # Make sure Ryuk cleanup is not active from previous test runs
54
+ Reaper . delete_instance ()
55
+ monkeypatch .setattr (testcontainers_config , " reuse_enabled" , True )
56
+ monkeypatch .setattr (testcontainers_config , " ryuk_disabled" , True )
37
57
with DockerContainer ("hello-world" ).with_reuse () as container :
38
58
assert container ._reuse == True
39
59
id = container ._container .id
60
+ wait_for_logs (container , "Hello from Docker!" )
40
61
containers = DockerClient ().client .containers .list (all = True )
41
62
assert id in [container .id for container in containers ]
63
+ # Cleanup after keeping container alive (with_reuse)
64
+ container ._container .remove (force = True )
42
65
43
66
44
67
def test_docker_container_labels_hash ():
68
+ expected_hash = "91fde3c09244e1d3ec6f18a225b9261396b9a1cb0f6365b39b9795782817c128"
45
69
with DockerContainer ("hello-world" ).with_reuse () as container :
46
- assert container ._container .labels ["hash" ] == "505d1d913abe7f54b5a66202e8559a4f798038a204d39fe8b1577735ed632e32"
70
+ assert container ._container .labels ["hash" ] == expected_hash
47
71
48
72
49
73
def test_docker_client_find_container_by_hash_not_existing ():
@@ -53,5 +77,6 @@ def test_docker_client_find_container_by_hash_not_existing():
53
77
54
78
def test_docker_client_find_container_by_hash_existing ():
55
79
with DockerContainer ("hello-world" ).with_reuse () as container :
56
- found_container = DockerClient ().find_container_by_hash (container ._container .labels ["hash" ])
80
+ hash_ = container ._container .labels ["hash" ]
81
+ found_container = DockerClient ().find_container_by_hash (hash_ )
57
82
assert isinstance (found_container , Container )
0 commit comments