From 78f2709a24328225f0c3c2392cf0a3b8072a877f Mon Sep 17 00:00:00 2001 From: Bob Evans Date: Wed, 20 Nov 2024 13:45:14 -0500 Subject: [PATCH] test: Migrated root smoke tests to `node:test` (#2771) --- test/smoke/client-s3.tap.js | 77 ------------------- test/smoke/client-s3.test.js | 66 ++++++++++++++++ ...sp_connect.tap.js => lasp-connect.test.js} | 32 ++++---- ...p.js => proxy-api-connection-auth.test.js} | 17 ++-- ...s => proxy-api-connection-noproxy.test.js} | 21 +++-- test/smoke/s3-presigned-url.tap.js | 72 ----------------- test/smoke/s3-presigned-url.test.js | 65 ++++++++++++++++ 7 files changed, 167 insertions(+), 183 deletions(-) delete mode 100644 test/smoke/client-s3.tap.js create mode 100644 test/smoke/client-s3.test.js rename test/smoke/{lasp_connect.tap.js => lasp-connect.test.js} (65%) rename test/smoke/{proxy-api-connection-auth.tap.js => proxy-api-connection-auth.test.js} (72%) rename test/smoke/{proxy-api-connection-noproxy.tap.js => proxy-api-connection-noproxy.test.js} (65%) delete mode 100644 test/smoke/s3-presigned-url.tap.js create mode 100644 test/smoke/s3-presigned-url.test.js diff --git a/test/smoke/client-s3.tap.js b/test/smoke/client-s3.tap.js deleted file mode 100644 index 56bbca7ffa..0000000000 --- a/test/smoke/client-s3.tap.js +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright 2023 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const tap = require('tap') -const helper = require('../lib/agent_helper') -const { - DESTINATIONS: { TRANS_SEGMENT } -} = require('../../lib/config/attribute-filter') - -tap.test('@aws-sdk/client-s3 functionality', (t) => { - t.before(() => { - const { version, name } = require('@aws-sdk/client-s3/package') - // eslint-disable-next-line no-console - console.log(`AWS package: ${name} version: ${version}`) - }) - - t.beforeEach((t) => { - t.context.agent = helper.instrumentMockedAgent() - const { S3, ...lib } = require('@aws-sdk/client-s3') - t.context.client = new S3({ region: 'us-east-2' }) - t.context.GetObjectCommand = lib.GetObjectCommand - }) - - t.afterEach((t) => { - helper.unloadAgent(t.context.agent) - }) - - t.test('GetObjectCommand', (t) => { - const { agent, client, GetObjectCommand } = t.context - helper.runInTransaction(agent, async (transaction) => { - const command = new GetObjectCommand({ - Bucket: 'node-agent-aws-smoke-tests', - Key: 'test-file.json' - }) - - const { Body } = await client.send(command) - const fileContents = JSON.parse(await Body.transformToString()) - - t.same( - fileContents, - { items: [{ name: 'Item 1' }, { name: 'Item 2' }] }, - 'should successfully fetch test-file.json from S3' - ) - - transaction.end() - - const { url, procedure, ...awsAttributes } = - transaction.trace.root.children[1].attributes.get(TRANS_SEGMENT) - - delete awsAttributes.nr_exclusive_duration_millis - - t.equal( - url, - 'http://node-agent-aws-smoke-tests.s3.us-east-2.amazonaws.com/test-file.json', - 'should have url attribute in segment' - ) - t.equal(procedure, 'GET', 'should have method attribute in segment') - t.same( - awsAttributes, - { - 'aws.operation': 'GetObjectCommand', - 'aws.requestId': 'Unknown', - 'aws.service': 'S3', - 'aws.region': 'us-east-2' - }, - 'should have the proper AWS attributes in segment' - ) - - t.end() - }) - }) - t.end() -}) diff --git a/test/smoke/client-s3.test.js b/test/smoke/client-s3.test.js new file mode 100644 index 0000000000..1e0944d6c3 --- /dev/null +++ b/test/smoke/client-s3.test.js @@ -0,0 +1,66 @@ +/* + * Copyright 2023 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +const test = require('node:test') +const assert = require('node:assert') +const helper = require('../lib/agent_helper') +const { + DESTINATIONS: { TRANS_SEGMENT } +} = require('../../lib/config/attribute-filter') + +test('@aws-sdk/client-s3 functionality', async (t) => { + const { version, name } = require('@aws-sdk/client-s3/package') + // eslint-disable-next-line no-console + console.log(`AWS package: ${name} version: ${version}`) + const agent = helper.instrumentMockedAgent() + const { S3, ...lib } = require('@aws-sdk/client-s3') + const client = new S3({ region: 'us-east-2' }) + const { GetObjectCommand } = lib + + t.after(() => { + helper.unloadAgent(agent) + }) + + await helper.runInTransaction(agent, async (transaction) => { + const command = new GetObjectCommand({ + Bucket: 'node-agent-aws-smoke-tests', + Key: 'test-file.json' + }) + + const { Body } = await client.send(command) + const fileContents = JSON.parse(await Body.transformToString()) + + assert.deepEqual( + fileContents, + { items: [{ name: 'Item 1' }, { name: 'Item 2' }] }, + 'should successfully fetch test-file.json from S3' + ) + + transaction.end() + + const { url, procedure, ...awsAttributes } = + transaction.trace.root.children[1].attributes.get(TRANS_SEGMENT) + + delete awsAttributes.nr_exclusive_duration_millis + + assert.equal( + url, + 'http://node-agent-aws-smoke-tests.s3.us-east-2.amazonaws.com/test-file.json', + 'should have url attribute in segment' + ) + assert.equal(procedure, 'GET', 'should have method attribute in segment') + assert.deepEqual( + awsAttributes, + { + 'aws.operation': 'GetObjectCommand', + 'aws.requestId': 'Unknown', + 'aws.service': 'S3', + 'aws.region': 'us-east-2' + }, + 'should have the proper AWS attributes in segment' + ) + }) +}) diff --git a/test/smoke/lasp_connect.tap.js b/test/smoke/lasp-connect.test.js similarity index 65% rename from test/smoke/lasp_connect.tap.js rename to test/smoke/lasp-connect.test.js index 2499130347..915306c9bc 100644 --- a/test/smoke/lasp_connect.tap.js +++ b/test/smoke/lasp-connect.test.js @@ -4,8 +4,8 @@ */ '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 CollectorAPI = require('../../lib/collector/api') @@ -13,7 +13,7 @@ const { getTestSecret } = require('../helpers/secrets') const laspLicense = getTestSecret('LASP_LICENSE') const laspSecureLicense = getTestSecret('LASP_SECURE_LICENSE') -tap.test('connecting with a LASP token should not error', (t) => { +test('connecting with a LASP token should not error', (t, end) => { const config = configurator.initialize({ app_name: 'node.js Tests', license_key: laspLicense, @@ -34,22 +34,22 @@ tap.test('connecting with a LASP token should not error', (t) => { const api = new CollectorAPI(agent) api.connect(function (error, response) { - t.notOk(error, 'connected without error') + assert.ok(!error, 'connected without error') const returned = response && response.payload - 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(returned, 'got boot configuration') + assert.ok(returned.agent_run_id, 'got run ID') + assert.ok(agent.config.run_id, 'run ID set in configuration') api.shutdown(function (error) { - t.notOk(error, 'should have shut down without issue') - t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown') - t.end() + assert.ok(!error, 'should have shut down without issue') + assert.ok(!agent.config.run_id, 'run ID should have been cleared by shutdown') + end() }) }) }) -tap.test('missing required policies should result in shutdown', (t) => { +test('missing required policies should result in shutdown', (t, end) => { const config = configurator.initialize({ app_name: 'node.js Tests', license_key: laspSecureLicense, @@ -69,10 +69,10 @@ tap.test('missing required policies should result in shutdown', (t) => { const agent = new Agent(config) agent.start(function (error, response) { - t.ok(error, 'should have error') - t.equal(error.message, 'Failed to connect to collector') - t.notOk(response, 'should not have response payload') - t.equal(agent._state, 'errored') - t.end() + assert.ok(error, 'should have error') + assert.equal(error.message, 'Failed to connect to collector') + assert.ok(!response, 'should not have response payload') + assert.equal(agent._state, 'errored') + end() }) }) diff --git a/test/smoke/proxy-api-connection-auth.tap.js b/test/smoke/proxy-api-connection-auth.test.js similarity index 72% rename from test/smoke/proxy-api-connection-auth.tap.js rename to test/smoke/proxy-api-connection-auth.test.js index 1b96a8b55b..38b85adbd0 100644 --- a/test/smoke/proxy-api-connection-auth.tap.js +++ b/test/smoke/proxy-api-connection-auth.test.js @@ -4,25 +4,25 @@ */ 'use strict' - +const test = require('node:test') const net = require('net') -const tap = require('tap') const configurator = require('../../lib/config') const Agent = require('../../lib/agent') const CollectorAPI = require('../../lib/collector/api') const { getTestSecret } = require('../helpers/secrets') const license = getTestSecret('TEST_LICENSE') +const { tspl } = require('@matteo.collina/tspl') -tap.test('proxy authentication should set headers', (t) => { - t.plan(2) +test('proxy authentication should set headers', async (t) => { + const plan = tspl(t, { plan: 2 }) const server = net.createServer() server.on('connection', (socket) => { socket.on('data', (chunk) => { const data = chunk.toString().split('\r\n') - t.equal(data[0], 'CONNECT staging-collector.newrelic.com:443 HTTP/1.1') - t.equal(data[1], 'Proxy-Authorization: Basic YTpi') + plan.equal(data[0], 'CONNECT staging-collector.newrelic.com:443 HTTP/1.1') + plan.equal(data[1], 'Proxy-Authorization: Basic YTpi') server.close() }) socket.end() @@ -52,7 +52,10 @@ tap.test('proxy authentication should set headers', (t) => { const api = new CollectorAPI(agent) api.connect(() => { - t.end() + // need a callback even though we dont care and + // are just asserting some of the outgoing http requests above }) }) + + await plan.completed }) diff --git a/test/smoke/proxy-api-connection-noproxy.tap.js b/test/smoke/proxy-api-connection-noproxy.test.js similarity index 65% rename from test/smoke/proxy-api-connection-noproxy.tap.js rename to test/smoke/proxy-api-connection-noproxy.test.js index 447fc90ce6..f0404df590 100644 --- a/test/smoke/proxy-api-connection-noproxy.tap.js +++ b/test/smoke/proxy-api-connection-noproxy.test.js @@ -4,15 +4,15 @@ */ '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 CollectorAPI = require('../../lib/collector/api') const { getTestSecret } = require('../helpers/secrets') const license = getTestSecret('TEST_LICENSE') -tap.test('no proxy set should not use proxy agent', (t) => { +test('no proxy set should not use proxy agent', (t, end) => { const config = configurator.initialize({ app_name: 'node.js Tests', license_key: license, @@ -34,18 +34,17 @@ tap.test('no proxy set should not use proxy agent', (t) => { const api = new CollectorAPI(agent) api.connect((error, response) => { - t.notOk(error, 'connected without error') + assert.ok(!error, 'connected without error') const returned = response && response.payload - 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(returned, 'got boot configuration') + assert.ok(returned.agent_run_id, 'got run ID') + assert.ok(agent.config.run_id, 'run ID set in configuration') api.shutdown((error) => { - t.notOk(error, 'should have shut down without issue') - t.notOk(agent.config.run_id, 'run ID should have been cleared by shutdown') - - t.end() + assert.ok(!error, 'should have shut down without issue') + assert.ok(!agent.config.run_id, 'run ID should have been cleared by shutdown') + end() }) }) }) diff --git a/test/smoke/s3-presigned-url.tap.js b/test/smoke/s3-presigned-url.tap.js deleted file mode 100644 index 000090529a..0000000000 --- a/test/smoke/s3-presigned-url.tap.js +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2023 New Relic Corporation. All rights reserved. - * SPDX-License-Identifier: Apache-2.0 - */ - -'use strict' - -const tap = require('tap') -const helper = require('../lib/agent_helper') -const https = require('https') - -tap.test('@aws-sdk/s3-request-presigner functionality', (t) => { - t.before(() => { - const { version, name } = require('@aws-sdk/s3-request-presigner/package') - // eslint-disable-next-line no-console - console.log(`AWS package: ${name} version: ${version}`) - }) - - t.beforeEach((t) => { - t.context.agent = helper.instrumentMockedAgent() - const { S3, ...lib } = require('@aws-sdk/client-s3') - t.context.client = new S3({ region: 'us-east-2' }) - t.context.GetObjectCommand = lib.GetObjectCommand - - const requestPresigner = require('@aws-sdk/s3-request-presigner') - t.context.getSignedUrl = requestPresigner.getSignedUrl - }) - - t.afterEach((t) => { - helper.unloadAgent(t.context.agent) - }) - - t.test('GetObjectCommand', (t) => { - const { agent, client, GetObjectCommand, getSignedUrl } = t.context - helper.runInTransaction(agent, async () => { - const command = new GetObjectCommand({ - Bucket: 'node-agent-aws-smoke-tests', - Key: 'test-file.json' - }) - - // VERY IMPORTANT: DO NOT LOG THIS OUT IN CI, ANYONE WITH ACCESS TO THE URL GETS ACCESS TO THE OBJECT - const url = await getSignedUrl(client, command, { expiresIn: 2 }) - - https - .get(url, (res) => { - const { statusCode } = res - - t.equal(statusCode, 200, 'should successfully access the object using the presigned url') - - let buff = '' - res.on('data', (chunk) => { - buff = buff + chunk.toString() - }) - - res.on('end', () => { - const body = JSON.parse(buff) - - t.same( - body, - { items: [{ name: 'Item 1' }, { name: 'Item 2' }] }, - 'should successfully fetch the object using the presigned url' - ) - t.end() - }) - }) - .on('error', () => { - throw new Error('Fetching object using presigned url failed, debug locally') - }) - }) - }) - t.end() -}) diff --git a/test/smoke/s3-presigned-url.test.js b/test/smoke/s3-presigned-url.test.js new file mode 100644 index 0000000000..d0c5372081 --- /dev/null +++ b/test/smoke/s3-presigned-url.test.js @@ -0,0 +1,65 @@ +/* + * Copyright 2023 New Relic Corporation. All rights reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +'use strict' +const test = require('node:test') +const assert = require('node:assert') +const helper = require('../lib/agent_helper') +const https = require('https') + +test('@aws-sdk/s3-request-presigner functionality', (t, end) => { + const { version, name } = require('@aws-sdk/s3-request-presigner/package') + // eslint-disable-next-line no-console + console.log(`AWS package: ${name} version: ${version}`) + const agent = helper.instrumentMockedAgent() + const { S3, ...lib } = require('@aws-sdk/client-s3') + const client = new S3({ region: 'us-east-2' }) + const { GetObjectCommand } = lib + const { getSignedUrl } = require('@aws-sdk/s3-request-presigner') + + t.after(() => { + helper.unloadAgent(agent) + }) + + helper.runInTransaction(agent, async () => { + const command = new GetObjectCommand({ + Bucket: 'node-agent-aws-smoke-tests', + Key: 'test-file.json' + }) + + // VERY IMPORTANT: DO NOT LOG THIS OUT IN CI, ANYONE WITH ACCESS TO THE URL GETS ACCESS TO THE OBJECT + const url = await getSignedUrl(client, command, { expiresIn: 2 }) + + https + .get(url, (res) => { + const { statusCode } = res + + assert.equal( + statusCode, + 200, + 'should successfully access the object using the presigned url' + ) + + let buff = '' + res.on('data', (chunk) => { + buff = buff + chunk.toString() + }) + + res.on('end', () => { + const body = JSON.parse(buff) + + assert.deepEqual( + body, + { items: [{ name: 'Item 1' }, { name: 'Item 2' }] }, + 'should successfully fetch the object using the presigned url' + ) + end() + }) + }) + .on('error', () => { + throw new Error('Fetching object using presigned url failed, debug locally') + }) + }) +})