forked from awslabs/aws-solutions-constructs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevents-rule-step-function.test.ts
195 lines (163 loc) · 5.5 KB
/
events-rule-step-function.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
import * as events from '@aws-cdk/aws-events';
import { EventsRuleToStepFunction, EventsRuleToStepFunctionProps } from '../lib/index';
import { Duration } from '@aws-cdk/core';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import '@aws-cdk/assert/jest';
import * as cdk from '@aws-cdk/core';
function deployNewStateMachine(stack: cdk.Stack) {
const startState = new sfn.Pass(stack, 'StartState');
const props: EventsRuleToStepFunctionProps = {
stateMachineProps: {
definition: startState
},
eventRuleProps: {
schedule: events.Schedule.rate(Duration.minutes(5))
}
};
return new EventsRuleToStepFunction(stack, 'test-events-rule-step-function', props);
}
function deployNewStateMachineAndEventBus(stack: cdk.Stack) {
const startState = new sfn.Pass(stack, 'StartState');
const props: EventsRuleToStepFunctionProps = {
stateMachineProps: {
definition: startState
},
eventRuleProps: {
eventPattern: {
source: ['solutionsconstructs']
}
},
eventBusProps: {}
};
return new EventsRuleToStepFunction(stack, 'test-eventrules-stepfunctions-eventbus', props);
}
test('check events rule role policy permissions', () => {
const stack = new cdk.Stack();
deployNewStateMachine(stack);
expect(stack).toHaveResource("AWS::IAM::Policy", {
PolicyDocument: {
Statement: [
{
Action: "states:StartExecution",
Effect: "Allow",
Resource: {
Ref: "testeventsrulestepfunctiontesteventsrulestepfunctionWStateMachine64FD5A64"
}
}
],
Version: "2012-10-17"
}
});
});
test('check events rule properties', () => {
const stack = new cdk.Stack();
deployNewStateMachine(stack);
expect(stack).toHaveResource('AWS::Events::Rule', {
ScheduleExpression: "rate(5 minutes)",
State: "ENABLED",
Targets: [
{
Arn: {
Ref: "testeventsrulestepfunctiontesteventsrulestepfunctionWStateMachine64FD5A64"
},
Id: "Target0",
RoleArn: {
"Fn::GetAtt": [
"testeventsrulestepfunctiontesteventsrulestepfunctionWEventsRuleRole992B57E4",
"Arn"
]
}
}
]
});
});
test('check properties', () => {
const stack = new cdk.Stack();
const construct: EventsRuleToStepFunction = deployNewStateMachine(stack);
expect(construct.cloudwatchAlarms !== null);
expect(construct.stateMachine !== null);
expect(construct.eventsRule !== null);
expect(construct.stateMachineLogGroup !== null);
});
test('check properties with no CW Alarms', () => {
const stack = new cdk.Stack();
const startState = new sfn.Pass(stack, 'StartState');
const props: EventsRuleToStepFunctionProps = {
stateMachineProps: {
definition: startState
},
eventRuleProps: {
schedule: events.Schedule.rate(Duration.minutes(5))
},
createCloudWatchAlarms: false
};
const construct: EventsRuleToStepFunction = new EventsRuleToStepFunction(stack, 'test-events-rule-step-function', props);
expect(construct.cloudwatchAlarms === null);
expect(construct.stateMachine !== null);
expect(construct.eventsRule !== null);
expect(construct.stateMachineLogGroup !== null);
});
test('check eventbus property, snapshot & eventbus exists', () => {
const stack = new cdk.Stack();
const construct: EventsRuleToStepFunction = deployNewStateMachineAndEventBus(stack);
expect(construct.cloudwatchAlarms !== null);
expect(construct.stateMachine !== null);
expect(construct.eventsRule !== null);
expect(construct.stateMachineLogGroup !== null);
expect(construct.eventBus !== null);
// Check whether eventbus exists
expect(stack).toHaveResource('AWS::Events::EventBus');
});
test('check exception while passing existingEventBus & eventBusProps', () => {
const stack = new cdk.Stack();
const startState = new sfn.Pass(stack, 'StartState');
const props: EventsRuleToStepFunctionProps = {
stateMachineProps: {
definition: startState
},
eventRuleProps: {
eventPattern: {
source: ['solutionsconstructs']
}
},
eventBusProps: {},
existingEventBusInterface: new events.EventBus(stack, `test-existing-new-eventbus`, {})
};
const app = () => {
new EventsRuleToStepFunction(stack, 'test-eventsrule-stepfunctions', props);
};
expect(app).toThrowError();
});
test('check custom event bus resource with props when deploy:true', () => {
const stack = new cdk.Stack();
const startState = new sfn.Pass(stack, 'StartState');
const props: EventsRuleToStepFunctionProps = {
stateMachineProps: {
definition: startState
},
eventBusProps: {
eventBusName: 'testcustomeventbus'
},
eventRuleProps: {
eventPattern: {
source: ['solutionsconstructs']
}
}
};
new EventsRuleToStepFunction(stack, 'test-new-eventsrule-stepfunctions', props);
expect(stack).toHaveResource('AWS::Events::EventBus', {
Name: 'testcustomeventbus'
});
});