-
Notifications
You must be signed in to change notification settings - Fork 116
/
test.transport.shell.js
429 lines (313 loc) · 12.5 KB
/
test.transport.shell.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
var expect = require('chai').expect
, proxyquire = require('proxyquire')
, sinon = require('sinon')
, runWithinFiber = require('./utils/run-within-fiber')
, childProcess = require('child_process')
, errors = require('../lib/errors')
, fixtures = require('./fixtures')
, extend = require('util')._extend;
describe('transport/shell', function() {
var LOGGER_STUB = {
command: sinon.stub(),
success: sinon.stub(),
warn: sinon.stub(),
error: sinon.stub(),
stdout: sinon.stub(),
stdwarn: sinon.stub(),
stderr: sinon.stub()
};
var CHILD_PROCESS_SPY = sinon.spy(childProcess, 'exec');
var WRITE_TEMP_FILE_STUB = sinon.stub().returns('/path/to/tmp/file');
var Transport = proxyquire('../lib/transport', {
'../logger': function() { return LOGGER_STUB; },
'child_process': { exec: CHILD_PROCESS_SPY }
});
var MOCKS = {
'./index': Transport,
'../utils': {
writeTempFile: WRITE_TEMP_FILE_STUB
},
'fs': {
unlink: function() {}
}
};
var CONTEXT = {
options: {},
hosts: fixtures.HOSTS,
remote: { host: 'localhost' }
};
var Shell;
beforeEach(function() {
Shell = proxyquire('../lib/transport/shell', MOCKS);
});
afterEach(function() {
Object.keys(LOGGER_STUB).forEach(function(k) {
LOGGER_STUB[k].reset();
});
CHILD_PROCESS_SPY.reset();
WRITE_TEMP_FILE_STUB.reset();
});
describe('initialize', function() {
it('should inherit from Transport', function() {
var shell = new Shell(CONTEXT);
expect(shell).to.be.instanceof(Transport);
});
it('should call the super constructor', function() {
var SUPER_CALL_STUB = sinon.stub(Shell.super_, 'call');
var shell = new Shell(CONTEXT);
expect(SUPER_CALL_STUB.calledOnce).to.be.true;
expect(SUPER_CALL_STUB.lastCall.args).to.deep.equal([
shell,
CONTEXT
]);
SUPER_CALL_STUB.restore();
});
});
describe('#_exec()', function() {
it('should execute a command', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
var result = shell._exec('echo "hello world"');
expect(result).to.deep.equal({
code: 0,
stdout: 'hello world\n',
stderr: null
});
expect(LOGGER_STUB.command.calledOnce).to.be.true;
expect(LOGGER_STUB.command.lastCall.args[0]).to.contain('echo "hello world"');
expect(LOGGER_STUB.stdout.lastCall.args[0]).to.contain('hello world');
expect(LOGGER_STUB.success.lastCall.args[0]).to.contain('ok');
testDone();
});
});
it('should correctly merge options with transport options', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.silent();
shell._exec('echo "hello world"');
expect(LOGGER_STUB.stdout.notCalled).to.be.true;
shell._exec('echo "hello world"', { silent: false });
expect(LOGGER_STUB.stdout.notCalled).to.be.false;
testDone();
});
});
it('should correctly handle the silent option', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
var result = shell._exec('echo "silent world"', { silent: true });
expect(result).to.deep.equal({
code: 0,
stdout: 'silent world\n',
stderr: null
});
expect(LOGGER_STUB.command.calledOnce).to.be.true;
expect(LOGGER_STUB.command.lastCall.args[0]).to.contain('echo "silent world"');
expect(LOGGER_STUB.stdout.notCalled).to.be.true;
expect(LOGGER_STUB.success.lastCall.args[0]).to.contain('ok');
testDone();
});
});
it('should correctly handle the failsafe option', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
var result = shell._exec('invalid-command', { failsafe: true });
expect(result).to.have.property('code', 127);
expect(result).to.have.property('stdout', null);
expect(result).to.have.property('stderr').that.contains('not found');
expect(LOGGER_STUB.command.calledOnce).to.be.true;
expect(LOGGER_STUB.command.lastCall.args[0]).to.contain('invalid-command');
expect(LOGGER_STUB.stdwarn.calledOnce).to.be.true;
expect(LOGGER_STUB.stdwarn.lastCall.args[0]).to.contain('not found');
expect(LOGGER_STUB.warn.lastCall.args[0]).to.contain('failed safely');
testDone();
});
});
it('should throw and stop when a command fails', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
expect(function() {
shell._exec('invalid-command');
shell._exec('never-called');
}).to.throw(errors.CommandExitedAbnormallyError, 'exited abnormally');
expect(LOGGER_STUB.command.calledOnce).to.be.true;
expect(LOGGER_STUB.command.lastCall.args[0]).to.contain('invalid-command');
expect(LOGGER_STUB.stderr.calledOnce).to.be.true;
expect(LOGGER_STUB.stderr.lastCall.args[0]).to.contain('not found');
expect(LOGGER_STUB.error.lastCall.args[0]).to.contain('failed');
testDone();
});
});
it('should handle `exec` options', function(testDone) {
var EXEC_OPTIONS = { maxBuffer: 1337, 'some-option': 'some-value' };
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell._exec('echo "test"', { exec: EXEC_OPTIONS });
expect(CHILD_PROCESS_SPY.calledOnce).to.be.true;
expect(CHILD_PROCESS_SPY.lastCall.args[1]).to.deep.equal(EXEC_OPTIONS);
testDone();
});
});
it('should use a sane default `maxBuffer` size', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell._exec('echo "test"');
expect(CHILD_PROCESS_SPY.calledOnce).to.be.true;
expect(CHILD_PROCESS_SPY.lastCall.args[1].maxBuffer).to.equal(1000 * 1024);
testDone();
});
});
});
describe('#transfer()', function() {
var EXEC_STUB;
beforeEach(function() {
EXEC_STUB = sinon.stub(Shell.prototype, '_exec').returns('test');
});
afterEach(function() {
EXEC_STUB.restore();
});
it('should upload a single file', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer('/path/to/file', '/remote/path');
expect(WRITE_TEMP_FILE_STUB.calledOnce).to.be.true;
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0]).to.contain('/path/to/file');
expect(EXEC_STUB.lastCall.args[0]).to.contain('--files-from /path/to/tmp/file');
testDone();
});
});
it('should upload an array of files', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer(['/path/to/file', '/path/to/another/file'], '/remote/path');
expect(WRITE_TEMP_FILE_STUB.calledOnce).to.be.true;
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0]).to.contain('/path/to/file');
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0]).to.contain('/path/to/another/file');
expect(EXEC_STUB.lastCall.args[0]).to.contain('--files-from /path/to/tmp/file');
testDone();
});
});
it('should upload an array of files', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer(['/path/to/file', '/path/to/another/file'], '/remote/path');
expect(WRITE_TEMP_FILE_STUB.calledOnce).to.be.true;
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0])
.to.contain('/path/to/file\n/path/to/another/file');
expect(EXEC_STUB.lastCall.args[0]).to.contain('--files-from /path/to/tmp/file');
testDone();
});
});
it('should upload a zero-delimited list of files', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer(['/path/to/file\0/path/to/another/file'], '/remote/path');
expect(WRITE_TEMP_FILE_STUB.calledOnce).to.be.true;
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0])
.to.contain('/path/to/file\n/path/to/another/file');
expect(EXEC_STUB.lastCall.args[0]).to.contain('--files-from /path/to/tmp/file');
testDone();
});
});
it('should upload a newline-delimited list of files', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer(['/path/to/file\n/path/to/another/file'], '/remote/path');
expect(WRITE_TEMP_FILE_STUB.calledOnce).to.be.true;
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0])
.to.contain('/path/to/file\n/path/to/another/file');
expect(EXEC_STUB.lastCall.args[0]).to.contain('--files-from /path/to/tmp/file');
testDone();
});
});
it('should upload files from the result of a command', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer({ stdout: '/path/to/file\n/path/to/another/file' }, '/remote/path');
expect(WRITE_TEMP_FILE_STUB.calledOnce).to.be.true;
expect(WRITE_TEMP_FILE_STUB.lastCall.args[0])
.to.contain('/path/to/file\n/path/to/another/file');
expect(EXEC_STUB.lastCall.args[0]).to.contain('--files-from /path/to/tmp/file');
testDone();
});
});
it('should throw when passing an empty file list', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
expect(function() {
shell.transfer('\n', '/remote/path');
}).to.throw(errors.InvalidArgumentError, 'Empty file list');
expect(function() {
shell.transfer([], '/remote/path');
}).to.throw(errors.InvalidArgumentError, 'Empty file list');
expect(function() {
shell.transfer({}, '/remote/path');
}).to.throw(errors.InvalidArgumentError, 'Invalid object');
testDone();
});
});
it('should throw when remote path is empty', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
expect(function() {
shell.transfer('/path/to/file');
}).to.throw(errors.InvalidArgumentError, 'Missing remote path');
testDone();
});
});
it('should call rsync with the correct flags', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer('/path/to/file', '/remote/path');
expect(EXEC_STUB.calledTwice).to.be.true;
expect(EXEC_STUB.firstCall.args[0]).to.contain(' -az ');
var CONTEXT_WITH_DEBUG = extend(CONTEXT, { options: { debug: true }});
shell = new Shell(CONTEXT_WITH_DEBUG);
shell.transfer('/path/to/file', '/remote/path');
expect(EXEC_STUB.lastCall.args[0]).to.contain(' -azvv ');
testDone();
});
});
it('should use the `username` property of the remote if specified', function(testDone) {
runWithinFiber(function() {
var CONTEXT_WITH_USERNAME = {
options: {},
remote: { host: 'localhost' },
hosts: [{
host: 'example.org',
username: 'remote-user'
}]
};
var shell = new Shell(CONTEXT_WITH_USERNAME);
shell.transfer('/path/to/file', '/remote/path');
expect(EXEC_STUB.lastCall.args[0]).to.contain(' remote-user@example.org:');
testDone();
});
});
it('should use the path to a private key', function(testDone) {
runWithinFiber(function() {
var CONTEXT_WITH_PRIVATE_KEY = {
options: {},
remote: { host: 'localhost' },
hosts: [{
host: 'example.org',
privateKey: fixtures.PRIVATE_KEY_PATH
}]
};
var shell = new Shell(CONTEXT_WITH_PRIVATE_KEY);
shell.transfer('/path/to/file', '/remote/path');
expect(EXEC_STUB.lastCall.args[0]).to.contain('-i ' + fixtures.PRIVATE_KEY_PATH);
testDone();
});
});
it('should upload files to multiple remote hosts', function(testDone) {
runWithinFiber(function() {
var shell = new Shell(CONTEXT);
shell.transfer('/path/to/file', '/remote/path');
expect(EXEC_STUB.calledTwice).to.be.true;
expect(EXEC_STUB.firstCall.args[0]).to.match(/-p22.*\.\/.*example\.com:\/remote\/path/);
expect(EXEC_STUB.secondCall.args[0]).to.match(/-p22022.*\.\/.*example\.org:\/remote\/path/);
testDone();
});
});
});
});