From 6f001dacfb0e6fa942c282c7981afdd8d911e8b0 Mon Sep 17 00:00:00 2001 From: Alex Casalboni Date: Mon, 20 May 2024 16:00:12 +0200 Subject: [PATCH] Improve publisher coverage & linting --- lambda/publisher.js | 23 +++++++++++------- test/unit/test-lambda.js | 52 +++++++++++++++++++++++++++++++--------- 2 files changed, 55 insertions(+), 20 deletions(-) diff --git a/lambda/publisher.js b/lambda/publisher.js index 84ae18b9..f1743cfe 100644 --- a/lambda/publisher.js +++ b/lambda/publisher.js @@ -9,17 +9,22 @@ module.exports.handler = async(event, context) => { // publish version & assign alias (if present) await utils.createPowerConfiguration(lambdaARN, currConfig.powerValue, currConfig.alias, currConfig.description); - // update iterator - const updatedIterator = { - index: (currentIterator.index + 1), - count: currentIterator.count, - continue: ((currentIterator.index + 1) < currentIterator.count), - }; - return { - initConfigurations: ((updatedIterator.continue) ? lambdaConfigurations.initConfigurations : undefined), - iterator: updatedIterator, + const result = { powerValues: lambdaConfigurations.powerValues, + initConfigurations: lambdaConfigurations.initConfigurations, + iterator: { + index: (currentIterator.index + 1), + count: currentIterator.count, + continue: ((currentIterator.index + 1) < currentIterator.count), + }, }; + + if (!result.iterator.continue) { + // clean the list of configuration if we're done iterating + delete result.initConfigurations; + } + + return result; }; function validateInputs(event) { if (!event.lambdaARN) { diff --git a/test/unit/test-lambda.js b/test/unit/test-lambda.js index 75b60bfb..59521d58 100644 --- a/test/unit/test-lambda.js +++ b/test/unit/test-lambda.js @@ -238,7 +238,8 @@ describe('Lambda Functions', async() => { { }, {lambdaARN: 'arnOK'}, {lambdaARN: 'arnOK', lambdaConfigurations: {}}, - {lambdaARN: 'arnOK', + { + lambdaARN: 'arnOK', lambdaConfigurations: { initConfigurations: [{ powerValue: 512, @@ -246,7 +247,8 @@ describe('Lambda Functions', async() => { }], }, }, - {lambdaARN: 'arnOK', + { + lambdaARN: 'arnOK', lambdaConfigurations: { iterator: { index: 1, @@ -254,7 +256,8 @@ describe('Lambda Functions', async() => { }, }, }, - {lambdaARN: 'arnOK', + { + lambdaARN: 'arnOK', lambdaConfigurations: { initConfigurations: [{ powerValue: 512, @@ -269,7 +272,8 @@ describe('Lambda Functions', async() => { }, }, }, - {lambdaARN: 'arnOK', + { + lambdaARN: 'arnOK', lambdaConfigurations: { initConfigurations: [{ powerValue: 512, @@ -292,28 +296,54 @@ describe('Lambda Functions', async() => { }); }); - it('should publish the given lambda version', async() => { + it('should publish the given lambda version (first iteration)', async() => { + const generatedValues = await invokeForSuccess(handler, { + lambdaARN: 'arnOK', + lambdaConfigurations: { + initConfigurations: [{ + powerValue: 512, + alias: 'RAM512', + }, { + powerValue: 1024, + alias: 'RAM1024', + }], + iterator: { + index: 0, + count: 2, + }, + }}); + expect(setLambdaPowerCounter).to.be(1); + expect(waitForFunctionUpdateCounter).to.be(1); + expect(publishLambdaVersionCounter).to.be(1); + expect(createLambdaAliasCounter).to.be(1); + expect(generatedValues.iterator.index).to.be(1); // index should be incremented by 1 + expect(generatedValues.iterator.continue).to.be(true); // the iterator should be set to continue=false + expect(generatedValues.initConfigurations).to.be.a('array'); // initConfigurations should be a list + }); - const aliasValue = 'RAM512'; - const originalIndex = 0; + it('should publish the given lambda version (last iteration)', async() => { const generatedValues = await invokeForSuccess(handler, { lambdaARN: 'arnOK', lambdaConfigurations: { initConfigurations: [{ powerValue: 512, - alias: aliasValue, + alias: 'RAM512', + }, { + powerValue: 1024, + alias: 'RAM1024', }], iterator: { - index: originalIndex, - count: 1, + index: 1, + count: 2, }, }}); expect(setLambdaPowerCounter).to.be(1); expect(waitForFunctionUpdateCounter).to.be(1); expect(publishLambdaVersionCounter).to.be(1); expect(createLambdaAliasCounter).to.be(1); - expect(generatedValues.iterator.index).to.be(originalIndex + 1); // index should be incremented by 1 + expect(generatedValues.iterator.index).to.be(2); // index should be incremented by 1 expect(generatedValues.iterator.continue).to.be(false); // the iterator should be set to continue=false + expect(generatedValues.initConfigurations).to.be(undefined); // initConfigurations should be unset }); it('should publish the version even if an alias is not specified', async() => {