-
Notifications
You must be signed in to change notification settings - Fork 329
feat: reusable containers #636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a933873
f0e2bc7
08e33ba
d2a83bc
c781606
dd429e7
e87e782
c656660
efb1265
d4445d6
1ea9ed1
ea6fec7
7c1e8e7
2113561
0615c29
23e436a
2bfb36d
6796a9a
3050fee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from time import sleep | ||
|
||
from docker.models.containers import Container | ||
|
||
from testcontainers.core.config import testcontainers_config | ||
from testcontainers.core.container import DockerContainer | ||
from testcontainers.core.docker_client import DockerClient | ||
from testcontainers.core.waiting_utils import wait_for_logs | ||
from testcontainers.core.container import Reaper | ||
|
||
|
||
def test_docker_container_reuse_default(): | ||
# Make sure Ryuk cleanup is not active from previous test runs | ||
Reaper.delete_instance() | ||
|
||
container = DockerContainer("hello-world").start() | ||
wait_for_logs(container, "Hello from Docker!") | ||
|
||
assert container._reuse == False | ||
assert testcontainers_config.tc_properties_testcontainers_reuse_enable == False | ||
assert Reaper._socket is not None | ||
|
||
container.stop() | ||
containers = DockerClient().client.containers.list(all=True) | ||
assert container._container.id not in [container.id for container in containers] | ||
|
||
|
||
def test_docker_container_with_reuse_reuse_disabled(caplog): | ||
# Make sure Ryuk cleanup is not active from previous test runs | ||
Reaper.delete_instance() | ||
|
||
container = DockerContainer("hello-world").with_reuse().start() | ||
wait_for_logs(container, "Hello from Docker!") | ||
|
||
assert container._reuse == True | ||
assert testcontainers_config.tc_properties_testcontainers_reuse_enable == False | ||
assert ( | ||
"Reuse was requested (`with_reuse`) but the environment does not support the " | ||
+ "reuse of containers. To enable container reuse, add " | ||
+ "'testcontainers.reuse.enable=true' to '~/.testcontainers.properties'." | ||
) in caplog.text | ||
assert Reaper._socket is not None | ||
|
||
container.stop() | ||
containers = DockerClient().client.containers.list(all=True) | ||
assert container._container.id not in [container.id for container in containers] | ||
|
||
|
||
def test_docker_container_without_reuse_reuse_enabled(monkeypatch): | ||
# Make sure Ryuk cleanup is not active from previous test runs | ||
Reaper.delete_instance() | ||
|
||
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} | ||
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) | ||
|
||
container = DockerContainer("hello-world").start() | ||
wait_for_logs(container, "Hello from Docker!") | ||
|
||
assert container._reuse == False | ||
assert testcontainers_config.tc_properties_testcontainers_reuse_enable == True | ||
assert Reaper._socket is not None | ||
|
||
container.stop() | ||
containers = DockerClient().client.containers.list(all=True) | ||
assert container._container.id not in [container.id for container in containers] | ||
|
||
|
||
def test_docker_container_with_reuse_reuse_enabled(monkeypatch): | ||
# Make sure Ryuk cleanup is not active from previous test runs | ||
Reaper.delete_instance() | ||
|
||
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} | ||
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) | ||
|
||
container = DockerContainer("hello-world").with_reuse().start() | ||
wait_for_logs(container, "Hello from Docker!") | ||
|
||
assert Reaper._socket is None | ||
|
||
containers = DockerClient().client.containers.list(all=True) | ||
assert container._container.id in [container.id for container in containers] | ||
# Cleanup after keeping container alive (with_reuse) | ||
container.stop() | ||
|
||
|
||
def test_docker_container_with_reuse_reuse_enabled_same_id(monkeypatch): | ||
# Make sure Ryuk cleanup is not active from previous test runs | ||
Reaper.delete_instance() | ||
|
||
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} | ||
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) | ||
|
||
container_1 = DockerContainer("hello-world").with_reuse().start() | ||
id_1 = container_1._container.id | ||
container_2 = DockerContainer("hello-world").with_reuse().start() | ||
id_2 = container_2._container.id | ||
assert Reaper._socket is None | ||
assert id_1 == id_2 | ||
# Cleanup after keeping container alive (with_reuse) | ||
container_1.stop() | ||
# container_2.stop() is not needed since it is the same as container_1 | ||
|
||
|
||
def test_docker_container_labels_hash_default(): | ||
# w/out reuse | ||
with DockerContainer("hello-world") as container: | ||
assert container._container.labels["hash"] == "" | ||
|
||
|
||
def test_docker_container_labels_hash(monkeypatch): | ||
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} | ||
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) | ||
expected_hash = "1bade17a9d8236ba71ffbb676f2ece3fb419ea0e6adb5f82b5a026213c431d8e" | ||
with DockerContainer("hello-world").with_reuse() as container: | ||
assert container._container.labels["hash"] == expected_hash | ||
|
||
|
||
def test_docker_client_find_container_by_hash_not_existing(): | ||
with DockerContainer("hello-world"): | ||
assert DockerClient().find_container_by_hash("foo") == None | ||
|
||
|
||
def test_docker_client_find_container_by_hash_existing(monkeypatch): | ||
tc_properties_mock = testcontainers_config.tc_properties | {"testcontainers.reuse.enable": "true"} | ||
monkeypatch.setattr(testcontainers_config, "tc_properties", tc_properties_mock) | ||
with DockerContainer("hello-world").with_reuse() as container: | ||
hash_ = container._container.labels["hash"] | ||
found_container = DockerClient().find_container_by_hash(hash_) | ||
assert isinstance(found_container, Container) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,6 @@ When trying to launch Testcontainers from within a Docker container, e.g., in co | |
1. The container has to provide a docker client installation. Either use an image that has docker pre-installed (e.g. the `official docker images <https://hub.docker.com/_/docker>`_) or install the client from within the `Dockerfile` specification. | ||
2. The container has to have access to the docker daemon which can be achieved by mounting `/var/run/docker.sock` or setting the `DOCKER_HOST` environment variable as part of your `docker run` command. | ||
|
||
|
||
Private Docker registry | ||
----------------------- | ||
|
||
|
@@ -119,6 +118,36 @@ Fetching passwords from cloud providers: | |
GCP_PASSWORD = $(gcloud auth print-access-token) | ||
AZURE_PASSWORD = $(az acr login --name <registry-name> --expose-token --output tsv) | ||
|
||
Reusable Containers (Experimental) | ||
---------------------------------- | ||
|
||
.. warning:: | ||
Reusable Containers is still an experimental feature and the behavior can change. | ||
Those containers won't stop after all tests are finished. | ||
|
||
Containers can be reused across consecutive test runs. To reuse a container, the container has to be started manually by calling the `start()` method. Do not call the `stop()` method directly or indirectly via a `with` statement (context manager). To reuse a container, the container configuration must be the same. | ||
|
||
Containers that are set up for reuse will not be automatically removed. Thus, if they are not needed anymore, those containers must be removed manually. | ||
|
||
Containers should not be reused in a CI environment. | ||
|
||
How to use? | ||
^^^^^^^^^^^ | ||
|
||
1. Add :code:`testcontainers.reuse.enable=true` to :code:`~/.testcontainers.properties` | ||
2. Disable ryuk by setting the environment variable :code:`TESTCONTAINERS_RYUK_DISABLED=true` | ||
3. Instantiate a container using :code:`with_reuse()` and :code:`start()` | ||
|
||
.. doctest:: | ||
|
||
>>> from testcontainers.core.container import DockerContainer | ||
|
||
>>> container = DockerContainer("hello-world").with_reuse().start() | ||
>>> first_id = container._container.id | ||
>>> container = DockerContainer("hello-world").with_reuse().start() | ||
>>> second_id == container._container.id | ||
>>> print(first_id == second_id) | ||
True | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the user be warned that by using this feature, containers need to be removed manually? (That this feature should not be used in a CI) Also, do we need to make clear how this feature works (explaining the hash in use). -> If a container's run configuration changes, the hash changes and a new container will be used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like you have added these comments to the doc, i think that is fine. the hash would be great to add as users would benefit from knowing exactly what is hashed.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note to self: Maybe use something like https://github.com/knowsuchagency/picocache/blob/main/picocache/utils.py#L9 for making the hash key |
||
Configuration | ||
------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
apparently this part is also fairly jank and we should remove/rework so as a note to myself i can only do that after this pr merges