-
Notifications
You must be signed in to change notification settings - Fork 2
/
CreateStackTest.cs
214 lines (202 loc) · 8.01 KB
/
CreateStackTest.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon;
using Amazon.CloudFormation;
using Amazon.CloudFormation.Model;
using Amazon.S3;
using Amazon.S3.Model;
using Comformation.IntrinsicFunctions;
using Comformation.S3.Bucket;
using Xunit;
namespace Comformation.Tests
{
public class CreateStackTest
{
private readonly AmazonCloudFormationClient _cloudformation;
public CreateStackTest()
{
_cloudformation = new AmazonCloudFormationClient(RegionEndpoint.EUWest1);
}
/// <summary>
/// This test is a sample of usage for the Comformation library.
/// The generated template plays arround mappings, input and output parameters.
/// The template creates S3 bucket whose name generated using several intrinsic functions.
/// </summary>
[Fact]
public async Task CreateSimpleStack()
{
// Generate template
var template = CreateSimpleTemplate();
var request = new CreateStackRequest
{
StackName = $"ComformationTestSimpleTemplate-{Guid.NewGuid()}",
TemplateBody = template.ToString(),
Parameters = new List<Amazon.CloudFormation.Model.Parameter>
{
new Amazon.CloudFormation.Model.Parameter
{
ParameterKey = "Param2",
ParameterValue = "2"
}
},
};
try
{
// Create stack
var createResponse = await _cloudformation.CreateStackAsync(request);
try
{
// Get created stack details
var stack = await _cloudformation.GetCreatedStack(createResponse.StackId);
// Validate created stack
await AssertCreatedStuck(stack, template);
}
finally
{
// cleanup
var deleteResponse =
await _cloudformation.DeleteStackAsync(new DeleteStackRequest
{
StackName = createResponse.StackId
});
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}
private async Task AssertCreatedStuck(Stack stack, Template template)
{
var createdBucket = stack.Outputs.FirstOrDefault(x => x.OutputKey == "CreatedBucket");
Assert.NotNull(createdBucket);
Assert.StartsWith("test-bucket-ireland-2-", createdBucket.OutputValue);
var s3 = new AmazonS3Client(RegionEndpoint.EUWest1);
var locationResponse = await s3.GetBucketLocationAsync(createdBucket.OutputValue);
Assert.Equal(locationResponse.Location, S3Region.EUW1);
var taggingResponse = await s3.GetBucketTaggingAsync(
new GetBucketTaggingRequest {BucketName = createdBucket.OutputValue});
Assert.Contains(taggingResponse.TagSet, x => x.Key == "Project" && x.Value == "Comformation");
}
private Template CreateSimpleTemplate()
{
var template = new Template
{
Description = "Test comformation",
Parameters = new Parameters
{
new Parameter
{
LogicalId = "Param1",
Type = ParameterType.String,
Default = "Value1",
Description = "Parameter 1",
},
new Parameter
{
LogicalId = "Param2",
Type = ParameterType.Number,
AllowedValues = new object [] { 1, 2 },
Description = "Parameter 2",
}
},
Resources = new Resources
{
new BucketResource
{
LogicalId = "MyBucket",
Properties =
{
// resource name build using intrinsic functions and based on input parameters and mapping
BucketName = Fn.Sub($"test-bucket-${{sufix}}-{Guid.NewGuid()}",
new Sub.Item("sufix", Fn.FindInMap("BucketSufix", Fn.Ref(PseudoParameter.Region), Fn.Ref("Param2")))),
Tags = new List<Tag>
{
new Tag
{
Key = "Project", Value = "Comformation"
}
}
}
}
},
Mappings = new Mappings
{
new Mapping
{
Name = "BucketSufix",
Regions =
{
new RegionMap
{
Region = RegionEndpoint.EUWest1,
Map = { { "1", "ireland-1" }, {"2", "ireland-2"} }
},
new RegionMap
{
Region = RegionEndpoint.EUWest2,
Map = { { "1", "london-1" }, { "2", "london-2"} }
}
}
}
},
Outputs = new Outputs
{
// output based on input parameter
new Output
{
Description = "Input Parameter 1",
LogicalId = "Input1",
Value = Fn.Ref("Param1")
},
// output based on input parameter
new Output
{
Description = "Input Parameter 2",
LogicalId = "Input2",
Value = Fn.Ref("Param2")
},
// output based on pseudo parameter - region where stack run
new Output
{
LogicalId = "Region",
Description = "Region",
Value = Fn.Ref(PseudoParameter.Region)
},
// output based on pseudo parameter - created stack id
new Output
{
LogicalId = "StackId",
Description = "Stack Id",
Value = Fn.Ref(PseudoParameter.StackId)
},
// output based on pseudo parameter - created stack name
new Output
{
LogicalId = "StackName",
Description = "Stack Name",
Value = Fn.Ref(PseudoParameter.StackName)
},
// output based on created resource
new Output
{
LogicalId = "CreatedBucket",
Description = "Name of created bucket",
Value = Fn.Ref("MyBucket")
},
// output based on input param and mapping
new Output
{
LogicalId = "Sufix",
Description = "Bucket name sufix",
Value = Fn.FindInMap("BucketSufix", Fn.Ref(PseudoParameter.Region), Fn.Ref("Param2"))
}
}
};
return template;
}
}
}