This repository has been archived by the owner on Jun 28, 2021. It is now read-only.
forked from erlcloud/erlcloud
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherlcloud_cloudformation_tests.erl
597 lines (505 loc) · 22.7 KB
/
erlcloud_cloudformation_tests.erl
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
%% -*- mode: erlang;erlang-indent-level: 4;indent-tabs-mode: nil -*-
-module(erlcloud_cloudformation_tests).
-include_lib("eunit/include/eunit.hrl").
-include("erlcloud.hrl").
-include("erlcloud_aws.hrl").
-define(_cloudformation_test(T), {?LINE, T}).
-define(_f(F), fun() -> F end).
%%==============================================================================
%% Test entry points
%%==============================================================================
describe_cloudformation_test_() ->
{foreach, fun start/0, fun stop/1, [
fun list_stacks_all_output_tests/1,
fun list_stack_resources_output_tests/1,
fun get_template_summary_output_tests/1,
fun get_template_output_tests/1,
fun get_stack_policy_output_tests/1,
fun describe_stacks_output_tests/1,
fun describe_stack_resources_output_tests/1,
fun describe_stack_resource_output_tests/1,
fun describe_stack_events_output_tests/1,
fun describe_account_limits_output_tests/1,
fun list_stacks_all_input_tests/1,
fun list_stack_resources_input_tests/1,
fun get_template_summary_input_tests/1,
fun get_template_input_tests/1,
fun get_stack_policy_input_tests/1,
fun describe_stacks_input_tests/1,
fun describe_stack_resources_input_tests/1,
fun describe_stack_resource_input_tests/1,
fun describe_stack_events_input_tests/1,
fun describe_account_limits_input_tests/1
]}.
start() ->
meck:new(erlcloud_aws),
ok.
stop(_) ->
meck:unload(erlcloud_aws).
%%==============================================================================
%% Output Test helpers
%%==============================================================================
output_test(Fun, {Line, {Description, Response, Result}}) ->
{Description, {Line, fun() ->
meck:expect(erlcloud_aws, aws_request_xml4, 6,
{ok, element(1, xmerl_scan:string(
binary_to_list(Response)))}
),
Actual = Fun(),
?assertEqual(Result, Actual)
end}}.
%%==============================================================================
%% Input Test helpers
%%==============================================================================
validate_param(_Param = {Key, _Value}, Params) ->
true = proplists:is_defined(Key, Params).
validate_params(Params, Expected) ->
[validate_param(X, Params)
|| X <- [{"Action", ""}, {"Version", ""} | Expected]
].
input_expect(Response, Params) ->
fun(post, "cloudformation.us-east-1.amazonaws.com", "/", QParams,
"cloudformation", _) ->
validate_params(QParams, Params),
Response
end.
input_test(Response, {Line, {Description, Fun, Params}})
when is_list(Description) ->
InputFunction = input_expect(Response, Params),
meck:expect(erlcloud_aws, aws_request_xml4, InputFunction),
{Description, {Line, fun() ->
Fun()
end}}.
%%==============================================================================
%% Input Tests
%%==============================================================================
list_stacks_all_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<ListStacksResponse>null</ListStacksResponse>">>)
))},
ExpectedParams = [],
input_test(Response, ?_cloudformation_test({"Test list all stacks input",
?_f(erlcloud_cloudformation:list_stacks_all([], #aws_config{})),
ExpectedParams})).
list_stack_resources_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<ListStackResourcesResponse>null"
"</ListStackResourcesResponse>">>)
))},
ExpectedParams = [{"StackName", ""}],
input_test(Response, ?_cloudformation_test({"Test list all stack resources",
?_f(erlcloud_cloudformation:list_stack_resources_all([],
"Stack Name", #aws_config{})), ExpectedParams})).
get_template_summary_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<GetTemplateSummaryResponse>null"
"</GetTemplateSummaryResponse>">>)
))},
ExpectedParams = [{"StackName", ""}],
input_test(Response, ?_cloudformation_test({"Test get template summary",
?_f(erlcloud_cloudformation:get_template_summary([],
"Stack Name", #aws_config{})), ExpectedParams})).
get_template_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<GetTemplateResponse>null</GetTemplateResponse>">>)
))},
ExpectedParams = [{"StackName", ""}],
input_test(Response, ?_cloudformation_test({"Test get template",
?_f(erlcloud_cloudformation:get_template("Stack Name", #aws_config{})),
ExpectedParams})).
get_stack_policy_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<GetStackPolicyResult>null</GetStackPolicyResult>">>)
))},
ExpectedParams = [{"StackName", ""}],
input_test(Response, ?_cloudformation_test({"Test get stack policy",
?_f(erlcloud_cloudformation:get_stack_policy([],
"Stack Name", #aws_config{})), ExpectedParams})).
describe_stacks_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<DescribeStacksResponse>null"
"</DescribeStacksResponse>">>)
))},
ExpectedParams = [],
input_test(Response, ?_cloudformation_test({"Test describe stacks all",
?_f(erlcloud_cloudformation:describe_stacks_all(
ExpectedParams,#aws_config{})), ExpectedParams})).
describe_stack_resources_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<DescribeStackResourcesResponse>null"
"</DescribeStackResourcesResponse>">>)
))},
ExpectedParams = [{"StackName", ""}],
input_test(Response, ?_cloudformation_test(
{"Test describe stack resources all",
?_f(erlcloud_cloudformation:describe_stack_resources([],
"Stack Name", #aws_config{})), ExpectedParams})).
describe_stack_resource_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<DescribeStackResourceResponse>null"
"</DescribeStackResourceResponse>">>)
))},
ExpectedParams = [{"StackName", ""}, {"LogicalResourceId", ""}],
input_test(Response, ?_cloudformation_test(
{"Test describe stack resource",
?_f(erlcloud_cloudformation:describe_stack_resource(
[], "Stack Name", "Logical Resource Id",
#aws_config{})), ExpectedParams})).
describe_stack_events_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<DescribeStackEventsResponse>null"
"</DescribeStackEventsResponse>">>)
))},
ExpectedParams = [{"StackName", ""}],
input_test(Response, ?_cloudformation_test(
{"Test describe all stack events",
?_f(erlcloud_cloudformation:describe_stack_events_all(
[{stack_name, ""}], #aws_config{})), ExpectedParams})).
describe_account_limits_input_tests(_) ->
Response = {ok, element(1, xmerl_scan:string(
binary_to_list(<<"<DescribeAccountLimitsResponse>null"
"</DescribeAccountLimitsResponse>">>)
))},
ExpectedParams = [],
input_test(Response, ?_cloudformation_test(
{"test describe all account limits",
?_f(erlcloud_cloudformation:describe_account_limits_all(
#aws_config{})),
ExpectedParams})).
%%==============================================================================
%% Output Tests
%%==============================================================================
list_stacks_all_output_tests(_) ->
Test = ?_cloudformation_test({"Test listing all stacks",
<<"<ListStacksResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<NextToken>
null
</NextToken>
<ListStacksResult>
<StackSummaries>
<member>
<StackId>Stack ID</StackId>
<StackStatus>Status</StackStatus>
<StackName>Stack Name</StackName>
<CreationTime>2015-05-23T15:47:44Z</CreationTime>
<TemplateDescription>Description"
"</TemplateDescription>
<ResourceTypes><member>Resource</member>"
"</ResourceTypes>
</member>
</StackSummaries>
</ListStacksResult>
</ListStacksResponse>">>,
[
[{summaries, [[{member,
[[{stack_id, "Stack ID"},
{stack_status, "Status"},
{stack_name, "Stack Name"},
{creation_time, "2015-05-23T15:47:44Z"},
{template_description, "Description"},
{resource_types, ["Resource"]}
]]}
]]}]
]
}),
output_test(?_f(erlcloud_cloudformation:list_stacks_all(
[], #aws_config{})), Test).
list_stack_resources_output_tests(_) ->
Test = ?_cloudformation_test({"Test listing all stack resources",
<<"<ListStackResourcesResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<ListStackResourcesResult>
<StackResourceSummaries>
<member>
<ResourceStatus>Some Status</ResourceStatus>
<LogicalResourceId>DBSecurityGroup"
"</LogicalResourceId>
<LastUpdatedTimestamp>2015-06-21T20:25:57Z"
"</LastUpdatedTimestamp>
<PhysicalResourceId>Some Resource ID"
"</PhysicalResourceId>
<ResourceType>Some Resource Type</ResourceType>
</member>
</StackResourceSummaries>
</ListStackResourcesResult>
<ResponseMetadata>
<RequestId>Some Request ID</RequestId>
</ResponseMetadata>
</ListStackResourcesResponse>">>,
[
[{summaries, [[{member,
[[{resource_status, "Some Status"},
{logical_resource_id, "DBSecurityGroup"},
{last_updated_timestamp, "2015-06-21T20:25:57Z"},
{physical_resource_id, "Some Resource ID"},
{resource_type, "Some Resource Type"}]]
}]]}]
]
}),
output_test(?_f(erlcloud_cloudformation:list_stack_resources_all([],
"Stack Name", #aws_config{})), Test).
get_template_summary_output_tests(_) ->
Test = ?_cloudformation_test({"Test get template summary",
<<"<GetTemplateSummaryResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<GetTemplateSummaryResult>
<Description>description</Description>
<Parameters>
<member>
<NoEcho>false</NoEcho>
<ParameterKey>KeyName</ParameterKey>
<Description>description</Description>
<ParameterType>Type</ParameterType>
</member>
</Parameters>
<Metadata>metadata</Metadata>
<Version>version</Version>
</GetTemplateSummaryResult>
<ResponseMetadata>
<RequestId>request ID</RequestId>
</ResponseMetadata>
</GetTemplateSummaryResponse>">>,
[
{template_summary_result, [[{description, "description"},
{parameters, [[
{member, [[{no_echo, "false"},
{parameter_key, "KeyName"},
{description, "description"},
{parameter_type, "Type"}]]}
]]},
{metadata, "metadata"},
{version, "version"}]]},
{response_meta, [[{request_id, "request ID"}]]}
]
}),
output_test(?_f(erlcloud_cloudformation:get_template_summary([],
"Stack Name", #aws_config{}
)), Test).
get_template_output_tests(_) ->
Test = ?_cloudformation_test({"Test get template",
<<"<GetTemplateResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<GetTemplateResult>
<TemplateBody>template body</TemplateBody>
</GetTemplateResult>
<ResponseMetadata>
<RequestId>request ID</RequestId>
</ResponseMetadata>
</GetTemplateResponse>">>,
[
{template_result, [[{template_body, "template body"}]]},
{response_meta, [[{request_id, "request ID"}]]}
]
}),
output_test(?_f(erlcloud_cloudformation:get_template(
"Some Stack Name", #aws_config{}
)), Test).
get_stack_policy_output_tests(_) ->
Test = ?_cloudformation_test({"Test get stack policy",
<<"<GetStackPolicyResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<GetStackPolicyResult>
<StackPolicyBody>Policy</StackPolicyBody>
</GetStackPolicyResult>
<ResponseMetadata>
<RequestId>request ID</RequestId>
</ResponseMetadata>
</GetStackPolicyResponse>">>,
[
[{stack_policy_body ,"Policy"},
{response_meta, [[{request_id, "request ID"}]]}]
]
}),
output_test(?_f(erlcloud_cloudformation:get_stack_policy([],
"Stack Name", #aws_config{}
)), Test).
describe_stacks_output_tests(_) ->
Test = ?_cloudformation_test({"Test describe all stacks",
<<"<DescribeStacksResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<DescribeStacksResult>
<Stacks>
<member>
<StackName>My Stack</StackName>
<StackId>Stack Id</StackId>
<CreationTime>Not Today</CreationTime>
<StackStatus>status</StackStatus>
<DisableRollback>false</DisableRollback>
<Parameters>
<member>
<ParameterKey>Some Key</ParameterKey>
<ParameterValue>Parameter</ParameterValue>
</member>
</Parameters>
<Outputs>
<member>
<Description>Some Description</Description>
<OutputKey>Some Key</OutputKey>
<OutputValue>Output</OutputValue>
</member>
</Outputs>
</member>
</Stacks>
</DescribeStacksResult>
<ResponseMetadata>
<RequestId>request ID</RequestId>
</ResponseMetadata>
</DescribeStacksResponse>">>,
[[{stacks, [[
{member, [[{stack_name, "My Stack"},
{stack_id, "Stack Id"},
{creation_time, "Not Today"},
{stack_status, "status"},
{disable_rollback, "false"},
{parameters, [[
{member, [[
{parameter_key, "Some Key"},
{parameter_value, "Parameter"}
]]}
]]},
{outputs, [[
{member, [[
{description, "Some Description"},
{output_key, "Some Key"},
{output_value, "Output"}
]]}
]]}
]]}
]]},{response_meta, [[{request_id, "request ID"}]]}]]
}),
output_test(?_f(erlcloud_cloudformation:describe_stacks_all(
[{stack_name, "Some Stack Name"}], #aws_config{}
)), Test).
describe_stack_resources_output_tests(_) ->
Test = ?_cloudformation_test({"Test describe stack resources",
<<"<DescribeStackResourcesResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<DescribeStackResourcesResult>
<StackResources>
<member>
<StackId>Stack ID</StackId>
<StackName>My Stack</StackName>
<LogicalResourceId>MyDBInstance</LogicalResourceId>
<PhysicalResourceId>Resource ID</PhysicalResourceId>
<ResourceType>Resource Type</ResourceType>
<Timestamp>Timestamp</Timestamp>
<ResourceStatus>Status</ResourceStatus>
</member>
</StackResources>
</DescribeStackResourcesResult>
<ResponseMetadata>
<RequestId>Request ID</RequestId>
</ResponseMetadata>
</DescribeStackResourcesResponse>">>,
[[{resources, [[{member, [[
{stack_id, "Stack ID"},
{stack_name, "My Stack"},
{logical_resource_id, "MyDBInstance"},
{physical_resource_id, "Resource ID"},
{resource_type, "Resource Type"},
{timestamp, "Timestamp"},
{resource_status, "Status"}]]}]]},
{response_meta, [[{request_id, "Request ID"}]]}]
]
}),
output_test(?_f(erlcloud_cloudformation:describe_stack_resources([],
"Stack Name", #aws_config{}
)), Test).
describe_stack_resource_output_tests(_) ->
Test = ?_cloudformation_test({"Test describe stack resource",
<<"<DescribeStackResourceResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<DescribeStackResourceResult>
<StackResourceDetail>
<StackId>Stack ID</StackId>
<StackName>My Stack</StackName>
<LogicalResourceId>MyDBInstance</LogicalResourceId>
<PhysicalResourceId>Resource ID</PhysicalResourceId>
<ResourceType>Resource Type</ResourceType>
<LastUpdatedTimestamp>Tiemstamp</LastUpdatedTimestamp>
<ResourceStatus>Status</ResourceStatus>
</StackResourceDetail>
</DescribeStackResourceResult>
<ResponseMetadata>
<RequestId>Request ID</RequestId>
</ResponseMetadata>
</DescribeStackResourceResponse>">>,
[[{resources,
[[{stack_id, "Stack ID"},
{stack_name, "My Stack"},
{logical_resource_id, "MyDBInstance"},
{physical_resource_id, "Resource ID"},
{resource_type, "Resource Type"},
{resource_status, "Status"}]]
},
{response_meta, [[{request_id, "Request ID"}]]}]]
}),
output_test(?_f(erlcloud_cloudformation:describe_stack_resource([],
"Stack Name", "Logical Id", #aws_config{}
)), Test).
describe_stack_events_output_tests(_) ->
Test = ?_cloudformation_test({"Test describe alls tack events",
<<"<DescribeStackEventsResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<DescribeStackEventsResult>
<StackEvents>
<member>
<EventId>Event ID</EventId>
<StackId>Stack ID</StackId>
<StackName>My Stack</StackName>
<LogicalResourceId>Resource ID</LogicalResourceId>
<PhysicalResourceId>Resource ID</PhysicalResourceId>
<ResourceType>Resource Type</ResourceType>
<Timestamp>Timestamp</Timestamp>
<ResourceStatus>Resource Status</ResourceStatus>
<ResourceStatusReason>Resource Status Reason"
"</ResourceStatusReason>
</member>
</StackEvents>
</DescribeStackEventsResult>
<ResponseMetadata>
<RequestId>Request ID</RequestId>
</ResponseMetadata>
</DescribeStackEventsResponse>">>,
[[{stack_events, [[{
member, [[
{event_id, "Event ID"},
{stack_id, "Stack ID"},
{stack_name, "My Stack"},
{logical_resource_id, "Resource ID"},
{physical_resource_id, "Resource ID"},
{resource_type, "Resource Type"},
{resource_status, "Resource Status"}]]}]]},
{response_meta, [[{request_id, "Request ID"}]]}]]
}),
output_test(?_f(erlcloud_cloudformation:describe_stack_events_all(
[{stack_name, "Some Stack Name"}], #aws_config{}
)), Test).
describe_account_limits_output_tests(_) ->
Test = ?_cloudformation_test({"Test describe all account limits",
<<"<DescribeAccountLimitsResponse xmlns="
"\"http://cloudformation.amazonaws.com/doc/2010-05-15/\">
<DescribeAccountLimitsResult>
<AccountLimits>
<member>
<Name>Name</Name>
<Value>Value</Value>
</member>
</AccountLimits>
</DescribeAccountLimitsResult>
<ResponseMetadata>
<RequestId>Request ID</RequestId>
</ResponseMetadata>
</DescribeAccountLimitsResponse>">>,
[[{describe_account_limits_result,[[{
account_limits, [[{member, [[
{name, "Name"},
{value, "Value"}
]]}]]
}]]},
{response_meta, [[{request_id, "Request ID"}]]}]]
}),
output_test(?_f(erlcloud_cloudformation:describe_account_limits_all(
#aws_config{}
)), Test).