Skip to content

Commit df63d42

Browse files
jappurohit041KillDozerX2
authored andcommitted
test(EventBridge Scheduler): ✅ test cases added and restructured the error message
1 parent ca4a4f7 commit df63d42

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

lib/deploy/events/schedule/compileScheduledEvents.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
const _ = require('lodash');
44
const BbPromise = require('bluebird');
55

6+
const METHOD_SCHEDULER = 'scheduler';
7+
const METHOD_EVENT_BUS = 'eventBus';
68
module.exports = {
79
compileScheduledEvents() {
810
const service = this.serverless.service;
911
const permissionsBoundary = service.provider.rolePermissionsBoundary;
1012
_.forEach(this.getAllStateMachines(), (stateMachineName) => {
1113
const stateMachineObj = this.getStateMachine(stateMachineName);
1214
let scheduleNumberInFunction = 0;
13-
const METHOD_SCHEDULER = 'scheduler';
14-
const METHOD_EVENT_BUS = 'eventBus';
1515

1616
if (stateMachineObj.events) {
1717
_.forEach(stateMachineObj.events, (event) => {
@@ -86,26 +86,21 @@ module.exports = {
8686
if (method === METHOD_SCHEDULER) {
8787
const errorMessage = [
8888
'Cannot setup "schedule" event: "inputTransformer" is not supported with "scheduler" mode',
89-
'SCHEDULE_PARAMETER_NOT_SUPPORTED',
9089
].join('');
9190
throw new this.serverless.classes
9291
.Error(errorMessage);
93-
} else {
94-
InputTransformer = this.formatInputTransformer(InputTransformer);
9592
}
9693
}
9794
if (InputPath && method === METHOD_SCHEDULER) {
9895
const errorMessage = [
9996
'Cannot setup "schedule" event: "inputPath" is not supported with "scheduler" mode',
100-
'SCHEDULE_PARAMETER_NOT_SUPPORTED',
10197
].join('');
10298
throw new this.serverless.classes
10399
.Error(errorMessage);
104100
}
105101
if (timezone && method !== METHOD_SCHEDULER) {
106102
const errorMessage = [
107103
'Cannot setup "schedule" event: "timezone" is only supported with "scheduler" mode',
108-
'SCHEDULE_PARAMETER_NOT_SUPPORTED',
109104
].join('');
110105
throw new this.serverless.classes
111106
.Error(errorMessage);
@@ -146,7 +141,8 @@ module.exports = {
146141

147142
let scheduleTemplate;
148143
let iamRoleTemplate;
149-
144+
// If condition for the event bridge schedular and it define
145+
// resource template and iamrole for the same
150146
if (method === METHOD_SCHEDULER) {
151147
scheduleTemplate = `{
152148
"Type": "AWS::Scheduler::Schedule",
@@ -203,6 +199,8 @@ module.exports = {
203199
}
204200
}`;
205201
} else {
202+
// else condition for the event rule and
203+
// it define resource template and iamrole for the same
206204
scheduleTemplate = `{
207205
"Type": "AWS::Events::Rule",
208206
"Properties": {

lib/deploy/events/schedule/compileScheduledEvents.test.js

+83
Original file line numberDiff line numberDiff line change
@@ -447,4 +447,87 @@ describe('#httpValidate()', () => {
447447
.FirstScheduleToStepFunctionsRole
448448
.Properties.PermissionsBoundary).to.equal('arn:aws:iam::myAccount:policy/permission_boundary');
449449
});
450+
451+
it('should have type of AWS::Scheduler::Schedule if method is scheduler', () => {
452+
serverlessStepFunctions.serverless.service.stepFunctions = {
453+
stateMachines: {
454+
first: {
455+
events: [
456+
{
457+
schedule: {
458+
method: 'scheduler',
459+
rate: 'rate(10 minutes)',
460+
enabled: false,
461+
timezone: 'Asia/Mumbai',
462+
},
463+
},
464+
],
465+
},
466+
},
467+
};
468+
serverlessStepFunctions.compileScheduledEvents();
469+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate.Resources.FirstStepFunctionsEventsRuleSchedule1.Type).to.equal('AWS::Scheduler::Schedule');
470+
});
471+
472+
it('should have service as scheduler.amazonaws.com if method is scheduler', () => {
473+
serverlessStepFunctions.serverless.service.stepFunctions = {
474+
stateMachines: {
475+
first: {
476+
events: [
477+
{
478+
schedule: {
479+
method: 'scheduler',
480+
rate: 'rate(10 minutes)',
481+
enabled: false,
482+
timezone: 'Asia/Mumbai',
483+
},
484+
},
485+
],
486+
},
487+
},
488+
};
489+
serverlessStepFunctions.compileScheduledEvents();
490+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate.Resources.FirstScheduleToStepFunctionsRole.Properties.AssumeRolePolicyDocument.Statement[0].Principal.Service).to.equal('scheduler.amazonaws.com');
491+
});
492+
493+
it('should define timezone when schedular and timezone given', () => {
494+
serverlessStepFunctions.serverless.service.stepFunctions = {
495+
stateMachines: {
496+
first: {
497+
events: [
498+
{
499+
schedule: {
500+
method: 'scheduler',
501+
rate: 'rate(10 minutes)',
502+
enabled: false,
503+
timezone: 'Asia/Mumbai',
504+
},
505+
},
506+
],
507+
},
508+
},
509+
};
510+
serverlessStepFunctions.compileScheduledEvents();
511+
512+
expect(serverlessStepFunctions.serverless.service.provider.compiledCloudFormationTemplate.Resources.FirstStepFunctionsEventsRuleSchedule1.Properties.ScheduleExpressionTimezone).to.equal('Asia/Mumbai');
513+
});
514+
515+
it('should accept timezone only if method is scheduler', () => {
516+
serverlessStepFunctions.serverless.service.stepFunctions = {
517+
stateMachines: {
518+
first: {
519+
events: [
520+
{
521+
schedule: {
522+
rate: 'rate(10 minutes)',
523+
enabled: false,
524+
timezone: 'Asia/Mumbai',
525+
},
526+
},
527+
],
528+
},
529+
},
530+
};
531+
expect(() => serverlessStepFunctions.compileScheduledEvents()).to.throw(Error);
532+
});
450533
});

0 commit comments

Comments
 (0)