-
Notifications
You must be signed in to change notification settings - Fork 603
/
worker-test.js
500 lines (441 loc) Β· 11.7 KB
/
worker-test.js
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
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @emails oncall+metro_bundler
* @format
*/
'use strict';
jest
.mock('console')
.mock('fs', () => new (require('metro-memory-fs'))())
.mock('temp', () => ({
path() {
return '/tmp/repro.args';
},
}))
.useRealTimers();
const JSONStream = require('JSONStream');
const buckWorker = require('../worker-tool');
const path = require('path');
const mkdirp = require('mkdirp');
// mocked
const {Console} = require('console');
const fs = require('fs');
const {any, anything} = expect;
const UNKNOWN_MESSAGE = 1;
const INVALID_MESSAGE = 2;
describe('Buck worker:', () => {
let commands, inStream, worker, written;
beforeEach(() => {
commands = {};
worker = buckWorker(commands);
inStream = JSONStream.stringify();
inStream.pipe(worker);
written = [];
worker.on('data', chunk => written.push(chunk));
});
describe('handshake:', () => {
it('responds to a correct handshake', () => {
inStream.write(handshake());
return end().then(data => expect(data).toEqual([handshake()]));
});
it('responds to a handshake with a `protocol_version` different from "0"', () => {
inStream.write({
id: 0,
type: 'handshake',
protocol_version: '2',
capabilities: [],
});
return end().then(responses =>
expect(responses).toEqual([
{
id: 0,
type: 'error',
exit_code: INVALID_MESSAGE,
},
]),
);
});
it('errors for a second handshake', () => {
inStream.write(handshake());
inStream.write(handshake(1));
return end().then(([, response]) =>
expect(response).toEqual({
id: 1,
type: 'error',
exit_code: UNKNOWN_MESSAGE,
}),
);
});
});
it('errors for unknown message types', () => {
inStream.write(handshake());
inStream.write({id: 1, type: 'arbitrary'});
return end().then(([, response]) =>
expect(response).toEqual({
id: 1,
type: 'error',
exit_code: UNKNOWN_MESSAGE,
}),
);
});
describe('commands:', () => {
let createWriteStreamImpl, openedStreams;
function mockFiles(files) {
writeFiles(files, '/');
}
function writeFiles(files, dirPath) {
for (const key in files) {
const entry = files[key];
if (entry == null || typeof entry === 'string') {
fs.writeFileSync(path.join(dirPath, key), entry || '');
} else {
const subDirPath = path.join(dirPath, key);
mkdirp.sync(subDirPath);
writeFiles(entry, subDirPath);
}
}
}
beforeAll(() => {
createWriteStreamImpl = fs.createWriteStream;
fs.createWriteStream = (...args) => {
const writeStream = createWriteStreamImpl(...args);
++openedStreams;
writeStream.on('finish', () => --openedStreams);
return writeStream;
};
});
afterAll(() => {
fs.createWriteStream = createWriteStreamImpl;
});
beforeEach(() => {
fs.reset();
openedStreams = 0;
mockFiles({
arbitrary: {
args: '',
stdout: '',
stderr: '',
},
// When an error happens, the worker writes a repro file to the
// temporary folder.
tmp: {},
});
inStream.write(handshake());
});
afterEach(function assertThatAllWriteStreamsWereClosed() {
expect(openedStreams).toBe(0);
});
it('errors if `args_path` cannot be opened', () => {
mockFiles({some: {'args-path': undefined}});
inStream.write(command({id: 5, args_path: '/some/args-path'}));
return end(2).then(([, response]) => {
expect(response).toEqual({
id: 5,
type: 'error',
exit_code: INVALID_MESSAGE,
});
});
});
it('errors if `stdout_path` cannot be opened', () => {
const path = '/does/not/exist';
inStream.write(command({id: 5, stdout_path: path}));
return end(2).then(([, response]) => {
expect(response).toEqual({
id: 5,
type: 'error',
exit_code: INVALID_MESSAGE,
});
});
});
it('errors if `stderr_path` cannot be opened', () => {
const path = '/does/not/exist';
inStream.write(command({id: 5, stderr_path: path}));
return end(2).then(([, response]) => {
expect(response).toEqual({
id: 5,
type: 'error',
exit_code: INVALID_MESSAGE,
});
});
});
it('errors for unspecified commands', () => {
mockFiles({
arbitrary: {
file: '--flag-without-preceding-command',
},
});
inStream.write(
command({
id: 1,
args_path: '/arbitrary/file',
}),
);
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 1,
type: 'error',
exit_code: INVALID_MESSAGE,
}),
);
});
it('errors for empty commands', () => {
mockFiles({
arbitrary: {
file: '',
},
});
inStream.write(
command({
id: 2,
args_path: '/arbitrary/file',
}),
);
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 2,
type: 'error',
exit_code: INVALID_MESSAGE,
}),
);
});
it('errors for unknown commands', () => {
mockFiles({
arbitrary: {
file: 'arbitrary',
},
});
inStream.write(
command({
id: 3,
args_path: '/arbitrary/file',
}),
);
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 3,
type: 'error',
exit_code: INVALID_MESSAGE,
}),
);
});
it('errors if no `args_path` is specified', () => {
inStream.write({
id: 1,
type: 'command',
stdout_path: '/arbitrary',
stderr_path: '/arbitrary',
});
return end().then(([, response]) =>
expect(response).toEqual({
id: 1,
type: 'error',
exit_code: INVALID_MESSAGE,
}),
);
});
it('errors if no `stdout_path` is specified', () => {
inStream.write({
id: 1,
type: 'command',
args_path: '/arbitrary',
stderr_path: '/arbitrary',
});
return end().then(([, response]) =>
expect(response).toEqual({
id: 1,
type: 'error',
exit_code: INVALID_MESSAGE,
}),
);
});
it('errors if no `stderr_path` is specified', () => {
inStream.write({
id: 1,
type: 'command',
args_path: '/arbitrary',
stdout_path: '/arbitrary',
});
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 1,
type: 'error',
exit_code: INVALID_MESSAGE,
}),
);
});
it('passes arguments to an existing command', () => {
commands.transform = jest.fn();
const args = 'foo bar baz\tmore';
mockFiles({
arbitrary: {
file: 'transform ' + args,
},
});
inStream.write(
command({
args_path: '/arbitrary/file',
}),
);
return end(1).then(() =>
expect(commands.transform).toBeCalledWith(
args.split(/\s+/),
null,
anything(),
),
);
});
it('passes JSON/structured arguments to an existing command', async () => {
commands.transform = jest.fn();
const args = {foo: 'bar', baz: 'glo'};
mockFiles({
arbitrary: {
file: JSON.stringify({...args, command: 'transform'}),
},
});
inStream.write(
command({
args_path: '/arbitrary/file',
}),
);
await end(1);
expect(commands.transform).toBeCalledWith([], args, anything());
});
it('passes a console object to the command', () => {
mockFiles({
args: 'transform',
stdio: {},
});
commands.transform = jest.fn();
inStream.write(
command({
args_path: '/args',
stdout_path: '/stdio/out',
stderr_path: '/stdio/err',
}),
);
return end().then(() => {
const streams = last(Console.mock.calls);
expect(streams[0].path).toEqual('/stdio/out');
expect(streams[1].path).toEqual('/stdio/err');
expect(commands.transform).toBeCalledWith(
anything(),
null,
any(Console),
);
});
});
it('responds with success if the command finishes succesfully', () => {
commands.transform = (args, _) => {};
mockFiles({path: {to: {args: 'transform'}}});
inStream.write(
command({
id: 123,
args_path: '/path/to/args',
}),
);
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 123,
type: 'result',
exit_code: 0,
}),
);
});
it('responds with error if the command does not exist', async () => {
commands.transform = jest.fn(() => Promise.resolve());
mockFiles({path: {to: {args: 'inexistent_command'}}});
inStream.write(
command({
id: 123,
args_path: '/path/to/args',
}),
);
const [, response] = await end(2);
expect(response).toEqual({
id: 123,
type: 'error',
exit_code: 2,
});
expect(fs.readFileSync('/arbitrary/stderr', 'utf8')).toEqual(
'This worker does not have a command named `inexistent_command`. Available commands are: transform',
);
});
it('responds with error if the command errors asynchronously', () => {
commands.transform = jest.fn((args, _, callback) =>
Promise.reject(new Error('arbitrary')),
);
mockFiles({path: {to: {args: 'transform'}}});
inStream.write(
command({
id: 123,
args_path: '/path/to/args',
}),
);
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 123,
type: 'error',
exit_code: 3,
}),
);
});
it('responds with error if the command throws synchronously', () => {
commands.transform = (args, _) => {
throw new Error('arbitrary');
};
mockFiles({path: {to: {args: 'transform'}}});
inStream.write(
command({
id: 456,
args_path: '/path/to/args',
}),
);
return end(2).then(([, response]) =>
expect(response).toEqual({
id: 456,
type: 'error',
exit_code: 3,
}),
);
});
});
function end(afterMessages) {
return new Promise((resolve, reject) => {
worker.once('error', reject).once('end', () => resolve(written.join('')));
if (afterMessages == null || written.length >= afterMessages) {
inStream.end();
} else {
worker.on('data', () => {
if (written.length === afterMessages) {
inStream.end();
}
});
}
}).then(JSON.parse);
}
});
function command(overrides) {
return {
id: 4, // chosen by fair dice roll
type: 'command',
args_path: '/arbitrary/args',
stdout_path: '/arbitrary/stdout',
stderr_path: '/arbitrary/stderr',
...overrides,
};
}
function handshake(id = 0) {
return {
id,
type: 'handshake',
protocol_version: '0',
capabilities: [],
};
}
function last(arrayLike) {
return arrayLike[arrayLike.length - 1];
}