-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall.spec.ts
541 lines (502 loc) · 16.5 KB
/
all.spec.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
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
/**
* @license
* Copyright 2019 Balena Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import * as fs from 'fs';
import { Readable } from 'stream';
import * as tar from 'tar-stream';
import * as TarUtils from 'tar-utils';
import * as Resolve from '../../lib/resolve';
import * as Utils from '../../lib/resolve/utils';
use(chaiAsPromised);
const defaultResolvers: () => Resolve.Resolver[] = () =>
Resolve.getDefaultResolvers();
function getDockerfileFromTarStream(
stream: Readable,
name = 'Dockerfile',
): Promise<string> {
return new Promise<string>((resolve, reject) => {
const extract = tar.extract();
let foundDockerfile = false;
extract.on(
'entry',
(
header: tar.Headers,
entryStream: NodeJS.ReadableStream,
next: () => void,
) => {
if (TarUtils.normalizeTarEntry(header.name) === name) {
foundDockerfile = true;
let contents = '';
entryStream.on('data', (data: string) => {
contents += data;
});
entryStream.on('end', () => {
resolve(contents);
});
} else {
entryStream.resume();
}
next();
},
);
stream.on('error', reject);
extract.on('error', reject);
extract.on('finish', () => {
if (!foundDockerfile) {
reject(new Error('Could not find a dockerfile in returned archive'));
}
});
stream.pipe(extract);
});
}
function getPromiseForEvents(
events: { [event: string]: (eventArg: Error | string) => void },
rejectOnError = true,
resolveOnEnd = false,
): [Promise<unknown>, Resolve.ResolveListeners] {
const listeners: Resolve.ResolveListeners = {};
const resolvePromise = new Promise((resolve, reject) => {
listeners['error'] = [rejectOnError ? reject : resolve];
if (resolveOnEnd) {
listeners['end'] = [resolve];
}
for (const event of Object.keys(events)) {
listeners[event] = [
(eventArg) => {
try {
events[event](eventArg);
if (resolveOnEnd) {
if (event === 'end') {
resolve({ event: eventArg });
}
} else {
resolve({ event: eventArg });
}
} catch (error) {
reject(error);
}
},
];
}
});
return [resolvePromise, listeners];
}
async function testResolveInput({
architecture = '',
deviceType = '',
dockerfileContentMatcher = (contents) => contents === 'correct',
expectedResolvedDockerfilePath,
expectedResolverName,
shouldCallHook = true,
specifiedDockerfilePath,
tarFilePath,
additionalTemplateVars,
dockerfileHook,
}: {
architecture?: string;
deviceType?: string;
dockerfileContentMatcher?: (contents: string) => boolean;
expectedResolvedDockerfilePath: string;
expectedResolverName: string;
shouldCallHook?: boolean;
specifiedDockerfilePath: string;
tarFilePath: string;
additionalTemplateVars?: { [key: string]: string };
dockerfileHook?: (content: string) => string | Promise<string>;
}) {
let content: string;
let resolvedName: string;
let resolverName: string;
const hook =
dockerfileHook ??
((hookContent) => {
if (shouldCallHook) {
content = hookContent;
} else {
throw new Error('hook should not be called');
}
});
const tarStream = fs.createReadStream(require.resolve(tarFilePath));
const bundle = new Resolve.Bundle(tarStream, deviceType, architecture, hook);
const [resolvePromise, listeners] = getPromiseForEvents(
{
resolver: (name: string) => {
resolverName = name;
},
'resolved-name': (name: string) => {
resolvedName = name;
},
},
true,
true,
);
const outputStream = Resolve.resolveInput(
bundle,
defaultResolvers(),
listeners,
specifiedDockerfilePath,
additionalTemplateVars,
);
let tarContent;
if (expectedResolvedDockerfilePath) {
tarContent = await getDockerfileFromTarStream(
outputStream,
expectedResolvedDockerfilePath,
);
} else {
outputStream.resume();
}
await resolvePromise;
expect(resolverName).to.equal(expectedResolverName);
if (expectedResolvedDockerfilePath) {
if (specifiedDockerfilePath) {
expect(resolvedName).to.equal(expectedResolvedDockerfilePath);
}
expect(dockerfileContentMatcher(tarContent.trim())).to.equal(
true,
`Bad stream contents for "${expectedResolvedDockerfilePath}": ${tarContent.trim()}`,
);
}
if (shouldCallHook && dockerfileHook == null) {
expect(dockerfileContentMatcher(content.trim())).to.equal(
true,
`Bad contents in hook call: ${content.trim()}`,
);
}
}
describe('Resolvers', () => {
it('should return resolve a standard Dockerfile project', () => {
return testResolveInput({
dockerfileContentMatcher: (contents) =>
contents === `FROM debian:jessie\n\nRUN apt-get update`,
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Standard Dockerfile',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/Dockerfile/archive.tar',
});
});
it('should resolve a Dockerfile.template correctly', () => {
const deviceType = 'device-type-test';
const arch = 'architecture-test';
return testResolveInput({
architecture: arch,
deviceType,
dockerfileContentMatcher: (contents) =>
contents === `FROM resin/${deviceType}-node:slim\nRUN echo ${arch}`,
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Dockerfile.template',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/DockerfileTemplate/archive.tar',
});
});
it('should resolve a balena Dockerfile.template correctly', () => {
const deviceType = 'device-type-test';
const arch = 'architecture-test';
return testResolveInput({
architecture: arch,
deviceType,
dockerfileContentMatcher: (contents) =>
contents === `FROM resin/${deviceType}-node:slim\nRUN echo ${arch}`,
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Dockerfile.template',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/BalenaDockerfileTemplate/archive.tar',
});
});
it('should resolve an architecture specific dockerfile', () => {
return testResolveInput({
architecture: 'i386',
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Architecture-specific Dockerfile',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/ArchitectureDockerfile/archive.tar',
});
});
it('should prioritise architecture dockerfiles over dockerfile templates', () => {
return testResolveInput({
architecture: 'i386',
dockerfileContentMatcher: (contents) => contents === 'i386',
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Architecture-specific Dockerfile',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/ArchPriority/archive.tar',
});
});
it('should prioritise device type over architecture dockerfiles', () => {
return testResolveInput({
architecture: 'armv7hf',
deviceType: 'raspberry-pi2',
dockerfileContentMatcher: (contents) => contents === 'raspberry-pi2',
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Architecture-specific Dockerfile',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/ArchPriority/archive.tar',
});
});
it('should handle incorrect template variables', () => {
const resolvers = defaultResolvers();
const stream = fs.createReadStream(
require.resolve('./test-files/IncorrectTemplateMacros/archive.tar'),
);
const bundle = new Resolve.Bundle(stream, '', '');
const [resolvePromise, listeners] = getPromiseForEvents(
{
end: () => {
throw new Error('No error thrown for incorrect template variables');
},
error: (err) => {
expect(typeof err === 'string' ? err : err.message).to.equal(
'RESIN_DEVICE_TYPE is not defined',
);
},
},
false,
);
const outStream = Resolve.resolveInput(bundle, resolvers, listeners);
outStream.resume();
return resolvePromise;
});
it('should reject a nodeJS project with no engines entry', async function () {
let errorMessage: string;
try {
await testResolveInput({
expectedResolvedDockerfilePath: undefined,
expectedResolverName: 'NodeJS',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/NoEngineNodeProject/archive.tar',
});
} catch (err) {
errorMessage = err.message;
}
expect(errorMessage).to.equal(
'package.json: engines.node must be specified',
);
});
it.skip('should resolve a nodeJS project', function () {
this.timeout(3600000);
const deviceType = 'raspberrypi3';
return testResolveInput({
deviceType,
dockerfileContentMatcher: (contents) =>
contents.startsWith(`FROM resin/${deviceType}-node:10.0.0-onbuild`),
expectedResolvedDockerfilePath: undefined,
expectedResolverName: 'NodeJS',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/NodeProject/archive.tar',
});
});
});
describe('Hooks', () => {
it('should call a hook on a resolved Dockerfile.template bundle', () => {
const arch = 'arch';
const deviceType = 'dt';
return testResolveInput({
architecture: arch,
deviceType,
dockerfileContentMatcher: (contents) =>
contents === `${deviceType}:${arch}`,
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Dockerfile.template',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/Hooks/Template/archive.tar',
});
});
it('should call a hook on a resolved Dockerfile bundle', () => {
return testResolveInput({
dockerfileContentMatcher: (contents) =>
contents === 'This is the dockerfile contents',
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Standard Dockerfile',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/Hooks/Dockerfile/archive.tar',
});
});
it('should allow a hook to change a Dockerfile in-place', () => {
return testResolveInput({
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Standard Dockerfile',
specifiedDockerfilePath: undefined,
dockerfileContentMatcher: (contents) => contents === 'Hook replacement',
dockerfileHook: () => {
console.log('The dockerfile hook is being called');
return Promise.resolve('Hook replacement');
},
tarFilePath: './test-files/Hooks/Dockerfile/archive.tar',
});
});
it('should allow a hook to change a Dockerfile.template in-place', () => {
const deviceType = 'device-type-test';
const arch = 'architecture-test';
return testResolveInput({
architecture: arch,
deviceType,
dockerfileContentMatcher: (contents) => contents === 'Hook replacement',
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Dockerfile.template',
dockerfileHook: () => Promise.resolve('Hook replacement'),
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/DockerfileTemplate/archive.tar',
});
});
});
describe('Utils', () => {
it('should correctly normalize tar entries', () => {
const fn = TarUtils.normalizeTarEntry;
expect(fn('Dockerfile')).to.equal('Dockerfile');
expect(fn('./Dockerfile')).to.equal('Dockerfile');
expect(fn('../Dockerfile')).to.equal('../Dockerfile');
expect(fn('/Dockerfile')).to.equal('Dockerfile');
expect(fn('./a/b/Dockerfile')).to.equal('a/b/Dockerfile');
});
it('should correctly remove file extensions', () => {
const fn = Utils.removeExtension;
expect(fn('Dockerfile.template')).to.equal('Dockerfile');
expect(fn('test/Dockerfile.template')).to.equal('test/Dockerfile');
expect(fn('Dockerfile')).to.equal('Dockerfile');
expect(fn('test/Dockerfile')).to.equal('test/Dockerfile');
});
});
describe('Specifying dockerfiles', () => {
it('should allow a Dockerfile to be specified in a different location', () => {
return testResolveInput({
expectedResolvedDockerfilePath: 'test/Dockerfile',
expectedResolverName: 'Standard Dockerfile',
specifiedDockerfilePath: 'test/Dockerfile',
tarFilePath: './test-files/SpecifiedDockerfile/archive.tar',
});
});
it('should allow a Dockerfile.template to be specified in a different location', () => {
return testResolveInput({
expectedResolvedDockerfilePath: 'test/Dockerfile',
expectedResolverName: 'Dockerfile.template',
specifiedDockerfilePath: 'test/Dockerfile.template',
tarFilePath: './test-files/SpecifiedDockerfileTemplate/archive.tar',
});
});
it('should allow a Dockerfile.template to have a different name', () => {
return testResolveInput({
expectedResolvedDockerfilePath: 'MyDockerfile',
expectedResolverName: 'Dockerfile.template',
specifiedDockerfilePath: 'MyDockerfile.template',
tarFilePath: './test-files/SpecifiedMyDockerfileTemplate/archive.tar',
});
});
it('should allow an arch-specific dockerfile to be specified in a different location', () => {
return testResolveInput({
architecture: 'armv7hf',
expectedResolvedDockerfilePath: 'test/Dockerfile',
expectedResolverName: 'Architecture-specific Dockerfile',
specifiedDockerfilePath: 'test/Dockerfile.armv7hf',
tarFilePath: './test-files/SpecifiedArchDockerfile/archive.tar',
});
});
it('should allow an arch-specific dockerfile to have a different name', () => {
return testResolveInput({
architecture: 'armv7hf',
expectedResolvedDockerfilePath: 'MyDockerfile',
expectedResolverName: 'Architecture-specific Dockerfile',
specifiedDockerfilePath: 'MyDockerfile.armv7hf',
tarFilePath: './test-files/SpecifiedArchMyDockerfile/archive.tar',
});
});
it('should allow a Dockerfile to be specified in a different location', () => {
return testResolveInput({
expectedResolvedDockerfilePath: 'random',
expectedResolverName: 'Standard Dockerfile',
specifiedDockerfilePath: 'random',
tarFilePath: './test-files/SpecifiedRandomFile/archive.tar',
});
});
it('should detect the right Dockerfile when there are many', () => {
return testResolveInput({
expectedResolvedDockerfilePath: 'Dockerfile',
expectedResolverName: 'Standard Dockerfile',
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/SpecifiedDockerfile/correct-dockerfile.tar',
});
});
it('should emit an error if a specified Dockerfile cannot be found', async () => {
let errorMessage: string;
try {
await testResolveInput({
expectedResolvedDockerfilePath: undefined,
expectedResolverName: undefined,
specifiedDockerfilePath: 'InexistentDockerfile',
tarFilePath: './test-files/SpecifiedArchMyDockerfile/archive.tar',
});
} catch (err) {
errorMessage = err.message;
}
expect(errorMessage).to.equal(
'Specified file not found or is invalid: InexistentDockerfile',
);
});
it('should emit an error if an unspecified Dockerfile cannot be found', async () => {
let errorMessage: string;
try {
await testResolveInput({
expectedResolvedDockerfilePath: undefined,
expectedResolverName: undefined,
shouldCallHook: false,
specifiedDockerfilePath: undefined,
tarFilePath: './test-files/MissingDockerfile/archive.tar',
});
} catch (err) {
errorMessage = err.message;
}
expect(errorMessage).to.equal(
'Could not find a Dockerfile for this service',
);
});
});
describe('Additional template variables', () => {
it('Should allow providing extra template variables', () => {
return testResolveInput({
expectedResolvedDockerfilePath: undefined,
specifiedDockerfilePath: undefined,
dockerfileContentMatcher: (content) => {
return content === `test1\ntest2`;
},
expectedResolverName: 'Dockerfile.template',
shouldCallHook: true,
tarFilePath: './test-files/AdditionalTemplateVars/archive.tar',
additionalTemplateVars: {
TEST_VAR: 'test1',
SECOND_TEST_VAR: 'test2',
},
});
});
it('should allow extra template variables in arch/dt specific dockerfiles', () => {
return testResolveInput({
architecture: 'armv7hf',
deviceType: '',
expectedResolvedDockerfilePath: undefined,
specifiedDockerfilePath: undefined,
dockerfileContentMatcher: (content) => {
return content === `test1\ntest2`;
},
expectedResolverName: 'Architecture-specific Dockerfile',
shouldCallHook: true,
tarFilePath: './test-files/AdditionalTemplateVars/arch-template.tar',
additionalTemplateVars: {
TEST_VAR: 'test1',
SECOND_TEST_VAR: 'test2',
},
});
});
});