From 7a03d47608ec01fcd7d9a189815100b169f778a7 Mon Sep 17 00:00:00 2001 From: Pranav Sharma Date: Wed, 18 Sep 2024 20:17:29 +0000 Subject: [PATCH] Add test for detecting cloud run job --- .../gcp/resource/GCPResourceProviderTest.java | 48 ++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java index 2b73b0d76..a399fce7f 100644 --- a/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java +++ b/gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java @@ -16,6 +16,8 @@ import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_ID; import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_NAME; import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_MACHINE_TYPE; +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_EXECUTION_KEY; +import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_TASK_INDEX; import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION; import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION_TYPE; import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_NAME; @@ -27,6 +29,7 @@ import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID; import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_NAME; import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_REVISION; +import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.GCP_CLOUD_RUN_JOB_TASK_INDEX; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_ACCOUNT_ID; import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_AVAILABILITY_ZONE; @@ -42,6 +45,7 @@ import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_INSTANCE; import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_NAME; import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_VERSION; +import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_CLOUD_RUN_JOB_EXECUTION; import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_GCE_INSTANCE_HOSTNAME; import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_GCE_INSTANCE_NAME; import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_ID; @@ -133,6 +137,23 @@ private static DetectedPlatform generateMockServerlessPlatform( return mockServerlessPlatform; } + private static DetectedPlatform generateMockGcrJobPlatform() { + Map mockAttributes = + new HashMap<>( + ImmutableMap.of( + SERVERLESS_COMPUTE_NAME, "serverless-job", + SERVERLESS_COMPUTE_INSTANCE_ID, "serverless-instance-id", + SERVERLESS_COMPUTE_CLOUD_REGION, "us-central1", + GCR_JOB_TASK_INDEX, "1", + GCR_JOB_EXECUTION_KEY, "serverless-job-a1b2c3")); + DetectedPlatform mockServerlessPlatform = Mockito.mock(DetectedPlatform.class); + Mockito.when(mockServerlessPlatform.getSupportedPlatform()) + .thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB); + Mockito.when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes); + Mockito.when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID); + return mockServerlessPlatform; + } + private static DetectedPlatform generateMockGaePlatform() { Map mockAttributes = new HashMap<>( @@ -274,7 +295,7 @@ private static void verifyGkeMapping(Resource gotResource, DetectedPlatform dete } @Test - public void testGcrResourceAttributesMapping() { + public void testGcrServiceResourceAttributesMapping() { GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class); DetectedPlatform mockPlatform = generateMockServerlessPlatform(GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN); @@ -321,6 +342,31 @@ private static void verifyServerlessMapping( .containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)); } + @Test + public void testGcrJobResourceAttributesMapping() { + GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class); + DetectedPlatform mockPlatform = generateMockGcrJobPlatform(); + Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform); + Map detectedAttributes = mockPlatform.getAttributes(); + + Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps); + verify(mockPlatform, Mockito.times(1)).getProjectId(); + + assertThat(gotResource.getAttributes()) + .hasSize(8) + .containsEntry(CLOUD_PROVIDER, GCP) + .containsEntry(CLOUD_PLATFORM, GCP_CLOUD_RUN) + .containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID) + .containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME)) + .containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME)) + .containsEntry(FAAS_INSTANCE, detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID)) + .containsEntry(GCP_CLOUD_RUN_JOB_EXECUTION, detectedAttributes.get(GCR_JOB_EXECUTION_KEY)) + .containsEntry( + GCP_CLOUD_RUN_JOB_TASK_INDEX, + Integer.parseInt(detectedAttributes.get(GCR_JOB_TASK_INDEX))) + .containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION)); + } + @Test public void testGaeResourceAttributeMapping() { GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);