|
1 | 1 | import contextlib
|
2 | 2 | import hashlib
|
| 3 | +import logging |
3 | 4 | from platform import system
|
4 | 5 | from socket import socket
|
5 | 6 | from typing import TYPE_CHECKING, Optional
|
@@ -50,7 +51,7 @@ def __init__(
|
50 | 51 | self._name = None
|
51 | 52 | self._network: Optional[Network] = None
|
52 | 53 | self._network_aliases: Optional[list[str]] = None
|
53 |
| - self._reuse: bool = kwargs.pop("reuse", False) |
| 54 | + self._reuse: bool = False |
54 | 55 | self._kwargs = kwargs
|
55 | 56 |
|
56 | 57 | def with_env(self, key: str, value: str) -> Self:
|
@@ -106,9 +107,16 @@ def start(self) -> Self:
|
106 | 107 | )
|
107 | 108 | hash_ = hashlib.sha256(bytes(str(args), encoding="utf-8")).hexdigest()
|
108 | 109 |
|
109 |
| - # TODO: warn user if self._reuse=True but reuse_enabled=False |
110 |
| - reuse_enabled = c.reuse_enabled or c.tc_properties_testcontainers_reuse_enabled |
111 |
| - if self._reuse and reuse_enabled: |
| 110 | + # TODO: check also if ryuk is disabled |
| 111 | + if self._reuse and not c.tc_properties_testcontainers_reuse_enable: |
| 112 | + logging.warning( |
| 113 | + "Reuse was requested (`with_reuse`) but the environment does not " |
| 114 | + + "support the reuse of containers. To enable container reuse, add " |
| 115 | + + "the property 'testcontainers.reuse.enable=true' to a file at " |
| 116 | + + "~/.testcontainers.properties (you may need to create it)." |
| 117 | + ) |
| 118 | + |
| 119 | + if self._reuse and c.tc_properties_testcontainers_reuse_enable: |
112 | 120 | docker_client = self.get_docker_client()
|
113 | 121 | container = docker_client.find_container_by_hash(hash_)
|
114 | 122 | if container:
|
@@ -143,7 +151,7 @@ def _start(self, hash_):
|
143 | 151 |
|
144 | 152 | def stop(self, force=True, delete_volume=True) -> None:
|
145 | 153 | if self._container:
|
146 |
| - if self._reuse and c.reuse_enabled: |
| 154 | + if self._reuse and c.tc_properties_testcontainers_reuse_enable: |
147 | 155 | self._container.stop()
|
148 | 156 | else:
|
149 | 157 | self._container.remove(force=force, v=delete_volume)
|
|
0 commit comments