-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
template.ts
310 lines (283 loc) · 13.4 KB
/
template.ts
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
import * as path from 'path';
import * as fs from 'fs-extra';
import { Match } from './match';
import { Matcher } from './matcher';
import { findConditions, hasCondition } from './private/conditions';
import { checkTemplateForCyclicDependencies } from './private/cyclic';
import { findMappings, hasMapping } from './private/mappings';
import { findOutputs, hasOutput } from './private/outputs';
import { findParameters, hasParameter } from './private/parameters';
import { allResources, allResourcesProperties, countResources, countResourcesProperties, findResources, hasResource, hasResourceProperties } from './private/resources';
import { Template as TemplateType } from './private/template';
import { Stack, Stage } from '../../core';
/**
* Suite of assertions that can be run on a CDK stack.
* Typically used, as part of unit tests, to validate that the rendered
* CloudFormation template has expected resources and properties.
*/
export class Template {
/**
* Base your assertions on the CloudFormation template synthesized by a CDK `Stack`.
* @param stack the CDK Stack to run assertions on
* @param templateParsingOptions Optional param to configure template parsing behavior, such as disregarding circular
* dependencies.
*/
public static fromStack(stack: Stack, templateParsingOptions?: TemplateParsingOptions): Template {
return new Template(toTemplate(stack), templateParsingOptions);
}
/**
* Base your assertions from an existing CloudFormation template formatted as an in-memory
* JSON object.
* @param template the CloudFormation template formatted as a nested set of records
* @param templateParsingOptions Optional param to configure template parsing behavior, such as disregarding circular
* dependencies.
*/
public static fromJSON(template: { [key: string] : any }, templateParsingOptions?: TemplateParsingOptions): Template {
return new Template(template, templateParsingOptions);
}
/**
* Base your assertions from an existing CloudFormation template formatted as a
* JSON string.
* @param template the CloudFormation template in
* @param templateParsingOptions Optional param to configure template parsing behavior, such as disregarding circular
* dependencies.
*/
public static fromString(template: string, templateParsingOptions?: TemplateParsingOptions): Template {
return new Template(JSON.parse(template), templateParsingOptions);
}
private readonly template: TemplateType;
private constructor(template: { [key: string]: any }, templateParsingOptions: TemplateParsingOptions = {}) {
this.template = template as TemplateType;
if (!templateParsingOptions?.skipCyclicalDependenciesCheck ?? true) {
checkTemplateForCyclicDependencies(this.template);
}
}
/**
* The CloudFormation template deserialized into an object.
*/
public toJSON(): { [key: string]: any } {
return this.template;
}
/**
* Assert that the given number of resources of the given type exist in the
* template.
* @param type the resource type; ex: `AWS::S3::Bucket`
* @param count number of expected instances
*/
public resourceCountIs(type: string, count: number): void {
const counted = countResources(this.template, type);
if (counted !== count) {
throw new Error(`Expected ${count} resources of type ${type} but found ${counted}`);
}
}
/**
* Assert that the given number of resources of the given type and properties exists in the
* CloudFormation template.
* @param type the resource type; ex: `AWS::S3::Bucket`
* @param props the 'Properties' section of the resource as should be expected in the template.
* @param count number of expected instances
*/
public resourcePropertiesCountIs(type: string, props: any, count: number): void {
const counted = countResourcesProperties(this.template, type, props);
if (counted !== count) {
throw new Error(`Expected ${count} resources of type ${type} but found ${counted}`);
}
}
/**
* Assert that a resource of the given type and properties exists in the
* CloudFormation template.
* By default, performs partial matching on the `Properties` key of the resource, via the
* `Match.objectLike()`. To configure different behavior, use other matchers in the `Match` class.
* @param type the resource type; ex: `AWS::S3::Bucket`
* @param props the 'Properties' section of the resource as should be expected in the template.
*/
public hasResourceProperties(type: string, props: any): void {
const matchError = hasResourceProperties(this.template, type, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Assert that a resource of the given type and given definition exists in the
* CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param type the resource type; ex: `AWS::S3::Bucket`
* @param props the entire definition of the resource as should be expected in the template.
*/
public hasResource(type: string, props: any): void {
const matchError = hasResource(this.template, type, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Get the set of matching resources of a given type and properties in the CloudFormation template.
* @param type the type to match in the CloudFormation template
* @param props by default, matches all resources with the given type.
* When a literal is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour.
*/
public findResources(type: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findResources(this.template, type, props);
}
/**
* Assert that all resources of the given type contain the given definition in the
* CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param type the resource type; ex: `AWS::S3::Bucket`
* @param props the entire definition of the resources as they should be expected in the template.
*/
public allResources(type: string, props: any): void {
const matchError = allResources(this.template, type, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Assert that all resources of the given type contain the given properties
* CloudFormation template.
* By default, performs partial matching on the `Properties` key of the resource, via the
* `Match.objectLike()`. To configure different behavior, use other matchers in the `Match` class.
* @param type the resource type; ex: `AWS::S3::Bucket`
* @param props the 'Properties' section of the resource as should be expected in the template.
*/
public allResourcesProperties(type: string, props: any): void {
const matchError = allResourcesProperties(this.template, type, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Assert that a Parameter with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the parameter, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param logicalId the name of the parameter. Provide `'*'` to match all parameters in the template.
* @param props the parameter as should be expected in the template.
*/
public hasParameter(logicalId: string, props: any): void {
const matchError = hasParameter(this.template, logicalId, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Get the set of matching Parameters that match the given properties in the CloudFormation template.
* @param logicalId the name of the parameter. Provide `'*'` to match all parameters in the template.
* @param props by default, matches all Parameters in the template.
* When a literal object is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour.
*/
public findParameters(logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findParameters(this.template, logicalId, props);
}
/**
* Assert that an Output with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param logicalId the name of the output. Provide `'*'` to match all outputs in the template.
* @param props the output as should be expected in the template.
*/
public hasOutput(logicalId: string, props: any): void {
const matchError = hasOutput(this.template, logicalId, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Get the set of matching Outputs that match the given properties in the CloudFormation template.
* @param logicalId the name of the output. Provide `'*'` to match all outputs in the template.
* @param props by default, matches all Outputs in the template.
* When a literal object is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour.
*/
public findOutputs(logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findOutputs(this.template, logicalId, props);
}
/**
* Assert that a Mapping with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param logicalId the name of the mapping. Provide `'*'` to match all mappings in the template.
* @param props the output as should be expected in the template.
*/
public hasMapping(logicalId: string, props: any): void {
const matchError = hasMapping(this.template, logicalId, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Get the set of matching Mappings that match the given properties in the CloudFormation template.
* @param logicalId the name of the mapping. Provide `'*'` to match all mappings in the template.
* @param props by default, matches all Mappings in the template.
* When a literal object is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour.
*/
public findMappings(logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findMappings(this.template, logicalId, props);
}
/**
* Assert that a Condition with the given properties exists in the CloudFormation template.
* By default, performs partial matching on the resource, via the `Match.objectLike()`.
* To configure different behavior, use other matchers in the `Match` class.
* @param logicalId the name of the mapping. Provide `'*'` to match all conditions in the template.
* @param props the output as should be expected in the template.
*/
public hasCondition(logicalId: string, props: any): void {
const matchError = hasCondition(this.template, logicalId, props);
if (matchError) {
throw new Error(matchError);
}
}
/**
* Get the set of matching Conditions that match the given properties in the CloudFormation template.
* @param logicalId the name of the condition. Provide `'*'` to match all conditions in the template.
* @param props by default, matches all Conditions in the template.
* When a literal object is provided, performs a partial match via `Match.objectLike()`.
* Use the `Match` APIs to configure a different behaviour.
*/
public findConditions(logicalId: string, props: any = {}): { [key: string]: { [key: string]: any } } {
return findConditions(this.template, logicalId, props);
}
/**
* Assert that the CloudFormation template matches the given value
* @param expected the expected CloudFormation template as key-value pairs.
*/
public templateMatches(expected: any): void {
const matcher = Matcher.isMatcher(expected) ? expected : Match.objectLike(expected);
const result = matcher.test(this.template);
if (result.hasFailed()) {
throw new Error([
'Template did not match as expected. The following mismatches were found:',
...result.toHumanStrings().map(s => `\t${s}`),
].join('\n'));
}
}
}
/**
* Options to configure template parsing behavior, such as disregarding circular
* dependencies.
*/
export interface TemplateParsingOptions {
/**
* If set to true, will skip checking for cyclical / circular dependencies. Should be set to false other than for
* templates that are valid despite containing cycles, such as unprocessed transform stacks.
*
* @default false
*/
readonly skipCyclicalDependenciesCheck?: boolean;
}
function toTemplate(stack: Stack): any {
const stage = Stage.of(stack);
if (!Stage.isStage(stage)) {
throw new Error('unexpected: all stacks must be part of a Stage or an App');
}
const assembly = stage.synth();
if (stack.nestedStackParent) {
// if this is a nested stack (it has a parent), then just read the template as a string
return JSON.parse(fs.readFileSync(path.join(assembly.directory, stack.templateFile)).toString('utf-8'));
}
return assembly.getStackArtifact(stack.artifactId).template;
}