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 ;
5150public class DebuggerAgentTest {
5251
5352 public static final String URL_PATH = "/foo" ;
53+ @ Mock Config config ;
5454 @ Mock Instrumentation inst ;
5555 final MockWebServer server = new MockWebServer ();
5656 HttpUrl url ;
57-
5857 private ControllableEnvironmentVariables env = ControllableEnvironmentVariables .setup ();
5958
6059 private static void setFieldInContainerInfo (
@@ -76,6 +75,7 @@ public void setUp() {
7675
7776 @ AfterEach
7877 public void tearDown () throws IOException {
78+ DebuggerAgent .reset ();
7979 try {
8080 server .shutdown ();
8181 } catch (final IOException e ) {
@@ -86,64 +86,67 @@ public void tearDown() throws IOException {
8686 @ Test
8787 @ EnabledOnJre ({JAVA_8 , JAVA_11 })
8888 public void runDisabled () {
89- setFieldInConfig ( Config . get (), "dynamicInstrumentationEnabled" , false );
90- DebuggerAgent .run (inst , new SharedCommunicationObjects ());
89+ when ( config . isDynamicInstrumentationEnabled ()). thenReturn ( false );
90+ DebuggerAgent .run (config , inst , new SharedCommunicationObjects ());
9191 verify (inst , never ()).addTransformer (any (), eq (true ));
9292 }
9393
9494 @ Flaky
9595 @ Test
9696 @ EnabledOnJre ({JAVA_8 , JAVA_11 })
9797 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 );
98+ when (config .isDynamicInstrumentationEnabled ()).thenReturn (true );
99+ when (config .isRemoteConfigEnabled ()).thenReturn (true );
100+ when (config .getAgentUrl ()).thenReturn (url .toString ());
101+ when (config .getDynamicInstrumentationUploadBatchSize ()).thenReturn (100 );
102+ when (config .getRemoteConfigTargetsKeyId ())
103+ .thenReturn (Config .get ().getRemoteConfigTargetsKeyId ());
104+ when (config .getRemoteConfigTargetsKey ()).thenReturn (Config .get ().getRemoteConfigTargetsKey ());
105+ when (config .getFinalDebuggerSymDBUrl ()).thenReturn ("http://localhost:8126/symdb/v1/input" );
106+ when (config .getRemoteConfigMaxPayloadSizeBytes ())
107+ .thenReturn (Config .get ().getRemoteConfigMaxPayloadSizeBytes ());
108+ assertTrue (config .isDynamicInstrumentationEnabled ());
107109 setFieldInContainerInfo (ContainerInfo .get (), "containerId" , "" );
108110 String infoContent =
109111 "{\" 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 ));
112+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
113+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
112114 try (BufferedReader reader =
113115 new BufferedReader (
114116 new InputStreamReader (
115117 DebuggerAgentTest .class .getResourceAsStream ("/test_probe.json" )))) {
116118 String content = reader .lines ().collect (Collectors .joining ("\n " ));
117119 String rcContent = RemoteConfigHelper .encode (content , "petclinic" );
118- datadogAgentServer .enqueue (new MockResponse ().setResponseCode (200 ).setBody (rcContent ));
120+ server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (rcContent ));
119121 } catch (IOException e ) {
120122 e .printStackTrace ();
121123 }
122124 SharedCommunicationObjects sharedCommunicationObjects = new SharedCommunicationObjects ();
123- DebuggerAgent .run (inst , sharedCommunicationObjects );
125+ DebuggerAgent .run (config , inst , sharedCommunicationObjects );
124126 ConfigurationPoller configurationPoller =
125- ( ConfigurationPoller ) sharedCommunicationObjects .configurationPoller (Config . get () );
127+ sharedCommunicationObjects .configurationPoller (config );
126128 configurationPoller .start ();
127129 RecordedRequest request ;
128130 do {
129- request = datadogAgentServer .takeRequest (5 , TimeUnit .SECONDS );
131+ request = server .takeRequest (5 , TimeUnit .SECONDS );
130132 assertNotNull (request );
131133 } while ("/info" .equals (request .getPath ()));
132134 assertEquals ("/v0.7/config" , request .getPath ());
133135 DebuggerAgent .stop ();
134- datadogAgentServer .shutdown ();
135136 }
136137
137138 @ Test
138139 @ EnabledOnJre ({JAVA_8 , JAVA_11 })
139140 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 );
141+ when (config .isDynamicInstrumentationEnabled ()).thenReturn (true );
142+ when (config .getAgentUrl ()).thenReturn (url .toString ());
143+ when (config .getFinalDebuggerSnapshotUrl ())
144+ .thenReturn ("http://localhost:8126/debugger/v1/input" );
145+ when (config .getDynamicInstrumentationUploadBatchSize ()).thenReturn (100 );
146+ when (config .getFinalDebuggerSymDBUrl ()).thenReturn ("http://localhost:8126/symdb/v1/input" );
144147 String infoContent = "{\" endpoints\" : [\" v0.4/traces\" ]}" ;
145148 server .enqueue (new MockResponse ().setResponseCode (200 ).setBody (infoContent ));
146- DebuggerAgent .run (inst , new SharedCommunicationObjects ());
149+ DebuggerAgent .run (config , inst , new SharedCommunicationObjects ());
147150 verify (inst , never ()).addTransformer (any (), eq (true ));
148151 }
149152
@@ -152,18 +155,18 @@ public void runEnabledWithUnsupportedDatadogAgent() throws InterruptedException
152155 public void readFromFile () throws URISyntaxException {
153156 URL res = getClass ().getClassLoader ().getResource ("test_probe_file.json" );
154157 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 );
158+ when (config .getServiceName ()).thenReturn ("petclinic" );
159+ when (config .isDynamicInstrumentationEnabled ()).thenReturn (true );
160+ when (config .getAgentUrl ()).thenReturn (url .toString ());
161+ when (config .getDynamicInstrumentationMaxPayloadSize ()).thenReturn (4096L );
162+ when (config .getDynamicInstrumentationProbeFile ()).thenReturn (probeDefinitionPath );
163+ when (config .getDynamicInstrumentationUploadBatchSize ()).thenReturn (100 );
164+ when (config .getFinalDebuggerSymDBUrl ()).thenReturn ("http://localhost:8126/symdb/v1/input" );
161165 String infoContent =
162- "{\" endpoints\" : [\" v0.4/traces\" , \" debugger/v1/input\" , \" v0.7/config\" ] }" ;
166+ "{\" endpoints\" : [\" v0.4/traces\" , \" debugger/v1/input\" , \" debugger/v1/diagnostics \" , \" v0.7/config\" ] }" ;
163167 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 ());
168+ when (inst .getAllLoadedClasses ()).thenReturn (new Class [0 ]);
169+ DebuggerAgent .run (config , inst , new SharedCommunicationObjects ());
167170 verify (inst , atLeastOnce ()).addTransformer (any (), eq (true ));
168171 }
169172
0 commit comments