diff --git a/tests/parametric/conftest.py b/tests/parametric/conftest.py index 857e5be9c17..9fee9d6d197 100644 --- a/tests/parametric/conftest.py +++ b/tests/parametric/conftest.py @@ -297,8 +297,8 @@ def restart_and_get_runtime_id(test_agent: TestAgentAPI, test_library: APMLibrar return test_agent.wait_for_telemetry_runtime_id(exclude=previous_runtime_id) -def nodejs_startup_config(test_library: APMLibrary) -> dict: - """Parse the tracer's published `DATADOG TRACER CONFIGURATION - {json}` startup line. +def nodejs_startup_config(test_library: APMLibrary) -> dict[str, object]: + """Read the tracer's startup configuration from plain text or a JSON log message. Some facts have no telemetry signal: an unreachable agent never delivers telemetry (so the resolved agent URL is unobservable that way), and OTEL_LOG_LEVEL=debug toggles debug logging @@ -307,7 +307,12 @@ def nodejs_startup_config(test_library: APMLibrary) -> dict: """ marker = "DATADOG TRACER CONFIGURATION - " for line in test_library.get_logs().splitlines(): - index = line.find(marker) - if index != -1: - return json.loads(line[index + len(marker) :]) + if marker not in line: + continue + message = json.loads(line).get("message") if line.startswith("{") else line + if not isinstance(message, str): + continue + _, separator, configuration = message.partition(marker) + if separator: + return json.loads(configuration) raise AssertionError("No 'DATADOG TRACER CONFIGURATION' line found in container logs") diff --git a/tests/test_the_test/test_nodejs_startup_config.py b/tests/test_the_test/test_nodejs_startup_config.py new file mode 100644 index 00000000000..16eb5a55ee7 --- /dev/null +++ b/tests/test_the_test/test_nodejs_startup_config.py @@ -0,0 +1,29 @@ +import json +from unittest.mock import Mock + +from tests.parametric.conftest import APMLibrary, nodejs_startup_config +from utils import pytest, scenarios + + +@scenarios.test_the_test +@pytest.mark.parametrize("structured", [False, True], ids=["plain", "json"]) +def test_nodejs_startup_config_reads_the_configuration(*, structured: bool) -> None: + configuration = {"agent_url": "http://[::1]:8126", "debug": True, "tags": {"example": 'value with "quotes"'}} + message = f"DATADOG TRACER CONFIGURATION - {json.dumps(configuration)}" + line = json.dumps({"status": "info", "message": message}) if structured else message + library = Mock(spec=APMLibrary) + library.get_logs.return_value = f"Server ready\n{line}\n" + + assert nodejs_startup_config(library) == configuration + + +@scenarios.test_the_test +def test_nodejs_startup_config_ignores_markers_outside_log_messages() -> None: + library = Mock(spec=APMLibrary) + library.get_logs.return_value = "\n".join( + json.dumps({"label": "DATADOG TRACER CONFIGURATION - {}", **fields}) + for fields in ({}, {"message": "Server ready"}) + ) + + with pytest.raises(AssertionError, match="No .*DATADOG TRACER CONFIGURATION"): + nodejs_startup_config(library)