Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Migrated aws-sdk-v2 and aws-sdk-v3 tests to node:test #2620

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions test/lib/custom-assertions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
'use strict'
const assert = require('node:assert')
const { isSimpleObject } = require('../../lib/util/objects')
const typeMappings = {
String: 'string',
Number: 'number'
}

function assertExactClmAttrs(segmentStub, expectedAttrs) {
const attrs = segmentStub.addAttribute.args
Expand Down Expand Up @@ -204,37 +208,24 @@ function assertSegments(parent, expected, options) {
*/
function match(actual, expected) {
if (typeof actual === 'string' && typeof expected === 'string') {
const patterns = expected
.trim()
.split(/\r?\n/)
.map((s) => s.trim())

let lastIndex = -1
for (const pattern of patterns) {
const index = actual.indexOf(pattern)
if (index === -1 || index < lastIndex) {
return false
}
lastIndex = index
}
return true
assert.ok(actual.indexOf(expected) > -1)
return
}

for (const key in expected) {
if (key in actual) {
if (typeof expected[key] === 'object' && expected[key] !== null) {
/* c8 ignore next 3 */
if (!match(actual[key], expected[key])) {
return false
}
} else if (actual[key] !== expected[key]) {
return false
if (typeof expected[key] === 'function') {
const type = expected[key]
assert.ok(typeof actual[key] === typeMappings[type.name])
} else if (expected[key] instanceof RegExp) {
assert.ok(expected[key].test(actual[key]))
Comment on lines +217 to +221
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling that the documentation needs to be updated to illustrate these cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea i'll update the jsdoc

} else if (typeof expected[key] === 'object' && expected[key] !== null) {
match(actual[key], expected[key])
} else {
assert.equal(actual[key], expected[key])
}
} else {
return false
}
}
return true
}

/**
Expand Down
10 changes: 3 additions & 7 deletions test/unit/analytics_events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
const test = require('node:test')
const assert = require('node:assert')

const { match } = require('../lib/custom-assertions')
const Transaction = require('../../lib/transaction')
const helper = require('../lib/agent_helper')

Expand Down Expand Up @@ -63,12 +62,9 @@ test('when host name is specified by user', async (t) => {

const events = getTransactionEvents(agent)
const firstEvent = events[first]
assert.equal(
match(firstEvent[agentAttrs], {
'host.displayName': 'test-value'
}),
true
)
assert.deepEqual(firstEvent[agentAttrs], {
'host.displayName': 'test-value'
})
})
})

Expand Down
3 changes: 1 addition & 2 deletions test/unit/attributes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
const test = require('node:test')
const assert = require('node:assert')

const { match } = require('../lib/custom-assertions')
const helper = require('../lib/agent_helper')
const { Attributes } = require('../../lib/attributes')
const AttributeFilter = require('../../lib/config/attribute-filter')
Expand Down Expand Up @@ -204,6 +203,6 @@ test('#reset', async (t) => {

inst.reset()

assert.equal(match(inst.attributes, {}), true)
assert.deepEqual(inst.attributes, {})
})
})
23 changes: 11 additions & 12 deletions test/unit/collector/facts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const fs = require('node:fs')
const net = require('node:net')

const helper = require('../../lib/agent_helper')
const { match } = require('../../lib/custom-assertions')
const sysInfo = require('../../../lib/system-info')
const utilTests = require('../../lib/cross_agent_tests/utilization/utilization_json')
const bootIdTests = require('../../lib/cross_agent_tests/utilization/boot_id')
Expand Down Expand Up @@ -174,23 +173,23 @@ test('fun facts about apps that New Relic is interested in including', async (t)
}
]
]
assert.equal(match(t.nr.logs.debug, expectedLogs), true, 'New Relic metadata logged properly')
assert.deepEqual(t.nr.logs.debug, expectedLogs, 'New Relic metadata logged properly')
end()
})
})

await t.test('empty `metadata` object if no metadata env vars found', (t, end) => {
const { agent, facts } = t.nr
facts(agent, (result) => {
assert.equal(match(result.metadata, {}), true)
assert.deepEqual(result.metadata, {})
end()
})
})

await t.test('only returns expected facts', (t, end) => {
const { agent, facts } = t.nr
facts(agent, (result) => {
assert.equal(match(Object.keys(result).sort(), EXPECTED_FACTS.sort()), true)
assert.deepEqual(Object.keys(result).sort(), EXPECTED_FACTS.sort())
end()
})
})
Expand All @@ -208,7 +207,7 @@ test('fun facts about apps that New Relic is interested in including', async (t)
{ label_type: 'a', label_value: 'b' },
{ label_type: '€'.repeat(255), label_value: '𝌆'.repeat(255) }
]
assert.equal(match(result.labels, expected), true)
assert.deepEqual(result.labels, expected)
end()
})
})
Expand All @@ -223,7 +222,7 @@ test('fun facts about apps that New Relic is interested in including', async (t)
{ label_type: 'a', label_value: 'b' },
{ label_type: '€'.repeat(255), label_value: '𝌆'.repeat(255) }
]
assert.equal(match(result.labels, expected), true)
assert.deepEqual(result.labels, expected)
end()
})
})
Expand Down Expand Up @@ -252,7 +251,7 @@ test('fun facts about apps that New Relic is interested in including', async (t)
}

facts(agent, (result) => {
assert.equal(match(result.event_harvest_config, expectedHarvestConfig), true)
assert.deepEqual(result.event_harvest_config, expectedHarvestConfig)
end()
})
}
Expand Down Expand Up @@ -441,7 +440,7 @@ test('utilization facts', async (t) => {
}

t.nr.facts(agent, (result) => {
assert.equal(match(result.utilization, expected), true)
assert.deepEqual(result.utilization, expected)
end()
})

Expand Down Expand Up @@ -684,15 +683,15 @@ test('display_host facts', async (t) => {
agent.config.process_host.display_name = 'test-value2'

facts(agent, (result2) => {
assert.equal(match(result2.display_host, displayHost1), true)
assert.equal(match(result2.host, host1), true)
assert.deepEqual(result2.display_host, displayHost1)
assert.deepEqual(result2.host, host1)

agent.config.clearHostnameCache()
agent.config.clearDisplayHostCache()

facts(agent, (result3) => {
assert.equal(match(result3.display_host, 'test-value2'), true)
assert.equal(match(result3.host, os.hostname()), true)
assert.deepEqual(result3.display_host, 'test-value2')
assert.deepEqual(result3.host, os.hostname())

end()
})
Expand Down
12 changes: 4 additions & 8 deletions test/unit/environment.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ const { spawn } = require('node:child_process')
// environment when testing.
delete process.env.NODE_ENV

const { match } = require('../lib/custom-assertions')
const { isSupportedVersion } = require('../lib/agent_helper')
const environment = require('../../lib/environment')

Expand Down Expand Up @@ -217,13 +216,10 @@ test('should get correct version for dependencies', async () => {
return map
}, {})

assert.equal(
match(versions, {
'invalid-json': '<unknown>',
'valid-json': '1.2.3'
}),
true
)
assert.deepEqual(versions, {
'invalid-json': '<unknown>',
'valid-json': '1.2.3'
})
})

// TODO: remove this test when we drop support for node 18
Expand Down
33 changes: 16 additions & 17 deletions test/unit/errors/error-collector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
const test = require('node:test')
const assert = require('node:assert')

const { match } = require('../../lib/custom-assertions')
const helper = require('../../lib/agent_helper')
const Exception = require('../../../lib/errors').Exception
const ErrorCollector = require('../../../lib/errors/error-collector')
Expand Down Expand Up @@ -118,12 +117,12 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
let params = errorTraces[0][PARAMS]
assert.equal(match(params.agentAttributes, { 'request.parameters.a': 'A' }), true)
assert.deepEqual(params.agentAttributes, { 'request.parameters.a': 'A' })

// Error events
const errorEvents = getErrorEvents(errors)
params = errorEvents[0][2]
assert.equal(match(params, { 'request.parameters.a': 'A' }), true)
assert.deepEqual(params, { 'request.parameters.a': 'A' })
})

await t.test('record custom parameters', (t) => {
Expand All @@ -134,11 +133,11 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
let params = errorTraces[0][PARAMS]
assert.equal(match(params.userAttributes, { a: 'A' }), true)
assert.deepEqual(params.userAttributes, { a: 'A' })

const errorEvents = getErrorEvents(errors)
params = errorEvents[0][1]
assert.equal(match(params, { a: 'A' }), true)
assert.deepEqual(params, { a: 'A' })
})

await t.test('merge custom parameters', (t) => {
Expand All @@ -149,11 +148,11 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
let params = errorTraces[0][PARAMS]
assert.equal(match(params.userAttributes, { a: 'A', b: 'B' }), true)
assert.deepEqual(params.userAttributes, { a: 'A', b: 'B' })

const errorEvents = getErrorEvents(errors)
params = errorEvents[0][1]
assert.equal(match(params, { a: 'A', b: 'B' }), true)
assert.deepEqual(params, { a: 'A', b: 'B' })
})

await t.test('overrides existing custom attributes with new custom attributes', (t) => {
Expand All @@ -164,11 +163,11 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
let params = errorTraces[0][PARAMS]
assert.equal(match(params.userAttributes, { a: 'AA' }), true)
assert.deepEqual(params.userAttributes, { a: 'AA' })

const errorEvents = getErrorEvents(errors)
params = errorEvents[0][1]
assert.equal(match(params, { a: 'AA' }), true)
assert.deepEqual(params, { a: 'AA' })
})

await t.test('does not add custom attributes in high security mode', (t) => {
Expand All @@ -179,11 +178,11 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
let params = errorTraces[0][PARAMS]
assert.equal(match(params.userAttributes, {}), true)
assert.deepEqual(params.userAttributes, {})

const errorEvents = getErrorEvents(errors)
params = errorEvents[0][1]
assert.equal(match(params, {}), true)
assert.deepEqual(params, {})
})

await t.test('redacts the error message in high security mode', (t) => {
Expand Down Expand Up @@ -388,7 +387,7 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
const params = errorTraces[0][PARAMS]
assert.equal(match(params.agentAttributes, { 'host.displayName': 'test-value' }), true)
assert.deepEqual(params.agentAttributes, { 'host.displayName': 'test-value' })
})

await t.test('should not be in agent attributes if not set by user', (t) => {
Expand All @@ -399,7 +398,7 @@ test('Errors', async (t) => {

const errorTraces = getErrorTraces(errors)
const params = errorTraces[0][PARAMS]
assert.equal(match(params.agentAttributes, {}), true)
assert.deepEqual(params.agentAttributes, {})
})
})

Expand Down Expand Up @@ -923,7 +922,7 @@ test('Errors', async (t) => {
const errorTraces = getErrorTraces(errors)
const errorJSON = errorTraces[0]
const params = errorJSON[4]
assert.equal(match(params.agentAttributes, { test_param: 'a value' }), true)
assert.deepEqual(params.agentAttributes, { test_param: 'a value' })
})

await t.test('with a thrown TypeError object and no transaction', async (t) => {
Expand Down Expand Up @@ -2281,15 +2280,15 @@ test('_processErrors', async (t) => {
const errorType = 'user'
const result = errorCollector._getIterableProperty(tx, errorType)

assert.equal(match(result, []), true)
assert.deepEqual(result, [])
})

await t.test('if type is transactionException, return an array of objects', (t) => {
const { errorCollector, tx } = t.nr
const errorType = 'transactionException'
const result = errorCollector._getIterableProperty(tx, errorType)

assert.equal(match(result, []), true)
assert.deepEqual(result, [])
})

await t.test(
Expand All @@ -2301,7 +2300,7 @@ test('_processErrors', async (t) => {
const expectedErrors = 0
const result = errorCollector._processErrors(tx, collectedErrors, expectedErrors, errorType)

assert.equal(match(result, [collectedErrors, expectedErrors]), true)
assert.deepEqual(result, [collectedErrors, expectedErrors])
}
)
})
5 changes: 2 additions & 3 deletions test/unit/errors/error-event-aggregator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const assert = require('node:assert')

const ErrorEventAggregator = require('../../../lib/errors/error-event-aggregator')
const Metrics = require('../../../lib/metrics')
const { match } = require('../../lib/custom-assertions')

const RUN_ID = 1337
const LIMIT = 5
Expand Down Expand Up @@ -61,8 +60,8 @@ test('Error Event Aggregator', async (t) => {

const [runId, eventMetrics, errorEventData] = payload
assert.equal(runId, RUN_ID)
assert.equal(match(eventMetrics, expectedMetrics), true)
assert.equal(match(errorEventData, [rawErrorEvent]), true)
assert.deepEqual(eventMetrics, expectedMetrics)
assert.deepEqual(errorEventData, [rawErrorEvent])
})

await t.test('toPayload() should return nothing with no error event data', (t) => {
Expand Down
Loading
Loading