1- from collections .abc import Callable
2-
31import pytest
42
53from tests .parametric .conftest import assert_nodejs_telemetry_config
@@ -14,27 +12,24 @@ def _configuration_event(
1412 "request_type" : "app-started" ,
1513 "runtime_id" : runtime_id ,
1614 "tracer_time" : tracer_time ,
17- "application" : {
18- "language_name" : "nodejs" ,
19- "language_version" : "24.4.1" ,
20- "service_name" : "parametric" ,
21- "tracer_version" : "5.62.0" ,
22- },
2315 "payload" : {"configuration" : configurations },
24- "seq_id" : 1 ,
2516 }
2617
2718
2819def _config (name : str , value : str , * , seq_id : int = 0 ) -> dict [str , object ]:
29- return {"name" : name , "origin" : "local_stable_config" , "seq_id" : seq_id , "value" : value }
20+ return {"name" : name , "seq_id" : seq_id , "value" : value }
21+
3022
23+ def _test_agent (monkeypatch : pytest .MonkeyPatch , responses : list [list [dict [str , object ]]]) -> TestAgentAPI :
24+ test_agent = object .__new__ (TestAgentAPI )
3125
32- def _telemetry (events : list [dict [str , object ]]) -> Callable [..., list [dict [str , object ]]]:
33- def get_events (* , clear : bool = False ) -> list [dict [str , object ]]:
26+ def telemetry (* , clear : bool = False ) -> list [dict [str , object ]]:
3427 assert clear is False
35- return events
28+ return responses . pop ( 0 ) if len ( responses ) > 1 else responses [ 0 ]
3629
37- return get_events
30+ monkeypatch .setattr (test_agent , "telemetry" , telemetry )
31+ monkeypatch .setattr ("utils.docker_fixtures._test_agent.time.sleep" , lambda _seconds : None )
32+ return test_agent
3833
3934
4035@scenarios .test_the_test
@@ -49,114 +44,57 @@ def test_nodejs_telemetry_assertion_waits_for_post_restart_runtime(monkeypatch:
4944 tracer_time = 2 ,
5045 configurations = [_config ("DD_TRACE_PROPAGATION_STYLE" , "tracecontext" )],
5146 )
52- telemetry_responses = [[stale_event ], [stale_event , current_event ]]
53-
54- test_agent = object .__new__ (TestAgentAPI )
55-
56- def telemetry (* , clear : bool = False ) -> list [dict [str , object ]]:
57- assert clear is False
58- if len (telemetry_responses ) > 1 :
59- return telemetry_responses .pop (0 )
60- return telemetry_responses [0 ]
61-
62- monkeypatch .setattr (test_agent , "telemetry" , telemetry )
47+ test_agent = _test_agent (monkeypatch , [[stale_event ], [stale_event , current_event ]])
48+ runtime_id = test_agent .wait_for_telemetry_runtime_id (exclude = "before-restart" )
6349
6450 assert_nodejs_telemetry_config (
6551 test_agent ,
6652 {"dd_trace_propagation_style" : "tracecontext" },
67- runtime_id = "after-restart" ,
53+ runtime_id = runtime_id ,
6854 )
6955
7056
7157@scenarios .test_the_test
72- def test_nodejs_telemetry_assertion_rejects_stale_matching_value (monkeypatch : pytest .MonkeyPatch ) -> None :
73- events = [
74- _configuration_event (
75- runtime_id = "before-restart" ,
76- tracer_time = 1 ,
77- configurations = [_config ("DD_TRACE_PROPAGATION_STYLE" , "tracecontext" )],
58+ @pytest .mark .parametrize (
59+ ("events" , "expected" ),
60+ [
61+ (
62+ [
63+ _configuration_event (
64+ runtime_id = "before-restart" ,
65+ tracer_time = 1 ,
66+ configurations = [_config ("DD_TRACE_PROPAGATION_STYLE" , "tracecontext" )],
67+ ),
68+ _configuration_event (
69+ runtime_id = "after-restart" ,
70+ tracer_time = 2 ,
71+ configurations = [_config ("DD_TRACE_PROPAGATION_STYLE" , "datadog" )],
72+ ),
73+ ],
74+ {"dd_trace_propagation_style" : "tracecontext" },
7875 ),
79- _configuration_event (
80- runtime_id = "after-restart" ,
81- tracer_time = 2 ,
82- configurations = [_config ("DD_TRACE_PROPAGATION_STYLE" , "datadog" )],
76+ (
77+ [
78+ _configuration_event (
79+ runtime_id = "after-restart" ,
80+ tracer_time = 2 ,
81+ configurations = [
82+ _config ("DD_SERVICE" , "expected" , seq_id = 0 ),
83+ _config ("DD_SERVICE" , "wrong" , seq_id = 1 ),
84+ ],
85+ )
86+ ],
87+ {"dd_service" : "expected" },
8388 ),
84- ]
85- test_agent = object .__new__ (TestAgentAPI )
86- monkeypatch .setattr (test_agent , "telemetry" , _telemetry (events ))
87- monkeypatch .setattr ("utils.docker_fixtures._test_agent.time.sleep" , lambda _seconds : None )
88-
89- with pytest .raises (AssertionError ):
90- assert_nodejs_telemetry_config (
91- test_agent ,
92- {"dd_trace_propagation_style" : "tracecontext" },
93- runtime_id = "after-restart" ,
94- )
95-
96-
97- @scenarios .test_the_test
98- def test_nodejs_telemetry_assertion_requires_one_runtime_to_match_all_values (
89+ ],
90+ ids = ["stale-match" , "superseded-sequence" ],
91+ )
92+ def test_nodejs_telemetry_assertion_rejects_invalid_runtime_snapshot (
9993 monkeypatch : pytest .MonkeyPatch ,
94+ events : list [dict [str , object ]],
95+ expected : dict [str , object ],
10096) -> None :
101- events = [
102- _configuration_event (
103- runtime_id = "before-restart" ,
104- tracer_time = 1 ,
105- configurations = [_config ("DD_SERVICE" , "expected" ), _config ("DD_ENV" , "wrong" )],
106- ),
107- _configuration_event (
108- runtime_id = "after-restart" ,
109- tracer_time = 2 ,
110- configurations = [_config ("DD_SERVICE" , "wrong" ), _config ("DD_ENV" , "expected" )],
111- ),
112- ]
113- test_agent = object .__new__ (TestAgentAPI )
114- monkeypatch .setattr (test_agent , "telemetry" , _telemetry (events ))
115- monkeypatch .setattr ("utils.docker_fixtures._test_agent.time.sleep" , lambda _seconds : None )
116-
117- with pytest .raises (AssertionError ):
118- assert_nodejs_telemetry_config (
119- test_agent ,
120- {"dd_service" : "expected" , "dd_env" : "expected" },
121- runtime_id = "after-restart" ,
122- )
123-
124-
125- @scenarios .test_the_test
126- def test_nodejs_telemetry_assertion_uses_latest_configuration_sequence (monkeypatch : pytest .MonkeyPatch ) -> None :
127- events = [
128- _configuration_event (
129- runtime_id = "after-restart" ,
130- tracer_time = 2 ,
131- configurations = [
132- _config ("DD_SERVICE" , "expected" , seq_id = 0 ),
133- _config ("DD_SERVICE" , "wrong" , seq_id = 1 ),
134- ],
135- )
136- ]
137- test_agent = object .__new__ (TestAgentAPI )
138- monkeypatch .setattr (test_agent , "telemetry" , _telemetry (events ))
139- monkeypatch .setattr ("utils.docker_fixtures._test_agent.time.sleep" , lambda _seconds : None )
97+ test_agent = _test_agent (monkeypatch , [events ])
14098
14199 with pytest .raises (AssertionError ):
142- assert_nodejs_telemetry_config (test_agent , {"dd_service" : "expected" }, runtime_id = "after-restart" )
143-
144-
145- @scenarios .test_the_test
146- def test_wait_for_telemetry_runtime_id_ignores_excluded_runtime (monkeypatch : pytest .MonkeyPatch ) -> None :
147- stale_event = _configuration_event (runtime_id = "before-restart" , tracer_time = 1 , configurations = [])
148- current_event = _configuration_event (runtime_id = "after-restart" , tracer_time = 2 , configurations = [])
149- telemetry_responses = [[stale_event ], [stale_event , current_event ]]
150- test_agent = object .__new__ (TestAgentAPI )
151-
152- def telemetry (* , clear : bool = False ) -> list [dict [str , object ]]:
153- assert clear is False
154- if len (telemetry_responses ) > 1 :
155- return telemetry_responses .pop (0 )
156- return telemetry_responses [0 ]
157-
158- monkeypatch .setattr (test_agent , "telemetry" , telemetry )
159-
160- runtime_id = test_agent .wait_for_telemetry_runtime_id (exclude = {"before-restart" })
161-
162- assert runtime_id == "after-restart"
100+ assert_nodejs_telemetry_config (test_agent , expected , runtime_id = "after-restart" )
0 commit comments