-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathlocal.js
More file actions
465 lines (402 loc) · 16.3 KB
/
local.js
File metadata and controls
465 lines (402 loc) · 16.3 KB
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
var expect = require('expect.js'),
sinon = require('sinon'),
mocks = require('mocks'),
path = require('path'),
fs = require('fs'),
rimraf = require('rimraf'),
Proxy = require('proxy'),
tempfs = require('temp-fs'),
browserstack = require('../index'),
LocalBinary = require('../lib/LocalBinary');
const MAX_TIMEOUT = 600000;
describe('Local', function () {
var bsLocal;
beforeEach(function () {
bsLocal = new browserstack.Local();
});
it('should have pid when running', function (done) {
this.timeout(600000);
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(){
expect(bsLocal.tunnel.pid).to.not.equal(0);
done();
});
});
it('should return is running properly', function (done) {
this.timeout(60000);
expect(bsLocal.isRunning()).to.not.equal(true);
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(){
expect(bsLocal.isRunning()).to.equal(true);
done();
});
});
it.skip('should throw error on running multiple binary', function (done) {
this.timeout(60000);
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY }, function(error){
bsLocal_2 = new browserstack.Local();
var tempLogPath = path.join(process.cwd(), 'log2.log');
bsLocal_2.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath }, function(error){
expect(error.toString().trim()).to.equal('LocalError: Either another browserstack local client is running on your machine or some server is listening on port 45690');
fs.unlinkSync(tempLogPath);
done();
});
});
});
it('should enable verbose', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'verbose': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--verbose')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('1')).to.not.equal(-1);
done();
});
});
it('should enable verbose with log level', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'verbose': 2 }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--verbose')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('2')).to.not.equal(-1);
done();
});
});
it('should enable verbose with log level string', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'verbose': '2' }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--verbose')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('2')).to.not.equal(-1);
done();
});
});
it('should set folder testing', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'f': '/var/html' }, function(){
expect(bsLocal.getBinaryArgs().indexOf('-f')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('/var/html')).to.not.equal(-1);
done();
});
});
it('should set folder testing with folder option', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'folder': '/var/html' }, function(){
expect(bsLocal.getBinaryArgs().indexOf('-f')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('/var/html')).to.not.equal(-1);
done();
});
});
it('should enable force', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'force': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--force')).to.not.equal(-1);
done();
});
});
it('should enable only', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'only': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--only')).to.not.equal(-1);
done();
});
});
it('should enable onlyAutomate', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'onlyAutomate': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--only-automate')).to.not.equal(-1);
done();
});
});
it('should enable forcelocal', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'forcelocal': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--force-local')).to.not.equal(-1);
done();
});
});
it('should enable forcelocal with camel case', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'forceLocal': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--force-local')).to.not.equal(-1);
done();
});
});
it('should enable custom boolean args', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'boolArg1': true, 'boolArg2': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--boolArg1')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('--boolArg2')).to.not.equal(-1);
done();
});
});
it('should enable custom keyval args', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'customKey1': 'custom value1', 'customKey2': 'custom value2' }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--customKey1')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('custom value1')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('--customKey2')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('custom value2')).to.not.equal(-1);
done();
});
});
it('should enable forceproxy', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'forceproxy': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--force-proxy')).to.not.equal(-1);
done();
});
});
it('should enable forceproxy with camel case', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'forceProxy': true }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--force-proxy')).to.not.equal(-1);
done();
});
});
it('should set localIdentifier', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'localIdentifier': 'abcdef' }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--local-identifier')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('abcdef')).to.not.equal(-1);
done();
});
});
it('should set parallelRuns', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'parallelRuns': '10' }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--parallel-runs')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('10')).to.not.equal(-1);
done();
});
});
it('should set parallelRuns with integer value', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'parallelRuns': 10 }, function(){
expect(bsLocal.getBinaryArgs().indexOf('--parallel-runs')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('10')).to.not.equal(-1);
done();
});
});
it('should set proxy', function (done) {
bsLocal.start({
'key': process.env.BROWSERSTACK_ACCESS_KEY,
onlyCommand: true,
'proxyHost': 'localhost',
'proxyPort': 8080,
'proxyUser': 'user',
'proxyPass': 'pass'
}, function(){
expect(bsLocal.getBinaryArgs().indexOf('--proxy-host')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('localhost')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('--proxy-port')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf(8080)).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('--proxy-user')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('user')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('--proxy-pass')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('pass')).to.not.equal(-1);
done();
});
});
it('should set hosts', function (done) {
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, onlyCommand: true, 'only': 'localhost,8000,0'}, function(){
expect(bsLocal.getBinaryArgs().indexOf('--only')).to.not.equal(-1);
expect(bsLocal.getBinaryArgs().indexOf('localhost,8000,0')).to.not.equal(-1);
done();
});
});
it('should stop local', function (done) {
this.timeout(MAX_TIMEOUT);
bsLocal.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY}, function(){
expect(bsLocal.isRunning()).to.equal(true);
bsLocal.stop(function(){
expect(bsLocal.isRunning()).to.equal(false);
done();
});
});
});
afterEach(function (done) {
this.timeout(60000);
bsLocal.stop(done);
});
});
describe('Start sync', () => {
var bsLocal, bsLocal_2;
beforeEach(function () {
bsLocal = new browserstack.Local();
});
it('should have pid when running', function () {
this.timeout(60000);
bsLocal.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY});
expect(bsLocal.tunnel.pid).to.not.equal(0);
});
it('should return is running properly', function () {
this.timeout(60000);
expect(bsLocal.isRunning()).to.not.equal(true);
bsLocal.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY});
expect(bsLocal.isRunning()).to.equal(true);
});
it.skip('should throw error on running multiple binary', function () {
this.timeout(60000);
bsLocal.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY });
bsLocal_2 = new browserstack.Local();
var tempLogPath = path.join(process.cwd(), 'log2.log');
const error = bsLocal_2.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath });
expect(error.toString().trim()).to.equal('LocalError: Either another browserstack local client is running on your machine or some server is listening on port 45690');
fs.unlinkSync(tempLogPath);
});
afterEach(function (done) {
this.timeout(60000);
bsLocal.stop(() => {
if (bsLocal_2) {
bsLocal_2.stop(done);
} else {
done();
}
});
});
})
describe('LocalBinary', function () {
describe('Retries', function() {
var unlinkTmp,
defaultBinaryPath,
validBinaryPath,
sandBox;
before(function(done) {
this.timeout(MAX_TIMEOUT);
// ensure that we have a valid binary downloaded
// removeIfInvalid();
(new LocalBinary()).binaryPath({}, 'abc', 9, function(binaryPath) {
defaultBinaryPath = binaryPath;
tempfs.mkdir({
recursive: true
}, function(err, dir) {
if(err) { throw err; }
validBinaryPath = path.join(dir.path, path.basename(binaryPath));
fs.rename(defaultBinaryPath, validBinaryPath, function(err) {
if(err) { throw err; }
unlinkTmp = dir.unlink;
done();
});
});
});
});
beforeEach(function() {
sandBox = sinon.sandbox.create();
});
it('Tries to download binary if its corrupted', function(done) {
fs.unlink(defaultBinaryPath, function() {
var localBinary = new LocalBinary();
var downloadStub = sandBox.stub(localBinary, 'download', function() {
downloadStub.callArgWith(2, [ defaultBinaryPath ]);
expect(downloadStub.args[0][3]).to.be(5);
});
fs.writeFile(defaultBinaryPath, 'Random String', function() {
fs.chmod(defaultBinaryPath, '0755', function() {
localBinary.binaryPath({
}, 'abc', 9, function(binaryPath) {
expect(downloadStub.called).to.be.true;
done();
});
});
});
});
});
it('Tries to download binary if its not present', function(done) {
fs.unlink(defaultBinaryPath, function() {
var localBinary = new LocalBinary();
var downloadStub = sandBox.stub(localBinary, 'download', function() {
downloadStub.callArgWith(2, [ defaultBinaryPath ]);
expect(downloadStub.args[0][3]).to.be(5);
});
localBinary.binaryPath({
}, 'abc', 9, function(binaryPath) {
expect(downloadStub.called).to.be.true;
done();
});
});
});
afterEach(function(done) {
sandBox.restore();
done();
});
after(function(done) {
fs.rename(validBinaryPath, defaultBinaryPath, function(err) {
if(err) { throw err; }
unlinkTmp(done);
});
});
});
describe('Download Path', function() {
var sandBox;
var localBinary;
beforeEach(function() {
sandBox = sinon.sandbox.create();
localBinary = new LocalBinary();
});
it('should return download path of darwin binary', function() {
var osNames = ['darwin', 'mac os'];
osNames.forEach(function(os) {
sandBox.stub(localBinary, 'hostOS', os);
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-darwin-x64');
});
});
it('should return download path of exe binary', function() {
var osNames = ['mswin', 'msys', 'mingw', 'cygwin', 'bccwin', 'wince', 'emc', 'win32'];
osNames.forEach(function(os) {
sandBox.stub(localBinary, 'hostOS', os);
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal.exe');
});
});
it('should return download path of linux 64 arch binary', function() {
sandBox.stub(localBinary, 'hostOS', 'linux');
sandBox.stub(localBinary, 'is64bits', true);
localBinary.isAlpine = sandBox.stub(localBinary, 'isAlpine').returns(false);
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-x64');
});
it('should return download path of linux 32 arch binary', function() {
sandBox.stub(localBinary, 'hostOS', 'linux');
sandBox.stub(localBinary, 'is64bits', false);
localBinary.isAlpine = sandBox.stub(localBinary, 'isAlpine').returns(false);
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-linux-ia32');
});
it('should return download path of alpine linux binary', function() {
sandBox.stub(localBinary, 'hostOS', 'linux');
localBinary.isAlpine = sandBox.stub(localBinary, 'isAlpine').returns(true);
sandBox.stub(localBinary, 'is64bits', true);
expect(localBinary.getDownloadPath()).to.equal('https://www.browserstack.com/local-testing/downloads/binaries/BrowserStackLocal-alpine');
});
afterEach(function(done) {
sandBox.restore();
done();
});
});
describe('Download', function() {
var proxy;
var proxyPort;
var binary;
var tempDownloadPath;
before(function (done) {
// setup HTTP proxy server
proxy = new Proxy();
proxy.listen(function () {
proxyPort = proxy.address().port;
done();
});
});
after(function (done) {
proxy.once('close', function () { done(); });
proxy.close();
});
beforeEach(function () {
binary = new LocalBinary();
tempDownloadPath = path.join(process.cwd(), 'download');
});
afterEach(function () {
rimraf.sync(tempDownloadPath);
});
it('should download binaries without proxy', function (done) {
this.timeout(MAX_TIMEOUT);
var conf = {};
binary.download(conf, tempDownloadPath, function (result) {
expect(fs.existsSync(result)).to.equal(true);
done();
});
});
it('should download binaries with proxy', function (done) {
this.timeout(MAX_TIMEOUT);
var conf = {
proxyHost: '127.0.0.1',
proxyPort: proxyPort
};
binary.download(conf, tempDownloadPath, function (result) {
// test for file existence
expect(fs.existsSync(result)).to.equal(true);
done();
});
});
it('should download binaries in sync', function () {
this.timeout(MAX_TIMEOUT);
var conf = {};
const result = binary.downloadSync(conf, tempDownloadPath);
expect(fs.existsSync(result)).to.equal(true);
});
});
});