Skip to content

Commit a634c73

Browse files
test: added tests
1 parent 6ebbad2 commit a634c73

File tree

5 files changed

+142
-6
lines changed

5 files changed

+142
-6
lines changed

lib/deploy/stepFunctions/compileIamRole.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,4 +1726,51 @@ describe('#compileIamRole', () => {
17261726
];
17271727
expect(lambdaPermissions[0].Resource).to.deep.equal(lambdaArns);
17281728
});
1729+
1730+
it('should give CloudWatch Logs permissions for Express Workflow', () => {
1731+
serverless.service.stepFunctions = {
1732+
stateMachines: {
1733+
myStateMachine1: {
1734+
name: 'stateMachineBeta1',
1735+
type: 'EXPRESS',
1736+
loggingConfig: {
1737+
destinations: [
1738+
{
1739+
'Fn::GetAtt': ['MyLogGroup', 'Arn'],
1740+
},
1741+
],
1742+
},
1743+
definition: {
1744+
StartAt: 'A',
1745+
States: {
1746+
A: {
1747+
Type: 'Task',
1748+
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
1749+
End: true,
1750+
},
1751+
},
1752+
},
1753+
},
1754+
},
1755+
};
1756+
1757+
serverlessStepFunctions.compileIamRole();
1758+
const statements = serverlessStepFunctions.serverless.service
1759+
.provider.compiledCloudFormationTemplate.Resources.IamRoleStateMachineExecution
1760+
.Properties.Policies[0].PolicyDocument.Statement;
1761+
1762+
const logsPermissions = statements.filter(s => s.Action.includes('logs:CreateLogDelivery'));
1763+
expect(logsPermissions).to.have.lengthOf(1);
1764+
expect(logsPermissions[0].Resource).to.equal('*');
1765+
expect(logsPermissions[0].Action).to.deep.equal([
1766+
'logs:CreateLogDelivery',
1767+
'logs:GetLogDelivery',
1768+
'logs:UpdateLogDelivery',
1769+
'logs:DeleteLogDelivery',
1770+
'logs:ListLogDeliveries',
1771+
'logs:PutResourcePolicy',
1772+
'logs:DescribeResourcePolicies',
1773+
'logs:DescribeLogGroups',
1774+
]);
1775+
});
17291776
});

lib/deploy/stepFunctions/compileNotifications.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ function validateConfig(serverless, stateMachineName, stateMachineObj, notificat
313313

314314
if (stateMachineObj.type === 'EXPRESS') {
315315
serverless.cli.consoleLog(
316-
`State machine [${stateMachineName}] : notifications is not enabled on Express Workflows. `
316+
`State machine [${stateMachineName}] : notifications are not supported on Express Workflows. `
317317
+ 'Please see https://docs.aws.amazon.com/step-functions/latest/dg/concepts-standard-vs-express.html for difference between Step Functions Standard and Express Workflow.',
318318
);
319319
return false;

lib/deploy/stepFunctions/compileNotifications.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,4 +433,40 @@ describe('#compileNotifications', () => {
433433
expect(logMessage.startsWith('State machine [Beta1] : notifications config is malformed.'))
434434
.to.equal(true);
435435
});
436+
437+
it('should log the validation errors if state machine is an Express Workflow', () => {
438+
const genStateMachine = name => ({
439+
name,
440+
type: 'EXPRESS',
441+
definition: {
442+
StartAt: 'A',
443+
States: {
444+
A: {
445+
Type: 'Pass',
446+
End: true,
447+
},
448+
},
449+
},
450+
notifications: {
451+
ABORTED: [{ sns: 'SNS_TOPIC_ARN' }],
452+
},
453+
});
454+
455+
serverless.service.stepFunctions = {
456+
stateMachines: {
457+
beta1: genStateMachine('Beta1'),
458+
},
459+
};
460+
461+
serverlessStepFunctions.compileNotifications();
462+
const resources = serverlessStepFunctions.serverless.service
463+
.provider.compiledCloudFormationTemplate.Resources;
464+
expect(_.keys(resources)).to.have.lengthOf(0);
465+
466+
expect(consoleLogSpy.callCount).equal(1);
467+
const { args } = consoleLogSpy.lastCall;
468+
const [logMessage] = args;
469+
expect(logMessage.startsWith('State machine [Beta1] : notifications are not supported on Express Workflows.'))
470+
.to.equal(true);
471+
});
436472
});

