Skip to content

Commit ea6fec7

Browse files
refactor: move hash generation into if clause
1 parent 1ea9ed1 commit ea6fec7

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

core/testcontainers/core/container.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -99,18 +99,6 @@ def start(self) -> Self:
9999
logger.info("Pulling image %s", self.image)
100100
self._configure()
101101

102-
# container hash consisting of run arguments
103-
args = (
104-
self.image,
105-
self._command,
106-
self.env,
107-
self.ports,
108-
self._name,
109-
self.volumes,
110-
str(tuple(sorted(self._kwargs.items()))),
111-
)
112-
hash_ = hashlib.sha256(bytes(str(args), encoding="utf-8")).hexdigest()
113-
114102
if self._reuse and not c.tc_properties_testcontainers_reuse_enable:
115103
logging.warning(
116104
"Reuse was requested (`with_reuse`) but the environment does not "
@@ -119,24 +107,36 @@ def start(self) -> Self:
119107
)
120108

121109
if self._reuse and c.tc_properties_testcontainers_reuse_enable:
110+
# NOTE: ideally the docker client would return the full container create
111+
# request which could be used to generate the hash.
112+
args = [ # Docker run arguments
113+
self.image,
114+
self._command,
115+
self.env,
116+
self.ports,
117+
self._name,
118+
self.volumes,
119+
str(tuple(sorted(self._kwargs.values()))),
120+
]
121+
hash_ = hashlib.sha256(bytes(str(args), encoding="utf-8")).hexdigest()
122122
docker_client = self.get_docker_client()
123123
container = docker_client.find_container_by_hash(hash_)
124124
if container:
125125
if container.status != "running":
126126
container.start()
127127
logger.info("Existing container started: %s", container.id)
128-
logger.info("Container is already running: %s", container.id)
129128
self._container = container
129+
logger.info("Container is already running: %s", container.id)
130130
else:
131131
self._start(hash_)
132132
else:
133-
self._start(hash_)
133+
self._start()
134134

135135
if self._network:
136136
self._network.connect(self._container.id, self._network_aliases)
137137
return self
138138

139-
def _start(self, hash_):
139+
def _start(self, hash_=None):
140140
docker_client = self.get_docker_client()
141141
self._container = docker_client.run(
142142
self.image,
@@ -146,7 +146,7 @@ def _start(self, hash_):
146146
ports=self.ports,
147147
name=self._name,
148148
volumes=self.volumes,
149-
labels={"hash": hash_},
149+
labels={"hash": hash_} if hash is not None else {},
150150
**self._kwargs,
151151
)
152152
logger.info("Container started: %s", self._container.short_id)

core/tests/test_reusable_containers.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ def test_docker_container_with_reuse_reuse_enabled(monkeypatch):
8383
container.stop()
8484

8585

86-
def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled_same_id(monkeypatch):
86+
def test_docker_container_with_reuse_reuse_enabled_same_id(monkeypatch):
8787
# Make sure Ryuk cleanup is not active from previous test runs
8888
Reaper.delete_instance()
8989

9090
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"}
9191
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
92-
monkeypatch.setattr(testcontainers_config, "ryuk_disabled", True)
9392

9493
container_1 = DockerContainer("hello-world").with_reuse().start()
9594
id_1 = container_1._container.id
@@ -102,8 +101,16 @@ def test_docker_container_with_reuse_reuse_enabled_ryuk_disabled_same_id(monkeyp
102101
# container_2.stop() is not needed since it is the same as container_1
103102

104103

105-
def test_docker_container_labels_hash():
106-
expected_hash = "91fde3c09244e1d3ec6f18a225b9261396b9a1cb0f6365b39b9795782817c128"
104+
def test_docker_container_labels_hash_default():
105+
# w/out reuse
106+
with DockerContainer("hello-world") as container:
107+
assert container._container.labels["hash"] == ""
108+
109+
110+
def test_docker_container_labels_hash(monkeypatch):
111+
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"}
112+
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
113+
expected_hash = "1bade17a9d8236ba71ffbb676f2ece3fb419ea0e6adb5f82b5a026213c431d8e"
107114
with DockerContainer("hello-world").with_reuse() as container:
108115
assert container._container.labels["hash"] == expected_hash
109116

@@ -113,7 +120,9 @@ def test_docker_client_find_container_by_hash_not_existing():
113120
assert DockerClient().find_container_by_hash("foo") == None
114121

115122

116-
def test_docker_client_find_container_by_hash_existing():
123+
def test_docker_client_find_container_by_hash_existing(monkeypatch):
124+
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"}
125+
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock)
117126
with DockerContainer("hello-world").with_reuse() as container:
118127
hash_ = container._container.labels["hash"]
119128
found_container = DockerClient().find_container_by_hash(hash_)

0 commit comments

Comments
 (0)