From 502753f10078fc13e73fb7e99674e91bbe47d916 Mon Sep 17 00:00:00 2001 From: Nidhi Work Date: Thu, 29 Feb 2024 17:55:30 +0000 Subject: [PATCH] test(cli): add unit tests for resolving config with applyScriptChanges --- .../lib/platform/aws-ecs/legacy/bom.js | 2 +- .../fargate-bom-applyScriptChanges.test.js | 151 ++++++++++++++++++ 2 files changed, 152 insertions(+), 1 deletion(-) create mode 100644 packages/artillery/test/unit/fargate-bom-applyScriptChanges.test.js diff --git a/packages/artillery/lib/platform/aws-ecs/legacy/bom.js b/packages/artillery/lib/platform/aws-ecs/legacy/bom.js index f6eedc6e1d..ac0894f167 100644 --- a/packages/artillery/lib/platform/aws-ecs/legacy/bom.js +++ b/packages/artillery/lib/platform/aws-ecs/legacy/bom.js @@ -472,4 +472,4 @@ function prettyPrint(manifest) { artillery.log(); } -module.exports = { createBOM, commonPrefix, prettyPrint }; +module.exports = { createBOM, commonPrefix, prettyPrint, applyScriptChanges }; diff --git a/packages/artillery/test/unit/fargate-bom-applyScriptChanges.test.js b/packages/artillery/test/unit/fargate-bom-applyScriptChanges.test.js new file mode 100644 index 0000000000..39ac8b6a20 --- /dev/null +++ b/packages/artillery/test/unit/fargate-bom-applyScriptChanges.test.js @@ -0,0 +1,151 @@ +'use strict'; + +const { applyScriptChanges } = require('../../lib/platform/aws-ecs/legacy/bom'); + +const { test } = require('tap'); + +test('applyScriptChanges', async (t) => { + await t.test( + 'should resolve config templates with cli variables', + async (t) => { + global.artillery.testRunId = 'bombolini_id_1234567890'; + const context = { + opts: { + scriptData: { + config: { + payload: { + path: '{{ myPayloadPath }}' + }, + plugins: { + 'publish-metrics': [ + { + type: 'datadog', + apiKey: '{{ myKey }}', + traces: { + serviceName: '{{ name }}', + attributes: { + testId: '{{ $testId }}' + } + } + } + ] + } + } + }, + absoluteScriptPath: '/path/to/script.yml', + flags: { + variables: JSON.stringify({ + name: 'Bombolini', + myPayloadPath: '/path/to/payload.json', + myKey: 'my_bombolini_key_1234567890' + }) + } + } + }; + + const next = (err, context) => { + if (err) { + t.fail(err); + } + t.equal( + context.opts.scriptData.config.payload.path, + '/path/to/payload.json' + ); + t.equal( + context.opts.scriptData.config.plugins['publish-metrics'][0].apiKey, + 'my_bombolini_key_1234567890', + 'Should resolve config templates with cli variables' + ); + t.equal( + context.opts.scriptData.config.plugins['publish-metrics'][0].traces + .serviceName, + 'Bombolini', + 'Should resolve config templates with cli variables on all config depth levels' + ); + t.equal( + context.opts.scriptData.config.plugins['publish-metrics'][0].traces + .attributes.testId, + 'bombolini_id_1234567890', + 'Should resolve $testId with global.artillery.testRunId' + ); + }; + + applyScriptChanges(context, next); + delete global.artillery.testRunId; + } + ); + + await t.test( + 'should resolve config templates with env variables', + async (t) => { + process.env.BOMBOLINI_PATH_TO_PAYLOAD = '/path/to/payload.json'; + process.env.MY_BOMBOLINI_KEY = 'my_bombolini_key_1234567890'; + process.env.BOMBOLINI_TEST_ID = 'bombolini_id_1234567890'; + const context = { + opts: { + scriptData: { + config: { + payload: { + path: '{{ $env.BOMBOLINI_PATH_TO_PAYLOAD }}' + }, + plugins: { + 'publish-metrics': [ + { + type: 'datadog', + apiKey: '{{ $processEnvironment.MY_BOMBOLINI_KEY }}', + traces: { + serviceName: '{{ $environment.BOMBOLINI_NAME }}', + attributes: { + testId: '{{ $env.BOMBOLINI_TEST_ID }}' + } + } + } + ] + } + } + }, + absoluteScriptPath: '/path/to/script.yml', + flags: { + environment: { + BOMBOLINI_NAME: 'Bombolini' + } + } + } + }; + + const next = (err, context) => { + if (err) { + t.fail(err); + } + t.equal( + context.opts.scriptData.config.payload.path, + '/path/to/payload.json', + 'Should resolve $env templates with env vars' + ); + t.equal( + context.opts.scriptData.config.plugins['publish-metrics'][0].apiKey, + 'my_bombolini_key_1234567890', + 'Should resolve $processEnvironment templates with env vars' + ); + t.equal( + context.opts.scriptData.config.plugins['publish-metrics'][0].traces + .serviceName, + 'Bombolini', + 'Should resolve $environment templates with vars from flags.environment' + ); + t.equal( + context.opts.scriptData.config.plugins['publish-metrics'][0].traces + .attributes.testId, + 'bombolini_id_1234567890', + 'Should resolve env vars on all levels of test script' + ); + }; + + applyScriptChanges(context, next); + + delete process.env.BOMBOLINI_PATH_TO_PAYLOAD; + delete process.env.MY_BOMBOLINI_KEY; + delete process.env.BOMBOLINI_TEST_ID; + } + ); +});