UNPKG

9.95 kBJavaScriptView Raw
1const build = require('../lib/build').build;
2// const cf = require('@mapbox/cloudfriend');
3const test = require('tape');
4
5test('[build] error scenarios', assert => {
6 // Test that it doesn't work for a bogus resource
7 assert.throws(() => {
8 build({
9 CustomResourceName: 'bogus',
10 LogicalName: 'BogusResource',
11 S3Bucket: 'code bucket',
12 S3Key: 'lambda/code',
13 Handler: 'my.handler',
14 Properties: { isBogus: true }
15 });
16 }, /bogus is not an available magical-cfn-resource/, 'not a valid CustomResourceName');
17
18 // Test that you need CustomFunctionName
19 assert.throws(() => {
20 build({
21 customresourcename: 'SpotFleet',
22 LogicalName: 'SpotFleetResource',
23 S3Key: 'lambda/code',
24 S3Bucket: 'code',
25 Handler: 'my.handler',
26 Properties: {
27 SpotFleetRequestConfigData: { },
28 Region: 'region'
29 }
30 });
31 }, /Missing CustomResourceName/, 'missing CustomResourceName');
32
33 // Test that you need LogicalName
34 assert.throws(() => {
35 build({
36 CustomResourceName: 'SpotFleet',
37 Handler: 'my.handler',
38 Properties: {
39 SpotFleetRequestConfigData: { },
40 Region: 'region'
41 }
42 });
43 }, /Missing LogicalName/, 'missing LogicalName');
44
45 // Test that you need S3Bucket
46 assert.throws(() => {
47 build({
48 CustomResourceName: 'SpotFleet',
49 LogicalName: 'SpotFleetResource',
50 S3Key: 'lambda/code',
51 Handler: 'my.handler',
52 Properties: {
53 SpotFleetRequestConfigData: { },
54 Region: 'region'
55 }
56 });
57 }, /Missing S3Bucket/, 'missing S3Bucket');
58
59 // Test that you need S3Key
60 assert.throws(() => {
61 build({
62 CustomResourceName: 'SpotFleet',
63 LogicalName: 'SpotFleetResource',
64 S3Bucket: 'code',
65 Handler: 'my.handler',
66 Properties: {
67 SpotFleetRequestConfigData: { },
68 Region: 'region'
69 }
70 });
71 }, /Missing S3Key/, 'missing S3Key');
72
73 // Test that you need Handler
74 assert.throws(() => {
75 build({
76 CustomResourceName: 'SpotFleet',
77 LogicalName: 'SpotFleetResource',
78 S3Key: 'lambda/code',
79 S3Bucket: 'code'
80 });
81 }, /Missing Handler/, 'missing Handler');
82
83 // Test that you need Properties
84 assert.throws(() => {
85 build({
86 CustomResourceName: 'SpotFleet',
87 LogicalName: 'SpotFleetResource',
88 S3Key: 'lambda/code',
89 S3Bucket: 'code',
90 Handler: 'my.handler'
91 });
92 }, /Missing Properties/, 'missing Properties');
93
94 // Test that you need Properties
95 assert.throws(() => {
96 build({
97 CustomResourceName: 'SpotFleet',
98 LogicalName: 'SpotFleetResource',
99 S3Key: 'lambda/code',
100 S3Bucket: 'code',
101 Handler: 'my.handler',
102 Properties: {
103 Region: 'region'
104 }
105 });
106 }, /Missing SpotFleetRequestConfigData/, 'Missing SpotFleetRequestConfigData');
107
108 // Test that you need SpotFleetRequestConfigData.IamInstanceProfile
109 assert.throws(() => {
110 build({
111 CustomResourceName: 'SpotFleet',
112 LogicalName: 'SpotFleetResource',
113 S3Key: 'lambda/code',
114 S3Bucket: 'code',
115 Handler: 'my.handler',
116 Properties: {
117 Region: 'region',
118 SpotFleetRequestConfigData: {}
119 }
120 });
121 }, /Missing LaunchSpecifications in SpotFleetRequestConfigData/, 'Missing LaunchSpecifications in SpotFleetRequestConfigData');
122
123 // Test that you need SpotFleetRequestConfigData.IamInstanceProfile
124 assert.throws(() => {
125 build({
126 CustomResourceName: 'SpotFleet',
127 LogicalName: 'SpotFleetResource',
128 S3Key: 'lambda/code',
129 S3Bucket: 'code',
130 Handler: 'my.handler',
131 Properties: {
132 Region: 'region',
133 SpotFleetRequestConfigData: {
134 LaunchSpecifications: []
135 }
136 }
137 });
138 }, /Missing LaunchSpecifications\[0\] in SpotFleetRequestConfigData/, 'Missing LaunchSpecifications in SpotFleetRequestConfigData');
139
140 // Test that you need Properties
141 assert.throws(() => {
142 build({
143 CustomResourceName: 'SpotFleet',
144 LogicalName: 'SpotFleetResource',
145 S3Key: 'lambda/code',
146 S3Bucket: 'code',
147 Handler: 'my.handler',
148 Properties: {
149 Region: 'region',
150 SpotFleetRequestConfigData: {
151 LaunchSpecifications: [{
152 IamInstanceProfile: 'cauliflower'
153 }]
154 }
155 }
156 });
157 }, /Missing IamFleetRole in SpotFleetRequestConfigData/, 'Missing IamFleetRole in SpotFleetRequestConfigData');
158
159 assert.end();
160});
161
162// Test that once you have everything, it returns the right resource with all of the right fields
163test('[build] success', assert => {
164 const template = build({
165 CustomResourceName: 'SpotFleet',
166 LogicalName: 'SpotFleetLogicalName',
167 S3Key: 'lambda/code',
168 S3Bucket: 'code',
169 Handler: 'my.handler',
170 Properties: {
171 SpotFleetRequestConfigData: {
172 LaunchSpecifications: [
173 {
174 IamInstanceProfile: {'Arn': 'cauliflower'}
175 },
176 {
177 IamInstanceProfile: {'Arn': 'cauliflower2'}
178 },
179 {
180 IamInstanceProfile: {'Arn': 'cauliflower3'}
181 },
182 {
183 YeaNoProfile: 'cauliflower5'
184 }
185 ],
186 IamFleetRole: 'parsnips'
187 },
188 Region: 'region'
189 }
190 });
191
192 assert.deepEquals(Object.keys(template.Resources), ['SpotFleetLogicalNameRole', 'SpotFleetLogicalNameFunction', 'SpotFleetLogicalName'], 'role, function, and custom resource use logical name');
193 assert.equals(template.Resources.SpotFleetLogicalNameRole.Type, 'AWS::IAM::Role', 'Type is AWS::IAM::Role');
194 assert.equals(template.Resources.SpotFleetLogicalNameFunction.Type, 'AWS::Lambda::Function', 'Type is AWS::Lambda::Function');
195
196 assert.equals(template.Resources.SpotFleetLogicalNameFunction.Properties.Code.S3Bucket, 'code', 'S3Bucket is params.S3Bucket');
197 assert.deepEquals(template.Resources.SpotFleetLogicalNameFunction.Properties.Code.S3Key, 'lambda/code', 'S3Key is params.S3Key');
198 assert.equals(template.Resources.SpotFleetLogicalNameFunction.Properties.Handler, 'my.handler', 'Handler is params.Handler');
199
200 // Special spotfleet logic
201 assert.equals(template.Resources.SpotFleetLogicalNameRole.Properties.Policies[0].PolicyDocument.Statement[2].Resource, 'parsnips');
202 assert.deepEqual(template.Resources.SpotFleetLogicalNameRole.Properties.Policies[0].PolicyDocument.Statement[3].Resource,'cauliflower');
203
204 assert.end();
205});
206
207test('[build] success on SnsSubscription', assert => {
208 const template = build({
209 CustomResourceName: 'SnsSubscription',
210 LogicalName: 'SnsSubscriptionLogicalName',
211 S3Key: 'lambda/code',
212 S3Bucket: 'code',
213 Handler: 'my.handler',
214 Properties: {
215 Protocol: 'email',
216 TopicArn: 'arn:aws:sns:us-east-1:special-topic',
217 Endpoint: 'someone@mapbox.com'
218 }
219 });
220
221 assert.deepEquals(Object.keys(template.Resources), ['SnsSubscriptionLogicalNameRole', 'SnsSubscriptionLogicalNameFunction', 'SnsSubscriptionLogicalName'], 'role, function, and custom resource use logical name');
222 assert.equals(template.Resources.SnsSubscriptionLogicalNameRole.Type, 'AWS::IAM::Role', 'Type is AWS::IAM::Role');
223 assert.equals(template.Resources.SnsSubscriptionLogicalNameFunction.Type, 'AWS::Lambda::Function', 'Type is AWS::Lambda::Function');
224
225 assert.equals(template.Resources.SnsSubscriptionLogicalNameFunction.Properties.Code.S3Bucket, 'code', 'S3Bucket is params.S3Bucket');
226 assert.deepEquals(template.Resources.SnsSubscriptionLogicalNameFunction.Properties.Code.S3Key, 'lambda/code', 'S3Key is params.S3Key');
227 assert.deepEqual(template.Resources.SnsSubscriptionLogicalName.Type,'Custom::SnsSubscription', 'Type equals Custom::SnSSubnscription');
228 assert.ok( typeof template.Resources.SnsSubscriptionLogicalName.Type === 'string', 'Resource Type name is a string');
229 assert.equals(template.Resources.SnsSubscriptionLogicalNameFunction.Properties.Handler, 'my.handler', 'Handler is params.Handler');
230
231 assert.end();
232});
233
234test('[build] success with Conditional', assert => {
235 const params = {
236 CustomResourceName: 'SpotFleet',
237 LogicalName: 'SpotFleetLogicalName',
238 S3Key: 'lambda/code',
239 S3Bucket: 'code',
240 Handler: 'my.handler',
241 Properties: {
242 SpotFleetRequestConfigData: {
243 LaunchSpecifications: [{
244 IamInstanceProfile: 'cauliflower'
245 }],
246 IamFleetRole: 'parsnips'
247 },
248 SpotFleetRegion: 'region'
249 },
250 Condition: 'Conditional'
251 };
252 const template = build(params);
253 assert.equals(template.Resources.SpotFleetLogicalNameRole.Condition, 'Conditional', 'Conditional in Role');
254 assert.equals(template.Resources.SpotFleetLogicalNameFunction.Condition, 'Conditional', 'Conditional in Function');
255 assert.equals(template.Resources.SpotFleetLogicalName.Condition, 'Conditional', 'Conditional in Custom Resource');
256 assert.deepEqual(template.Resources.SpotFleetLogicalName.Type,'Custom::'+params.CustomResourceName, 'Type equals Custom::params.CustomResourceName');
257 assert.ok( typeof template.Resources.SpotFleetLogicalName.Type === 'string', 'Resource Type name is a string');
258 assert.end();
259})
260
261test('[build] success with other resources for S3NotificationTopicConfig', assert => {
262 const params = {
263 CustomResourceName: 'S3NotificationTopicConfig',
264 LogicalName: 'S3TopicConfig',
265 S3Bucket: 'code',
266 S3Key: 'lambda/code',
267 Handler: 'my.handler',
268 Properties: {
269 Id: 'test-config',
270 SnsTopicArn: 'arn:aws:sns:us-east-1:special-topic',
271 Bucket: 'test-bucket',
272 BucketRegion: 'us-east-1',
273 EventTypes: ['s3:ObjectCreated:*'],
274 BucketNotificationResources: [ 'arn:aws:s3:::test-bucket' ]
275 }
276 };
277
278 const template = build(params);
279 assert.ok(template.Resources.hasOwnProperty('S3TopicConfigSnsPolicy'), 'an SNS Policy is part of the resources built');
280 assert.end();
281});
\No newline at end of file