Skip to content

Commit f8a8cc3

Browse files
committed
add tests
1 parent 0527209 commit f8a8cc3

File tree

3 files changed

+112
-3
lines changed

3 files changed

+112
-3
lines changed

lib/config/default.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1596,10 +1596,10 @@ defaultConfig.definition = () => ({
15961596
},
15971597

15981598
/**
1599-
* When enabled, it will use GCP metadata id
1599+
* When enabled, it will use GCP metadata id
16001600
* to set the hostname of the running application
16011601
*/
1602-
gcp_cloud_run:{
1602+
gcp_cloud_run: {
16031603
use_instance_as_host: {
16041604
default: true,
16051605
formatter: boolean

lib/utilization/gcp-info.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Copyright 2025 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
16
'use strict'
27

38
const logger = require('../logger.js').child({ component: 'gcp-info' })
@@ -61,4 +66,4 @@ function fetchGCPInfo(agent, callback) {
6166
callback(null, results)
6267
}
6368
)
64-
}
69+
}

test/unit/collector/facts.test.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,110 @@ test('display_host facts', async (t) => {
789789
)
790790
})
791791

792+
test('host facts', async (t) => {
793+
t.beforeEach((ctx) => {
794+
ctx.nr = {}
795+
796+
// Mock fetchSystemInfo to return a specific value
797+
const mockSysInfo = async (agent, callback) => {
798+
const systemInfo = {}
799+
800+
// Mock utilization.getVendors
801+
const vendorStats = {
802+
gcp: {
803+
id: 'mock-gcp-instance-id',
804+
zone: 'us-central1-a'
805+
}
806+
}
807+
systemInfo.vendors = vendorStats
808+
809+
callback(null, systemInfo)
810+
}
811+
812+
const systemInfoPath = require.resolve('../../../lib/system-info')
813+
ctx.originalSystemInfoModule = require.cache[systemInfoPath]
814+
ctx.systemInfoPath = systemInfoPath
815+
require.cache[systemInfoPath] = {
816+
id: systemInfoPath,
817+
filename: systemInfoPath,
818+
loaded: true,
819+
exports: mockSysInfo
820+
}
821+
822+
const factsPath = require.resolve('../../../lib/collector/facts')
823+
ctx.originalFactsModule = require.cache[factsPath]
824+
ctx.factsPath = factsPath
825+
delete require.cache[factsPath]
826+
827+
const facts = require('../../../lib/collector/facts')
828+
ctx.nr.facts = function (agent, callback) {
829+
return facts(agent, callback, { logger: ctx.nr.logger })
830+
}
831+
832+
ctx.nr.agent = helper.loadMockedAgent(structuredClone(DISABLE_ALL_DETECTIONS))
833+
ctx.nr.agent.config.utilization = null
834+
ctx.nr.agent.config.getHostnameSafe = () => 'localhost'
835+
})
836+
837+
t.afterEach((ctx) => {
838+
helper.unloadAgent(ctx.nr.agent)
839+
delete process.env.K_SERVICE
840+
841+
// Restore system-info cache
842+
if (ctx.systemInfoPath) {
843+
if (ctx.originalSystemInfoModule === undefined) {
844+
delete require.cache[ctx.systemInfoPath]
845+
} else {
846+
require.cache[ctx.systemInfoPath] = ctx.originalSystemInfoModule
847+
}
848+
}
849+
850+
// Restore facts cache
851+
if (ctx.factsPath) {
852+
if (ctx.originalFactsModule === undefined) {
853+
delete require.cache[ctx.factsPath]
854+
} else {
855+
require.cache[ctx.factsPath] = ctx.originalFactsModule
856+
}
857+
}
858+
})
859+
860+
await t.test('should use GCP instance ID as hostname when K_SERVICE is set', (t, end) => {
861+
const { agent, facts } = t.nr
862+
863+
agent.config.gcp_cloud_run = { use_instance_as_host: true }
864+
process.env.K_SERVICE = 'mock-service'
865+
866+
facts(agent, (result) => {
867+
assert.equal(result.host, 'mock-gcp-instance-id', 'Hostname should be set to GCP instance ID')
868+
end()
869+
})
870+
})
871+
872+
await t.test('should use getHostnameSafe as hostname when K_SERVICE is not present', (t, end) => {
873+
const { agent, facts } = t.nr
874+
875+
agent.config.gcp_cloud_run = { use_instance_as_host: true }
876+
877+
facts(agent, (result) => {
878+
assert.equal(result.host, 'localhost', 'Hostname should be set to GCP instance ID')
879+
end()
880+
})
881+
})
882+
883+
await t.test('should use getHostnameSafe when K_SERVICE is set but gcp_cloud_run.use_instance_as_host is false', (t, end) => {
884+
const { agent, facts } = t.nr
885+
886+
agent.config.gcp_cloud_run = { use_instance_as_host: false }
887+
process.env.K_SERVICE = 'mock-service'
888+
889+
facts(agent, (result) => {
890+
assert.equal(result.host, 'localhost', 'Hostname should be set to GCP instance ID')
891+
end()
892+
})
893+
})
894+
})
895+
792896
function mockIpAddresses(values) {
793897
os.networkInterfaces = () => {
794898
return {

0 commit comments

Comments
 (0)