Skip to content

Commit

Permalink
test: Migrated smoke tests in test/smoke/agent, test/smoke/e2e, a…
Browse files Browse the repository at this point in the history
…nd `test/smoke/index` to `node:test`
  • Loading branch information
bizob2828 committed Nov 20, 2024
1 parent 459a3f1 commit 5453269
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
tap.test('Collector API should connect to staging-collector.newrelic.com', (t) => {
test('Collector API should connect to staging-collector.newrelic.com', (t, end) => {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
Expand All @@ -31,9 +32,9 @@ tap.test('Collector API should connect to staging-collector.newrelic.com', (t) =
const agent = new Agent(config)

agent.start((error, returned) => {
t.notOk(error, 'connected without error')
t.ok(returned, 'got boot configuration')
t.ok(returned.agent_run_id, 'got run ID')
assert.ok(!error, 'connected without error')
assert.ok(returned, 'got boot configuration')
assert.ok(returned.agent_run_id, 'got run ID')

const initialStoppedListeners = agent.listenerCount('stopped')
const initialErroredListeners = agent.listenerCount('errored')
Expand All @@ -43,24 +44,24 @@ tap.test('Collector API should connect to staging-collector.newrelic.com', (t) =
const currentStoppedListeners = agent.listenerCount('stopped')
const currentErroredListeners = agent.listenerCount('errored')
const currentDisconnectedListeners = agent.listenerCount('disconnected')
t.equal(
assert.equal(
currentStoppedListeners,
initialStoppedListeners,
'should not have extra stopped listeners'
)
t.equal(
assert.equal(
currentErroredListeners,
initialErroredListeners,
'should not have extra errored listeners'
)
t.equal(
assert.equal(
currentDisconnectedListeners,
initialDisconnectedListeners,
'should not have extra disconnected listeners'
)

agent.stop(() => {
t.end()
end()
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const API = require('../../../api')
const { getTestSecret } = require('../../helpers/secrets')
const license = getTestSecret('LASP_LICENSE')

tap.test('LASP-enabled agent', (t) => {
let agent = null
let api = null
let config = null

t.beforeEach(function () {
config = configurator.initialize({
test('LASP-enabled agent', async (t) => {
t.beforeEach(function (ctx) {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
security_policies_token: 'ffff-ffff-ffff-ffff',
Expand All @@ -36,14 +33,19 @@ tap.test('LASP-enabled agent', (t) => {
}
})

agent = new Agent(config)
api = new API(agent)
const agent = new Agent(config)
const api = new API(agent)

// Agent cannot create transactions from initial 'stopped' state
agent.setState('started')
ctx.nr = {
agent,
api
}
})

t.test('drops full trace if custom attributes are disabled by LASP', function (t) {
await t.test('drops full trace if custom attributes are disabled by LASP', function (t, end) {
const { agent, api } = t.nr
let transaction
const proxy = agent.tracer.transactionProxy(function () {
transaction = agent.getTransaction()
Expand All @@ -53,26 +55,31 @@ tap.test('LASP-enabled agent', (t) => {
api.addCustomAttribute('foo', 'bar')
api.addCustomAttribute('fizz', 'buzz')
const attributes = transaction.trace.custom.attributes
t.same(Object.keys(attributes), ['foo', 'fizz'], 'transaction trace has custom attributes')
assert.deepEqual(
Object.keys(attributes),
['foo', 'fizz'],
'transaction trace has custom attributes'
)
})
proxy()

transaction.end()
t.ok(agent.traces.trace, 'should have a trace before connect')
assert.ok(agent.traces.trace, 'should have a trace before connect')

agent.start(function (error) {
t.error(error, 'connected without error')
t.notOk(agent.traces.trace, 'should no longer have a trace')
assert.ok(!error, 'connected without error')
assert.ok(!agent.traces.trace, 'should no longer have a trace')

agent.stop(function (error) {
t.error(error, 'stopped without error')
assert.ok(!error, 'stopped without error')

t.end()
end()
})
})
})

t.test('drops full trace if attributes.include is disabled by LASP', function (t) {
await t.test('drops full trace if attributes.include is disabled by LASP', function (t, end) {
const { agent, api } = t.nr
agent.config.attributes.include = ['f*']
agent.config.emit('attributes.include')
let transaction
Expand All @@ -84,24 +91,26 @@ tap.test('LASP-enabled agent', (t) => {
api.addCustomAttribute('foo', 'bar')
api.addCustomAttribute('fizz', 'buzz')
const attributes = transaction.trace.custom.attributes
t.same(Object.keys(attributes), ['foo', 'fizz'], 'transaction trace has custom attributes')
assert.deepEqual(
Object.keys(attributes),
['foo', 'fizz'],
'transaction trace has custom attributes'
)
})
proxy()

transaction.end()
t.ok(agent.traces.trace, 'should have a trace before connect')
assert.ok(agent.traces.trace, 'should have a trace before connect')

agent.start(function (error) {
t.error(error, 'connected without error')
t.notOk(agent.traces.trace, 'should no longer have a trace')
assert.ok(!error, 'connected without error')
assert.ok(!agent.traces.trace, 'should no longer have a trace')

agent.stop(function (error) {
t.error(error, 'stopped without error')
assert.ok(!error, 'stopped without error')

t.end()
end()
})
})
})

t.autoend()
})
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const configurator = require('../../../lib/config')
const Agent = require('../../../lib/agent')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
tap.test('Collector API should connect to staging-collector.newrelic.com', (t) => {
test('Collector API should connect to staging-collector.newrelic.com', (t, end) => {
const config = configurator.initialize({
app_name: 'node.js Tests',
license_key: license,
Expand All @@ -31,16 +31,16 @@ tap.test('Collector API should connect to staging-collector.newrelic.com', (t) =
const agent = new Agent(config)

agent.start((error, returned) => {
t.notOk(error, 'connected without error')
t.ok(returned, 'got boot configuration')
t.ok(returned.agent_run_id, 'got run ID')
t.ok(agent.config.run_id, 'run ID set in configuration')
assert.ok(!error, 'connected without error')
assert.ok(returned, 'got boot configuration')
assert.ok(returned.agent_run_id, 'got run ID')
assert.ok(agent.config.run_id, 'run ID set in configuration')

agent.stop((error) => {
t.notOk(error, 'should have shut down without issue')
t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown')
assert.ok(!error, 'should have shut down without issue')
assert.ok(!agent.config.run_id, 'run ID should have been cleared by shutdown')

t.end()
end()
})
})
})
10 changes: 5 additions & 5 deletions test/smoke/e2e/express.tap.js → test/smoke/e2e/express.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
*/

'use strict'

const cp = require('child_process')
const http = require('http')
const path = require('path')
const util = require('util')
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')

const EXIT_TYPES = ['error', 'exit', 'disconnect', 'close']
const exitHandlers = {}

tap.test('Express e2e request smoke test', (t) => {
test('Express e2e request smoke test', (t, end) => {
const server = cp.fork(path.join(__dirname, 'express-server'))

// Bind to the various ways failure could happen
Expand All @@ -40,13 +40,13 @@ tap.test('Express e2e request smoke test', (t) => {
})

res.on('end', function onEnd() {
t.equal(data, 'hello world!', 'should return appropriate response data')
assert.equal(data, 'hello world!', 'should return appropriate response data')
// unbind from the exit conditions
EXIT_TYPES.forEach((type) => {
server.removeListener(type, exitHandlers[type])
})
server.kill()
t.end()
end()
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
*/

'use strict'

const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const { getTestSecret } = require('../../helpers/secrets')
const StubApi = require('../../../stub_api')

const license = getTestSecret('TEST_LICENSE')
const VERSIONS = ['garbage', '4.0.0']
tap.test('load agent with bad versions should load stub agent', (t) => {
test('load agent with bad versions should load stub agent', async (t) => {
process.env.NEW_RELIC_HOME = __dirname + '/..'
process.env.NEW_RELIC_HOST = 'staging-collector.newrelic.com'
process.env.NEW_RELIC_LICENSE_KEY = license
Expand All @@ -23,21 +23,19 @@ tap.test('load agent with bad versions should load stub agent', (t) => {
delete require.cache[require.resolve('../../../index.js')]
})

VERSIONS.forEach((version) => {
t.test(`agent version: ${version}`, (t) => {
t.doesNotThrow(function () {
for (const version of VERSIONS) {
await t.test(`agent version: ${version}`, (t, end) => {
assert.doesNotThrow(function () {
const _version = process.version
Object.defineProperty(process, 'version', { value: version, writable: true })
t.equal(process.version, version, 'should have set bad version')
assert.equal(process.version, version, 'should have set bad version')

const api = require('../../../index.js')
t.ok(api instanceof StubApi)
assert.ok(api instanceof StubApi)

process.version = _version
}, "malformed process.version doesn't blow up the process")
t.end()
end()
})
})

t.end()
}
})
26 changes: 13 additions & 13 deletions test/smoke/index/index.tap.js → test/smoke/index/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,40 @@
*/

'use strict'

const { test } = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const { getTestSecret } = require('../../helpers/secrets')

const license = getTestSecret('TEST_LICENSE')
test('loading the application via index.js', { timeout: 15000 }, (t) => {
test('loading the application via index.js', { timeout: 15000 }, (t, end) => {
let agent = null

process.env.NEW_RELIC_HOME = __dirname + '/..'
process.env.NEW_RELIC_HOST = 'staging-collector.newrelic.com'
process.env.NEW_RELIC_LICENSE_KEY = license

t.doesNotThrow(function () {
assert.doesNotThrow(function () {
const api = require('../../../index.js')
agent = api.agent
t.equal(agent._state, 'connecting', 'agent is booting')
assert.equal(agent._state, 'connecting', 'agent is booting')
}, "just loading the agent doesn't throw")

let metric = agent.metrics.getMetric('Supportability/Nodejs/FeatureFlag/await_support/enabled')
t.notOk(metric, 'should not create metric for unchanged feature flags')
assert.ok(!metric, 'should not create metric for unchanged feature flags')

metric = agent.metrics.getMetric('Supportability/Nodejs/FeatureFlag/internal_test_only/enabled')
t.ok(metric, 'should create metric for changed feature flags')
assert.ok(metric, 'should create metric for changed feature flags')

function shutdown() {
t.equal(agent._state, 'started', "agent didn't error connecting to staging")
t.same(agent.config.applications(), ['My Application'], 'app name is valid')
t.equal(agent.config.agent_enabled, true, 'the agent is still enabled')
assert.equal(agent._state, 'started', "agent didn't error connecting to staging")
assert.deepEqual(agent.config.applications(), ['My Application'], 'app name is valid')
assert.equal(agent.config.agent_enabled, true, 'the agent is still enabled')

agent.stop(function cbStop(err) {
t.notOk(err, 'should not error when stopping')
t.equal(agent._state, 'stopped', "agent didn't error shutting down")
assert.ok(!err, 'should not error when stopping')
assert.equal(agent._state, 'stopped', "agent didn't error shutting down")

t.end()
end()
})
}

Expand Down

0 comments on commit 5453269

Please sign in to comment.