|
1 | 1 | import pytest
|
2 | 2 | import tempfile
|
| 3 | +import random |
| 4 | + |
| 5 | +from typing import Optional |
3 | 6 |
|
4 | 7 | from testcontainers.core.container import DockerContainer
|
5 | 8 | from testcontainers.core.image import DockerImage
|
6 | 9 | from testcontainers.core.waiting_utils import wait_for_logs
|
7 | 10 | from testcontainers.core.generic import CustomContainer
|
| 11 | +from testcontainers.core.docker_client import DockerClient |
8 | 12 |
|
9 | 13 |
|
10 | 14 | def test_timeout_is_raised_when_waiting_for_logs():
|
@@ -36,41 +40,64 @@ def test_can_get_logs():
|
36 | 40 | assert stdout, "There should be something on stdout"
|
37 | 41 |
|
38 | 42 |
|
| 43 | +def check_for_image(image_short_id: str) -> bool: |
| 44 | + """ |
| 45 | + Check if the image with the given short_id is present in the list of images |
| 46 | + """ |
| 47 | + client = DockerClient() |
| 48 | + images = client.client.images.list() |
| 49 | + return any(image.short_id.endswith(image_short_id) for image in images) |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.parametrize("test_cleanup", [True, False]) |
39 | 53 | @pytest.mark.parametrize("test_image_tag", [None, "test-image:latest"])
|
40 |
| -def test_docker_image(test_image_tag): |
| 54 | +def test_docker_image(test_image_tag: Optional[str], test_cleanup: bool): |
41 | 55 | with tempfile.TemporaryDirectory() as temp_directory:
|
| 56 | + # It's important to use a random string to avoid image caching |
| 57 | + random_string = "Hello from Docker Image! " + str(random.randint(0, 1000)) |
42 | 58 | with open(f"{temp_directory}/Dockerfile", "w") as f:
|
43 | 59 | f.write(
|
44 |
| - """ |
| 60 | + f""" |
45 | 61 | FROM alpine:latest
|
46 |
| - CMD echo "Hello from Docker Image!" |
| 62 | + CMD echo "{random_string}" |
47 | 63 | """
|
48 | 64 | )
|
49 | 65 | f.close()
|
50 |
| - with DockerImage(path=temp_directory, tag=test_image_tag) as image: |
| 66 | + with DockerImage(path=temp_directory, tag=test_image_tag, clean_up=test_cleanup) as image: |
| 67 | + image_short_id = image.short_id |
51 | 68 | assert image.tag is test_image_tag
|
52 | 69 | assert image.short_id is not None
|
53 | 70 | logs = image.get_logs()
|
54 | 71 | assert isinstance(logs, list)
|
55 | 72 | assert logs[0] == {"stream": "Step 1/2 : FROM alpine:latest"}
|
56 |
| - assert logs[3] == {"stream": 'Step 2/2 : CMD echo "Hello from Docker Image!"'} |
| 73 | + assert logs[3] == {"stream": f'Step 2/2 : CMD echo "{random_string}"'} |
57 | 74 | with DockerContainer(str(image)) as container:
|
58 |
| - wait_for_logs(container, "Hello from Docker Image!") |
59 |
| - assert container._container.image.short_id.endswith(image.short_id) |
| 75 | + wait_for_logs(container, random_string) |
| 76 | + assert container._container.image.short_id.endswith(image_short_id) |
60 | 77 |
|
| 78 | + if not test_cleanup: |
| 79 | + assert check_for_image(image_short_id) |
61 | 80 |
|
| 81 | + |
| 82 | +@pytest.mark.parametrize("test_image_cleanup", [True, False]) |
62 | 83 | @pytest.mark.parametrize("test_image_tag", [None, "custom-image:test"])
|
63 |
| -def test_custom_container(test_image_tag): |
| 84 | +def test_custom_container(test_image_tag: Optional[str], test_image_cleanup: bool): |
64 | 85 | with tempfile.TemporaryDirectory() as temp_directory:
|
| 86 | + # It's important to use a random string to avoid image caching |
| 87 | + random_string = "This is a custom Docker Image! " + str(random.randint(0, 1000)) |
65 | 88 | with open(f"{temp_directory}/Dockerfile", "w") as f:
|
66 | 89 | f.write(
|
67 |
| - """ |
| 90 | + f""" |
68 | 91 | FROM alpine:latest
|
69 |
| - CMD echo "This is a custom Docker Image!" |
| 92 | + CMD echo "{random_string}" |
70 | 93 | """
|
71 | 94 | )
|
72 | 95 | f.close()
|
73 |
| - with CustomContainer(path=temp_directory, tag=test_image_tag) as server: |
| 96 | + with CustomContainer(path=temp_directory, tag=test_image_tag, image_cleanup=test_image_cleanup) as server: |
| 97 | + image_short_id = server.docker_image.short_id |
74 | 98 | image_build_logs = server.docker_image.get_logs()
|
75 |
| - assert image_build_logs[3] == {"stream": 'Step 2/2 : CMD echo "This is a custom Docker Image!"'} |
76 |
| - wait_for_logs(server, "This is a custom Docker Image!") |
| 99 | + assert image_build_logs[3] == {"stream": f'Step 2/2 : CMD echo "{random_string}"'} |
| 100 | + wait_for_logs(server, random_string) |
| 101 | + |
| 102 | + if not test_image_cleanup: |
| 103 | + assert check_for_image(image_short_id) |
0 commit comments