Skip to content

Commit

Permalink
chore: Updated shimmer, uninstrumented, environment, flatten, and log…
Browse files Browse the repository at this point in the history
…ger integration tests to node:test (#2802)
  • Loading branch information
jsumners-nr authored Nov 26, 2024
1 parent 83ad1f3 commit 83fd7ee
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,26 @@

'use strict'

const tap = require('tap')
const test = tap.test
const test = require('node:test')
const assert = require('node:assert')

const configurator = require('../../lib/config')
const Agent = require('../../lib/agent')

test("Using should shouldn't cause the agent to explode on startup.", function (t) {
t.plan(2)

t.doesNotThrow(function () {
test("Using should shouldn't cause the agent to explode on startup.", (_, end) => {
assert.doesNotThrow(function () {
require('should')
const agent = new Agent(configurator.initialize())
t.ok(agent.should)
assert.ok(agent.should)
end()
}, "shouldn't throw when should is included.")
})

test("Environment scraper shouldn't die if HOME isn't set.", function (t) {
t.plan(2)

test("Environment scraper shouldn't die if HOME isn't set.", () => {
delete process.env.HOME

t.notOk(process.env.HOME, 'HOME has been nuked.')
t.doesNotThrow(function () {
assert.equal(process.env.HOME, undefined, 'HOME has been nuked.')
assert.doesNotThrow(function () {
return new Agent(configurator.initialize())
}, "shouldn't throw just because HOME isn't set")
})
24 changes: 11 additions & 13 deletions test/integration/logger.tap.js → test/integration/logger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

'use strict'

const path = require('path')
const fs = require('fs')
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const path = require('node:path')
const fs = require('node:fs')

const rimraf = require('rimraf')
const DIRNAME = 'XXXNOCONFTEST'

tap.test('logger', function (t) {
t.autoend()

test('logger', async (t) => {
t.afterEach(async () => {
if (path.basename(process.cwd()) === DIRNAME) {
process.chdir('..')
Expand All @@ -31,21 +31,19 @@ tap.test('logger', function (t) {
delete process.env.NEW_RELIC_LOG
})

t.test('configuration from environment', function (t) {
await t.test('configuration from environment', (t, end) => {
fs.mkdir(DIRNAME, function (error) {
if (!t.error(error, 'should not fail to make directory')) {
return t.end()
}
assert.ifError(error, 'should not fail to make directory')

process.chdir(DIRNAME)

process.env.NEW_RELIC_LOG = 'stdout'

t.doesNotThrow(function () {
t.ok(require('../../lib/logger'), 'requiring logger returned a logging object')
assert.doesNotThrow(function () {
assert.ok(require('../../lib/logger'), 'requiring logger returned a logging object')
})

t.end()
end()
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const tspl = require('@matteo.collina/tspl')

const helper = require('../../lib/agent_helper')

tap.beforeEach((t) => {
t.context.agent = helper.instrumentMockedAgent({})
test.beforeEach((ctx) => {
ctx.nr = {}
ctx.nr.agent = helper.instrumentMockedAgent({})
})

tap.afterEach((t) => {
helper.unloadAgent(t.context.agent)
test.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

tap.test('can instrument the same module from multiple installs', (t) => {
t.plan(3)
test('can instrument the same module from multiple installs', async (t) => {
const plan = tspl(t, { plan: 3 })

const { agent } = t.context
const { agent } = t.nr
agent.start(() => {
const api = helper.getAgentApi()

Expand All @@ -44,13 +47,13 @@ tap.test('can instrument the same module from multiple installs', (t) => {
}
})
const person = new Person(dest)
t.equal(person.isHuman, true)
t.same(lines, ['human constructed\n', 'person constructed\n'])
plan.equal(person.isHuman, true)
plan.deepStrictEqual(lines, ['human constructed\n', 'person constructed\n'])

// We loaded the same module from two different installed paths.
// Thus, we should have two instrumentations.
t.equal(instrumentedCount, 2)

t.end()
plan.equal(instrumentedCount, 2)
})

await plan.completed
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@

'use strict'

const test = require('node:test')
const assert = require('node:assert')

/* eslint-disable node/no-unpublished-require */
const Metrics = require('../../../lib/metrics')
const MetricNormalizer = require('../../../lib/metrics/normalizer')
const MetricMapper = require('../../../lib/metrics/mapper')
// eslint-disable-next-line node/no-extraneous-require
const tap = require('tap')

const uninstrumented = require('../../../lib/uninstrumented')
const helper = require('../../lib/agent_helper')
const shimmer = require('../../../lib/shimmer')
/* eslint-enable node/no-unpublished-require */

tap.test('does not mark files with known module names as uninstrumented', function (t) {
test('does not mark files with known module names as uninstrumented', (t) => {
const loaded = []

require('./mock-config/redis')
loaded.push('redis')

t.ok(loaded.length > 0, 'should have loaded at least one module')
assert.ok(loaded.length > 0, 'should have loaded at least one module')

const agent = helper.instrumentMockedAgent()

t.teardown(() => {
t.after(() => {
helper.unloadAgent(agent)
})

Expand All @@ -40,14 +43,12 @@ tap.test('does not mark files with known module names as uninstrumented', functi
const flagMetrics = metrics.toJSON().filter(function (metric) {
return metric[0].name === 'Supportability/Uninstrumented/redis'
})
t.equal(flagMetrics.length, 0, 'No uninstrumented flag metric present')

t.end()
assert.equal(flagMetrics.length, 0, 'No uninstrumented flag metric present')
})

// This doesn't test the core http and https modules because we can't detect if
// core modules have already been loaded.
tap.test('all instrumented modules should be detected when uninstrumented', function (t) {
test('all instrumented modules should be detected when uninstrumented', (t, end) => {
const loaded = []

const instrumentations = Object.keys(shimmer.registeredInstrumentations)
Expand All @@ -62,16 +63,16 @@ tap.test('all instrumented modules should be detected when uninstrumented', func
require(module)
loaded.push(module)
} catch (err) {
t.comment('failed to load ' + module)
t.diagnostic('failed to load ' + module)
}
}
})

t.ok(loaded.length > 0, 'should have loaded at least one module')
assert.ok(loaded.length > 0, 'should have loaded at least one module')

const agent = helper.instrumentMockedAgent()

t.teardown(() => {
t.after(() => {
helper.unloadAgent(agent)
})

Expand All @@ -87,13 +88,13 @@ tap.test('all instrumented modules should be detected when uninstrumented', func
const flagMetrics = metricsJSON.filter(function (metric) {
return metric[0].name === 'Supportability/Uninstrumented'
})
t.equal(flagMetrics.length, 1, 'Uninstrumented flag metric present')
assert.equal(flagMetrics.length, 1, 'Uninstrumented flag metric present')

if (flagMetrics.length !== 1) {
return t.end()
return end()
}

t.ok(
assert.ok(
flagMetrics[0][1].callCount > 0,
'Callcount for uninstrumented flag metric > 0 (' + flagMetrics[0][1].callCount + ')'
)
Expand All @@ -103,15 +104,15 @@ tap.test('all instrumented modules should be detected when uninstrumented', func

metricsJSON.forEach(function (metric) {
if (metric[0].name === 'Supportability/Uninstrumented/' + module) {
t.ok(metric[1].callCount > 0, 'should have uninstrumented metric for ' + module)
assert.ok(metric[1].callCount > 0, 'should have uninstrumented metric for ' + module)
found = true
}
})

if (!found) {
t.fail('No uninstrumented module metric found for ' + module)
assert.fail('No uninstrumented module metric found for ' + module)
}
})

t.end()
end()
})
47 changes: 21 additions & 26 deletions test/integration/flatten.tap.js → test/unit/util/flatten.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,58 +5,53 @@

'use strict'

const tap = require('tap')
const flatten = require('../../lib/util/flatten')
const test = require('node:test')
const assert = require('node:assert')

tap.test('util.flatten', function (t) {
t.autoend()
const flatten = require('../../../lib/util/flatten')

t.test('flattens things', function (t) {
t.same(flatten({}, '', { a: 5, b: true }), { a: 5, b: true }, '1 level')
t.same(
test('util.flatten', async (t) => {
await t.test('flattens things', () => {
assert.deepStrictEqual(flatten({}, '', { a: 5, b: true }), { a: 5, b: true }, '1 level')
assert.deepStrictEqual(
flatten({}, '', { a: 5, b: { c: true, d: 7 } }),
{ 'a': 5, 'b.c': true, 'b.d': 7 },
'2 levels'
)
t.same(
assert.deepStrictEqual(
flatten({}, '', { a: 5, b: { c: true, d: 7, e: { foo: 'efoo', bar: 'ebar' } } }),
{ 'a': 5, 'b.c': true, 'b.d': 7, 'b.e.foo': 'efoo', 'b.e.bar': 'ebar' },
'3 levels'
)

t.end()
})

t.test('flattens recursive objects', function (t) {
await t.test('flattens recursive objects', () => {
// eslint-disable-next-line sonarjs/prefer-object-literal -- Disabled so we can create cyclical objects
const obj = {}
obj.x = obj
t.same(flatten({}, '', obj), {})

t.end()
assert.deepStrictEqual(flatten({}, '', obj), {})
})
})

tap.test('util.flatten.keys', function (t) {
t.autoend()

t.test('gets flattened keys', function (t) {
t.same(flatten.keys({ a: 5, b: true }), ['a', 'b'], '1 level')
t.same(flatten.keys({ a: 5, b: { c: true, d: 7 } }), ['a', 'b.c', 'b.d'], '2 levels')
t.same(
test('util.flatten.keys', async (t) => {
await t.test('gets flattened keys', () => {
assert.deepStrictEqual(flatten.keys({ a: 5, b: true }), ['a', 'b'], '1 level')
assert.deepStrictEqual(
flatten.keys({ a: 5, b: { c: true, d: 7 } }),
['a', 'b.c', 'b.d'],
'2 levels'
)
assert.deepStrictEqual(
flatten.keys({ a: 5, b: { c: true, d: 7, e: { foo: 'efoo', bar: 'ebar' } } }),
['a', 'b.c', 'b.d', 'b.e.foo', 'b.e.bar'],
'3 levels'
)

t.end()
})

t.test('flattens recursive objects', function (t) {
await t.test('flattens recursive objects', () => {
// eslint-disable-next-line sonarjs/prefer-object-literal -- Disabled so we can create cyclical objects
const obj = {}
obj.x = obj
t.same(flatten.keys(obj), [])
t.end()
assert.deepStrictEqual(flatten.keys(obj), [])
})
})

0 comments on commit 83fd7ee

Please sign in to comment.