2
2
3
3
const CreateDynamoDBGlobalTables = require ( '../src/index.js' ) ;
4
4
const AWSMock = require ( 'aws-sdk-mock' ) ;
5
- require ( 'chai' ) . should ( ) ;
5
+ var chai = require ( 'chai' ) ;
6
+ var sinon = require ( 'sinon' ) ;
7
+ var sinonChai = require ( 'sinon-chai' ) ;
8
+ var chaiAsPromised = require ( 'chai-as-promised' ) ;
9
+ chai . should ( ) ;
10
+ chai . use ( sinonChai ) ;
11
+ chai . use ( chaiAsPromised ) ;
6
12
7
13
describe ( 'Creating a global dynamodb tables' , ( ) => {
8
- let serverless , options , plugin ;
14
+ let serverless , plugin ;
15
+ let createGlobalTables = sinon . stub ( ) . resolves ( ) ;
16
+ let addReplicas = sinon . stub ( ) . resolves ( ) ;
9
17
10
- describe . only ( 'when the stack does not contain any dynamodb tables' , ( ) => {
18
+ before ( async ( ) => {
19
+ AWSMock . mock ( 'DynamoDB' , 'createGlobalTable' , createGlobalTables ) ;
20
+ AWSMock . mock ( 'DynamoDB' , 'updateGlobalTable' , addReplicas ) ;
21
+ } ) ;
22
+
23
+ describe ( 'when the stack does not contain any dynamodb tables' , ( ) => {
11
24
12
- const createdGlobalTables = [ ] ;
13
- const addedReplicas = [ ] ;
14
25
before ( async ( ) => {
15
26
serverless = given_a_serverless_stack_without_any_tables ( ) ;
16
- AWSMock . mock ( 'DynamoDB' , 'createGlobalTable' , params => createdGlobalTables . push ( params ) ) ;
17
- AWSMock . mock ( 'DynamoDB' , 'updateGlobalTable' , params => addedReplicas . push ( params ) ) ;
18
27
plugin = new CreateDynamoDBGlobalTables ( serverless ) ;
19
28
await plugin . createGlobalTables ( ) ;
20
29
} ) ;
21
30
31
+ after ( ( ) => {
32
+ createGlobalTables . reset ( ) ;
33
+ addReplicas . reset ( ) ;
34
+ } ) ;
35
+
22
36
it ( 'does not create any global tables' , ( ) => {
23
- createdGlobalTables . should . be . empty ;
37
+ createGlobalTables . should . not . have . been . called ;
24
38
} ) ;
25
39
26
40
it ( 'does not add any replicas' , ( ) => {
27
- addedReplicas . should . be . empty ;
41
+ addReplicas . should . not . have . been . called ;
28
42
} ) ;
29
43
} ) ;
30
44
31
- describe ( 'when the stack contains some dynamodb tables' , ( ) => {
45
+ describe ( 'when the stack contains dynamodb tables that have not yet been deployed ' , ( ) => {
32
46
33
- before ( ( ) => {
47
+ before ( async ( ) => {
48
+ let createGlobalTables = sinon . stub ( ) . resolves ( ) ;
49
+ let addReplicas = sinon . stub ( ) . resolves ( ) ;
50
+ AWSMock . mock ( 'DynamoDB' , 'createGlobalTable' , createGlobalTables ) ;
51
+ AWSMock . mock ( 'DynamoDB' , 'updateGlobalTable' , addReplicas ) ;
34
52
serverless = given_a_serverless_stack_with_some_tables ( ) ;
35
-
36
53
plugin = new CreateDynamoDBGlobalTables ( serverless ) ;
54
+ await plugin . createGlobalTables ( ) ;
37
55
} ) ;
38
56
39
- it ( 'enables them in the plugin' , ( ) => {
57
+ after ( ( ) => {
58
+ createGlobalTables . reset ( ) ;
59
+ addReplicas . reset ( ) ;
60
+ } ) ;
40
61
62
+ it ( 'creates a global table for all table resource' , ( ) => {
63
+ createGlobalTables . should . have . been . calledTwice ;
64
+ } ) ;
65
+
66
+ it ( 'the first global table is created with a replication region' , ( ) => {
67
+ createGlobalTables . should . have . been . calledWith ( {
68
+ GlobalTableName : 'table-one' ,
69
+ ReplicationGroup : [ { RegionName : 'eu-west-2' } ]
70
+ } ) ;
71
+ } ) ;
72
+
73
+ it ( 'the second global table is created with a replication region' , ( ) => {
74
+ createGlobalTables . should . have . been . calledWith ( {
75
+ GlobalTableName : 'table-two' ,
76
+ ReplicationGroup : [ { RegionName : 'eu-west-2' } ]
77
+ } ) ;
41
78
} ) ;
42
79
} ) ;
43
80
44
81
describe ( 'when the dynamodb global table already exists' , ( ) => {
45
82
46
- before ( ( ) => {
83
+ before ( async ( ) => {
47
84
serverless = given_a_serverless_stack_with_some_tables ( ) ;
48
- serverless . service . custom . apiGatewayCloudWatchSettings = { metricsEnabled : false } ;
49
-
85
+ createGlobalTables . rejects ( { code : 'GlobalTableAlreadyExistsException' } ) ;
50
86
plugin = new CreateDynamoDBGlobalTables ( serverless ) ;
87
+ await plugin . createGlobalTables ( ) ;
88
+ } ) ;
89
+
90
+ after ( ( ) => {
91
+ createGlobalTables . reset ( ) ;
92
+ addReplicas . reset ( ) ;
93
+ } ) ;
94
+
95
+ it ( 'adds a replica table for all table resource' , ( ) => {
96
+ addReplicas . should . have . been . calledTwice ;
97
+ } ) ;
98
+
99
+ it ( 'adds the first table replica to the existing global table' , ( ) => {
100
+ addReplicas . should . have . been . calledWith ( {
101
+ GlobalTableName : 'table-one' ,
102
+ ReplicaUpdates : [
103
+ { Create : { RegionName : 'eu-west-2' } }
104
+ ]
105
+ } ) ;
51
106
} ) ;
52
107
53
- it ( 'disables them in the plugin' , ( ) => {
54
- expect ( plugin . metricsEnabled ) . to . be . false ;
108
+ it ( 'adds the second table replica to the existing global table' , ( ) => {
109
+ addReplicas . should . have . been . calledWith ( {
110
+ GlobalTableName : 'table-one' ,
111
+ ReplicaUpdates : [
112
+ { Create : { RegionName : 'eu-west-2' } }
113
+ ]
114
+ } ) ;
55
115
} ) ;
56
116
} ) ;
57
117
58
118
describe ( 'when the replication group already exists' , ( ) => {
59
119
60
- before ( ( ) => {
120
+ before ( async ( ) => {
121
+ serverless = given_a_serverless_stack_with_some_tables ( ) ;
122
+ addReplicas . rejects ( { code : 'ReplicaAlreadyExistsException' } ) ;
123
+ plugin = new CreateDynamoDBGlobalTables ( serverless ) ;
124
+ await plugin . createGlobalTables ( ) ;
125
+ } ) ;
126
+
127
+ after ( ( ) => {
128
+ createGlobalTables . reset ( ) ;
129
+ addReplicas . reset ( ) ;
130
+ } ) ;
131
+
132
+ it ( 'attempts to add a replica table for all table resource' , ( ) => {
133
+ addReplicas . should . have . been . calledTwice ;
134
+ } ) ;
135
+ } ) ;
61
136
137
+ describe ( 'when an exception occurs creating the global table' , ( ) => {
138
+
139
+ before ( async ( ) => {
62
140
serverless = given_a_serverless_stack_with_some_tables ( ) ;
63
- serverless . service . custom . apiGatewayCloudWatchSettings = { metricsEnabled : true } ;
64
- options = { stage : 'test' , region : 'us-east-1' } ;
141
+ createGlobalTables . rejects ( { code : 'UnhandledException' } ) ;
142
+ plugin = new CreateDynamoDBGlobalTables ( serverless ) ;
143
+ } ) ;
65
144
66
- plugin = new CreateDynamoDBGlobalTables ( serverless , options ) ;
145
+ after ( ( ) => {
146
+ createGlobalTables . reset ( ) ;
147
+ addReplicas . reset ( ) ;
67
148
} ) ;
68
149
69
- it ( 'uses the stage option ' , ( ) => {
70
- expect ( plugin . stage ) . to . equal ( options . stage ) ;
150
+ it ( 'throws an exception ' , ( ) => {
151
+ plugin . createGlobalTables ( ) . should . be . rejected ;
71
152
} ) ;
153
+ } ) ;
72
154
73
- it ( 'not use the provider stage' , ( ) => {
74
- expect ( plugin . stage ) . to . not . equal ( serverless . service . provider . stage ) ;
155
+ describe ( 'when an exception occurs adding a replica table' , ( ) => {
156
+
157
+ before ( async ( ) => {
158
+ serverless = given_a_serverless_stack_with_some_tables ( ) ;
159
+ addReplicas . rejects ( { code : 'UnhandledException' } ) ;
160
+ plugin = new CreateDynamoDBGlobalTables ( serverless ) ;
75
161
} ) ;
76
162
77
- it ( 'uses the region option' , ( ) => {
78
- expect ( plugin . region ) . to . equal ( options . region ) ;
163
+ after ( ( ) => {
164
+ createGlobalTables . reset ( ) ;
165
+ addReplicas . reset ( ) ;
79
166
} ) ;
80
167
81
- it ( 'not use the provider region ' , ( ) => {
82
- expect ( plugin . region ) . to . not . equal ( serverless . service . provider . region ) ;
168
+ it ( 'throws an exception ' , ( ) => {
169
+ plugin . createGlobalTables ( ) . should . be . rejected ;
83
170
} ) ;
84
171
} ) ;
85
172
} ) ;
86
173
174
+
87
175
const given_a_serverless_stack_without_any_tables = ( ) => ( {
88
176
getProvider : ( ) => ( { getRegion : ( ) => 'eu-west-2' } ) ,
177
+ cli : { consoleLog : ( ) => { } } ,
89
178
service : {
90
179
resources : {
91
180
Resources : {
@@ -97,16 +186,21 @@ const given_a_serverless_stack_without_any_tables = () => ({
97
186
98
187
const given_a_serverless_stack_with_some_tables = ( ) => ( {
99
188
getProvider : ( ) => ( { getRegion : ( ) => 'eu-west-2' } ) ,
189
+ cli : { consoleLog : ( ) => { } } ,
100
190
service : {
101
191
resources : {
102
192
Resources : {
103
193
'table-one' : {
104
- TableName : 'table-one' ,
105
- Type : 'AWS::DynamoDB::Table'
194
+ Type : 'AWS::DynamoDB::Table' ,
195
+ Properties : {
196
+ TableName : 'table-one'
197
+ }
106
198
} ,
107
199
'table-two' : {
108
- TableName : 'table-two' ,
109
- Type : 'AWS::DynamoDB::Table'
200
+ Type : 'AWS::DynamoDB::Table' ,
201
+ Properties : {
202
+ TableName : 'table-two'
203
+ }
110
204
}
111
205
}
112
206
}
0 commit comments