/
test.staging.ts
295 lines (248 loc) · 9.75 KB
/
test.staging.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
import * as cxapi from '@aws-cdk/cx-api';
import * as fs from 'fs-extra';
import { Test } from 'nodeunit';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import { App, AssetHashType, AssetStaging, BundlingDockerImage, Stack } from '../lib';
const STUB_INPUT_FILE = '/tmp/docker-stub.input';
enum DockerStubCommand {
SUCCESS = 'DOCKER_STUB_SUCCESS',
FAIL = 'DOCKER_STUB_FAIL',
SUCCESS_NO_OUTPUT = 'DOCKER_STUB_SUCCESS_NO_OUTPUT'
}
const userInfo = os.userInfo();
const USER_ARG = `-u ${userInfo.uid}:${userInfo.gid}`;
// this is a way to provide a custom "docker" command for staging.
process.env.CDK_DOCKER = `${__dirname}/docker-stub.sh`;
export = {
'tearDown'(cb: any) {
if (fs.existsSync(STUB_INPUT_FILE)) {
fs.unlinkSync(STUB_INPUT_FILE);
}
cb();
sinon.restore();
},
'base case'(test: Test) {
// GIVEN
const stack = new Stack();
const sourcePath = path.join(__dirname, 'fs', 'fixtures', 'test1');
// WHEN
const staging = new AssetStaging(stack, 's1', { sourcePath });
test.deepEqual(staging.sourceHash, '2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
test.deepEqual(staging.sourcePath, sourcePath);
test.deepEqual(stack.resolve(staging.stagedPath), 'asset.2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
test.done();
},
'staging can be disabled through context'(test: Test) {
// GIVEN
const stack = new Stack();
stack.node.setContext(cxapi.DISABLE_ASSET_STAGING_CONTEXT, true);
const sourcePath = path.join(__dirname, 'fs', 'fixtures', 'test1');
// WHEN
const staging = new AssetStaging(stack, 's1', { sourcePath });
test.deepEqual(staging.sourceHash, '2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
test.deepEqual(staging.sourcePath, sourcePath);
test.deepEqual(stack.resolve(staging.stagedPath), sourcePath);
test.done();
},
'files are copied to the output directory during synth'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
const file = path.join(__dirname, 'fs', 'fixtures.tar.gz');
// WHEN
new AssetStaging(stack, 's1', { sourcePath: directory });
new AssetStaging(stack, 'file', { sourcePath: file });
// THEN
const assembly = app.synth();
test.deepEqual(fs.readdirSync(assembly.directory), [
'asset.2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00',
'asset.af10ac04b3b607b0f8659c8f0cee8c343025ee75baf0b146f10f0e5311d2c46b.gz',
'cdk.out',
'manifest.json',
'stack.template.json',
'tree.json',
]);
test.done();
},
'allow specifying extra data to include in the source hash'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// WHEN
const withoutExtra = new AssetStaging(stack, 'withoutExtra', { sourcePath: directory });
const withExtra = new AssetStaging(stack, 'withExtra', { sourcePath: directory, extraHash: 'boom' });
// THEN
test.notEqual(withoutExtra.sourceHash, withExtra.sourceHash);
test.deepEqual(withoutExtra.sourceHash, '2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00');
test.deepEqual(withExtra.sourceHash, 'c95c915a5722bb9019e2c725d11868e5a619b55f36172f76bcbcaa8bb2d10c5f');
test.done();
},
'with bundling'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
const ensureDirSyncSpy = sinon.spy(fs, 'ensureDirSync');
const mkdtempSyncSpy = sinon.spy(fs, 'mkdtempSync');
const chmodSyncSpy = sinon.spy(fs, 'chmodSync');
// WHEN
new AssetStaging(stack, 'Asset', {
sourcePath: directory,
bundling: {
image: BundlingDockerImage.fromRegistry('alpine'),
command: [ DockerStubCommand.SUCCESS ],
},
});
// THEN
const assembly = app.synth();
test.deepEqual(
readDockerStubInput(),
`run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input alpine DOCKER_STUB_SUCCESS`,
);
test.deepEqual(fs.readdirSync(assembly.directory), [
'asset.2f37f937c51e2c191af66acf9b09f548926008ec68c575bd2ee54b6e997c0e00',
'cdk.out',
'manifest.json',
'stack.template.json',
'tree.json',
]);
// asset is bundled in a directory inside .cdk.staging
const stagingTmp = path.join('.', '.cdk.staging');
test.ok(ensureDirSyncSpy.calledWith(stagingTmp));
test.ok(mkdtempSyncSpy.calledWith(sinon.match(path.join(stagingTmp, 'asset-bundle-'))));
test.ok(chmodSyncSpy.calledWith(sinon.match(path.join(stagingTmp, 'asset-bundle-')), 0o777));
test.done();
},
'bundling throws when /asset-ouput is empty'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// THEN
test.throws(() => new AssetStaging(stack, 'Asset', {
sourcePath: directory,
bundling: {
image: BundlingDockerImage.fromRegistry('alpine'),
command: [ DockerStubCommand.SUCCESS_NO_OUTPUT ],
},
}), /Bundling did not produce any output/);
test.equal(
readDockerStubInput(),
`run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input alpine DOCKER_STUB_SUCCESS_NO_OUTPUT`,
);
test.done();
},
'bundling with BUNDLE asset hash type'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// WHEN
const asset = new AssetStaging(stack, 'Asset', {
sourcePath: directory,
bundling: {
image: BundlingDockerImage.fromRegistry('alpine'),
command: [ DockerStubCommand.SUCCESS ],
},
assetHashType: AssetHashType.BUNDLE,
});
// THEN
test.equal(
readDockerStubInput(),
`run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input alpine DOCKER_STUB_SUCCESS`,
);
test.equal(asset.assetHash, '33cbf2cae5432438e0f046bc45ba8c3cef7b6afcf47b59d1c183775c1918fb1f');
test.done();
},
'custom hash'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// WHEN
const asset = new AssetStaging(stack, 'Asset', {
sourcePath: directory,
assetHash: 'my-custom-hash',
});
// THEN
test.equal(fs.existsSync(STUB_INPUT_FILE), false);
test.equal(asset.assetHash, 'b9c77053f5b83bbe5ba343bc18e92db939a49017010813225fea91fa892c4823'); // hash of 'my-custom-hash'
test.done();
},
'throws with assetHash and not CUSTOM hash type'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// THEN
test.throws(() => new AssetStaging(stack, 'Asset', {
sourcePath: directory,
bundling: {
image: BundlingDockerImage.fromRegistry('alpine'),
command: [ DockerStubCommand.SUCCESS ],
},
assetHash: 'my-custom-hash',
assetHashType: AssetHashType.BUNDLE,
}), /Cannot specify `bundle` for `assetHashType`/);
test.equal(
readDockerStubInput(),
`run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input alpine DOCKER_STUB_SUCCESS`,
);
test.done();
},
'throws with BUNDLE hash type and no bundling'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// THEN
test.throws(() => new AssetStaging(stack, 'Asset', {
sourcePath: directory,
assetHashType: AssetHashType.BUNDLE,
}), /Cannot use `AssetHashType.BUNDLE` when `bundling` is not specified/);
test.equal(fs.existsSync(STUB_INPUT_FILE), false);
test.done();
},
'throws with CUSTOM and no hash'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// THEN
test.throws(() => new AssetStaging(stack, 'Asset', {
sourcePath: directory,
assetHashType: AssetHashType.CUSTOM,
}), /`assetHash` must be specified when `assetHashType` is set to `AssetHashType.CUSTOM`/);
test.equal(fs.existsSync(STUB_INPUT_FILE), false); // "docker" not executed
test.done();
},
'throws when bundling fails'(test: Test) {
// GIVEN
const app = new App();
const stack = new Stack(app, 'stack');
const directory = path.join(__dirname, 'fs', 'fixtures', 'test1');
// THEN
test.throws(() => new AssetStaging(stack, 'Asset', {
sourcePath: directory,
bundling: {
image: BundlingDockerImage.fromRegistry('this-is-an-invalid-docker-image'),
command: [ DockerStubCommand.FAIL ],
},
}), /Failed to run bundling Docker image for asset stack\/Asset/);
test.equal(
readDockerStubInput(),
`run --rm ${USER_ARG} -v /input:/asset-input:delegated -v /output:/asset-output:delegated -w /asset-input this-is-an-invalid-docker-image DOCKER_STUB_FAIL`,
);
test.done();
},
};
function readDockerStubInput() {
const out = fs.readFileSync(STUB_INPUT_FILE, 'utf-8').trim();
return out
.replace(/-v ([^:]+):\/asset-input/, '-v /input:/asset-input')
.replace(/-v ([^:]+):\/asset-output/, '-v /output:/asset-output');
}