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: Updated tests to use helper.loadTestAgent where applicable #2640

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions test/lib/agent_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ helper.unloadAgent = (agent, shimmer = require('../../lib/shimmer')) => {

helper.loadTestAgent = (t, conf, setState = true) => {
const agent = helper.instrumentMockedAgent(conf, setState)

if (t.after) {
t.after(() => {
helper.unloadAgent(agent)
Expand Down
12 changes: 2 additions & 10 deletions test/unit/agent/agent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ test('when forcing transaction ignore status', async (t) => {
})

test('#harvesters.start should start all aggregators', (t) => {
const agent = helper.loadMockedAgent(null, false)
t.after(() => {
helper.unloadAgent(agent)
})

const agent = helper.loadTestAgent(t, null, false)
agent.harvester.start()
const aggregators = [
agent.traces,
Expand All @@ -201,11 +197,7 @@ test('#harvesters.start should start all aggregators', (t) => {

test('#harvesters.stop should stop all aggregators', (t) => {
// Load agent with default 'stopped' state:
const agent = helper.loadMockedAgent(null, false)
t.after(() => {
helper.unloadAgent(agent)
})

const agent = helper.loadTestAgent(t, null, false)
agent.harvester.start()
agent.harvester.stop()

Expand Down
6 changes: 1 addition & 5 deletions test/unit/api/api-obfuscate-sql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ const API = require('../../../api')
const helper = require('../../lib/agent_helper')

test('Agent API - obfuscateSql', (t, end) => {
const agent = helper.instrumentMockedAgent()
const agent = helper.loadTestAgent(t)
const api = new API(agent)

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

const sql = `select * from foo where a='b' and c=100;`
const obfuscated = api.obfuscateSql(sql, 'postgres')
assert.equal(obfuscated, 'select * from foo where a=? and c=?;')
Expand Down
6 changes: 1 addition & 5 deletions test/unit/collector/api-connect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ const baseAgentConfig = {
}

test('requires a callback', (t) => {
const agent = helper.loadMockedAgent(baseAgentConfig)
const agent = helper.loadTestAgent(t, baseAgentConfig)
agent.reconfigure = () => {}
agent.setState = () => {}
t.after(() => {
helper.unloadAgent(agent)
})

const collectorApi = new CollectorApi(agent)
assert.throws(
() => {
Expand Down
9 changes: 2 additions & 7 deletions test/unit/collector/facts.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,11 +421,7 @@ test('utilization facts', async (t) => {
// We don't collect full hostnames.
delete expected.full_hostname

const agent = helper.loadMockedAgent(config)
t.after(() => {
helper.unloadAgent(agent)
})

const agent = helper.loadTestAgent(t, config)
if (mockHostname) {
agent.config.getHostnameSafe = mockHostname
}
Expand Down Expand Up @@ -567,8 +563,7 @@ test('boot id facts', async (t) => {
}

const expected = testCase.expected_output_json
agent = helper.loadMockedAgent(structuredClone(DISABLE_ALL_DETECTIONS))
t.after(() => helper.unloadAgent(agent))
agent = helper.loadTestAgent(t, structuredClone(DISABLE_ALL_DETECTIONS))

if (mockHostname) {
agent.config.getHostnameSafe = mockHostname
Expand Down
9 changes: 3 additions & 6 deletions test/unit/errors/error-collector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,12 @@ test('Errors', async (t) => {
// 9. new trace invokes getDisplayHost()
// 10. getDisplayHost() returns the original cached value because the agent has been reused
helper.unloadAgent(t.nr.agent)
const agent = helper.loadMockedAgent({
const agent = helper.loadTestAgent(t, {
attributes: { enabled: true },
process_host: {
display_name: 'test-value'
}
})
t.after(() => helper.unloadAgent(agent))

const tx = new Transaction(agent)
tx.url = '/'
Expand Down Expand Up @@ -1689,8 +1688,7 @@ test('Errors', async (t) => {

await t.test('not spill over reservoir size', (t) => {
helper.unloadAgent(t.nr.agent)
const agent = helper.loadMockedAgent({ error_collector: { max_event_samples_stored: 10 } })
t.after(() => helper.unloadAgent(agent))
const agent = helper.loadTestAgent(t, { error_collector: { max_event_samples_stored: 10 } })

for (let i = 0; i < 20; i++) {
agent.errors.add(null, Error('some error'))
Expand Down Expand Up @@ -2016,8 +2014,7 @@ test('Errors', async (t) => {

await t.test('includes http port if the transaction is a web transaction', (t, end) => {
helper.unloadAgent(t.nr.agent)
const agent = helper.instrumentMockedAgent()
t.after(() => helper.unloadAgent(agent))
const agent = helper.loadTestAgent(t)

const server = http.createServer(function createServerCb(req, res) {
assert.ok(agent.getTransaction())
Expand Down
5 changes: 1 addition & 4 deletions test/unit/instrumentation/core/globals.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ test('unhandledRejection should not report it if there is another handler', () =
})

test('should catch early throws with long chains', (t, end) => {
const agent = helper.instrumentMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})
const agent = helper.loadTestAgent(t)
let segment

helper.runInTransaction(agent, function (transaction) {
Expand Down
6 changes: 1 addition & 5 deletions test/unit/instrumentation/core/inspector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ const helper = require('../../../lib/agent_helper')
const inspectorInstrumentation = require('../../../../lib/instrumentation/core/inspector')

test('Inspector instrumentation', async (t) => {
const agent = helper.loadMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})

const agent = helper.loadTestAgent(t)
assert.doesNotThrow(inspectorInstrumentation.bind(null, agent, null))
})
6 changes: 1 addition & 5 deletions test/unit/instrumentation/generic-pool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ const helper = require('../../lib/agent_helper')
const Shim = require('../../../lib/shim/shim.js')

test('agent instrumentation of generic-pool', async function (t) {
const agent = helper.loadMockedAgent()
const agent = helper.loadTestAgent(t)
const shim = new Shim(agent, 'generic-pool')
const initialize = require('../../../lib/instrumentation/generic-pool')

t.after(function () {
helper.unloadAgent(agent)
})

await t.test("shouldn't cause bootstrapping to fail", async function (t) {
await t.test('when passed no module', async function () {
assert.doesNotThrow(function () {
Expand Down
12 changes: 2 additions & 10 deletions test/unit/instrumentation/hapi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@ const shims = require('../../../lib/shim')

test('an instrumented Hapi application', async function (t) {
await t.test("shouldn't cause bootstrapping to fail", async function (t) {
const agent = helper.loadMockedAgent()
const agent = helper.loadTestAgent(t)
const initialize = require('../../../lib/instrumentation/@hapi/hapi')

t.after(function () {
helper.unloadAgent(agent)
})

await t.test('when passed nothing', async function () {
assert.doesNotThrow(function () {
initialize()
Expand All @@ -42,7 +38,7 @@ test('an instrumented Hapi application', async function (t) {
await t.test(
'when stubbed should set framework to Hapi when a new app is created',
async function (t) {
const agent = helper.instrumentMockedAgent()
const agent = helper.loadTestAgent(t)
agent.environment.clearFramework()

function Server() {}
Expand All @@ -55,10 +51,6 @@ test('an instrumented Hapi application', async function (t) {

require('../../../lib/instrumentation/@hapi/hapi')(agent, stub, 'hapi', shim)

t.after(function () {
helper.unloadAgent(agent)
})

const server = new stub.Server()
server.start()

Expand Down
5 changes: 1 addition & 4 deletions test/unit/instrumentation/http/http.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ test('built-in http module instrumentation', async (t) => {

await t.test('request headers for outbound request', async (t) => {
await t.test('should preserve headers regardless of format', (t, end) => {
const agent = helper.instrumentMockedAgent({
const agent = helper.loadTestAgent(t, {
cross_application_tracer: { enabled: true },
distributed_tracing: { enabled: false },
encoding_key: encKey,
Expand All @@ -940,9 +940,6 @@ test('built-in http module instrumentation', async (t) => {

const http = require('http')
let hadExpect = 0
t.after(() => {
helper.unloadAgent(agent)
})

const server = http.createServer(function (req, res) {
if (req.headers.expect) {
Expand Down
6 changes: 1 addition & 5 deletions test/unit/instrumentation/memcached.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ const test = require('node:test')
const assert = require('node:assert')

test('agent instrumentation of memcached should not cause bootstrapping to fail', async function (t) {
const agent = helper.loadMockedAgent()
const agent = helper.loadTestAgent(t)
const initialize = require('../../../lib/instrumentation/memcached')

t.after(function () {
helper.unloadAgent(agent)
})

await t.test('when passed no module', async function () {
assert.doesNotThrow(() => {
initialize(agent)
Expand Down
12 changes: 2 additions & 10 deletions test/unit/shim/shim.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3096,11 +3096,7 @@ test('Shim', async function (t) {
})

await t.test('shim.specs', (t) => {
const agent = helper.loadMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})

const agent = helper.loadTestAgent(t)
const shim = new Shim(agent, 'test-mod')
assert.ok(shim.specs, 'should assign specs to an instance of shim')
assert.ok(shim.specs.ClassWrapSpec)
Expand All @@ -3120,11 +3116,7 @@ test('Shim', async function (t) {
})

await t.test('should not use functions in MessageSubscribeSpec if it is not an array', (t) => {
const agent = helper.loadMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})

const agent = helper.loadTestAgent(t)
const shim = new Shim(agent, 'test-mod')
const spec = new shim.specs.MessageSubscribeSpec({
functions: 'foo-bar'
Expand Down
21 changes: 4 additions & 17 deletions test/unit/shimmer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,11 +631,7 @@ test('shimmer', async function (t) {
})

test('Should not augment module when no instrumentation hooks provided', async (t) => {
const agent = helper.instrumentMockedAgent()

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

const instrumentationOpts = {
moduleName: TEST_MODULE_PATH,
Expand All @@ -659,30 +655,21 @@ test('Should not augment module when no instrumentation hooks provided', async (
})

test('Should not crash on empty instrumentation registration', async (t) => {
const agent = helper.instrumentMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})
helper.loadTestAgent(t)

assert.doesNotThrow(shimmer.registerInstrumentation)
})

test('Should not register instrumentation with no name provided', async (t) => {
const agent = helper.instrumentMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})
helper.loadTestAgent(t)

shimmer.registerInstrumentation({})

assert.ok(!shimmer.registeredInstrumentations.undefined)
})

test('Should not register when no hooks provided', async (t) => {
const agent = helper.instrumentMockedAgent()
t.after(() => {
helper.unloadAgent(agent)
})
helper.loadTestAgent(t)

const moduleName = 'test name'
shimmer.registerInstrumentation({
Expand Down
30 changes: 6 additions & 24 deletions test/unit/transaction/trace/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,16 +757,12 @@ test('when inserting segments', async (t) => {
test('should set URI to null when request.uri attribute is excluded globally', async (t) => {
const URL = '/test'

const agent = helper.loadMockedAgent({
const agent = helper.loadTestAgent(t, {
attributes: {
exclude: ['request.uri']
}
})

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

const transaction = new Transaction(agent)
transaction.url = URL
transaction.verb = 'GET'
Expand All @@ -784,18 +780,12 @@ test('should set URI to null when request.uri attribute is excluded globally', a
test('should set URI to null when request.uri attribute is exluded from traces', async (t) => {
const URL = '/test'

const agent = helper.loadMockedAgent({
transaction_tracer: {
attributes: {
exclude: ['request.uri']
}
const agent = helper.loadTestAgent(t, {
attributes: {
exclude: ['request.uri']
}
})

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

const transaction = new Transaction(agent)
transaction.url = URL
transaction.verb = 'GET'
Expand All @@ -811,11 +801,7 @@ test('should set URI to null when request.uri attribute is exluded from traces',
})

test('should set URI to /Unknown when URL is not known/set on transaction', async (t) => {
const agent = helper.loadMockedAgent()

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

const transaction = new Transaction(agent)
const trace = transaction.trace
Expand All @@ -830,7 +816,7 @@ test('should set URI to /Unknown when URL is not known/set on transaction', asyn

test('should obfuscate URI using regex when pattern is set', async (t) => {
const URL = '/abc/123/def/456/ghi'
const agent = helper.loadMockedAgent({
const agent = helper.loadTestAgent(t, {
url_obfuscation: {
enabled: true,
regex: {
Expand All @@ -841,10 +827,6 @@ test('should obfuscate URI using regex when pattern is set', async (t) => {
}
})

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

const transaction = new Transaction(agent)
transaction.url = URL
transaction.verb = 'GET'
Expand Down
6 changes: 1 addition & 5 deletions test/versioned/express/app-use.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@ const tsplan = require('@matteo.collina/tspl')
// This test is no longer applicable in express 5 as mounting a child router does not emit the same
// mount event
test('app should be at top of stack when mounted', { skip: isExpress5() }, async function (t) {
const agent = helper.instrumentMockedAgent()
helper.loadTestAgent(t)
const express = require('express')

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

const plan = tsplan(t, { plan: 1 })

const main = express()
Expand Down
Loading
Loading