From b2ffaa0c5ab079da0cae8cdf7af5d46b47bda4c4 Mon Sep 17 00:00:00 2001 From: Remy Gwaramadze Date: Fri, 27 Sep 2024 12:24:49 +0200 Subject: [PATCH] Remove testcontainers Network compatibility layer (#528) * Revert "Create a compatibility layer to support testing in Python 3.8" This reverts commit b67ecf86266d5ad59471fa3db77af96dadbdebbb. * Remove Python 3.8 specific dependency --- tests/compat/__init__.py | 9 --------- tests/compat/network.py | 16 ---------------- tests/conftest.py | 10 +++++----- tests/containerhelper.py | 15 ++++++--------- tests/requirements.txt | 3 +-- 5 files changed, 12 insertions(+), 41 deletions(-) delete mode 100644 tests/compat/__init__.py delete mode 100644 tests/compat/network.py diff --git a/tests/compat/__init__.py b/tests/compat/__init__.py deleted file mode 100644 index ec31acf7b..000000000 --- a/tests/compat/__init__.py +++ /dev/null @@ -1,9 +0,0 @@ -# For Python 3.8, we use testcontainers==3.7.1, -# which lacks the high-level network API. -# This can be removed once we stop supporting Python 3.8. -try: - from testcontainers.core.network import Network -except ImportError: - from .network import Network - -__all__ = ["Network"] diff --git a/tests/compat/network.py b/tests/compat/network.py deleted file mode 100644 index 31efab62a..000000000 --- a/tests/compat/network.py +++ /dev/null @@ -1,16 +0,0 @@ -import uuid - -from testcontainers.core.docker_client import DockerClient - - -class Network: - def __enter__(self) -> "Network": - name = str(uuid.uuid4()) - self._network = DockerClient().client.networks.create(name) - return self - - def __exit__(self, *_) -> None: - self._network.remove() - - def connect(self, container_id: str) -> None: - self._network.connect(container_id) diff --git a/tests/conftest.py b/tests/conftest.py index 511c287da..b5adbb684 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,8 +3,8 @@ from typing import Generator import pytest +from testcontainers.core.network import Network -from .compat import Network from .containerhelper import ContainerHelper from .logging import LOGGING_CONFIG, patch_logger_class @@ -54,13 +54,13 @@ def schema_registry_container( ) -> Generator[SchemaRegistryContainer, None, None]: container, schema_registry_address = ( ContainerHelper.create_schema_registry_container( - kafka_container.internal_broker_address + network, kafka_container.internal_broker_address ) ) test_logger.debug( f"Starting Schema Registry container on {schema_registry_address}" ) - ContainerHelper.start_schema_registry_container(container, network) + ContainerHelper.start_schema_registry_container(container) test_logger.debug(f"Started Schema Registry container on {schema_registry_address}") yield SchemaRegistryContainer(schema_registry_address=schema_registry_address) container.stop() @@ -73,9 +73,9 @@ def factory(): kafka_container, internal_broker_address, external_broker_address, - ) = ContainerHelper.create_kafka_container() + ) = ContainerHelper.create_kafka_container(network) test_logger.debug(f"Starting Kafka container on {external_broker_address}") - ContainerHelper.start_kafka_container(kafka_container, network) + ContainerHelper.start_kafka_container(kafka_container) test_logger.debug(f"Started Kafka container on {external_broker_address}") yield KafkaContainer( broker_address=external_broker_address, diff --git a/tests/containerhelper.py b/tests/containerhelper.py index 961e9b1fc..3c895889c 100644 --- a/tests/containerhelper.py +++ b/tests/containerhelper.py @@ -6,13 +6,12 @@ from typing import Tuple from testcontainers.core.container import DockerContainer - -from .compat import Network +from testcontainers.core.network import Network class ContainerHelper: @staticmethod - def create_kafka_container() -> Tuple[DockerContainer, str, str]: + def create_kafka_container(network: Network) -> Tuple[DockerContainer, str, str]: """ Returns (kafka container, internal broker address, external broker address) tuple """ @@ -50,19 +49,18 @@ def create_kafka_container() -> Tuple[DockerContainer, str, str]: .with_env("KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR", "1") .with_env("KAFKA_TRANSACTION_STATE_LOG_MIN_ISR", "1") .with_bind_ports(kafka_port, kafka_port) + .with_network(network) ) return kafka_container, internal_broker_address, external_broker_address @staticmethod - def start_kafka_container( - kafka_container: DockerContainer, network: Network - ) -> None: + def start_kafka_container(kafka_container: DockerContainer) -> None: kafka_container.start() - network.connect(kafka_container.get_wrapped_container().id) wait_for_container_readiness(kafka_container, "Kafka Server started") @staticmethod def create_schema_registry_container( + network: Network, broker_address: str, ) -> Tuple[DockerContainer, str]: docker_image_name = "confluentinc/cp-schema-registry" @@ -79,16 +77,15 @@ def create_schema_registry_container( .with_env("SCHEMA_REGISTRY_LISTENERS", schema_registry_address) .with_env("SCHEMA_REGISTRY_HOST_NAME", "localhost") .with_bind_ports(schema_registry_port, schema_registry_port) + .with_network(network) ) return schema_registry_container, schema_registry_address @staticmethod def start_schema_registry_container( schema_registry_container: DockerContainer, - network: Network, ) -> None: schema_registry_container.start() - network.connect(schema_registry_container.get_wrapped_container().id) wait_for_container_readiness( schema_registry_container, "Server started, listening for requests" ) diff --git a/tests/requirements.txt b/tests/requirements.txt index db1a812e6..2131abd44 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,5 +1,4 @@ -testcontainers==3.7.1; python_version < '3.9' -testcontainers==4.8.1; python_version >= '3.9' +testcontainers==4.8.1 pytest requests>=2.32 docker>=7.1.0 # Required to use requests>=2.32