From a5b1e78b3df4f1ec1650d7d67246418bf01aae45 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 12 Sep 2024 12:24:38 -0400 Subject: [PATCH] fixes --- lib/utilization/ecs-info.js | 4 ++ test/unit/facts.test.js | 67 ++++++++++++-------------- test/unit/utilization/ecs-info.test.js | 23 +++++++++ 3 files changed, 57 insertions(+), 37 deletions(-) diff --git a/lib/utilization/ecs-info.js b/lib/utilization/ecs-info.js index b247fe052e..9289811e1a 100644 --- a/lib/utilization/ecs-info.js +++ b/lib/utilization/ecs-info.js @@ -26,6 +26,10 @@ module.exports = function fetchEcsInfo( if (error) { return callback(error, null) } + if (dockerId === null) { + // Some error happened where we could not find the id. Skipping. + return callback(null, null) + } return callback(null, { ecsDockerId: dockerId }) } }) diff --git a/test/unit/facts.test.js b/test/unit/facts.test.js index fe96f02a74..aa79dea051 100644 --- a/test/unit/facts.test.js +++ b/test/unit/facts.test.js @@ -439,11 +439,6 @@ tap.test('utilization', (t) => { // We don't collect full hostnames delete expected.full_hostname - // Stub out docker container id query to make this consistent on all OSes. - sysInfo._getDockerContainerId = (_agent, callback) => { - return callback(null) - } - agent = helper.loadMockedAgent(config) if (mockHostname) { agent.config.getHostnameSafe = mockHostname @@ -470,33 +465,36 @@ tap.test('utilization', (t) => { function makeMockCommonRequest(t, test, type) { return (opts, _agent, cb) => { t.equal(_agent, agent) - setImmediate( - cb, - null, - JSON.stringify( - type === 'aws' - ? { - instanceId: test.input_aws_id, - instanceType: test.input_aws_type, - availabilityZone: test.input_aws_zone - } - : type === 'azure' - ? { - location: test.input_azure_location, - name: test.input_azure_name, - vmId: test.input_azure_id, - vmSize: test.input_azure_size - } - : type === 'gcp' - ? { - id: test.input_gcp_id, - machineType: test.input_gcp_type, - name: test.input_gcp_name, - zone: test.input_gcp_zone - } - : null - ) - ) + let payload = null + switch (type) { + case 'aws': { + payload = { + instanceId: test.input_aws_id, + instanceType: test.input_aws_type, + availabilityZone: test.input_aws_zone + } + break + } + case 'azure': { + payload = { + location: test.input_azure_location, + name: test.input_azure_name, + vmId: test.input_azure_id, + vmSize: test.input_azure_size + } + break + } + case 'gcp': { + payload = { + id: test.input_gcp_id, + machineType: test.input_gcp_type, + name: test.input_gcp_name, + zone: test.input_gcp_zone + } + break + } + } + setImmediate(cb, null, JSON.stringify(payload)) } } }) @@ -584,11 +582,6 @@ tap.test('boot_id', (t) => { const expected = test.expected_output_json - // Stub out docker container id query to make this consistent on all OSes. - sysInfo._getDockerContainerId = (_agent, callback) => { - return callback(null) - } - agent = helper.loadMockedAgent(DISABLE_ALL_DETECTIONS) if (mockHostname) { agent.config.getHostnameSafe = mockHostname diff --git a/test/unit/utilization/ecs-info.test.js b/test/unit/utilization/ecs-info.test.js index 5e32372aee..20a4f71a9f 100644 --- a/test/unit/utilization/ecs-info.test.js +++ b/test/unit/utilization/ecs-info.test.js @@ -62,6 +62,29 @@ test('returns null if error encountered', (t, end) => { } }) +test('returns null if got null', (t, end) => { + const agent = helper.loadMockedAgent({ + utilization: { + detect_aws: true + } + }) + t.after(() => helper.unloadAgent(agent)) + + fetchEcsInfo( + agent, + (error, data) => { + assert.equal(error, null) + assert.equal(data, null) + end() + }, + { getEcsContainerId } + ) + + function getEcsContainerId({ callback }) { + callback(null, null) + } +}) + test('returns container id', (t, end) => { const agent = helper.loadMockedAgent({ utilization: {