From 62d1c9e3d9342a97cec22a614490da9479b34343 Mon Sep 17 00:00:00 2001 From: Frank Schmid Date: Tue, 6 Jun 2017 22:34:54 +0200 Subject: [PATCH] Handle upgrade from SLS projects correctly. Added more unit tests. --- lib/stackops/lambdaRole.js | 5 ++++- test/stackops/lambdaRole.test.js | 38 ++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/stackops/lambdaRole.js b/lib/stackops/lambdaRole.js index 661c792..4eadbbf 100644 --- a/lib/stackops/lambdaRole.js +++ b/lib/stackops/lambdaRole.js @@ -51,7 +51,10 @@ module.exports = function(currentTemplate, aliasStackTemplates, currentAliasStac func.DependsOn[dependencyIndex] = roleName; }); - if (_.isEmpty(utils.findReferences(currentTemplate.Resources, 'IamRoleLambdaExecution')) && _.has(currentTemplate, 'Resources.IamRoleLambdaExecution')) { + if (_.has(currentTemplate, 'Resources.IamRoleLambdaExecution')) { + if (!_.isEmpty(utils.findReferences(currentTemplate.Resources, 'IamRoleLambdaExecution'))) { + stageStack.Resources.IamRoleLambdaExecution = currentTemplate.Resources.IamRoleLambdaExecution; + } delete currentTemplate.Resources.IamRoleLambdaExecution; } diff --git a/test/stackops/lambdaRole.test.js b/test/stackops/lambdaRole.test.js index d4bd25a..f2c9128 100644 --- a/test/stackops/lambdaRole.test.js +++ b/test/stackops/lambdaRole.test.js @@ -57,13 +57,47 @@ describe('lambdaRole', () => { let stack; beforeEach(() => { - stack = _.clone(require('../data/sls-stack-1.json')); - }) + stack = _.cloneDeep(require('../data/sls-stack-1.json')); + }); it('should succeed with standard template', () => { serverless.service.provider.compiledCloudFormationTemplate = stack; return expect(awsAlias.aliasHandleLambdaRole({}, [], {})).to.be.fulfilled; }); + it('should remove old global IAM role when there are no references', () => { + const currentTemplate = { + Resources: { + IamRoleLambdaExecution: {} + }, + Outputs: {} + }; + serverless.service.provider.compiledCloudFormationTemplate = stack; + return expect(awsAlias.aliasHandleLambdaRole(currentTemplate, [], {})).to.be.fulfilled + .then(() => expect(currentTemplate).to.not.have.a.property('IamRoleLambdaExecution')); + }); + + it('should retain existing alias roles', () => { + const aliasTemplates = [{ + Resources: {}, + Outputs: { + ServerlessAliasName: { + Description: 'The current alias', + Value: 'testAlias' + } + } + }]; + const currentTemplate = { + Resources: { + IamRoleLambdaExecution: {}, + IamRoleLambdaExecutiontestAlias: {} + }, + Outputs: {} + }; + const stackTemplate = serverless.service.provider.compiledCloudFormationTemplate = stack; + return expect(awsAlias.aliasHandleLambdaRole(currentTemplate, aliasTemplates, {})).to.be.fulfilled + .then(() => expect(stackTemplate).to.have.a.deep.property('Resources.IamRoleLambdaExecutiontestAlias')); + }); + }); });