Skip to content

Commit 03d4f71

Browse files
test: added more failing tests for new feature
1 parent 9a6b2e7 commit 03d4f71

File tree

2 files changed

+120
-1
lines changed

2 files changed

+120
-1
lines changed

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,6 +1261,54 @@ describe('#compileIamRole', () => {
12611261
});
12621262

12631263
it('should support local function names', () => {
1264+
const getStateMachine = name => ({
1265+
name,
1266+
definition: {
1267+
StartAt: 'A',
1268+
States: {
1269+
A: {
1270+
Type: 'Task',
1271+
Resource: {
1272+
'Fn::GetAtt': ['hello-world', 'Arn'],
1273+
},
1274+
End: true,
1275+
},
1276+
},
1277+
},
1278+
});
1279+
1280+
serverless.service.functions = {
1281+
'hello-world': {
1282+
handler: 'hello-world.handler',
1283+
},
1284+
};
1285+
1286+
serverless.service.stepFunctions = {
1287+
stateMachines: {
1288+
myStateMachine1: getStateMachine('sm1'),
1289+
},
1290+
};
1291+
1292+
serverlessStepFunctions.compileIamRole();
1293+
const statements = serverlessStepFunctions.serverless.service
1294+
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
1295+
.Properties.Policies[0].PolicyDocument.Statement;
1296+
1297+
const lambdaPermissions = statements.filter(s => _.isEqual(s.Action, ['lambda:InvokeFunction']));
1298+
expect(lambdaPermissions).to.have.lengthOf(1);
1299+
1300+
const lambdaArns = [
1301+
{
1302+
'Fn::GetAtt': [
1303+
'HelloDashworldLambdaFunction',
1304+
'Arn',
1305+
],
1306+
},
1307+
];
1308+
expect(lambdaPermissions[0].Resource).to.deep.eq(lambdaArns);
1309+
});
1310+
1311+
it('should support local function names for lambda::invoke resource type', () => {
12641312
const getStateMachine = (name, functionName) => ({
12651313
name,
12661314
definition: {
@@ -1284,7 +1332,7 @@ describe('#compileIamRole', () => {
12841332
const lambda1 = { Ref: 'hello-world' };
12851333
const lambda2 = { 'Fn::GetAtt': ['hello-world', 'Arn'] };
12861334

1287-
serverless.functions = {
1335+
serverless.service.functions = {
12881336
'hello-world': {
12891337
handler: 'hello-world.handler',
12901338
},

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,75 @@ describe('#compileStateMachines', () => {
777777
const refParam = params[functionParam];
778778
expect(refParam).to.eql({ 'Fn::GetAtt': ['HelloDashworldLambdaFunction', 'Arn'] });
779779
});
780+
781+
it('should support local function names for lambda::invoke resource type', () => {
782+
serverless.service.functions = {
783+
'hello-world': {
784+
handler: 'hello-world.handler',
785+
},
786+
};
787+
serverless.service.stepFunctions = {
788+
stateMachines: {
789+
myStateMachine1: {
790+
id: 'Test',
791+
definition: {
792+
StartAt: 'Lambda1',
793+
States: {
794+
Lambda1: {
795+
Type: 'Task',
796+
Resource: 'arn:aws:states:::lambda:invoke',
797+
Parameters: {
798+
FunctionName: {
799+
Ref: 'hello-world',
800+
},
801+
Payload: {
802+
'ExecutionName.$': '$$.Execution.Name',
803+
},
804+
},
805+
Next: 'Lambda2',
806+
},
807+
Lambda2: {
808+
Type: 'Task',
809+
Resource: 'arn:aws:states:::lambda:invoke',
810+
Parameters: {
811+
FunctionName: {
812+
'Fn::GetAtt': ['hello-world', 'Arn'],
813+
},
814+
Payload: {
815+
'ExecutionName.$': '$$.Execution.Name',
816+
},
817+
},
818+
End: true,
819+
},
820+
},
821+
},
822+
},
823+
},
824+
};
825+
826+
serverlessStepFunctions.compileStateMachines();
827+
const stateMachine = serverlessStepFunctions.serverless.service
828+
.provider.compiledCloudFormationTemplate.Resources
829+
.Test;
830+
831+
expect(stateMachine.Properties.DefinitionString).to.haveOwnProperty('Fn::Sub');
832+
expect(stateMachine.Properties.DefinitionString['Fn::Sub']).to.have.lengthOf(2);
833+
834+
const [json, params] = stateMachine.Properties.DefinitionString['Fn::Sub'];
835+
const modifiedDefinition = JSON.parse(json);
836+
837+
const lambda1 = modifiedDefinition.States.Lambda1;
838+
expect(lambda1.Parameters.FunctionName.startsWith('${')).to.eq(true);
839+
const lambda1ParamName = lambda1.Parameters.FunctionName.replace(/[${}]/g, '');
840+
expect(params).to.haveOwnProperty(lambda1ParamName);
841+
const lambda1Param = params[lambda1ParamName];
842+
expect(lambda1Param).to.eql({ Ref: 'HelloDashworldLambdaFunction' });
843+
844+
const lambda2 = modifiedDefinition.States.Lambda2;
845+
expect(lambda2.Parameters.FunctionName.startsWith('${')).to.eq(true);
846+
const lambda2ParamName = lambda2.Parameters.FunctionName.replace(/[${}]/g, '');
847+
expect(params).to.haveOwnProperty(lambda2ParamName);
848+
const lambda2Param = params[lambda2ParamName];
849+
expect(lambda2Param).to.eql({ 'Fn::GetAtt': ['HelloDashworldLambdaFunction', 'Arn'] });
850+
});
780851
});

0 commit comments

Comments
 (0)