forked from aws/aws-cdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.kinesis.ts
189 lines (164 loc) · 5.02 KB
/
test.kinesis.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
import { expect, haveResource } from '@aws-cdk/assert';
import * as kinesis from '@aws-cdk/aws-kinesis';
import * as lambda from '@aws-cdk/aws-lambda';
import * as cdk from '@aws-cdk/core';
import { Test } from 'nodeunit';
import * as sources from '../lib';
import { TestFunction } from './test-function';
// tslint:disable:object-literal-key-quotes
export = {
'sufficiently complex example'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');
// WHEN
fn.addEventSource(new sources.KinesisEventSource(stream, {
startingPosition: lambda.StartingPosition.TRIM_HORIZON
}));
// THEN
expect(stack).to(haveResource('AWS::IAM::Policy', {
'PolicyDocument': {
'Statement': [
{
'Action': [
'kinesis:DescribeStream',
'kinesis:DescribeStreamSummary',
'kinesis:GetRecords',
'kinesis:GetShardIterator',
'kinesis:ListShards',
'kinesis:SubscribeToShard'
],
'Effect': 'Allow',
'Resource': {
'Fn::GetAtt': [
'S509448A1',
'Arn'
]
}
}
],
'Version': '2012-10-17'
},
'PolicyName': 'FnServiceRoleDefaultPolicyC6A839BF',
'Roles': [{
'Ref': 'FnServiceRoleB9001A96'
}]
}));
expect(stack).to(haveResource('AWS::Lambda::EventSourceMapping', {
'EventSourceArn': {
'Fn::GetAtt': [
'S509448A1',
'Arn'
]
},
'FunctionName': {
'Ref': 'Fn9270CBC0'
},
'BatchSize': 100,
'StartingPosition': 'TRIM_HORIZON'
}));
test.done();
},
'specific batch size'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');
// WHEN
fn.addEventSource(new sources.KinesisEventSource(stream, {
batchSize: 50,
startingPosition: lambda.StartingPosition.LATEST
}));
// THEN
expect(stack).to(haveResource('AWS::Lambda::EventSourceMapping', {
'EventSourceArn': {
'Fn::GetAtt': [
'S509448A1',
'Arn'
]
},
'FunctionName': {
'Ref': 'Fn9270CBC0'
},
'BatchSize': 50,
'StartingPosition': 'LATEST'
}));
test.done();
},
'fails if batch size < 1'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');
// WHEN
test.throws(() => fn.addEventSource(new sources.KinesisEventSource(stream, {
batchSize: 0,
startingPosition: lambda.StartingPosition.LATEST
})), /Maximum batch size must be between 1 and 10000 inclusive \(given 0\)/);
test.done();
},
'fails if batch size > 10000'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');
// WHEN
test.throws(() => fn.addEventSource(new sources.KinesisEventSource(stream, {
batchSize: 10001,
startingPosition: lambda.StartingPosition.LATEST
})), /Maximum batch size must be between 1 and 10000 inclusive \(given 10001\)/);
test.done();
},
'specific maxBatchingWindow'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');
// WHEN
fn.addEventSource(new sources.KinesisEventSource(stream, {
maxBatchingWindow: cdk.Duration.minutes(2),
startingPosition: lambda.StartingPosition.LATEST
}));
// THEN
expect(stack).to(haveResource('AWS::Lambda::EventSourceMapping', {
'EventSourceArn': {
'Fn::GetAtt': [
'S509448A1',
'Arn'
]
},
'FunctionName': {
'Ref': 'Fn9270CBC0'
},
'MaximumBatchingWindowInSeconds': 120,
'StartingPosition': 'LATEST'
}));
test.done();
},
'contains eventSourceMappingId after lambda binding'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const fn = new TestFunction(stack, 'Fn');
const stream = new kinesis.Stream(stack, 'S');
const eventSource = new sources.KinesisEventSource(stream, {
startingPosition: lambda.StartingPosition.TRIM_HORIZON
});
// WHEN
fn.addEventSource(eventSource);
// THEN
test.ok(eventSource.eventSourceMappingId);
test.done();
},
'eventSourceMappingId throws error before binding to lambda'(test: Test) {
// GIVEN
const stack = new cdk.Stack();
const stream = new kinesis.Stream(stack, 'S');
const eventSource = new sources.KinesisEventSource(stream, {
startingPosition: lambda.StartingPosition.TRIM_HORIZON
});
// WHEN/THEN
test.throws(() => eventSource.eventSourceMappingId, /KinesisEventSource is not yet bound to an event source mapping/);
test.done();
},
};