lib/deploy/stepFunctions/compileStateMachines.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ module.exports = {
9999
let LoggingConfiguration;
100100
const Tags = toTags(this.serverless.service.provider.tags);
101101

102-
const { error } = Joi.validate(stateMachineObj, schema, { allowUnknown: false });
102+
const { error, value } = Joi.validate(stateMachineObj, schema, { allowUnknown: false });
103103
if (error) {
104104
const errorMessage = `State machine [${stateMachineName}] is malformed. `
105105
+ 'Please check the README for more info. '
@@ -182,16 +182,16 @@ module.exports = {
182182
_.forEach(stateMachineTags, tag => Tags.push(tag));
183183
}
184184

185-
if (stateMachineObj.type === 'EXPRESS' && stateMachineObj.loggingConfig) {
186-
const Destinations = (stateMachineObj.loggingConfig.destinations || [])
185+
if (value.type === 'EXPRESS' && value.loggingConfig) {
186+
const Destinations = (value.loggingConfig.destinations || [])
187187
.map(arn => ({
188188
CloudWatchLogsLogGroup: {
189189
LogGroupArn: arn,
190190
},
191191
}));
192192
LoggingConfiguration = {
193-
Level: stateMachineObj.loggingConfig.level,
194-
IncludeExecutionData: stateMachineObj.loggingConfig.includeExecutionData,
193+
Level: value.loggingConfig.level,
194+
IncludeExecutionData: value.loggingConfig.includeExecutionData,
195195
Destinations,
196196
};
197197
}

lib/deploy/stepFunctions/compileStateMachines.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,4 +1175,57 @@ describe('#compileStateMachines', () => {
11751175
expect(definitionString).to.contain('${AWS::AccountId}');
11761176
expect(definitionString).to.not.contain('#{AWS::AccountId}');
11771177
});
1178+
1179+
it('should compile logging configuration for Express Workflows', () => {
1180+
serverless.service.stepFunctions = {
1181+
stateMachines: {
1182+
myStateMachine1: {
1183+
name: 'stateMachineBeta1',
1184+
type: 'EXPRESS',
1185+
loggingConfig: {
1186+
destinations: [
1187+
{
1188+
'Fn::GetAtt': ['MyLogGroup', 'Arn'],
1189+
},
1190+
],
1191+
},
1192+
definition: {
1193+
StartAt: 'A',
1194+
States: {
1195+
A: {
1196+
Type: 'Task',
1197+
Resource: 'arn:aws:lambda:#{AWS::Region}:#{AWS::AccountId}:function:hello',
1198+
End: true,
1199+
},
1200+
},
1201+
},
1202+
},
1203+
},
1204+
};
1205+
1206+
serverlessStepFunctions.compileStateMachines();
1207+
const actual = serverlessStepFunctions
1208+
.serverless
1209+
.service
1210+
.provider
1211+
.compiledCloudFormationTemplate
1212+
.Resources
1213+
.StateMachineBeta1
1214+
.Properties;
1215+
1216+
expect(actual.StateMachineType).to.equal('EXPRESS');
1217+
expect(actual).to.haveOwnProperty('LoggingConfiguration');
1218+
expect(actual.LoggingConfiguration.Level).to.equal('OFF'); // default value
1219+
expect(actual.LoggingConfiguration.IncludeExecutionData).to.equal(false); // default value
1220+
expect(actual.LoggingConfiguration.Destinations).to.have.length(1);
1221+
1222+
const loggingDestination = actual.LoggingConfiguration.Destinations[0];
1223+
expect(loggingDestination).to.deep.equal({
1224+
CloudWatchLogsLogGroup: {
1225+
LogGroupArn: {
1226+
'Fn::GetAtt': ['MyLogGroup', 'Arn'],
1227+
},
1228+
},
1229+
});
1230+
});
11781231
});

0 commit comments

Comments
 (0)