Skip to content

Commit

Permalink
feat: use lambda container images with lambda
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardobridge committed Apr 16, 2024
1 parent 9c6625c commit 47449d2
Show file tree
Hide file tree
Showing 6 changed files with 536 additions and 184 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-ecs-worker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ jobs:
- name: Build the Docker image
run: |
docker build . --build-arg="WORKER_VERSION=${{ env.WORKER_VERSION }}" --tag public.ecr.aws/d8a4z9o5/artillery-worker:${{ env.WORKER_VERSION }} -f ./packages/artillery/lib/platform/aws-ecs/worker/Dockerfile
docker build . --build-arg="WORKER_VERSION=${{ env.WORKER_VERSION }}" --tag public.ecr.aws/d8a4z9o5/artillery-worker:${{ env.WORKER_VERSION }} -f ./packages/artillery/lib/platform/aws-lambda/lambda-handler/Dockerfile
- name: Push Docker image
run: |
Expand Down
142 changes: 142 additions & 0 deletions packages/artillery/lib/platform/aws-lambda/create-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const { promisify } = require('node:util');
const { createBOM } = require('../../create-bom/create-bom');
const createS3Client = require('../aws-ecs/legacy/create-s3-client');
const debug = require('debug')('aws:lambda');
const Table = require('cli-table3');
const fs = require('fs');

const prepareManifest = async (absoluteScriptPath, absoluteConfigPath) => {
let createBomOpts = {};
let entryPoint = absoluteScriptPath;
let extraFiles = [];
if (absoluteConfigPath) {
entryPoint = absoluteConfigPath;
extraFiles.push(absoluteScriptPath);
createBomOpts.entryPointIsConfig = true;
}
// TODO: custom package.json path here

// artillery.log('- Bundling test data');
const bom = await promisify(createBOM)(entryPoint, extraFiles, createBomOpts);

return bom;
};

const prettyPrintManifest = (bomManifest) => {
artillery.logger({ showTimestamp: true }).log('Test bundle prepared...');
artillery.log('Test bundle contents:');
const t = new Table({ head: ['Name', 'Type', 'Notes'] });
for (const f of bomManifest.files) {
t.push([f.noPrefix, 'file']);
}
for (const m of bomManifest.modules) {
t.push([
m,
'package',
bomManifest.pkgDeps.indexOf(m) === -1 ? 'not in package.json' : ''
]);
}
artillery.log(t.toString());
artillery.log();
};

async function uploadFileToS3(item, testRunId, bucketName) {
const plainS3 = createS3Client();
const prefix = `tests/${testRunId}`;
// If we can't read the file, it may have been specified with a
// template in its name, e.g. a payload file like:
// {{ $environment }}-users.csv
// If so, ignore it, hope config.includeFiles was used, and let
// "artillery run" in the worker deal with it.
let body;
try {
body = fs.readFileSync(item.orig);
} catch (fsErr) {
console.log(fsErr);
}

if (!body) {
return;
}

const key = prefix + '/' + item.noPrefix;

try {
await plainS3.putObject({
Bucket: bucketName,
Key: key,
// TODO: stream, not readFileSync
Body: body
}).promise();

console.log(`Uploaded ${key}`);
return;
} catch (err) {
//TODO: retry if needed
console.log(err);
return;
}
}

async function syncS3(bomManifest, testRunId, bucketName) {
console.log('Will try syncing to:', bucketName);

console.log('Manifest: ', bomManifest);
const metadata = {
createdOn: Date.now(),
name: testRunId,
modules: bomManifest.modules
};

//TODO: parallelise this
for (const file of bomManifest.files) {
console.log(`STARTING ON FILE:`)
console.log(file)
await uploadFileToS3(file, testRunId, bucketName);
}

const plainS3 = createS3Client();
const prefix = `tests/${testRunId}`;

console.log(bucketName)
console.log(prefix)

//TODO: add writeTestMetadata with configPath and newScriptPath if needed
try {
const key = prefix + '/metadata.json';
await plainS3.putObject({
Bucket: bucketName,
Key: key,
// TODO: stream, not readFileSync
Body: JSON.stringify(metadata)
}).promise();

console.log(`Uploaded ${key}`);
return;
} catch (err) {
//TODO: retry if needed
debug(err);
return;
}

}

const createTest = async (
absoluteScriptPath,
absoluteConfigPath,
testRunId,
bucketName
) => {
const bom = await prepareManifest(absoluteScriptPath, absoluteConfigPath);

prettyPrintManifest(bom);
console.log(bom)

await syncS3(bom, testRunId, bucketName);

return bom;
};

module.exports = {
createTest
};
Loading

0 comments on commit 47449d2

Please sign in to comment.