-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlauncher.js
More file actions
357 lines (259 loc) · 7.52 KB
/
Copy pathlauncher.js
File metadata and controls
357 lines (259 loc) · 7.52 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
const webdriver = require('selenium-webdriver');
const url = require('url');
const os = require('os');
const child_process = require('child_process');
var perfectoConnect;
var perfectoDisconnect = false;
var log;
var keepAliveTimer = null;
class DriverData{
constructor(){
this.driver = null;
this.reporterInitialized = false;
}
};
driverDataMap = new Map();
var tunnelId = null;
function getDriverData(id){
driverData = driverDataMap.get(id);
if (driverData)
return driverData;
driverData = new DriverData();
driverDataMap.set(id, driverData);
return driverData;
}
module.exports.getDriverData = getDriverData;
function haveReporter(config) {
reporters = config.reporters;
if (!reporters)
return false;
for (var i=0; i<reporters.length; i++){
if (reporters[i] == 'Perfecto')
return true;
}
return false;
}
function getIPAddress() {
var interfaces = os.networkInterfaces();
for (var devName in interfaces) {
var iface = interfaces[devName];
for (var i = 0; i < iface.length; i++) {
var alias = iface[i];
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
return alias.address;
}
}
}
return null;
}
function keepAlive() {
log.info('Sending keep alive');
for (var [id, driverData] of driverDataMap.entries()) {
driverData.driver.getCurrentUrl();
}
}
function cancelKeepAlive() {
if (!keepAliveTimer)
return;
log.info('Canceling keep alive');
clearInterval(keepAliveTimer);
keepAliveTimer = null;
}
function setupKeepAlive(perfectoConfig) {
if (keepAliveTimer)
return;
if (!perfectoConfig.keepAlive)
return;
if (perfectoConfig.keepAlive <= 0)
return;
log.info('Setting keep alive timeout to %d', perfectoConfig.keepAlive);
keepAliveTimer = setInterval(keepAlive, perfectoConfig.keepAlive);
// make sure the keep alive timer does not prevent us from exiting.
keepAliveTimer.unref();
if (!perfectoConfig.keepAliveDuration)
return;
if (perfectoConfig.keepAliveDuration <= 0)
return;
log.info('Setting keep alive duration to %d', perfectoConfig.keepAliveDuration);
// make sure the keep alive cancel timeout does not prevent us from exiting.
setTimeout(cancelKeepAlive, perfectoConfig.keepAliveDuration).unref();
}
function getHost(perfectoConfig){
if (!perfectoConfig.host)
return getIPAddress();
return perfectoConfig.host;
}
function getPerfectoUrl(pefectoConfig){
if (!perfectoConfig.perfectoUrl){
log.error('Missing perfectoUrl configuration parameter');
return null;
}
return perfectoConfig.perfectoUrl;
}
function getSecurityToken(perfectoConfig){
if (!perfectoConfig.securityToken){
log.error('Missing securityToken configuration parameter');
return null;
}
return perfectoConfig.securityToken;
}
function getTunnelId(perfectoConfig, securityToken, perfectoUrl){
var tunnelId = process.env.PERFECTO_TUNNEL_ID;
if (tunnelId){
log.info ('Extracting tunnel id %s from environment variable PERFECTO_TUNNEL_ID', tunnelId);
return tunnelId;
}
log.info('No PERFECTO_TUNNEL_ID environment variable');
tunnelId = perfectoConfig.tunnelId;
if (tunnelId){
log.info ('Extracting tunnel id %s from configuration parameter tunnelId', tunnelId);
return tunnelId;
}
log.info('No tunnelId configuration parameter');
log.info('Starting perfectoconnect');
perfectoConnect = perfectoConfig.perfectoConnect;
if(!perfectoConnect){
log.info('No perfectoConnect configured. using default');
perfectoConnect = 'perfectoconnect';
}else
log.info('Using perfectoConnect configuration %s',perfectoConnect);
var host = url.parse(perfectoUrl).host;
var cmd = perfectoConnect + ' start -c ' + host + ' -s ' + securityToken;
log.info('Sarting tunnel. executing ' + cmd);
var stdout;
try{
stdout = child_process.execSync(cmd).toString();
} catch (e) {
log.error('Error staring tunnel:' + e);
return null;
}
if (!stdout){
log.error('Error starting tunnel.');
return null;
}
perfectoDisconnect = true;
process.on('exit', (code) => {
closeTunnel();
});
out = stdout.trim();
words = out.split(' ');
return words[words.length - 1];
}
function closeTunnel(){
if (perfectoDisconnect){
log.info('Closing tunnel');
perfectoDisconnect = false;
try {
var cmd = perfectoConnect + ' stop';
child_process.execSync(cmd);
} catch (e) {
}
}
}
function forceKill() {
log.warn('Force kill');
process.exit(-1);
}
async function quitDriver(id) {
log.info('quitting driver %s', id);
try {
var driver = getDriverData(id).driver;
driverDataMap.delete(id);
await driver.quit();
if (!driverDataMap.size)
setTimeout(forceKill, 30000).unref();
} catch (e) {
log.error('Error terminating driver');
log.error(e);
}
}
module.exports.quitDriver = quitDriver;
module.exports.PerfectoBrowser = function PerfectoBrowser(baseLauncherDecorator, captureTimeoutLauncherDecorator, retryLauncherDecorator, logger, config, args) {
baseLauncherDecorator(this);
captureTimeoutLauncherDecorator(this);
retryLauncherDecorator(this);
log = logger.create('perfecto-launcher');
function error(self) {
self._done('failure');
}
this.name = 'Perfecto';
this.on('start', function(karmaUrl) {
// perfecto main configuration
perfectoConfig = config.perfecto;
if (!perfectoConfig){
log.error('Missing perfecto configuration section');
error(this);
return;
}
// host
const host = getHost(perfectoConfig);
if (host == null){
log.error('Cannot determine local host');
error(this);
return;
}
log.info('Using host [%s]', host);
// perfecto server url
const perfectoUrl = getPerfectoUrl(perfectoConfig);
if (perfectoUrl == null){
error(this);
return;
}
log.info('Using perfectoUrl [%s]', perfectoUrl);
// security token
const securityToken = getSecurityToken(perfectoConfig);
if (securityToken == null){
error(this);
return;
}
log.info('Using securityToken [%s]', securityToken);
// tunnelId;
if (tunnelId == null)
tunnelId = getTunnelId(perfectoConfig, securityToken, perfectoUrl);
if (tunnelId == null){
error(this);
return;
}
log.info('Using tunnelId [%s]', tunnelId);
var tunnelIdCap;
if (tunnelId == 'none' )
tunnelIdCap={};
else
tunnelIdCap={'tunnelId' : tunnelId};
var securityTokenCap = {
'securityToken' : securityToken,
}
if (!perfectoConfig.capabilities)
perfectoConfig.capabilities={};
var capabilities = Object.assign({}, tunnelIdCap, securityTokenCap, args.capabilities, perfectoConfig.capabilities);
log.info(capabilities);
setupKeepAlive(perfectoConfig);
try{
var parsed = url.parse(karmaUrl, true);
parsed.host = host + ':' + parsed.port;
karmaUrl = url.format(parsed);
log.info('URL %s', karmaUrl);
const driver = new webdriver.Builder()
.withCapabilities(capabilities)
.usingServer(perfectoUrl)
.build();
driver.get(karmaUrl).then(getDriverData(this.id).driver = driver);
}catch (e) {
log.error('cannot start driver');
error(this);
}
});
this.on('kill', async function(done) {
// If we are not running in single run mode, we always close here.
// If we do not have a reporter, we always close here.
if (!haveReporter(config) || !config.singleRun || !getDriverData(this.id).reporterInitialized){
// There is no reporter runnung. We have to close the driver here.
await quitDriver(this.id);
done();
}else{
// We have a reporter and we are in single run mode. The reporter will do the closing.
log.info('Reporter registered. Not quitting driver %s', this.id);
done();
}
});
}