-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathatsHelper.js
132 lines (113 loc) · 4.7 KB
/
atsHelper.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
const path = require('path');
const fs = require('fs');
const { consoleHolder } = require('../testObservability/helper/constants');
const HttpsProxyAgent = require('https-proxy-agent');
const axios = require('axios'),
logger = require('./logger').winstonLogger,
utils = require('./utils'),
config = require('./config');
Constants = require('./constants');
exports.isTurboScaleSession = (bsConfig) => {
// env var will override config
if (process.env.BROWSERSTACK_TURBOSCALE && process.env.BROWSERSTACK_TURBOSCALE === 'true') {
return true;
}
if (utils.isNotUndefined(bsConfig) && bsConfig.run_settings && bsConfig.run_settings.turboScale) {
return true;
}
return false;
};
exports.getTurboScaleOptions = (bsConfig) => {
if (bsConfig.run_settings && bsConfig.run_settings.turboScaleOptions) {
return bsConfig.run_settings.turboScaleOptions;
}
return {};
};
exports.getTurboScaleGridName = (bsConfig) => {
// env var will override config
if (process.env.BROWSERSTACK_TURBOSCALE_GRID_NAME) {
return process.env.BROWSERSTACK_TURBOSCALE_GRID_NAME;
}
if (bsConfig.run_settings && bsConfig.run_settings.turboScaleOptions && bsConfig.run_settings.turboScaleOptions.gridName) {
return bsConfig.run_settings.turboScaleOptions.gridName;
}
return 'NO_GRID_NAME_PASSED';
};
exports.getTurboScaleGridDetails = async (bsConfig, args, rawArgs) => {
try {
const gridName = this.getTurboScaleGridName(bsConfig);
return new Promise((resolve, reject) => {
let options = {
url: `${config.turboScaleAPIUrl}/grids/${gridName}`,
auth: {
username: bsConfig.auth.username,
password: bsConfig.auth.access_key,
},
headers: {
'User-Agent': utils.getUserAgent(),
}
};
if (process.env.HTTP_PROXY) {
options.proxy = false;
options.httpsAgent = new HttpsProxyAgent(process.env.HTTP_PROXY);
} else if (process.env.HTTPS_PROXY) {
options.proxy = false;
options.httpsAgent = new HttpsProxyAgent(process.env.HTTPS_PROXY);
}
let responseData = {};
axios(options).then(response => {
try {
responseData = response.data;
} catch (e) {
responseData = {};
}
if(response.status != 200) {
if (responseData.message == Constants.validationMessages.GRID_NOT_FOUND) {
logger.error(`Error: Invalid grid name '${gridName}' specified.`);
} else {
logger.error(`Error:Failed to fetch turboscale grid details with response code ${resp.statusCode}`);
};
utils.sendUsageReport(bsConfig, args, responseData["error"], Constants.messageTypes.ERROR, 'get_ats_details_failed', null, rawArgs);
resolve({});
}
resolve(responseData);
}).catch(error => {
logger.warn(utils.formatRequest(error, null, null));
utils.sendUsageReport(bsConfig, args, error, Constants.messageTypes.ERROR, 'get_ats_details_failed', null, rawArgs);
resolve({});
});
});
} catch (err) {
logger.error(`Failed to find TurboScale Grid: ${err}: ${err.stack}`);
}
};
exports.patchCypressConfigFileContent = (bsConfig) => {
try {
let cypressConfigFileData = fs.readFileSync(path.resolve(bsConfig.run_settings.cypress_config_file)).toString();
const patchedConfigFileData = cypressConfigFileData + '\n\n' + `
let originalFunction = module.exports.e2e.setupNodeEvents;
module.exports.e2e.setupNodeEvents = (on, config) => {
const bstackOn = require("./cypressPatch.js")(on);
if (originalFunction !== null && originalFunction !== undefined) {
originalFunction(bstackOn, config);
}
return config;
}
`
let confPath = bsConfig.run_settings.cypress_config_file;
let patchedConfPathList = confPath.split(path.sep);
patchedConfPathList[patchedConfPathList.length - 1] = 'patched_ats_config_file.js'
const patchedConfPath = patchedConfPathList.join(path.sep);
bsConfig.run_settings.patched_cypress_config_file = patchedConfPath;
fs.writeFileSync(path.resolve(bsConfig.run_settings.patched_cypress_config_file), patchedConfigFileData);
} catch(e) {
logger.error(`Encountered an error when trying to patch ATS Cypress Config File ${e}`);
return {};
}
}
exports.atsFileCleanup = (bsConfig) => {
const filePath = path.resolve(bsConfig.run_settings.patched_cypress_config_file);
if(fs.existsSync(filePath)){
fs.unlinkSync(filePath);
}
}