/
stage.test.ts
333 lines (269 loc) · 9.28 KB
/
stage.test.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as cxapi from '@aws-cdk/cx-api';
import { nodeunitShim, Test } from 'nodeunit-shim';
import { App, CfnResource, Construct, IAspect, IConstruct, Stack, Stage, Aspects } from '../lib';
nodeunitShim({
'Stack inherits unspecified part of the env from Stage'(test: Test) {
// GIVEN
const app = new App();
const stage = new Stage(app, 'Stage', {
env: { account: 'account', region: 'region' },
});
// WHEN
const stack1 = new Stack(stage, 'Stack1', { env: { region: 'elsewhere' } });
const stack2 = new Stack(stage, 'Stack2', { env: { account: 'tnuocca' } });
// THEN
test.deepEqual(acctRegion(stack1), ['account', 'elsewhere']);
test.deepEqual(acctRegion(stack2), ['tnuocca', 'region']);
test.done();
},
'envs are inherited deeply'(test: Test) {
// GIVEN
const app = new App();
const outer = new Stage(app, 'Stage', {
env: { account: 'account', region: 'region' },
});
// WHEN
const innerAcct = new Stage(outer, 'Acct', { env: { account: 'tnuocca' } });
const innerRegion = new Stage(outer, 'Rgn', { env: { region: 'elsewhere' } });
const innerNeither = new Stage(outer, 'Neither');
// THEN
test.deepEqual(acctRegion(new Stack(innerAcct, 'Stack')), ['tnuocca', 'region']);
test.deepEqual(acctRegion(new Stack(innerRegion, 'Stack')), ['account', 'elsewhere']);
test.deepEqual(acctRegion(new Stack(innerNeither, 'Stack')), ['account', 'region']);
test.done();
},
'The Stage Assembly is in the app Assembly\'s manifest'(test: Test) {
// WHEN
const app = new App();
const stage = new Stage(app, 'Stage');
new BogusStack(stage, 'Stack2');
// THEN -- app manifest contains a nested cloud assembly
const appAsm = app.synth();
const artifact = appAsm.artifacts.find(x => x instanceof cxapi.NestedCloudAssemblyArtifact);
test.ok(artifact);
test.done();
},
'Stacks in Stage are in a different cxasm than Stacks in App'(test: Test) {
// WHEN
const app = new App();
const stack1 = new BogusStack(app, 'Stack1');
const stage = new Stage(app, 'Stage');
const stack2 = new BogusStack(stage, 'Stack2');
// THEN
const stageAsm = stage.synth();
test.deepEqual(stageAsm.stacks.map(s => s.stackName), [stack2.stackName]);
const appAsm = app.synth();
test.deepEqual(appAsm.stacks.map(s => s.stackName), [stack1.stackName]);
test.done();
},
'Can nest Stages inside other Stages'(test: Test) {
// WHEN
const app = new App();
const outer = new Stage(app, 'Outer');
const inner = new Stage(outer, 'Inner');
const stack = new BogusStack(inner, 'Stack');
// WHEN
const appAsm = app.synth();
const outerAsm = appAsm.getNestedAssembly(outer.artifactId);
const innerAsm = outerAsm.getNestedAssembly(inner.artifactId);
test.ok(innerAsm.tryGetArtifact(stack.artifactId));
test.done();
},
'Default stack name in Stage objects incorporates the Stage name and no hash'(test: Test) {
// WHEN
const app = new App();
const stage = new Stage(app, 'MyStage');
const stack = new BogusStack(stage, 'MyStack');
// THEN
test.equal(stage.stageName, 'MyStage');
test.equal(stack.stackName, 'MyStage-MyStack');
test.done();
},
'Can not have dependencies to stacks outside the nested asm'(test: Test) {
// GIVEN
const app = new App();
const stack1 = new BogusStack(app, 'Stack1');
const stage = new Stage(app, 'MyStage');
const stack2 = new BogusStack(stage, 'Stack2');
// WHEN
test.throws(() => {
stack2.addDependency(stack1);
}, /dependency cannot cross stage boundaries/);
test.done();
},
'When we synth() a stage, prepare must be called on constructs in the stage'(test: Test) {
// GIVEN
const app = new App();
let prepared = false;
const stage = new Stage(app, 'MyStage');
const stack = new BogusStack(stage, 'Stack');
class HazPrepare extends Construct {
protected prepare() {
prepared = true;
}
}
new HazPrepare(stack, 'Preparable');
// WHEN
stage.synth();
// THEN
test.equals(prepared, true);
test.done();
},
'When we synth() a stage, aspects inside it must have been applied'(test: Test) {
// GIVEN
const app = new App();
const stage = new Stage(app, 'MyStage');
const stack = new BogusStack(stage, 'Stack');
// WHEN
const aspect = new TouchingAspect();
Aspects.of(stack).add(aspect);
// THEN
app.synth();
test.deepEqual(aspect.visits.map(c => c.node.path), [
'MyStage/Stack',
'MyStage/Stack/Resource',
]);
test.done();
},
'Aspects do not apply inside a Stage'(test: Test) {
// GIVEN
const app = new App();
const stage = new Stage(app, 'MyStage');
new BogusStack(stage, 'Stack');
// WHEN
const aspect = new TouchingAspect();
Aspects.of(app).add(aspect);
// THEN
app.synth();
test.deepEqual(aspect.visits.map(c => c.node.path), [
'',
'Tree',
]);
test.done();
},
'Automatic dependencies inside a stage are available immediately after synth'(test: Test) {
// GIVEN
const app = new App();
const stage = new Stage(app, 'MyStage');
const stack1 = new Stack(stage, 'Stack1');
const stack2 = new Stack(stage, 'Stack2');
// WHEN
const resource1 = new CfnResource(stack1, 'Resource', {
type: 'CDK::Test::Resource',
});
new CfnResource(stack2, 'Resource', {
type: 'CDK::Test::Resource',
properties: {
OtherThing: resource1.ref,
},
});
const asm = stage.synth();
// THEN
test.deepEqual(
asm.getStackArtifact(stack2.artifactId).dependencies.map(d => d.id),
[stack1.artifactId]);
test.done();
},
'Assemblies can be deeply nested'(test: Test) {
// GIVEN
const app = new App({ treeMetadata: false });
const level1 = new Stage(app, 'StageLevel1');
const level2 = new Stage(level1, 'StageLevel2');
new Stage(level2, 'StageLevel3');
// WHEN
const rootAssembly = app.synth();
// THEN
test.deepEqual(rootAssembly.manifest.artifacts, {
'assembly-StageLevel1': {
type: 'cdk:cloud-assembly',
properties: {
directoryName: 'assembly-StageLevel1',
displayName: 'StageLevel1',
},
},
});
const assemblyLevel1 = rootAssembly.getNestedAssembly('assembly-StageLevel1');
test.deepEqual(assemblyLevel1.manifest.artifacts, {
'assembly-StageLevel1-StageLevel2': {
type: 'cdk:cloud-assembly',
properties: {
directoryName: 'assembly-StageLevel1-StageLevel2',
displayName: 'StageLevel1/StageLevel2',
},
},
});
const assemblyLevel2 = assemblyLevel1.getNestedAssembly('assembly-StageLevel1-StageLevel2');
test.deepEqual(assemblyLevel2.manifest.artifacts, {
'assembly-StageLevel1-StageLevel2-StageLevel3': {
type: 'cdk:cloud-assembly',
properties: {
directoryName: 'assembly-StageLevel1-StageLevel2-StageLevel3',
displayName: 'StageLevel1/StageLevel2/StageLevel3',
},
},
});
test.done();
},
'stage name validation'(test: Test) {
const app = new App();
new Stage(app, 'abcd');
new Stage(app, 'abcd123');
new Stage(app, 'abcd123-588dfjjk');
new Stage(app, 'abcd123-588dfjjk.sss');
new Stage(app, 'abcd123-588dfjjk.sss_ajsid');
test.throws(() => new Stage(app, 'abcd123-588dfjjk.sss_ajsid '), /invalid stage name "abcd123-588dfjjk.sss_ajsid "/);
test.throws(() => new Stage(app, 'abcd123-588dfjjk.sss_ajsid/dfo'), /invalid stage name "abcd123-588dfjjk.sss_ajsid\/dfo"/);
test.throws(() => new Stage(app, '&'), /invalid stage name "&"/);
test.throws(() => new Stage(app, '45hello'), /invalid stage name "45hello"/);
test.throws(() => new Stage(app, 'f'), /invalid stage name "f"/);
test.done();
},
'outdir cannot be specified for nested stages'(test: Test) {
// WHEN
const app = new App();
// THEN
test.throws(() => new Stage(app, 'mystage', { outdir: '/tmp/foo/bar' }), /"outdir" cannot be specified for nested stages/);
test.done();
},
'Stage.isStage indicates that a construct is a stage'(test: Test) {
// WHEN
const app = new App();
const stack = new Stack();
const stage = new Stage(app, 'Stage');
// THEN
test.ok(Stage.isStage(stage));
test.ok(Stage.isStage(app));
test.ok(!Stage.isStage(stack));
test.done();
},
'Stage.isStage indicates that a construct is a stage based on symbol'(test: Test) {
// WHEN
const app = new App();
const stage = new Stage(app, 'Stage');
const externalStage = {};
const STAGE_SYMBOL = Symbol.for('@aws-cdk/core.Stage');
Object.defineProperty(externalStage, STAGE_SYMBOL, { value: true });
// THEN
test.ok(Stage.isStage(stage));
test.ok(Stage.isStage(app));
test.ok(Stage.isStage(externalStage));
test.done();
},
});
class TouchingAspect implements IAspect {
public readonly visits = new Array<IConstruct>();
public visit(node: IConstruct): void {
this.visits.push(node);
}
}
class BogusStack extends Stack {
constructor(scope: Construct, id: string) {
super(scope, id);
new CfnResource(this, 'Resource', {
type: 'CDK::Test::Resource',
});
}
}
function acctRegion(s: Stack) {
return [s.account, s.region];
}