22
33import static org .junit .jupiter .api .Assertions .assertEquals ;
44import static org .junit .jupiter .api .Assertions .assertNotNull ;
5- import static org .junit .jupiter .api .Assumptions . assumeTrue ;
5+ import static org .junit .jupiter .api .Assertions . assertTrue ;
66import static org .junit .jupiter .api .condition .JRE .JAVA_11 ;
77import static org .junit .jupiter .api .condition .JRE .JAVA_8 ;
88import static org .mockito .ArgumentMatchers .any ;
1212import static org .mockito .Mockito .never ;
1313import static org .mockito .Mockito .verify ;
1414import static org .mockito .Mockito .when ;
15- import static utils .TestHelper .setFieldInConfig ;
1615
1716import com .datadog .debugger .util .RemoteConfigHelper ;
1817import datadog .common .container .ContainerInfo ;
2221import datadog .trace .api .git .GitInfoProvider ;
2322import datadog .trace .bootstrap .instrumentation .api .Tags ;
2423import datadog .trace .test .util .ControllableEnvironmentVariables ;
25- import datadog .trace .test .util .Flaky ;
2624import java .io .BufferedReader ;
2725import java .io .IOException ;
2826import java .io .InputStreamReader ;
5149public class DebuggerAgentTest {
5250
5351 public static final String URL_PATH = "/foo" ;
52+ @ Mock Config config ;
5453 @ Mock Instrumentation inst ;
5554 final MockWebServer server = new MockWebServer ();
5655 HttpUrl url ;
57-
5856 private ControllableEnvironmentVariables env = ControllableEnvironmentVariables .setup ();
5957
6058 private static void setFieldInContainerInfo (
@@ -76,6 +74,7 @@ public void setUp() {
7674
7775 @ AfterEach
7876 public void tearDown () throws IOException {
77+ DebuggerAgent .reset ();
7978 try {
8079 server .shutdown ();
8180 } catch (final IOException e ) {
@@ -86,64 +85,66 @@ public void tearDown() throws IOException {
8685 @ Test
8786 @ EnabledOnJre ({JAVA_8 , JAVA_11 })
8887 public void runDisabled () {
89- setFieldInConfig ( Config . get (), "dynamicInstrumentationEnabled" , false );
90- DebuggerAgent .run (inst , new SharedCommunicationObjects ());
88+ when ( config . isDynamicInstrumentationEnabled ()). thenReturn ( false );
89+ DebuggerAgent .run (config , inst , new SharedCommunicationObjects ());
9190 verify (inst , never ()).addTransformer (any (), eq (true ));
9291 }
9392
94- @ Flaky
9593 @ Test
9694 @ EnabledOnJre ({JAVA_8 , JAVA_11 })
9795 public void runEnabledWithDatadogAgent () throws InterruptedException , IOException {
98- MockWebServer datadogAgentServer = new MockWebServer ();
99- HttpUrl datadogAgentUrl = datadogAgentServer .url (URL_PATH );
100- setFieldInConfig (Config .get (), "dynamicInstrumentationEnabled" , true );
101- setFieldInConfig (Config .get (), "remoteConfigEnabled" , true );
102- setFieldInConfig (Config .get (), "dynamicInstrumentationSnapshotUrl" , datadogAgentUrl .toString ());
103- setFieldInConfig (Config .get (), "agentUrl" , datadogAgentUrl .toString ());
104- setFieldInConfig (Config .get (), "agentHost" , "localhost" );
105- setFieldInConfig (Config .get (), "agentPort" , datadogAgentServer .getPort ());
106- setFieldInConfig (Config .get (), "dynamicInstrumentationMaxPayloadSize" , 4096L );
96+ when (config .isDynamicInstrumentationEnabled ()).thenReturn (true );
97+ when (config .isRemoteConfigEnabled ()).thenReturn (true );
98+ when (config .getAgentUrl ()).thenReturn (url .toString ());
99+ when (config .getDynamicInstrumentationUploadBatchSize ()).thenReturn (100 );
100+ when (config .getRemoteConfigTargetsKeyId ())
101+ .thenReturn (Config .get ().getRemoteConfigTargetsKeyId ());
102+ when (config .getRemoteConfigTargetsKey ()).thenReturn (Config .get ().getRemoteConfigTargetsKey ());
103+ when (config .getFinalDebuggerSymDBUrl ()).thenReturn ("http://localhost:8126/symdb/v1/input" );
104+ when (config .getRemoteConfigMaxPayloadSizeBytes ())
105+ .thenReturn (Config .get ().getRemoteConfigMaxPayloadSizeBytes ());
106+ assertTrue (config .isDynamicInstrumentationEnabled ());
107107 setFieldInContainerInfo (ContainerInfo .get (), "containerId" , "" );
108108 String infoContent =
109109 "{\" endpoints\" : [\" v0.4/traces\" , \" debugger/v1/input\" , \" debugger/v1/diagnostics\" , \" v0.7/config\" ] }" ;
110- datadogAgentServer .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
111- datadogAgentServer .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
110+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
111+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
112112 try (BufferedReader reader =
113113 new BufferedReader (
114114 new InputStreamReader (
115115 DebuggerAgentTest .class .getResourceAsStream ("/test_probe.json" )))) {
116116 String content = reader .lines ().collect (Collectors .joining ("\n " ));
117117 String rcContent = RemoteConfigHelper .encode (content , "petclinic" );
118- datadogAgentServer .enqueue (new MockResponse ().setResponseCode (200 ).setBody (rcContent ));
118+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (rcContent ));
119119 } catch (IOException e ) {
120120 e .printStackTrace ();
121121 }
122122 SharedCommunicationObjects sharedCommunicationObjects = new SharedCommunicationObjects ();
123- DebuggerAgent .run (inst , sharedCommunicationObjects );
123+ DebuggerAgent .run (config , inst , sharedCommunicationObjects );
124124 ConfigurationPoller configurationPoller =
125- ( ConfigurationPoller ) sharedCommunicationObjects .configurationPoller (Config . get () );
125+ sharedCommunicationObjects .configurationPoller (config );
126126 configurationPoller .start ();
127127 RecordedRequest request ;
128128 do {
129- request = datadogAgentServer .takeRequest (5 , TimeUnit .SECONDS );
129+ request = server .takeRequest (5 , TimeUnit .SECONDS );
130130 assertNotNull (request );
131131 } while ("/info" .equals (request .getPath ()));
132132 assertEquals ("/v0.7/config" , request .getPath ());
133133 DebuggerAgent .stop ();
134- datadogAgentServer .shutdown ();
135134 }
136135
137136 @ Test
138137 @ EnabledOnJre ({JAVA_8 , JAVA_11 })
139138 public void runEnabledWithUnsupportedDatadogAgent () throws InterruptedException {
140- setFieldInConfig (Config .get (), "dynamicInstrumentationEnabled" , true );
141- setFieldInConfig (Config .get (), "dynamicInstrumentationSnapshotUrl" , url .toString ());
142- setFieldInConfig (Config .get (), "agentUrl" , url .toString ());
143- setFieldInConfig (Config .get (), "dynamicInstrumentationMaxPayloadSize" , 1024L );
139+ when (config .isDynamicInstrumentationEnabled ()).thenReturn (true );
140+ when (config .getAgentUrl ()).thenReturn (url .toString ());
141+ when (config .getFinalDebuggerSnapshotUrl ())
142+ .thenReturn ("http://localhost:8126/debugger/v1/input" );
143+ when (config .getDynamicInstrumentationUploadBatchSize ()).thenReturn (100 );
144+ when (config .getFinalDebuggerSymDBUrl ()).thenReturn ("http://localhost:8126/symdb/v1/input" );
144145 String infoContent = "{\" endpoints\" : [\" v0.4/traces\" ]}" ;
145146 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
146- DebuggerAgent .run (inst , new SharedCommunicationObjects ());
147+ DebuggerAgent .run (config , inst , new SharedCommunicationObjects ());
147148 verify (inst , never ()).addTransformer (any (), eq (true ));
148149 }
149150
@@ -152,18 +153,18 @@ public void runEnabledWithUnsupportedDatadogAgent() throws InterruptedException
152153 public void readFromFile () throws URISyntaxException {
153154 URL res = getClass ().getClassLoader ().getResource ("test_probe_file.json" );
154155 String probeDefinitionPath = Paths .get (res .toURI ()).toFile ().getAbsolutePath ();
155- setFieldInConfig (Config .get (), "serviceName" , "petclinic" );
156- setFieldInConfig (Config .get (), "dynamicInstrumentationEnabled" , true );
157- setFieldInConfig (Config .get (), "dynamicInstrumentationSnapshotUrl" , url .toString ());
158- setFieldInConfig (Config .get (), "agentUrl" , url .toString ());
159- setFieldInConfig (Config .get (), "dynamicInstrumentationMaxPayloadSize" , 4096L );
160- setFieldInConfig (Config .get (), "dynamicInstrumentationProbeFile" , probeDefinitionPath );
156+ when (config .getServiceName ()).thenReturn ("petclinic" );
157+ when (config .isDynamicInstrumentationEnabled ()).thenReturn (true );
158+ when (config .getAgentUrl ()).thenReturn (url .toString ());
159+ when (config .getDynamicInstrumentationMaxPayloadSize ()).thenReturn (4096L );
160+ when (config .getDynamicInstrumentationProbeFile ()).thenReturn (probeDefinitionPath );
161+ when (config .getDynamicInstrumentationUploadBatchSize ()).thenReturn (100 );
162+ when (config .getFinalDebuggerSymDBUrl ()).thenReturn ("http://localhost:8126/symdb/v1/input" );
161163 String infoContent =
162- "{\" endpoints\" : [\" v0.4/traces\" , \" debugger/v1/input\" , \" v0.7/config\" ] }" ;
164+ "{\" endpoints\" : [\" v0.4/traces\" , \" debugger/v1/input\" , \" debugger/v1/diagnostics \" , \" v0.7/config\" ] }" ;
163165 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
164- // sometimes this test fails because getAllLoadedClasses returns null
165- assumeTrue (inst .getAllLoadedClasses () != null );
166- DebuggerAgent .run (inst , new SharedCommunicationObjects ());
166+ when (inst .getAllLoadedClasses ()).thenReturn (new Class [0 ]);
167+ DebuggerAgent .run (config , inst , new SharedCommunicationObjects ());
167168 verify (inst , atLeastOnce ()).addTransformer (any (), eq (true ));
168169 }
169170
0 commit comments