|
13 | 13 | import pytest |
14 | 14 | from _pytest.outcomes import Failed |
15 | 15 | from docker.models.containers import Container |
16 | | -from docker.models.networks import Network |
17 | | -from retry import retry |
18 | 16 |
|
| 17 | +from utils.docker_fixtures._test_agent import TestAgentFactory, TestAgentAPI |
19 | 18 | from utils._context.component_version import ComponentVersion |
20 | | -from utils._logger import logger |
21 | | - |
22 | 19 | from utils._context.docker import get_docker_client |
| 20 | +from utils._logger import logger |
23 | 21 | from .core import Scenario, scenario_groups |
24 | 22 |
|
25 | 23 |
|
@@ -56,9 +54,10 @@ class APMLibraryTestServer: |
56 | 54 |
|
57 | 55 |
|
58 | 56 | class ParametricScenario(Scenario): |
59 | | - TEST_AGENT_IMAGE = "ghcr.io/datadog/dd-apm-test-agent/ddapm-test-agent:v1.32.0" |
60 | 57 | apm_test_server_definition: APMLibraryTestServer |
61 | 58 |
|
| 59 | + _test_agent_factory: TestAgentFactory |
| 60 | + |
62 | 61 | class PersistentParametricTestConf(dict): |
63 | 62 | """Parametric tests are executed in multiple thread, we need a mechanism to persist |
64 | 63 | each parametrized_tests_metadata on a file |
@@ -123,13 +122,14 @@ def configure(self, config: pytest.Config): |
123 | 122 | "rust": rust_library_factory, |
124 | 123 | }[library] |
125 | 124 |
|
| 125 | + self._test_agent_factory = TestAgentFactory(self.host_log_folder) |
126 | 126 | self.apm_test_server_definition = factory() |
127 | 127 |
|
128 | 128 | if self.is_main_worker: |
129 | 129 | # https://github.com/pytest-dev/pytest-xdist/issues/271#issuecomment-826396320 |
130 | 130 | # we are in the main worker, not in a xdist sub-worker |
131 | 131 | self._build_apm_test_server_image(config.option.github_token_file) |
132 | | - self._pull_test_agent_image() |
| 132 | + self._test_agent_factory.pull() |
133 | 133 | self._clean_containers() |
134 | 134 | self._clean_networks() |
135 | 135 |
|
@@ -161,11 +161,6 @@ def get_warmups(self): |
161 | 161 |
|
162 | 162 | return result |
163 | 163 |
|
164 | | - @retry(delay=10, tries=3) |
165 | | - def _pull_test_agent_image(self): |
166 | | - logger.stdout("Pulling test agent image...") |
167 | | - get_docker_client().images.pull(self.TEST_AGENT_IMAGE) |
168 | | - |
169 | 164 | def _clean_containers(self): |
170 | 165 | """Some containers may still exists from previous unfinished sessions""" |
171 | 166 |
|
@@ -258,10 +253,21 @@ def _build_apm_test_server_image(self, github_token_file: str) -> None: |
258 | 253 |
|
259 | 254 | logger.debug("Build tested container finished") |
260 | 255 |
|
261 | | - def create_docker_network(self, test_id: str) -> Network: |
| 256 | + @contextlib.contextmanager |
| 257 | + def _get_docker_network(self, test_id: str) -> Generator[str, None, None]: |
262 | 258 | docker_network_name = f"{_NETWORK_PREFIX}_{test_id}" |
| 259 | + network = get_docker_client().networks.create(name=docker_network_name, driver="bridge") |
263 | 260 |
|
264 | | - return get_docker_client().networks.create(name=docker_network_name, driver="bridge") |
| 261 | + try: |
| 262 | + yield network.name |
| 263 | + finally: |
| 264 | + try: |
| 265 | + network.remove() |
| 266 | + except: |
| 267 | + # It's possible (why?) of having some container not stopped. |
| 268 | + # If it happens, failing here makes stdout tough to understand. |
| 269 | + # Let's ignore this, later calls will clean the mess |
| 270 | + logger.info("Failed to remove network, ignoring the error") |
265 | 271 |
|
266 | 272 | @staticmethod |
267 | 273 | def get_host_port(worker_id: str, base_port: int) -> int: |
@@ -341,6 +347,28 @@ def get_junit_properties(self) -> dict[str, str]: |
341 | 347 |
|
342 | 348 | return result |
343 | 349 |
|
| 350 | + @contextlib.contextmanager |
| 351 | + def get_test_agent_api( |
| 352 | + self, |
| 353 | + worker_id: str, |
| 354 | + request: pytest.FixtureRequest, |
| 355 | + test_id: str, |
| 356 | + container_otlp_http_port: int, |
| 357 | + container_otlp_grpc_port: int, |
| 358 | + ) -> Generator[TestAgentAPI, None, None]: |
| 359 | + with ( |
| 360 | + self._get_docker_network(test_id) as docker_network, |
| 361 | + self._test_agent_factory.get_test_agent_api( |
| 362 | + request=request, |
| 363 | + worker_id=worker_id, |
| 364 | + docker_network=docker_network, |
| 365 | + container_name=f"ddapm-test-agent-{test_id}", |
| 366 | + container_otlp_http_port=container_otlp_http_port, |
| 367 | + container_otlp_grpc_port=container_otlp_grpc_port, |
| 368 | + ) as result, |
| 369 | + ): |
| 370 | + yield result |
| 371 | + |
344 | 372 |
|
345 | 373 | def _get_base_directory() -> str: |
346 | 374 | return str(Path.cwd()) |
|
0 commit comments