This repository was archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcontainer_spec.ts
558 lines (410 loc) · 15.4 KB
/
container_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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// Copyright (c) 2018 Steven Enten. All rights reserved. Licensed under the MIT license.
import { ChildProcess } from 'child_process';
import * as path from 'path';
import debug = require('debug');
import exitHook = require('exit-hook');
import Watchpack = require('watchpack');
import {
ContainerArgs,
ContainerConfig,
ContainerRuntime,
UDKC_PROCESS_TITLE,
default as Container,
} from '../../../lib/util/container';
jest.mock('child_process', () => ({
fork: jest.fn(() => ({
on: jest.fn(),
once: jest.fn(),
})),
}));
jest.mock('exit-hook');
jest.mock('watchpack');
afterAll(() => {
jest.unmock('child_process');
jest.unmock('exit-hook');
jest.unmock('watchpack');
});
const ContainerRuntimePath = require.resolve('../../../lib/util/container');
describe('udk/lib/util/container', () => { // tslint:disable-line:no-big-function
beforeEach(() => {
jest.clearAllMocks();
});
describe('Container', () => {
it('should be an instance of ContainerRuntime', () => {
const c = Container({
argv: [],
cwd: process.cwd,
} as {} as NodeJS.Process);
expect(c).toBeInstanceOf(ContainerRuntime);
});
});
describe('ContainerRuntime', () => { // tslint:disable-line:no-big-function
describe('static bootstrapFork', () => {
it('should create and bootstrap', async () => {
const c = await ContainerRuntime.bootstrapFork({
argv: [ ContainerRuntimePath ],
cwd: process.cwd,
env: {},
send: () => {},
} as {} as NodeJS.Process);
expect(c).toBeInstanceOf(ContainerRuntime);
});
});
describe('constructor', () => {
it('should enable debug udk:* when debug flag is set', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: process.cwd,
argv: [ 'node', 'test', '--debug' ],
} as NodeJS.Process);
expect(c).toBeTruthy();
expect(debug.enabled('udk:*')).toBeTruthy();
c.close = jest.fn();
(exitHook as jest.Mock).mock.calls[0][0]();
expect(exitHook).toBeCalled();
expect(c.close).toBeCalled();
});
it('should handle preload modules', () => {
const preloadPath = require.resolve('../../../package');
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: process.cwd,
argv: [ 'node', 'test', '--require', preloadPath, '--require', preloadPath ],
} as NodeJS.Process);
expect(c).toBeTruthy();
expect(() => {
return new ContainerRuntime(ContainerRuntimePath, {
cwd: process.cwd,
argv: [ 'node', 'test', '--require', preloadPath + 'x' ],
} as NodeJS.Process);
}).toThrowError('x');
});
});
describe('bootstrap', () => {
it('should throw when is called in main process', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.isChild = false;
c.bootstrap().then(fail).catch(err =>
expect(err.message).toEqual('bootstrap() cannot be called in main process'));
});
it('should set process title', async () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.isChild = true;
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
// tslint:disable-next-line:no-any
c.loadConfig = () => ({ config: { processTitle: 'foo' } }) as any;
await c.bootstrap();
expect(c.proc.title).toEqual('foo');
// tslint:disable-next-line:no-any
c.loadConfig = () => ({ configPath: 'fake', config: {} }) as any;
await c.bootstrap();
expect(c.proc.title).toEqual(UDKC_PROCESS_TITLE);
});
it('should call config.bootstrap if exists', async () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.isChild = true;
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
const bootstrap = jest.fn();
// tslint:disable-next-line:no-any
c.loadConfig = () => ({ config: { bootstrap } }) as any;
await c.bootstrap();
expect(bootstrap).toBeCalled();
});
it('should call shutDown on exit', async () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.isChild = true;
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
// tslint:disable-next-line:no-any
c.loadConfig = () => ({ config: {} }) as any;
await c.bootstrap();
expect(exitHook).toBeCalled();
expect((exitHook as jest.Mock).mock.calls.length).toEqual(2);
expect(typeof (exitHook as jest.Mock).mock.calls[1][0]).toEqual('function');
const shutDown = jest.fn();
c.onShutDown = shutDown;
(exitHook as jest.Mock).mock.calls[1][0]();
expect(shutDown).toBeCalled();
});
});
describe('close', () => {
it('should send close action to parent in child process', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.isChild = true;
c.close();
c.proc.send = jest.fn();
c.close();
expect(c.proc.send).toBeCalledWith({ action: 'close' });
});
it('should close watcher', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const closeWatcher = jest.fn();
c.watcher = { close: closeWatcher } as any; // tslint:disable-line:no-any
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.close();
expect(c.watcher).toBeUndefined();
expect(closeWatcher).toBeCalled();
});
it('should close child', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.child = { pid: NaN } as ChildProcess;
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.close();
expect(c.child).toBeUndefined();
});
it('should handle callback', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const callback = jest.fn();
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.close(callback);
expect(callback).toBeCalled();
});
});
describe('run', () => {
it('should send run action to parent in child process', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
c.isChild = true;
c.run();
c.proc.send = jest.fn();
c.run();
expect(c.proc.send).toBeCalledWith({ action: 'run' });
});
it('should parent process listen to child process messages', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => path.resolve(__dirname, '..', '..', 'e2e'),
argv: [],
} as {} as NodeJS.Process);
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.run();
expect(c.child).toBeDefined();
const childOn = (c.child as {} as ChildProcess).on as jest.Mock;
expect(childOn).toBeCalled();
expect(childOn.mock.calls[0][0]).toBe('message');
const onMessage = childOn.mock.calls[0][1];
expect(typeof onMessage).toBe('function');
c.close = jest.fn();
c.run = jest.fn();
onMessage({ action: 'close' });
onMessage({ action: 'run' });
onMessage({ action: 'unknown' });
expect(c.close).toBeCalled();
expect(c.run).toBeCalled();
});
it('should create watcher and child', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => path.resolve(__dirname, '..', '..', 'e2e'),
argv: [],
} as {} as NodeJS.Process);
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.run();
expect(c.child).toBeDefined();
expect(c.watcher as Watchpack).toBeDefined();
expect(((c.watcher as Watchpack).watch as jest.Mock)).toBeCalled();
});
it('should remove child reference when exit or error', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => path.resolve(__dirname, '..', '..', 'e2e'),
argv: [],
} as {} as NodeJS.Process);
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.run();
expect(c.child).toBeDefined();
if (c.child) {
const childOnce = c.child.once as jest.Mock;
expect(childOnce).toBeCalled();
c.child = {} as ChildProcess;
expect(childOnce.mock.calls[0][0]).toEqual('exit');
expect(typeof childOnce.mock.calls[0][1]).toEqual('function');
expect(() => childOnce.mock.calls[0][1]()).not.toThrowError();
expect(c.child).toBeUndefined();
c.child = {} as ChildProcess;
expect(childOnce.mock.calls[1][0]).toEqual('error');
expect(typeof childOnce.mock.calls[1][1]).toEqual('function');
expect(() => childOnce.mock.calls[1][1]( new Error('fake error'))).toThrow('fake error');
expect(c.child).toBeUndefined();
}
});
it('should re-run when metafile change', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => __dirname,
argv: [],
} as {} as NodeJS.Process);
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
c.run();
const watcher = c.watcher as Watchpack;
let watchOnce = watcher.once as jest.Mock;
expect(watchOnce).toBeCalled();
expect(watchOnce.mock.calls.length).toEqual(1);
expect(watchOnce.mock.calls[0][0]).toEqual('aggregated');
expect(typeof watchOnce.mock.calls[0][1]).toEqual('function');
const change = watchOnce.mock.calls[0][1];
const watch = jest.fn();
const watchPause = jest.fn();
watchOnce = jest.fn();
c.watcher = {
options: {},
watcherOptions: {},
pause: watchPause,
watch,
once: watchOnce,
} as any; // tslint:disable-line:no-any
c.loadConfig = () => ({
config: {
context: '/',
metafiles: [ 'foo' ],
metadirs: [ 'bar' ],
watchOptions: { ignored: true, poll: true },
},
}) as any; // tslint:disable-line:no-any
change.call(c);
expect(watch).toBeCalled();
expect(watch.mock.calls[0][0]).toEqual([ path.resolve('/foo') ]);
expect(watch.mock.calls[0][1]).toEqual([ path.resolve('/bar') ]);
expect(watchOnce).toBeCalled();
expect(watchPause).toBeCalled();
expect(watchOnce).toBeCalled();
expect(watchPause).toBeCalled();
});
it('should handle callback', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => __dirname,
argv: [],
} as {} as NodeJS.Process);
(c as any).debug = jest.fn(); // tslint:disable-line:no-any
let callback = jest.fn();
c.run(callback);
expect(callback).toBeCalled();
callback = jest.fn();
c.run(callback);
expect(callback).toBeCalled();
});
});
describe('loadConfig', () => {
it('should load config given in cli', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const cwd = path.resolve(__dirname, '..', '..', 'e2e');
const configPath = path.resolve(cwd, 'udk.container.ts');
expect(c.loadConfig({
cwd,
config: 'udk.container',
} as ContainerArgs).configPath).toEqual(configPath);
});
it('should try to load default config name from cwd', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const cwd = path.resolve(__dirname, '..', '..', 'e2e');
const configPath = path.resolve(cwd, 'udk.container.ts');
const loading = c.loadConfig({ cwd } as ContainerArgs);
expect(loading.configPath).toEqual(configPath);
expect(loading.config.context).toEqual(cwd);
});
it('should return config with context only when there is no config file', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const loading = c.loadConfig({ cwd: __dirname } as ContainerArgs);
expect(loading.configPath).toBeUndefined();
expect(loading.config).toEqual({ context: __dirname });
});
});
describe('parseProcessArgsOptions', () => {
it('should return yargs options', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
expect(c.parseProcessArgsOptions()).toEqual({
alias: {
config: 'c',
cwd: 'C',
debug: 'D',
require: 'r',
},
array: [ 'require' ],
boolean: [ 'debug' ],
envPrefix: 'UDK',
default: {
cwd: '',
debug: false,
require: [],
},
string: [ 'config', 'cwd', 'require' ],
});
});
});
describe('prepareConfig', () => {
it('should define config.logger if not exists', async () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const config = {} as ContainerConfig;
await c.prepareConfig(config);
expect(config.logger).toBe(c.logger);
c.logger = {} as Console;
await c.prepareConfig(config);
expect(config.logger).not.toBe(c.logger);
});
});
describe('shutDown', () => {
it('should call config.onDown if exists', () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const config = {} as ContainerConfig;
const onDown = jest.fn();
c.onShutDown(config);
expect(onDown).not.toBeCalled();
config.onDown = onDown;
c.onShutDown(config);
expect(onDown).toBeCalledWith(c);
});
});
describe('shutUp', () => {
it('should call config.onUp if exists', async () => {
const c = new ContainerRuntime(ContainerRuntimePath, {
cwd: () => '',
argv: [],
} as {} as NodeJS.Process);
const config = {} as ContainerConfig;
const onUp = jest.fn();
await c.onShutUp(config);
expect(onUp).not.toBeCalled();
config.onUp = onUp;
await c.onShutUp(config);
expect(onUp).toBeCalledWith(c);
});
});
});
});