Skip to content

Commit 33337d4

Browse files
ajbenjampichlermarcdavid-luna
authored
chore(gcp-resource-detector): unit test to prevent a regression and ensure previously set attributes are not overwritten with blank values. (#2755)
Co-authored-by: Marc Pichler <marc.pichler@dynatrace.com> Co-authored-by: David Luna <david.luna@elastic.co>
1 parent 19a2ad5 commit 33337d4

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

detectors/node/opentelemetry-resource-detector-gcp/test/detectors/GcpDetector.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,46 @@ describe('gcpDetector', () => {
176176
});
177177
});
178178

179+
it('should return resource and undefined for non-available kubernetes attributes', async () => {
180+
process.env.KUBERNETES_SERVICE_HOST = 'my-host';
181+
process.env.HOSTNAME = 'my-hostname';
182+
process.env.CONTAINER_NAME = 'my-container-name';
183+
const scope = nock(HOST_ADDRESS)
184+
.get(INSTANCE_PATH)
185+
.reply(200, {}, HEADERS)
186+
.get(INSTANCE_ID_PATH)
187+
.reply(200, () => '4520031799277581759', HEADERS)
188+
.get(CLUSTER_NAME_PATH)
189+
.reply(200, () => 'my-cluster', HEADERS)
190+
.get(PROJECT_ID_PATH)
191+
.reply(200, () => 'my-project-id', HEADERS)
192+
.get(ZONE_PATH)
193+
.reply(200, () => 'project/zone/my-zone', HEADERS)
194+
.get(HOSTNAME_PATH)
195+
.reply(200, () => 'dev.my-project.local', HEADERS);
196+
const secondaryScope = nock(SECONDARY_HOST_ADDRESS)
197+
.get(INSTANCE_PATH)
198+
.reply(200, {}, HEADERS);
199+
200+
const resource = detectResources({ detectors: [gcpDetector] });
201+
await resource.waitForAsyncAttributes?.();
202+
203+
secondaryScope.done();
204+
scope.done();
205+
206+
assertCloudResource(resource, {
207+
provider: 'gcp',
208+
accountId: 'my-project-id',
209+
zone: 'my-zone',
210+
});
211+
assertK8sResource(resource, {
212+
clusterName: 'my-cluster',
213+
podName: 'my-hostname',
214+
namespaceName: undefined,
215+
});
216+
assertContainerResource(resource, { name: 'my-container-name' });
217+
});
218+
179219
it('returns empty resource if not detected', async () => {
180220
const resource = detectResources({ detectors: [gcpDetector] });
181221
await resource.waitForAsyncAttributes?.();

0 commit comments

Comments
 (0)