Skip to content

Commit c5b59ed

Browse files
committed
modified tests to fix logic
1 parent 6c0f40b commit c5b59ed

File tree

3 files changed

+70
-35
lines changed

3 files changed

+70
-35
lines changed

lib/utilization/gcp-info.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,23 +51,24 @@ function fetchGCPInfo(agent, callback) {
5151
}
5252

5353
// Most GCP services have id, zone, machineType, name
54-
const results = common.getKeys(data, ['id', 'zone', 'machineType', 'name'])
54+
// GCP Cloud Run only has id and zone
55+
const results = common.getKeys(data, ['id', 'zone'])
5556
if (!results) {
56-
// GCP cloud run metadata only has id and zone
57-
const cloudRunSpecificResults = common.getKeys(data, ['id', 'zone'])
58-
if (cloudRunSpecificResults && process.env.K_SERVICE) {
59-
resultDict = cloudRunSpecificResults
60-
}
57+
logger.debug({ utilization: 'gcp' }, 'GCP metadata was invalid.')
58+
agent.metrics.getOrCreateMetric(NAMES.UTILIZATION.GCP_ERROR).incrementCallCount()
6159
} else {
62-
results.machineType = results.machineType.split('/').pop()
63-
results.zone = results.zone.split('/').pop()
60+
if (common.checkValueString(data.name)) {
61+
results.name = data.name
62+
}
63+
if (common.checkValueString(data.machineType)) {
64+
results.machineType = data.machineType.split('/').pop()
65+
}
66+
if (common.checkValueString(results.zone)) {
67+
results.zone = results.zone.split('/').pop()
68+
}
6469

6570
resultDict = results
6671
}
67-
if (!resultDict) {
68-
logger.debug({ utilization: 'gcp' }, 'GCP metadata was invalid.')
69-
agent.metrics.getOrCreateMetric(NAMES.UTILIZATION.GCP_ERROR).incrementCallCount()
70-
}
7172
callback(null, results)
7273
}
7374
)

test/lib/cross_agent_tests/utilization_vendor_specific/gcp.json

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,16 @@
5454
"timeout": false
5555
}
5656
},
57-
"expected_vendors_hash": null,
57+
"expected_vendors_hash": {
58+
"gcp": {
59+
"id": "3161347020215157123",
60+
"name": "aef-default-20170501t160547-7gh8",
61+
"zone": "us-central1-c"
62+
}
63+
},
5864
"expected_metrics": {
5965
"Supportability/utilization/gcp/error": {
60-
"call_count": 1
66+
"call_count": 0
6167
}
6268
}
6369
},
@@ -74,10 +80,16 @@
7480
"timeout": false
7581
}
7682
},
77-
"expected_vendors_hash": null,
83+
"expected_vendors_hash": {
84+
"gcp": {
85+
"id": "3161347020215157123",
86+
"name": "aef-default-20170501t160547-7gh8",
87+
"zone": "us-central1-c"
88+
}
89+
},
7890
"expected_metrics": {
7991
"Supportability/utilization/gcp/error": {
80-
"call_count": 1
92+
"call_count": 0
8193
}
8294
}
8395
},
@@ -174,10 +186,16 @@
174186
"timeout": false
175187
}
176188
},
177-
"expected_vendors_hash": null,
189+
"expected_vendors_hash": {
190+
"gcp": {
191+
"id": "3161347020215157123",
192+
"machineType": "custom-1-1024",
193+
"zone": "us-central1-c"
194+
}
195+
},
178196
"expected_metrics": {
179197
"Supportability/utilization/gcp/error": {
180-
"call_count": 1
198+
"call_count": 0
181199
}
182200
}
183201
},
@@ -194,10 +212,16 @@
194212
"timeout": false
195213
}
196214
},
197-
"expected_vendors_hash": null,
215+
"expected_vendors_hash": {
216+
"gcp": {
217+
"id": "3161347020215157123",
218+
"machineType": "custom-1-1024",
219+
"zone": "us-central1-c"
220+
}
221+
},
198222
"expected_metrics": {
199223
"Supportability/utilization/gcp/error": {
200-
"call_count": 1
224+
"call_count": 0
201225
}
202226
}
203227
},
@@ -236,10 +260,16 @@
236260
"timeout": false
237261
}
238262
},
239-
"expected_vendors_hash": null,
263+
"expected_vendors_hash": {
264+
"gcp": {
265+
"id": "3161347020215157123",
266+
"machineType": "custom-1-1024",
267+
"zone": "us-central1-c"
268+
}
269+
},
240270
"expected_metrics": {
241271
"Supportability/utilization/gcp/error": {
242-
"call_count": 1
272+
"call_count": 0
243273
}
244274
}
245275
},
@@ -256,10 +286,16 @@
256286
"timeout": false
257287
}
258288
},
259-
"expected_vendors_hash": null,
289+
"expected_vendors_hash": {
290+
"gcp": {
291+
"id": "3161347020215157123",
292+
"machineType": "custom-1-1024",
293+
"zone": "us-central1-c"
294+
}
295+
},
260296
"expected_metrics": {
261297
"Supportability/utilization/gcp/error": {
262-
"call_count": 1
298+
"call_count": 0
263299
}
264300
}
265301
},
@@ -285,4 +321,4 @@
285321
}
286322
}
287323
}
288-
]
324+
]

test/unit/collector/facts.test.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -809,20 +809,18 @@ test('host facts', async (t) => {
809809
callback(null, systemInfo)
810810
}
811811

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,
812+
ctx.systemInfoPath = require.resolve('../../../lib/system-info')
813+
ctx.originalSystemInfoModule = require.cache[ctx.systemInfoPath]
814+
require.cache[ctx.systemInfoPath] = {
815+
id: ctx.systemInfoPath,
816+
filename: ctx.systemInfoPath,
818817
loaded: true,
819818
exports: mockSysInfo
820819
}
821820

822-
const factsPath = require.resolve('../../../lib/collector/facts')
823-
ctx.originalFactsModule = require.cache[factsPath]
824-
ctx.factsPath = factsPath
825-
delete require.cache[factsPath]
821+
ctx.factsPath = require.resolve('../../../lib/collector/facts')
822+
ctx.originalFactsModule = require.cache[ctx.factsPath]
823+
delete require.cache[ctx.factsPath]
826824

827825
const facts = require('../../../lib/collector/facts')
828826
ctx.nr.facts = function (agent, callback) {

0 commit comments

Comments
 (0)