Skip to content

Commit

Permalink
test(fargate): test loading ARTILLERY_CLOUD_API_KEY from .env on Farg…
Browse files Browse the repository at this point in the history
…ate (#3265)
  • Loading branch information
InesNi authored Jul 30, 2024
1 parent ca69167 commit 89fb8d5
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 12 deletions.
126 changes: 126 additions & 0 deletions packages/artillery/test/cloud-e2e/fargate/cloud-api-key.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const { test, before, beforeEach } = require('tap');
const { $ } = require('zx');
const fs = require('fs');
const got = require('got');
const {
generateTmpReportPath,
getTestTags,
getTestId
} = require('../../helpers');
const {
checkForNegativeValues,
checkAggregateCounterSums
} = require('../../helpers/expectations');

const A9_PATH = process.env.A9_PATH || 'artillery';

before(async () => {
await $`${A9_PATH} -V`;
});

const baseTags = getTestTags(['type:acceptance']);

beforeEach(async (t) => {
$.verbose = true;
t.context.reportFilePath = generateTmpReportPath(t.name, 'json');
});

test('Cloud API key gets loaded from dotenv on Fargate runs', async (t) => {
const scenarioPath = `${__dirname}/fixtures/cloud-api-key-load/scenario.yml`;
const dotEnvPath = `${__dirname}/fixtures/cloud-api-key-load/cloud-key-env`;

// Move the key from process.env to the dotenv file so we can test if it is being properly loaded
fs.writeFileSync(
dotEnvPath,
`ARTILLERY_CLOUD_API_KEY=${process.env.ARTILLERY_CLOUD_API_KEY}`
);
delete process.env.ARTILLERY_CLOUD_API_KEY;

// Run the test without it to make sure the key is not available without the dotenv file
try {
await $`${A9_PATH} run-fargate ${scenarioPath} --record --tags ${baseTags}`;
} catch (err) {
console.log('Error in test run without API key: ', err.message);
t.ok(
err.message.includes(
'Error: API key is required to record test results to Artillery Cloud'
),
'Should error if API key is not provided'
);
}

// Run the test with the key provided in the dotenv file
let output;
try {
output =
await $`${A9_PATH} run-fargate ${scenarioPath} --output ${t.context.reportFilePath} --record --tags ${baseTags} --dotenv ${dotEnvPath}`;
} catch (err) {
console.log(err);
t.error(
err,
'Should not have errored when running the test with the API key provided in the dotenv file'
);
t.ok(
!err.message.includes(
'Error: API key is required to record test results to Artillery Cloud'
),
'The API key should be available when provided in the dotenv file'
);
}

// Get the test from the Artillery Cloud API
const testRunId = getTestId(output.stdout);
const testRunCloudEndpoint = `${
process.env.ARTILLERY_CLOUD_ENDPOINT || 'https://app.artillery.io'
}/api/load-tests/${testRunId}`;
console.log('Test run Cloud API endpoint: ', testRunCloudEndpoint);

let res;
try {
res = await got(testRunCloudEndpoint, {
headers: {
'x-auth-token': `${fs
.readFileSync(dotEnvPath, 'utf8')
.split('=')[1]
.trim()}`
},
throwHttpErrors: false
});
} catch (err) {
t.error(
err,
'Should not have errored when getting the test from the Artillery Cloud API'
);
}
console.log(`Response status: ${res?.statusCode} ${res?.statusMessage}`);

const testData = JSON.parse(res.body);
const report = JSON.parse(fs.readFileSync(t.context.reportFilePath, 'utf8'));

// Assertions
t.equal(output.exitCode, 0, 'CLI Exit Code should be 0');
t.ok(
output.stdout.includes(
'Artillery Cloud reporting is configured for this test run'
),
'Should have configured Artillery Cloud reporting'
);
t.equal(
res.statusCode,
200,
'Should get a 200 response when getting the test by id from the Artillery Cloud API'
);
t.ok(
t.equal(testData.id, testRunId, 'Correct test should be returned') &&
t.match(
testData?.report?.summary,
report.summary,
'Report data should match the report file'
),
'Should have successfully recorded the test to Artillery Cloud'
);

fs.unlinkSync(dotEnvPath);
checkForNegativeValues(t, report);
checkAggregateCounterSums(t, report);
});
5 changes: 3 additions & 2 deletions packages/artillery/test/cloud-e2e/fargate/cw-adot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ const fs = require('fs');
const {
generateTmpReportPath,
deleteFile,
getTestTags
getTestTags,
getTestId
} = require('../../helpers');
const { getTestId, getXRayTraces } = require('./fixtures/adot/helpers.js');
const { getXRayTraces } = require('./fixtures/adot/helpers.js');
const {
checkForNegativeValues,
checkAggregateCounterSums
Expand Down
5 changes: 3 additions & 2 deletions packages/artillery/test/cloud-e2e/fargate/dd-adot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ const fs = require('fs');
const {
generateTmpReportPath,
deleteFile,
getTestTags
getTestTags,
getTestId
} = require('../../helpers');
const {
checkForNegativeValues,
checkAggregateCounterSums
} = require('../../helpers/expectations');
const { getDatadogSpans, getTestId } = require('./fixtures/adot/helpers.js');
const { getDatadogSpans } = require('./fixtures/adot/helpers.js');

const A9_PATH = process.env.A9_PATH || 'artillery';
//NOTE: This test reports to Artillery Dashboard to dogfood and improve visibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,10 @@ const AWS = require('aws-sdk');
const xray = new AWS.XRay({ region: 'us-east-1' });

module.exports = {
getTestId,
getDatadogSpans,
getXRayTraces
};

function getTestId(outputString) {
const regex = /Test run ID: \S+/;
const match = outputString.match(regex);
return match[0].replace('Test run ID: ', '');
}

async function getDatadogSpans(apiKey, appKey, testId, expectedTotalSpans) {
const url = 'https://api.datadoghq.com/api/v2/spans/events/search';
const headers = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
config:
target: http://asciiart.artillery.io:8080
phases:
- duration: 1
arrivalRate: 1
scenarios:
- name: load homepage
flow:
- get:
url: "/dino"
8 changes: 7 additions & 1 deletion packages/artillery/test/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ const toCorrectPath = (originalPath) => {
? originalPath.split(path.sep).join(path.posix.sep)
: originalPath;
};
function getTestId(outputString) {
const regex = /Test run ID: \S+/i;
const match = outputString.match(regex);
return match[0].replace('Test run ID: ', '');
}

module.exports = {
execute,
Expand All @@ -68,5 +73,6 @@ module.exports = {
generateTmpReportPath,
getTestTags,
toCorrectPath,
getImageArchitecture
getImageArchitecture,
getTestId
};

0 comments on commit 89fb8d5

Please sign in to comment.