Skip to content

Commit 4ea7b97

Browse files
committed
Add test for detecting cloud run job
1 parent c64be45 commit 4ea7b97

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

gcp-resources/src/test/java/io/opentelemetry/contrib/gcp/resource/GCPResourceProviderTest.java

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_ID;
1717
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_INSTANCE_NAME;
1818
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCE_MACHINE_TYPE;
19+
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_EXECUTION_KEY;
20+
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GCR_JOB_TASK_INDEX;
1921
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION;
2022
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_LOCATION_TYPE;
2123
import static com.google.cloud.opentelemetry.detection.AttributeKeys.GKE_CLUSTER_NAME;
@@ -27,6 +29,7 @@
2729
import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_INSTANCE_ID;
2830
import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_NAME;
2931
import static com.google.cloud.opentelemetry.detection.AttributeKeys.SERVERLESS_COMPUTE_REVISION;
32+
import static io.opentelemetry.contrib.gcp.resource.IncubatingAttributes.GCP_CLOUD_RUN_JOB_TASK_INDEX;
3033
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
3134
import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_ACCOUNT_ID;
3235
import static io.opentelemetry.semconv.incubating.CloudIncubatingAttributes.CLOUD_AVAILABILITY_ZONE;
@@ -42,6 +45,7 @@
4245
import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_INSTANCE;
4346
import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_NAME;
4447
import static io.opentelemetry.semconv.incubating.FaasIncubatingAttributes.FAAS_VERSION;
48+
import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_CLOUD_RUN_JOB_EXECUTION;
4549
import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_GCE_INSTANCE_HOSTNAME;
4650
import static io.opentelemetry.semconv.incubating.GcpIncubatingAttributes.GCP_GCE_INSTANCE_NAME;
4751
import static io.opentelemetry.semconv.incubating.HostIncubatingAttributes.HOST_ID;
@@ -133,6 +137,23 @@ private static DetectedPlatform generateMockServerlessPlatform(
133137
return mockServerlessPlatform;
134138
}
135139

140+
private static DetectedPlatform generateMockGcrJobPlatform() {
141+
Map<String, String> mockAttributes =
142+
new HashMap<>(
143+
ImmutableMap.of(
144+
SERVERLESS_COMPUTE_NAME, "serverless-job",
145+
SERVERLESS_COMPUTE_INSTANCE_ID, "serverless-instance-id",
146+
SERVERLESS_COMPUTE_CLOUD_REGION, "us-central1",
147+
GCR_JOB_TASK_INDEX, "1",
148+
GCR_JOB_EXECUTION_KEY, "serverless-job-a1b2c3"));
149+
DetectedPlatform mockServerlessPlatform = Mockito.mock(DetectedPlatform.class);
150+
Mockito.when(mockServerlessPlatform.getSupportedPlatform())
151+
.thenReturn(GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN_JOB);
152+
Mockito.when(mockServerlessPlatform.getAttributes()).thenReturn(mockAttributes);
153+
Mockito.when(mockServerlessPlatform.getProjectId()).thenReturn(DUMMY_PROJECT_ID);
154+
return mockServerlessPlatform;
155+
}
156+
136157
private static DetectedPlatform generateMockGaePlatform() {
137158
Map<String, String> mockAttributes =
138159
new HashMap<>(
@@ -274,7 +295,7 @@ private static void verifyGkeMapping(Resource gotResource, DetectedPlatform dete
274295
}
275296

276297
@Test
277-
public void testGcrResourceAttributesMapping() {
298+
public void testGcrServiceResourceAttributesMapping() {
278299
GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);
279300
DetectedPlatform mockPlatform =
280301
generateMockServerlessPlatform(GCPPlatformDetector.SupportedPlatform.GOOGLE_CLOUD_RUN);
@@ -321,6 +342,31 @@ private static void verifyServerlessMapping(
321342
.containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
322343
}
323344

345+
@Test
346+
public void testGcrJobResourceAttributesMapping() {
347+
GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);
348+
DetectedPlatform mockPlatform = generateMockGcrJobPlatform();
349+
Mockito.when(mockDetector.detectPlatform()).thenReturn(mockPlatform);
350+
Map<String, String> detectedAttributes = mockPlatform.getAttributes();
351+
352+
Resource gotResource = new GCPResourceProvider(mockDetector).createResource(mockConfigProps);
353+
verify(mockPlatform, Mockito.times(1)).getProjectId();
354+
355+
assertThat(gotResource.getAttributes())
356+
.hasSize(8)
357+
.containsEntry(CLOUD_PROVIDER, GCP)
358+
.containsEntry(CLOUD_PLATFORM, GCP_CLOUD_RUN)
359+
.containsEntry(CLOUD_ACCOUNT_ID, DUMMY_PROJECT_ID)
360+
.containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME))
361+
.containsEntry(FAAS_NAME, detectedAttributes.get(SERVERLESS_COMPUTE_NAME))
362+
.containsEntry(FAAS_INSTANCE, detectedAttributes.get(SERVERLESS_COMPUTE_INSTANCE_ID))
363+
.containsEntry(GCP_CLOUD_RUN_JOB_EXECUTION, detectedAttributes.get(GCR_JOB_EXECUTION_KEY))
364+
.containsEntry(
365+
GCP_CLOUD_RUN_JOB_TASK_INDEX,
366+
Integer.parseInt(detectedAttributes.get(GCR_JOB_TASK_INDEX)))
367+
.containsEntry(CLOUD_REGION, detectedAttributes.get(SERVERLESS_COMPUTE_CLOUD_REGION));
368+
}
369+
324370
@Test
325371
public void testGaeResourceAttributeMapping() {
326372
GCPPlatformDetector mockDetector = Mockito.mock(GCPPlatformDetector.class);

0 commit comments

Comments
 (0)