From c3318efc0eeb399c0aa14eeef638cc8b768784f8 Mon Sep 17 00:00:00 2001 From: bstrausser Date: Mon, 1 Apr 2024 20:28:13 +0000 Subject: [PATCH 1/4] Add common testcontainers labels --- core/testcontainers/core/labels.py | 12 ++++++++ core/tests/test_labels.py | 49 ++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 core/tests/test_labels.py diff --git a/core/testcontainers/core/labels.py b/core/testcontainers/core/labels.py index 144e4365e..8639dffcd 100644 --- a/core/testcontainers/core/labels.py +++ b/core/testcontainers/core/labels.py @@ -1,17 +1,29 @@ +import importlib from typing import Optional from uuid import uuid4 from testcontainers.core.config import testcontainers_config as c SESSION_ID: str = str(uuid4()) +TEST_CONTAINERS_NAMESPACE = "org.testcontainers" + +LABEL_TEST_CONTAINERS = TEST_CONTAINERS_NAMESPACE LABEL_SESSION_ID = "org.testcontainers.session-id" +LABEL_VERSION = "org.testcontainers.version" LABEL_LANG = "org.testcontainers.lang" def create_labels(image: str, labels: Optional[dict[str, str]]) -> dict[str, str]: if labels is None: labels = {} + else: + for k in labels: + if k.startswith(TEST_CONTAINERS_NAMESPACE): + raise ValueError("The org.testcontainers namespace is reserved for interal use") + labels[LABEL_LANG] = "python" + labels[LABEL_TEST_CONTAINERS] = "true" + labels[LABEL_VERSION] = importlib.metadata.version("testcontainers") if image == c.ryuk_image: return labels diff --git a/core/tests/test_labels.py b/core/tests/test_labels.py new file mode 100644 index 000000000..a844e0d7e --- /dev/null +++ b/core/tests/test_labels.py @@ -0,0 +1,49 @@ +from testcontainers.core.labels import ( + LABEL_LANG, + LABEL_SESSION_ID, + LABEL_TEST_CONTAINERS, + LABEL_VERSION, + create_labels, + TEST_CONTAINERS_NAMESPACE, +) +import pytest +from testcontainers.core.config import RYUK_IMAGE + + +def assert_in_with_value(labels: dict, label: str, value: str, known_before_test_time: bool) -> None: + assert label in labels + if known_before_test_time: + assert labels[label] == value + + +testdata = [ + (LABEL_LANG, "python", True), + (LABEL_TEST_CONTAINERS, "true", True), + (LABEL_SESSION_ID, "some", False), + (LABEL_VERSION, "some", False), +] + + +@pytest.mark.parametrize("label,value,known_before_test_time", testdata) +def test_containers_creates_expected_labels(label, value, known_before_test_time): + actual_labels = create_labels("not-ryuk", None) + assert_in_with_value(actual_labels, label, value, known_before_test_time) + + +def test_containers_throws_on_namespace_collision(): + + with pytest.raises(ValueError): + create_labels("not-ryuk", {TEST_CONTAINERS_NAMESPACE: "fake"}) + + +def test_containers_respect_custom_labels_if_no_collision(): + + custom_namespace = "org.foo.bar" + value = "fake" + actual_labels = create_labels("not-ryuk", {custom_namespace: value}) + assert_in_with_value(actual_labels, custom_namespace, value, True) + + +def test_if_ryuk_no_session(): + actual_labels = create_labels(RYUK_IMAGE, None) + assert LABEL_SESSION_ID not in actual_labels From bf9527ba910eca48224814f9cfac19d236a26e47 Mon Sep 17 00:00:00 2001 From: bstrausser Date: Mon, 1 Apr 2024 20:42:05 +0000 Subject: [PATCH 2/4] Add module session test --- core/tests/test_labels.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/core/tests/test_labels.py b/core/tests/test_labels.py index a844e0d7e..924e0bdc7 100644 --- a/core/tests/test_labels.py +++ b/core/tests/test_labels.py @@ -47,3 +47,14 @@ def test_containers_respect_custom_labels_if_no_collision(): def test_if_ryuk_no_session(): actual_labels = create_labels(RYUK_IMAGE, None) assert LABEL_SESSION_ID not in actual_labels + + +def test_session_are_module_import_scoped(): + """ + Asserts that sessions are a module-level variable and don't differ between invocation + """ + first_labels = create_labels("not-ryuk", None) + second_labels = create_labels("not-ryuk", None) + assert LABEL_SESSION_ID in first_labels + assert LABEL_SESSION_ID in second_labels + assert first_labels[LABEL_SESSION_ID] == second_labels[LABEL_SESSION_ID] From 466c494f29853e3140d125d0096fb8c4d15e9d8b Mon Sep 17 00:00:00 2001 From: bstrausser Date: Wed, 10 Apr 2024 14:03:19 +0000 Subject: [PATCH 3/4] Change label names --- core/testcontainers/core/labels.py | 8 ++++---- core/tests/test_labels.py | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/core/testcontainers/core/labels.py b/core/testcontainers/core/labels.py index 8639dffcd..df9c617b1 100644 --- a/core/testcontainers/core/labels.py +++ b/core/testcontainers/core/labels.py @@ -5,9 +5,9 @@ from testcontainers.core.config import testcontainers_config as c SESSION_ID: str = str(uuid4()) -TEST_CONTAINERS_NAMESPACE = "org.testcontainers" +TESTCONTAINERS_NAMESPACE = "org.testcontainers" -LABEL_TEST_CONTAINERS = TEST_CONTAINERS_NAMESPACE +LABEL_TESTCONTAINERS = TESTCONTAINERS_NAMESPACE LABEL_SESSION_ID = "org.testcontainers.session-id" LABEL_VERSION = "org.testcontainers.version" LABEL_LANG = "org.testcontainers.lang" @@ -18,11 +18,11 @@ def create_labels(image: str, labels: Optional[dict[str, str]]) -> dict[str, str labels = {} else: for k in labels: - if k.startswith(TEST_CONTAINERS_NAMESPACE): + if k.startswith(TESTCONTAINERS_NAMESPACE): raise ValueError("The org.testcontainers namespace is reserved for interal use") labels[LABEL_LANG] = "python" - labels[LABEL_TEST_CONTAINERS] = "true" + labels[LABEL_TESTCONTAINERS] = "true" labels[LABEL_VERSION] = importlib.metadata.version("testcontainers") if image == c.ryuk_image: diff --git a/core/tests/test_labels.py b/core/tests/test_labels.py index 924e0bdc7..ea1fd4a14 100644 --- a/core/tests/test_labels.py +++ b/core/tests/test_labels.py @@ -1,10 +1,10 @@ from testcontainers.core.labels import ( LABEL_LANG, LABEL_SESSION_ID, - LABEL_TEST_CONTAINERS, + LABEL_TESTCONTAINERS, LABEL_VERSION, create_labels, - TEST_CONTAINERS_NAMESPACE, + TESTCONTAINERS_NAMESPACE, ) import pytest from testcontainers.core.config import RYUK_IMAGE @@ -18,7 +18,7 @@ def assert_in_with_value(labels: dict, label: str, value: str, known_before_test testdata = [ (LABEL_LANG, "python", True), - (LABEL_TEST_CONTAINERS, "true", True), + (LABEL_TESTCONTAINERS, "true", True), (LABEL_SESSION_ID, "some", False), (LABEL_VERSION, "some", False), ] @@ -33,7 +33,7 @@ def test_containers_creates_expected_labels(label, value, known_before_test_time def test_containers_throws_on_namespace_collision(): with pytest.raises(ValueError): - create_labels("not-ryuk", {TEST_CONTAINERS_NAMESPACE: "fake"}) + create_labels("not-ryuk", {TESTCONTAINERS_NAMESPACE: "fake"}) def test_containers_respect_custom_labels_if_no_collision(): From 7a8f85f106d9efb315eac4a879cdbfb16e08807d Mon Sep 17 00:00:00 2001 From: David Ankin Date: Wed, 17 Apr 2024 06:06:34 -0400 Subject: [PATCH 4/4] remove extra space to appease ruff --- core/tests/test_labels.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/core/tests/test_labels.py b/core/tests/test_labels.py index ea1fd4a14..425aee7dd 100644 --- a/core/tests/test_labels.py +++ b/core/tests/test_labels.py @@ -31,13 +31,11 @@ def test_containers_creates_expected_labels(label, value, known_before_test_time def test_containers_throws_on_namespace_collision(): - with pytest.raises(ValueError): create_labels("not-ryuk", {TESTCONTAINERS_NAMESPACE: "fake"}) def test_containers_respect_custom_labels_if_no_collision(): - custom_namespace = "org.foo.bar" value = "fake" actual_labels = create_labels("not-ryuk", {custom_namespace: value})