1
- import { expect as cdkExpect , haveResource , ResourcePart } from '@aws-cdk/assert' ;
1
+ import { ABSENT , expect as cdkExpect , haveResource , ResourcePart } from '@aws-cdk/assert' ;
2
2
import '@aws-cdk/assert/jest' ;
3
3
import * as ec2 from '@aws-cdk/aws-ec2' ;
4
4
import * as kms from '@aws-cdk/aws-kms' ;
5
5
import * as s3 from '@aws-cdk/aws-s3' ;
6
6
import * as cdk from '@aws-cdk/core' ;
7
+ import { Cluster , ClusterParameterGroup , ClusterType } from '../lib' ;
7
8
8
- import { Cluster , ClusterParameterGroup , ClusterType , NodeType } from '../lib' ;
9
+ let stack : cdk . Stack ;
10
+ let vpc : ec2 . IVpc ;
9
11
10
- test ( 'check that instantiation works' , ( ) => {
11
- // GIVEN
12
- const stack = testStack ( ) ;
13
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
12
+ beforeEach ( ( ) => {
13
+ stack = testStack ( ) ;
14
+ vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
15
+ } ) ;
14
16
17
+ test ( 'check that instantiation works' , ( ) => {
15
18
// WHEN
16
19
new Cluster ( stack , 'Redshift' , {
17
20
masterUser : {
@@ -57,8 +60,7 @@ test('check that instantiation works', () => {
57
60
58
61
test ( 'can create a cluster with imported vpc and security group' , ( ) => {
59
62
// GIVEN
60
- const stack = testStack ( ) ;
61
- const vpc = ec2 . Vpc . fromLookup ( stack , 'VPC' , {
63
+ vpc = ec2 . Vpc . fromLookup ( stack , 'ImportedVPC' , {
62
64
vpcId : 'VPC12345' ,
63
65
} ) ;
64
66
const sg = ec2 . SecurityGroup . fromSecurityGroupId ( stack , 'SG' , 'SecurityGroupId12345' ) ;
@@ -83,10 +85,6 @@ test('can create a cluster with imported vpc and security group', () => {
83
85
} ) ;
84
86
85
87
test ( 'creates a secret when master credentials are not specified' , ( ) => {
86
- // GIVEN
87
- const stack = testStack ( ) ;
88
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
89
-
90
88
// WHEN
91
89
new Cluster ( stack , 'Redshift' , {
92
90
masterUser : {
@@ -133,34 +131,88 @@ test('creates a secret when master credentials are not specified', () => {
133
131
} ) ) ;
134
132
} ) ;
135
133
136
- test ( 'SIngle Node CLusters spawn only single node' , ( ) => {
137
- // GIVEN
138
- const stack = testStack ( ) ;
139
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
134
+ describe ( 'node count' , ( ) => {
135
+
136
+ test ( 'Single Node Clusters do not define node count' , ( ) => {
137
+ // WHEN
138
+ new Cluster ( stack , 'Redshift' , {
139
+ masterUser : {
140
+ masterUsername : 'admin' ,
141
+ } ,
142
+ vpc,
143
+ clusterType : ClusterType . SINGLE_NODE ,
144
+ } ) ;
145
+
146
+ // THEN
147
+ cdkExpect ( stack ) . to ( haveResource ( 'AWS::Redshift::Cluster' , {
148
+ ClusterType : 'single-node' ,
149
+ NumberOfNodes : ABSENT ,
150
+ } ) ) ;
151
+ } ) ;
140
152
141
- // WHEN
142
- new Cluster ( stack , 'Redshift' , {
143
- masterUser : {
144
- masterUsername : 'admin' ,
145
- } ,
146
- vpc,
147
- nodeType : NodeType . DC1_8XLARGE ,
148
- clusterType : ClusterType . SINGLE_NODE ,
153
+ test ( 'Single Node Clusters treat 1 node as undefined' , ( ) => {
154
+ // WHEN
155
+ new Cluster ( stack , 'Redshift' , {
156
+ masterUser : {
157
+ masterUsername : 'admin' ,
158
+ } ,
159
+ vpc,
160
+ clusterType : ClusterType . SINGLE_NODE ,
161
+ numberOfNodes : 1 ,
162
+ } ) ;
163
+
164
+ // THEN
165
+ cdkExpect ( stack ) . to ( haveResource ( 'AWS::Redshift::Cluster' , {
166
+ ClusterType : 'single-node' ,
167
+ NumberOfNodes : ABSENT ,
168
+ } ) ) ;
149
169
} ) ;
150
170
151
- // THEN
152
- cdkExpect ( stack ) . to ( haveResource ( 'AWS::Redshift::Cluster' , {
153
- ClusterType : 'single-node' ,
154
- NodeType : 'dc1.8xlarge' ,
155
- NumberOfNodes : 1 ,
156
- } ) ) ;
171
+ test ( 'Single Node Clusters throw if any other node count is specified' , ( ) => {
172
+ expect ( ( ) => {
173
+ new Cluster ( stack , 'Redshift' , {
174
+ masterUser : {
175
+ masterUsername : 'admin' ,
176
+ } ,
177
+ vpc,
178
+ clusterType : ClusterType . SINGLE_NODE ,
179
+ numberOfNodes : 2 ,
180
+ } ) ;
181
+ } ) . toThrow ( / N u m b e r o f n o d e s m u s t b e n o t b e s u p p l i e d o r b e 1 f o r c l u s t e r t y p e s i n g l e - n o d e / ) ;
182
+ } ) ;
183
+
184
+ test ( 'Multi-Node Clusters default to 2 nodes' , ( ) => {
185
+ // WHEN
186
+ new Cluster ( stack , 'Redshift' , {
187
+ masterUser : {
188
+ masterUsername : 'admin' ,
189
+ } ,
190
+ vpc,
191
+ clusterType : ClusterType . MULTI_NODE ,
192
+ } ) ;
193
+
194
+ // THEN
195
+ cdkExpect ( stack ) . to ( haveResource ( 'AWS::Redshift::Cluster' , {
196
+ ClusterType : 'multi-node' ,
197
+ NumberOfNodes : 2 ,
198
+ } ) ) ;
199
+ } ) ;
200
+
201
+ test . each ( [ 0 , 1 , - 1 , 101 ] ) ( 'Multi-Node Clusters throw with %s nodes' , ( numberOfNodes : number ) => {
202
+ expect ( ( ) => {
203
+ new Cluster ( stack , 'Redshift' , {
204
+ masterUser : {
205
+ masterUsername : 'admin' ,
206
+ } ,
207
+ vpc,
208
+ clusterType : ClusterType . MULTI_NODE ,
209
+ numberOfNodes,
210
+ } ) ;
211
+ } ) . toThrow ( / N u m b e r o f n o d e s f o r c l u s t e r t y p e m u l t i - n o d e m u s t b e a t l e a s t 2 a n d n o m o r e t h a n 1 0 0 / ) ;
212
+ } ) ;
157
213
} ) ;
158
214
159
215
test ( 'create an encrypted cluster with custom KMS key' , ( ) => {
160
- // GIVEN
161
- const stack = testStack ( ) ;
162
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
163
-
164
216
// WHEN
165
217
new Cluster ( stack , 'Redshift' , {
166
218
masterUser : {
@@ -182,10 +234,6 @@ test('create an encrypted cluster with custom KMS key', () => {
182
234
} ) ;
183
235
184
236
test ( 'cluster with parameter group' , ( ) => {
185
- // GIVEN
186
- const stack = testStack ( ) ;
187
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
188
-
189
237
// WHEN
190
238
const group = new ClusterParameterGroup ( stack , 'Params' , {
191
239
description : 'bye' ,
@@ -211,8 +259,6 @@ test('cluster with parameter group', () => {
211
259
212
260
test ( 'imported cluster with imported security group honors allowAllOutbound' , ( ) => {
213
261
// GIVEN
214
- const stack = testStack ( ) ;
215
-
216
262
const cluster = Cluster . fromClusterAttributes ( stack , 'Database' , {
217
263
clusterEndpointAddress : 'addr' ,
218
264
clusterName : 'identifier' ,
@@ -235,8 +281,6 @@ test('imported cluster with imported security group honors allowAllOutbound', ()
235
281
236
282
test ( 'can create a cluster with logging enabled' , ( ) => {
237
283
// GIVEN
238
- const stack = testStack ( ) ;
239
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
240
284
const bucket = s3 . Bucket . fromBucketName ( stack , 'bucket' , 'logging-bucket' ) ;
241
285
242
286
// WHEN
@@ -259,10 +303,6 @@ test('can create a cluster with logging enabled', () => {
259
303
} ) ;
260
304
261
305
test ( 'throws when trying to add rotation to a cluster without secret' , ( ) => {
262
- // GIVEN
263
- const stack = new cdk . Stack ( ) ;
264
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
265
-
266
306
// WHEN
267
307
const cluster = new Cluster ( stack , 'Redshift' , {
268
308
masterUser : {
@@ -281,8 +321,6 @@ test('throws when trying to add rotation to a cluster without secret', () => {
281
321
282
322
test ( 'throws validation error when trying to set encryptionKey without enabling encryption' , ( ) => {
283
323
// GIVEN
284
- const stack = new cdk . Stack ( ) ;
285
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
286
324
const key = new kms . Key ( stack , 'kms-key' ) ;
287
325
288
326
// WHEN
@@ -304,8 +342,6 @@ test('throws validation error when trying to set encryptionKey without enabling
304
342
305
343
test ( 'throws when trying to add single user rotation multiple times' , ( ) => {
306
344
// GIVEN
307
- const stack = new cdk . Stack ( ) ;
308
- const vpc = new ec2 . Vpc ( stack , 'VPC' ) ;
309
345
const cluster = new Cluster ( stack , 'Redshift' , {
310
346
masterUser : {
311
347
masterUsername : 'admin' ,
@@ -323,7 +359,7 @@ test('throws when trying to add single user rotation multiple times', () => {
323
359
} ) ;
324
360
325
361
function testStack ( ) {
326
- const stack = new cdk . Stack ( undefined , undefined , { env : { account : '12345' , region : 'us-test-1' } } ) ;
327
- stack . node . setContext ( 'availability-zones:12345:us-test-1' , [ 'us-test-1a' , 'us-test-1b' ] ) ;
328
- return stack ;
329
- }
362
+ const newTestStack = new cdk . Stack ( undefined , undefined , { env : { account : '12345' , region : 'us-test-1' } } ) ;
363
+ newTestStack . node . setContext ( 'availability-zones:12345:us-test-1' , [ 'us-test-1a' , 'us-test-1b' ] ) ;
364
+ return newTestStack ;
365
+ }
0 commit comments