5353import static io .opentelemetry .semconv .incubating .HostIncubatingAttributes .HOST_TYPE ;
5454import static io .opentelemetry .semconv .incubating .K8sIncubatingAttributes .K8S_CLUSTER_NAME ;
5555import static org .assertj .core .api .Assertions .fail ;
56+ import static org .mockito .Mockito .mock ;
57+ import static org .mockito .Mockito .times ;
5658import static org .mockito .Mockito .verify ;
59+ import static org .mockito .Mockito .when ;
5760
5861import com .google .common .collect .ImmutableMap ;
5962import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
6467import java .util .Map ;
6568import java .util .ServiceLoader ;
6669import org .junit .jupiter .api .Test ;
67- import org .mockito .Mockito ;
6870
6971class GCPResourceProviderTest {
7072 private static final String DUMMY_PROJECT_ID = "google-pid" ;
71- private final ConfigProperties mockConfigProps = Mockito . mock (ConfigProperties .class );
73+ private final ConfigProperties mockConfigProps = mock (ConfigProperties .class );
7274 private final Map <String , String > mockGKECommonAttributes =
7375 new HashMap <>(
7476 ImmutableMap .of (
@@ -86,11 +88,11 @@ private static DetectedPlatform generateMockGcePlatform() {
8688 GCE_INSTANCE_NAME , "instance-name" ,
8789 GCE_MACHINE_TYPE , "gce-m2" ,
8890 GCE_INSTANCE_HOSTNAME , "instance-hostname" ));
89- DetectedPlatform mockGCEPlatform = Mockito . mock (DetectedPlatform .class );
90- Mockito . when (mockGCEPlatform .getSupportedPlatform ())
91+ DetectedPlatform mockGCEPlatform = mock (DetectedPlatform .class );
92+ when (mockGCEPlatform .getSupportedPlatform ())
9193 .thenReturn (GcpPlatformDetector .SupportedPlatform .GOOGLE_COMPUTE_ENGINE );
92- Mockito . when (mockGCEPlatform .getAttributes ()).thenReturn (mockAttributes );
93- Mockito . when (mockGCEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
94+ when (mockGCEPlatform .getAttributes ()).thenReturn (mockAttributes );
95+ when (mockGCEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
9496 return mockGCEPlatform ;
9597 }
9698
@@ -103,11 +105,11 @@ private DetectedPlatform generateMockGkePlatform(String gkeClusterLocationType)
103105 }
104106 mockAttributes .put (GKE_CLUSTER_LOCATION_TYPE , gkeClusterLocationType );
105107
106- DetectedPlatform mockGKEPlatform = Mockito . mock (DetectedPlatform .class );
107- Mockito . when (mockGKEPlatform .getSupportedPlatform ())
108+ DetectedPlatform mockGKEPlatform = mock (DetectedPlatform .class );
109+ when (mockGKEPlatform .getSupportedPlatform ())
108110 .thenReturn (GcpPlatformDetector .SupportedPlatform .GOOGLE_KUBERNETES_ENGINE );
109- Mockito . when (mockGKEPlatform .getAttributes ()).thenReturn (mockAttributes );
110- Mockito . when (mockGKEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
111+ when (mockGKEPlatform .getAttributes ()).thenReturn (mockAttributes );
112+ when (mockGKEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
111113 return mockGKEPlatform ;
112114 }
113115
@@ -128,10 +130,10 @@ private static DetectedPlatform generateMockServerlessPlatform(
128130 SERVERLESS_COMPUTE_INSTANCE_ID , "serverless-instance-id" ,
129131 SERVERLESS_COMPUTE_CLOUD_REGION , "us-central1" ,
130132 SERVERLESS_COMPUTE_AVAILABILITY_ZONE , "us-central1-b" ));
131- DetectedPlatform mockServerlessPlatform = Mockito . mock (DetectedPlatform .class );
132- Mockito . when (mockServerlessPlatform .getSupportedPlatform ()).thenReturn (platform );
133- Mockito . when (mockServerlessPlatform .getAttributes ()).thenReturn (mockAttributes );
134- Mockito . when (mockServerlessPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
133+ DetectedPlatform mockServerlessPlatform = mock (DetectedPlatform .class );
134+ when (mockServerlessPlatform .getSupportedPlatform ()).thenReturn (platform );
135+ when (mockServerlessPlatform .getAttributes ()).thenReturn (mockAttributes );
136+ when (mockServerlessPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
135137 return mockServerlessPlatform ;
136138 }
137139
@@ -144,11 +146,11 @@ private static DetectedPlatform generateMockGcrJobPlatform() {
144146 SERVERLESS_COMPUTE_CLOUD_REGION , "us-central1" ,
145147 GCR_JOB_TASK_INDEX , "1" ,
146148 GCR_JOB_EXECUTION_KEY , "serverless-job-a1b2c3" ));
147- DetectedPlatform mockServerlessPlatform = Mockito . mock (DetectedPlatform .class );
148- Mockito . when (mockServerlessPlatform .getSupportedPlatform ())
149+ DetectedPlatform mockServerlessPlatform = mock (DetectedPlatform .class );
150+ when (mockServerlessPlatform .getSupportedPlatform ())
149151 .thenReturn (GcpPlatformDetector .SupportedPlatform .GOOGLE_CLOUD_RUN_JOB );
150- Mockito . when (mockServerlessPlatform .getAttributes ()).thenReturn (mockAttributes );
151- Mockito . when (mockServerlessPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
152+ when (mockServerlessPlatform .getAttributes ()).thenReturn (mockAttributes );
153+ when (mockServerlessPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
152154 return mockServerlessPlatform ;
153155 }
154156
@@ -161,11 +163,11 @@ private static DetectedPlatform generateMockGaePlatform() {
161163 GAE_INSTANCE_ID , "gae-instance-id" ,
162164 GAE_CLOUD_REGION , "us-central1" ,
163165 GAE_AVAILABILITY_ZONE , "us-central1-b" ));
164- DetectedPlatform mockGAEPlatform = Mockito . mock (DetectedPlatform .class );
165- Mockito . when (mockGAEPlatform .getSupportedPlatform ())
166+ DetectedPlatform mockGAEPlatform = mock (DetectedPlatform .class );
167+ when (mockGAEPlatform .getSupportedPlatform ())
166168 .thenReturn (GcpPlatformDetector .SupportedPlatform .GOOGLE_APP_ENGINE );
167- Mockito . when (mockGAEPlatform .getAttributes ()).thenReturn (mockAttributes );
168- Mockito . when (mockGAEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
169+ when (mockGAEPlatform .getAttributes ()).thenReturn (mockAttributes );
170+ when (mockGAEPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
169171 return mockGAEPlatform ;
170172 }
171173
@@ -176,22 +178,22 @@ private static DetectedPlatform generateMockUnknownPlatform() {
176178 GCE_INSTANCE_ID , "instance-id" ,
177179 GCE_CLOUD_REGION , "australia-southeast1" ));
178180
179- DetectedPlatform mockUnknownPlatform = Mockito . mock (DetectedPlatform .class );
180- Mockito . when (mockUnknownPlatform .getSupportedPlatform ())
181+ DetectedPlatform mockUnknownPlatform = mock (DetectedPlatform .class );
182+ when (mockUnknownPlatform .getSupportedPlatform ())
181183 .thenReturn (GcpPlatformDetector .SupportedPlatform .UNKNOWN_PLATFORM );
182- Mockito . when (mockUnknownPlatform .getAttributes ()).thenReturn (mockAttributes );
184+ when (mockUnknownPlatform .getAttributes ()).thenReturn (mockAttributes );
183185 return mockUnknownPlatform ;
184186 }
185187
186188 @ Test
187189 void testGceResourceAttributesMapping () {
188- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
190+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
189191 DetectedPlatform mockPlatform = generateMockGcePlatform ();
190- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
192+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
191193 Map <String , String > detectedAttributes = mockPlatform .getAttributes ();
192194
193195 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
194- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
196+ verify (mockPlatform , times (1 )).getProjectId ();
195197
196198 assertThat (gotResource .getAttributes ())
197199 .hasSize (10 )
@@ -209,12 +211,12 @@ void testGceResourceAttributesMapping() {
209211
210212 @ Test
211213 void testGkeResourceAttributesMapping_LocationTypeRegion () {
212- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
214+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
213215 DetectedPlatform mockPlatform = generateMockGkePlatform (GKE_LOCATION_TYPE_REGION );
214- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
216+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
215217
216218 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
217- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
219+ verify (mockPlatform , times (1 )).getProjectId ();
218220
219221 verifyGkeMapping (gotResource , mockPlatform );
220222 assertThat (gotResource .getAttributes ())
@@ -226,12 +228,12 @@ void testGkeResourceAttributesMapping_LocationTypeRegion() {
226228
227229 @ Test
228230 void testGkeResourceAttributesMapping_LocationTypeZone () {
229- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
231+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
230232 DetectedPlatform mockPlatform = generateMockGkePlatform (GKE_LOCATION_TYPE_ZONE );
231- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
233+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
232234
233235 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
234- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
236+ verify (mockPlatform , times (1 )).getProjectId ();
235237
236238 verifyGkeMapping (gotResource , mockPlatform );
237239 assertThat (gotResource .getAttributes ())
@@ -248,18 +250,18 @@ void testGkeResourceAttributesMapping_LocationTypeInvalid() {
248250 mockGKEAttributes .put (GKE_CLUSTER_LOCATION_TYPE , "INVALID" );
249251 mockGKEAttributes .put (GKE_CLUSTER_LOCATION , "some-location" );
250252
251- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
252- DetectedPlatform mockPlatform = Mockito . mock (DetectedPlatform .class );
253- Mockito . when (mockPlatform .getSupportedPlatform ())
253+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
254+ DetectedPlatform mockPlatform = mock (DetectedPlatform .class );
255+ when (mockPlatform .getSupportedPlatform ())
254256 .thenReturn (GcpPlatformDetector .SupportedPlatform .GOOGLE_KUBERNETES_ENGINE );
255- Mockito . when (mockPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
256- Mockito . when (mockPlatform .getAttributes ()).thenReturn (mockGKEAttributes );
257- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
257+ when (mockPlatform .getProjectId ()).thenReturn (DUMMY_PROJECT_ID );
258+ when (mockPlatform .getAttributes ()).thenReturn (mockGKEAttributes );
259+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
258260
259261 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
260262
261263 verifyGkeMapping (gotResource , mockPlatform );
262- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
264+ verify (mockPlatform , times (1 )).getProjectId ();
263265 assertThat (gotResource .getAttributes ())
264266 .hasSize (5 )
265267 .containsEntry (CLOUD_ACCOUNT_ID , DUMMY_PROJECT_ID )
@@ -269,12 +271,12 @@ void testGkeResourceAttributesMapping_LocationTypeInvalid() {
269271
270272 @ Test
271273 void testGkeResourceAttributesMapping_LocationMissing () {
272- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
274+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
273275 DetectedPlatform mockPlatform = generateMockGkePlatform ("" );
274- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
276+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
275277
276278 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
277- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
279+ verify (mockPlatform , times (1 )).getProjectId ();
278280 verifyGkeMapping (gotResource , mockPlatform );
279281 assertThat (gotResource .getAttributes ())
280282 .hasSize (5 )
@@ -294,13 +296,13 @@ private static void verifyGkeMapping(Resource gotResource, DetectedPlatform dete
294296
295297 @ Test
296298 void testGcrServiceResourceAttributesMapping () {
297- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
299+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
298300 DetectedPlatform mockPlatform =
299301 generateMockServerlessPlatform (GcpPlatformDetector .SupportedPlatform .GOOGLE_CLOUD_RUN );
300- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
302+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
301303
302304 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
303- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
305+ verify (mockPlatform , times (1 )).getProjectId ();
304306
305307 verifyServerlessMapping (gotResource , mockPlatform );
306308 assertThat (gotResource .getAttributes ())
@@ -311,14 +313,14 @@ void testGcrServiceResourceAttributesMapping() {
311313
312314 @ Test
313315 void testGcfResourceAttributeMapping () {
314- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
316+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
315317 DetectedPlatform mockPlatform =
316318 generateMockServerlessPlatform (
317319 GcpPlatformDetector .SupportedPlatform .GOOGLE_CLOUD_FUNCTIONS );
318- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
320+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
319321
320322 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
321- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
323+ verify (mockPlatform , times (1 )).getProjectId ();
322324
323325 verifyServerlessMapping (gotResource , mockPlatform );
324326 assertThat (gotResource .getAttributes ())
@@ -342,13 +344,13 @@ private static void verifyServerlessMapping(
342344
343345 @ Test
344346 void testGcrJobResourceAttributesMapping () {
345- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
347+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
346348 DetectedPlatform mockPlatform = generateMockGcrJobPlatform ();
347- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
349+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
348350 Map <String , String > detectedAttributes = mockPlatform .getAttributes ();
349351
350352 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
351- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
353+ verify (mockPlatform , times (1 )).getProjectId ();
352354
353355 assertThat (gotResource .getAttributes ())
354356 .hasSize (8 )
@@ -365,15 +367,29 @@ void testGcrJobResourceAttributesMapping() {
365367 .containsEntry (CLOUD_REGION , detectedAttributes .get (SERVERLESS_COMPUTE_CLOUD_REGION ));
366368 }
367369
370+ @ Test
371+ void testGcrJobResourceAttributesMappingWithMalformedTaskIndex () {
372+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
373+ DetectedPlatform mockPlatform = generateMockGcrJobPlatform ();
374+ mockPlatform .getAttributes ().put (GCR_JOB_TASK_INDEX , "not-a-number" );
375+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
376+
377+ Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
378+
379+ assertThat (gotResource .getAttributes ())
380+ .doesNotContainKey (GCP_CLOUD_RUN_JOB_TASK_INDEX )
381+ .containsEntry (CLOUD_PROVIDER , GCP );
382+ }
383+
368384 @ Test
369385 void testGaeResourceAttributeMapping () {
370- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
386+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
371387 DetectedPlatform mockPlatform = generateMockGaePlatform ();
372- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
388+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
373389 Map <String , String > detectedAttributes = mockPlatform .getAttributes ();
374390
375391 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
376- verify (mockPlatform , Mockito . times (1 )).getProjectId ();
392+ verify (mockPlatform , times (1 )).getProjectId ();
377393
378394 assertThat (gotResource .getAttributes ())
379395 .hasSize (8 )
@@ -389,9 +405,9 @@ void testGaeResourceAttributeMapping() {
389405
390406 @ Test
391407 void testUnknownPlatformResourceAttributesMapping () {
392- GcpPlatformDetector mockDetector = Mockito . mock (GcpPlatformDetector .class );
408+ GcpPlatformDetector mockDetector = mock (GcpPlatformDetector .class );
393409 DetectedPlatform mockPlatform = generateMockUnknownPlatform ();
394- Mockito . when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
410+ when (mockDetector .detectPlatform ()).thenReturn (mockPlatform );
395411
396412 Resource gotResource = new GCPResourceProvider (mockDetector ).createResource (mockConfigProps );
397413 assertThat (gotResource .getAttributes ()).isEmpty ();
0 commit comments