From 88f4f3619dfd79c116fb08aad2e5a7b798a86c8b Mon Sep 17 00:00:00 2001 From: Bryan English Date: Mon, 16 Dec 2024 10:32:52 -0500 Subject: [PATCH] fix aws-sdk --- .../test/aws-sdk.spec.js | 14 +++++++++---- .../test/kinesis.spec.js | 8 ++++++-- .../test/lambda.spec.js | 5 ++++- .../datadog-plugin-aws-sdk/test/s3.spec.js | 6 +++++- .../datadog-plugin-aws-sdk/test/sns.spec.js | 8 ++++++-- .../datadog-plugin-aws-sdk/test/sqs.spec.js | 14 +++++++++---- .../test/stepfunctions.spec.js | 5 ++++- .../test/index.spec.js | 10 +++------- .../test/setup/helpers/version-utils.js | 20 ++++++++++++++++++- packages/dd-trace/test/setup/mocha.js | 4 +++- 10 files changed, 70 insertions(+), 24 deletions(-) diff --git a/packages/datadog-plugin-aws-sdk/test/aws-sdk.spec.js b/packages/datadog-plugin-aws-sdk/test/aws-sdk.spec.js index 848b00855d4..882d5233c4d 100644 --- a/packages/datadog-plugin-aws-sdk/test/aws-sdk.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/aws-sdk.spec.js @@ -4,6 +4,7 @@ const agent = require('../../dd-trace/test/plugins/agent') const { setup, sort } = require('./spec_helpers') const semver = require('semver') const { ERROR_MESSAGE, ERROR_STACK, ERROR_TYPE } = require('../../dd-trace/src/constants') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') describe('Plugin', () => { // TODO: use the Request class directly for generic tests @@ -75,6 +76,7 @@ describe('Plugin', () => { const s3ClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-s3' : 'aws-sdk' const sqsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sqs' : 'aws-sdk' + const versionCtx = withVersions.context describe('without configuration', () => { before(() => { @@ -82,7 +84,8 @@ describe('Plugin', () => { }) before(() => { - AWS = require(`../../../versions/${s3ClientName}@${version}`).get() + const clientVersion = getEquivVersion(s3ClientName, versionCtx) + AWS = require(`../../../versions/${s3ClientName}@${clientVersion}`).get() s3 = new AWS.S3({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1', s3ForcePathStyle: true }) tracer = require('../../dd-trace') }) @@ -225,7 +228,8 @@ describe('Plugin', () => { }) before(() => { - AWS = require(`../../../versions/${s3ClientName}@${version}`).get() + const clientVersion = getEquivVersion(s3ClientName, versionCtx) + AWS = require(`../../../versions/${s3ClientName}@${clientVersion}`).get() s3 = new AWS.S3({ endpoint: 'http://127.0.0.1:5000', region: 'us-east-1', s3ForcePathStyle: true }) tracer = require('../../dd-trace') }) @@ -262,8 +266,10 @@ describe('Plugin', () => { }) before(() => { - const { S3 } = require(`../../../versions/${s3ClientName}@${version}`).get() - const { SQS } = require(`../../../versions/${sqsClientName}@${version}`).get() + const s3Version = getEquivVersion(s3ClientName, versionCtx) + const { S3 } = require(`../../../versions/${s3ClientName}@${s3Version}`).get() + const sqsVersion = getEquivVersion(s3ClientName, versionCtx) + const { SQS } = require(`../../../versions/${sqsClientName}@${sqsVersion}`).get() s3 = new S3({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1', s3ForcePathStyle: true }) sqs = new SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) diff --git a/packages/datadog-plugin-aws-sdk/test/kinesis.spec.js b/packages/datadog-plugin-aws-sdk/test/kinesis.spec.js index 2e3bf356f3e..8e9ef195221 100644 --- a/packages/datadog-plugin-aws-sdk/test/kinesis.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/kinesis.spec.js @@ -6,6 +6,7 @@ const agent = require('../../dd-trace/test/plugins/agent') const { setup } = require('./spec_helpers') const helpers = require('./kinesis_helpers') const { rawExpectedSchema } = require('./kinesis-naming') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') describe('Kinesis', function () { this.timeout(10000) @@ -19,9 +20,11 @@ describe('Kinesis', function () { const streamName = 'MyStream' const streamNameDSM = 'MyStreamDSM' const kinesisClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-kinesis' : 'aws-sdk' + const versionCtx = withVersions.context function createResources (streamName, cb) { - AWS = require(`../../../versions/${kinesisClientName}@${version}`).get() + const clientVersion = getEquivVersion(kinesisClientName, versionCtx) + AWS = require(`../../../versions/${kinesisClientName}@${clientVersion}`).get() const params = { endpoint: 'http://127.0.0.1:4566', @@ -29,7 +32,8 @@ describe('Kinesis', function () { } if (moduleName === '@aws-sdk/smithy-client') { - const { NodeHttpHandler } = require(`../../../versions/@aws-sdk/node-http-handler@${version}`).get() + const handlerVersion = getEquivVersion('@aws-sdk/node-http-handler', versionCtx) + const { NodeHttpHandler } = require(`../../../versions/@aws-sdk/node-http-handler@${handlerVersion}`).get() params.requestHandler = new NodeHttpHandler() } diff --git a/packages/datadog-plugin-aws-sdk/test/lambda.spec.js b/packages/datadog-plugin-aws-sdk/test/lambda.spec.js index 5529a84677d..ac43b0dda1b 100644 --- a/packages/datadog-plugin-aws-sdk/test/lambda.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/lambda.spec.js @@ -4,6 +4,7 @@ const JSZip = require('jszip') const agent = require('../../dd-trace/test/plugins/agent') const { setup } = require('./spec_helpers') const { rawExpectedSchema } = require('./lambda-naming') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') const zip = new JSZip() @@ -19,6 +20,7 @@ describe('Plugin', () => { let tracer const lambdaClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-lambda' : 'aws-sdk' + const versionCtx = withVersions.context const parsePayload = payload => { if (typeof payload !== 'string') { @@ -38,7 +40,8 @@ describe('Plugin', () => { }) before(done => { - AWS = require(`../../../versions/${lambdaClientName}@${version}`).get() + const clientVersion = getEquivVersion(lambdaClientName, versionCtx) + AWS = require(`../../../versions/${lambdaClientName}@${clientVersion}`).get() lambda = new AWS.Lambda({ endpoint: 'http://127.0.0.1:4567', region: 'us-east-1' }) lambda.createFunction({ diff --git a/packages/datadog-plugin-aws-sdk/test/s3.spec.js b/packages/datadog-plugin-aws-sdk/test/s3.spec.js index 6e896efa281..de88131f65c 100644 --- a/packages/datadog-plugin-aws-sdk/test/s3.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/s3.spec.js @@ -5,6 +5,7 @@ const { setup } = require('./spec_helpers') const axios = require('axios') const { rawExpectedSchema } = require('./s3-naming') const { S3_PTR_KIND, SPAN_POINTER_DIRECTION } = require('../../dd-trace/src/constants') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') const bucketName = 's3-bucket-name-test' @@ -27,6 +28,8 @@ describe('Plugin', () => { let s3 let tracer + const versionCtx = withVersions.context + const s3ClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-s3' : 'aws-sdk' describe('with configuration', () => { before(() => { @@ -36,7 +39,8 @@ describe('Plugin', () => { }) before(done => { - AWS = require(`../../../versions/${s3ClientName}@${version}`).get() + const clientVersion = getEquivVersion(s3ClientName, versionCtx) + AWS = require(`../../../versions/${s3ClientName}@${clientVersion}`).get() s3 = new AWS.S3({ endpoint: 'http://127.0.0.1:4566', s3ForcePathStyle: true, region: 'us-east-1' }) // Fix for LocationConstraint issue - only for SDK v2 diff --git a/packages/datadog-plugin-aws-sdk/test/sns.spec.js b/packages/datadog-plugin-aws-sdk/test/sns.spec.js index b205c652669..98c26dcf6c1 100644 --- a/packages/datadog-plugin-aws-sdk/test/sns.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/sns.spec.js @@ -6,6 +6,7 @@ const semver = require('semver') const agent = require('../../dd-trace/test/plugins/agent') const { setup } = require('./spec_helpers') const { rawExpectedSchema } = require('./sns-naming') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') describe('Sns', function () { setup() @@ -24,6 +25,7 @@ describe('Sns', function () { const snsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sns' : 'aws-sdk' const sqsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sqs' : 'aws-sdk' + const versionCtx = withVersions.context let childSpansFound = 0 const assertPropagation = (done, childSpans = 1) => { @@ -45,8 +47,10 @@ describe('Sns', function () { } function createResources (queueName, topicName, cb) { - const { SNS } = require(`../../../versions/${snsClientName}@${version}`).get() - const { SQS } = require(`../../../versions/${sqsClientName}@${version}`).get() + const snsVersion = getEquivVersion(snsClientName, versionCtx) + const { SNS } = require(`../../../versions/${snsClientName}@${snsVersion}`).get() + const sqsVersion = getEquivVersion(sqsClientName, versionCtx) + const { SQS } = require(`../../../versions/${sqsClientName}@${sqsVersion}`).get() sns = new SNS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) sqs = new SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) diff --git a/packages/datadog-plugin-aws-sdk/test/sqs.spec.js b/packages/datadog-plugin-aws-sdk/test/sqs.spec.js index 35e3ce39d8c..04aadbc99ad 100644 --- a/packages/datadog-plugin-aws-sdk/test/sqs.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/sqs.spec.js @@ -5,6 +5,7 @@ const agent = require('../../dd-trace/test/plugins/agent') const { setup } = require('./spec_helpers') const semver = require('semver') const { rawExpectedSchema } = require('./sqs-naming') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') const queueName = 'SQS_QUEUE_NAME' const queueNameDSM = 'SQS_QUEUE_NAME_DSM' @@ -36,6 +37,7 @@ describe('Plugin', () => { let tracer const sqsClientName = moduleName === '@aws-sdk/smithy-client' ? '@aws-sdk/client-sqs' : 'aws-sdk' + const versionCtx = withVersions.context describe('without configuration', () => { before(() => { @@ -49,7 +51,8 @@ describe('Plugin', () => { }) before(done => { - AWS = require(`../../../versions/${sqsClientName}@${version}`).get() + const clientVersion = getEquivVersion(sqsClientName, versionCtx) + AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get() sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) sqs.createQueue(queueOptions, (err, res) => { @@ -314,7 +317,8 @@ describe('Plugin', () => { }) before(done => { - AWS = require(`../../../versions/${sqsClientName}@${version}`).get() + const clientVersion = getEquivVersion(sqsClientName, versionCtx) + AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get() sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) sqs.createQueue(queueOptions, (err, res) => { @@ -405,7 +409,8 @@ describe('Plugin', () => { }) before(done => { - AWS = require(`../../../versions/${sqsClientName}@${version}`).get() + const clientVersion = getEquivVersion(sqsClientName, versionCtx) + AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get() sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) sqs.createQueue(queueOptionsDsm, (err, res) => { @@ -416,7 +421,8 @@ describe('Plugin', () => { }) before(done => { - AWS = require(`../../../versions/${sqsClientName}@${version}`).get() + const clientVersion = getEquivVersion(sqsClientName, versionCtx) + AWS = require(`../../../versions/${sqsClientName}@${clientVersion}`).get() sqs = new AWS.SQS({ endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' }) sqs.createQueue(queueOptionsDsmConsumerOnly, (err, res) => { diff --git a/packages/datadog-plugin-aws-sdk/test/stepfunctions.spec.js b/packages/datadog-plugin-aws-sdk/test/stepfunctions.spec.js index 44677b4efed..273c16e759e 100644 --- a/packages/datadog-plugin-aws-sdk/test/stepfunctions.spec.js +++ b/packages/datadog-plugin-aws-sdk/test/stepfunctions.spec.js @@ -4,6 +4,7 @@ const semver = require('semver') const agent = require('../../dd-trace/test/plugins/agent') const { setup } = require('./spec_helpers') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') const helloWorldSMD = { Comment: 'A Hello World example of the Amazon States Language using a Pass state', @@ -23,6 +24,7 @@ describe('Sfn', () => { withVersions('aws-sdk', ['aws-sdk', '@aws-sdk/smithy-client'], (version, moduleName) => { let stateMachineArn let client + const versionCtx = withVersions.context setup() @@ -33,7 +35,8 @@ describe('Sfn', () => { function getClient () { const params = { endpoint: 'http://127.0.0.1:4566', region: 'us-east-1' } if (moduleName === '@aws-sdk/smithy-client') { - const lib = require(`../../../versions/@aws-sdk/client-sfn@${version}`).get() + const sfnVersion = getEquivVersion('@aws-sdk/client-sfn', versionCtx) + const lib = require(`../../../versions/@aws-sdk/client-sfn@${sfnVersion}`).get() const client = new lib.SFNClient(params) return { client, diff --git a/packages/datadog-plugin-langchain/test/index.spec.js b/packages/datadog-plugin-langchain/test/index.spec.js index 0e3c0c96f82..e425fbb98bd 100644 --- a/packages/datadog-plugin-langchain/test/index.spec.js +++ b/packages/datadog-plugin-langchain/test/index.spec.js @@ -2,7 +2,7 @@ const { useEnv } = require('../../../integration-tests/helpers') const agent = require('../../dd-trace/test/plugins/agent') -const { getIdeallyTestedVersions } = require('../../dd-trace/test/setup/helpers/version-utils') +const { getEquivVersion } = require('../../dd-trace/test/setup/helpers/version-utils') const nock = require('nock') @@ -34,12 +34,8 @@ describe('Plugin', () => { describe('langchain', () => { withVersions('langchain', ['@langchain/core'], version => { - const coreVersions = getIdeallyTestedVersions('@langchain/core', [withVersions.range]) - const isFirst = coreVersions[0].version === version - const openAiVersions = getIdeallyTestedVersions('@langchain/openai', [withVersions.range]) - const openAiVersion = (openAiVersions[isFirst ? 0 : openAiVersions.length - 1]).version - const anthropicVersions = getIdeallyTestedVersions('@langchain/anthropic', [withVersions.range]) - const anthropicVersion = (anthropicVersions[isFirst ? 0 : anthropicVersions.length - 1]).version + const openAiVersion = getEquivVersion('@langchain/openai', withVersions.context) + const anthropicVersion = getEquivVersion('@langchain/anthropic', withVersions.context) beforeEach(() => { return agent.load('langchain') diff --git a/packages/dd-trace/test/setup/helpers/version-utils.js b/packages/dd-trace/test/setup/helpers/version-utils.js index 04f65b08e6b..132bacb3de7 100644 --- a/packages/dd-trace/test/setup/helpers/version-utils.js +++ b/packages/dd-trace/test/setup/helpers/version-utils.js @@ -69,9 +69,27 @@ function getIdeallyTestedVersions (name, ranges) { }, []) } +// For a given module name, get the version of it corresponding to the same +// range as the currently tested module from `withVersions`. Note that this +// should only be used when a module and a support module have the same version +// range, but not necessarily the same versions within that range. +// You should always pass `withVersions.context` into this funciton as `orig`, +// and always capture it in the first scope inside the `withVersions` callback. +function getEquivVersion (name, orig) { + if (!orig) { + throw new Error('No context found to get equiv version') + } + + const origVersions = getIdeallyTestedVersions(orig.moduleName, [orig.range]) + const isFirst = origVersions[0].version === orig.version + const otherModuleVersions = getIdeallyTestedVersions(name, [orig.range]) + return otherModuleVersions[isFirst ? 0 : otherModuleVersions.length - 1].version +} + module.exports = { getAllVersions, getAllVersionsMatching, getIdeallyTestedVersionRanges, - getIdeallyTestedVersions + getIdeallyTestedVersions, + getEquivVersion } diff --git a/packages/dd-trace/test/setup/mocha.js b/packages/dd-trace/test/setup/mocha.js index 7ee2caadab0..312b075960d 100644 --- a/packages/dd-trace/test/setup/mocha.js +++ b/packages/dd-trace/test/setup/mocha.js @@ -215,8 +215,10 @@ function withVersions (plugin, modules, range, cb) { require('module').Module._initPaths() }) - withVersions.range = v.range + const context = { moduleName, ...v } + withVersions.context = context cb(v.version, moduleName, v.version) // TODO get rid of 3rd param here + delete withVersions.context after(() => { process.env.NODE_PATH = nodePath