Skip to content

Commit f3e1340

Browse files
authored
Merge pull request #308 from LambdaTest/dev
Release 19 feb
2 parents bebc203 + d734963 commit f3e1340

13 files changed

+587
-1004
lines changed

commands/build_info.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45

@@ -40,6 +41,7 @@ function get_build_info(args) {
4041
}
4142
}
4243
let options = {
44+
method: 'get',
4345
url: constants[env].BUILD_BASE_URL + args.buildId,
4446
auth: {
4547
username: username,
@@ -55,25 +57,38 @@ function get_build_info(args) {
5557
return;
5658
} else {
5759
if (args["reject_unauthorized"] == "false") {
58-
options["rejectUnauthorized"] = false;
60+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
5961
console.log("Setting rejectUnauthorized to false for web requests");
6062
}
6163
}
6264
}
6365

64-
request.get(options, (err, res, body) => {
65-
if (err) {
66-
reject(err);
66+
axios(options)
67+
.then(response => {
68+
if (response.data.status == "success") {
69+
resolve(response.data.data);
6770
} else {
68-
if (res.statusCode == "401") {
71+
resolve(response.data.message);
72+
}
73+
})
74+
.catch(error => {
75+
if (error.response) {
76+
// The request was made and the server responded with a status code
77+
// that falls out of the range of 2xx
78+
if (error.response.status == 401) {
6979
resolve("Unauthorized");
70-
} else if (JSON.parse(body).status == "success") {
71-
resolve(JSON.parse(body).data);
7280
} else {
73-
resolve(JSON.parse(body).message);
81+
console.log(error.response.data);
7482
}
83+
} else if (error.request) {
84+
// The request was made but no response was received
85+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
86+
// http.ClientRequest in node.js
87+
console.log(error.cause);
88+
} else {
89+
reject(error);
7590
}
76-
});
91+
})
7792
});
7893
}
7994

commands/build_stop.js

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45
const fs = require("fs");
5-
function stop_session(args) {
6+
function stop_build(args) {
67
return new Promise(function (resolve, reject) {
78
var username = "";
89
var access_key = "";
@@ -25,33 +26,33 @@ function stop_session(args) {
2526
} else {
2627
reject("Access Key not provided");
2728
}
28-
if ("stop_last_session" in args) {
29+
if ("stop_last_build" in args) {
2930
const file_path = "lambdatest_run.json";
3031
if (fs.existsSync(file_path)) {
3132
let lambda_run = fs.readFileSync(file_path);
3233
try {
3334
let lambda_run_obj = JSON.parse(lambda_run);
34-
if (!("session_id" in lambda_run_obj)) {
35-
throw new Error("session_id is missing from the file");
35+
if (!("build_id" in lambda_run_obj)) {
36+
throw new Error("build_id is missing from the file");
3637
}
37-
args.session_id = lambda_run_obj.session_id;
38+
args.build_id = lambda_run_obj.build_id;
3839
} catch (e) {
3940
reject(
4041
"Error!! lambdatest_run.json file is tampered Err: " + e.message
4142
);
4243
}
4344
} else {
4445
reject(
45-
"Error!! Last session details not found, lambdatest_run.json file not present!!"
46+
"Error!! Last Build details not found, lambdatest_run.json file not present!!"
4647
);
4748
}
4849
} else {
4950
if (
50-
!("session_id" in args) ||
51-
args["session_id"] == "" ||
52-
args["session_id"] == undefined
51+
!("build_id" in args) ||
52+
args["build_id"] == "" ||
53+
args["build_id"] == undefined
5354
) {
54-
reject("Error!! Please provide a Session ID");
55+
reject("Error!! Please provide a Build ID");
5556
}
5657
}
5758
var env = "prod";
@@ -66,7 +67,8 @@ function stop_session(args) {
6667
}
6768

6869
let options = {
69-
url: constants[env].BUILD_STOP_URL + args.session_id,
70+
method: 'put',
71+
url: constants[env].BUILD_STOP_URL + "?buildId=" + args.build_id,
7072
headers: {
7173
Authorization: "Token " + access_key,
7274
Username: username,
@@ -82,44 +84,43 @@ function stop_session(args) {
8284
return;
8385
} else {
8486
if (args["reject_unauthorized"] == "false") {
85-
options["rejectUnauthorized"] = false;
87+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
8688
console.log("Setting rejectUnauthorized to false for web requests");
8789
}
8890
}
8991
}
90-
request.put(options, function (err, resp, body) {
91-
if (err) {
92-
reject(err);
92+
93+
axios(options)
94+
.then(response => {
95+
if(response.data.length == 0){
96+
resolve("No tests to stop in build " + args.build_id);
9397
} else {
94-
try {
95-
responseData = JSON.parse(body);
96-
} catch (e) {
97-
console.log("Error in JSON response", body);
98-
responseData = null;
99-
}
100-
if (resp.statusCode != 200) {
101-
if (responseData && responseData["error"]) {
102-
reject(responseData["error"]);
103-
} else {
104-
console.log(responseData);
105-
reject("error", responseData);
106-
}
107-
} else {
108-
if (responseData.length == 0) {
109-
resolve("No tests to stop in session " + args.session_id);
110-
}
111-
resolve(
112-
"Session Stopped successfully, No. of tests stopped are: " +
113-
responseData.length
114-
);
98+
resolve(
99+
"Build Stopped successfully, No. of tests stopped are: " +
100+
response.data.length
101+
);
102+
}
103+
})
104+
.catch(error => {
105+
if (error.response != null) {
106+
if (error.response.status != 200) {
107+
reject(error.response.data)
115108
}
109+
} else if (error.request) {
110+
// The request was made but no response was received
111+
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of http.ClientRequest in node.js
112+
reject(error.cause);
113+
} else {
114+
reject(error);
116115
}
117-
});
116+
})
117+
118+
118119
});
119120
}
120121

121122
module.exports = function (args) {
122-
stop_session(args)
123+
stop_build(args)
123124
.then(function (resp) {
124125
console.log(resp);
125126
})

commands/generate_reports.js

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
const request = require("request");
1+
const https = require('https');
2+
const axios = require('axios');
23
const constants = require("./utils/constants.js");
34
const process = require("process");
45
const build_stats = require("./utils/poller/build_stats.js");
@@ -27,33 +28,34 @@ function download_artefact(
2728
const stream = fs.createWriteStream(file_path);
2829
stream.end();
2930
let options = {
31+
method: 'get',
3032
url: constants[env].REPORT_URL + test_id,
3133
auth: {
3234
username: username,
3335
password: access_key,
3436
},
3537
gzip: true,
3638
timeout: 120000,
39+
responseType: 'stream'
3740
};
3841
if (rejectUnauthorized == false) {
39-
options["rejectUnauthorized"] = false;
42+
options.httpsAgent = new https.Agent({ rejectUnauthorized: false });
4043
console.log("Setting rejectUnauthorized to false for web requests");
4144
}
4245

43-
request(options, (err, res, body) => {
44-
if (err) {
45-
reject(err);
46-
}
47-
response_code = res.statusCode;
48-
resp = res
49-
}).pipe(
50-
fs
51-
.createWriteStream(file_path, {
46+
axios(options)
47+
.then((response) => {
48+
response_code = response.status;
49+
resp = response;
50+
response.data.pipe(
51+
fs.createWriteStream(file_path, {
5252
overwrite: true,
5353
})
54-
.on("finish", function () {
55-
if (response_code == 200) {
56-
const zip = new StreamZip({ file: file_path });
54+
);
55+
56+
response.data.on('end', function () {
57+
if (response_code == 200) {
58+
const zip = new StreamZip({ file: file_path });
5759
zip.on("ready", () => {
5860
zip.extract(null, old_path, (err, count) => {
5961
zip.close();
@@ -64,18 +66,41 @@ function download_artefact(
6466
: `Extracted ${count} entries for ` + test_id
6567
);
6668
});
67-
});
68-
} else {
69-
fs.unlinkSync(file_path);
70-
if (resp.body != null) {
71-
const responseObject = JSON.parse(resp.body);
72-
const dataValue = responseObject.data;
69+
})
70+
}
71+
});
72+
73+
})
74+
.catch((error) => {
75+
76+
if (error.response) {
77+
resp = error.response
78+
// The request was made and the server responded with a status code
79+
// that falls out of the range of 2xx
80+
if (error.response.status == 401) {
81+
resolve("Unauthorized");
82+
} else {
83+
fs.unlinkSync(file_path);
84+
if (resp.data != null) {
85+
const responseObject = resp.data;
86+
const dataValue = responseObject.data;
87+
if (dataValue != null) {
7388
reject("Could not download artefacts for test id " + test_id + " with reason " + dataValue);
89+
} else {
90+
reject("Could not download artefacts for test id " + test_id);
7491
}
75-
reject("Could not download artefacts for test id " + test_id);
7692
}
77-
})
78-
);
93+
reject("Could not download artefacts for test id " + test_id);
94+
}
95+
} else if (error.request) {
96+
console.log(error.cause);
97+
} else {
98+
reject(error);
99+
}
100+
101+
});
102+
103+
79104
});
80105
}
81106

@@ -233,6 +258,15 @@ function generate_report(args) {
233258
});
234259
}
235260

261+
function generate_report_command(args) {
262+
generate_report(args)
263+
.then(function (resp) {})
264+
.catch(function (err) {
265+
console.log("ERR:", err);
266+
});
267+
};
268+
236269
module.exports = {
237-
generate_report:generate_report
270+
generate_report:generate_report,
271+
generate_report_command:generate_report_command
238272
};

commands/run.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ module.exports = function (args) {
117117
]
118118
) {
119119
process.exit(exit_code);
120+
} else {
121+
process.exit(0);
120122
}
121123
})
122124
.catch(function (error) {
@@ -151,11 +153,15 @@ module.exports = function (args) {
151153
.then(function (exit_code) {
152154
if (lt_config["run_settings"]["exit-on-failure"]) {
153155
process.exit(exit_code);
156+
} else {
157+
process.exit(0);
154158
}
155159
})
156160
.catch(function (error) {
157161
if (lt_config["run_settings"]["exit-on-failure"]) {
158162
process.exit(1);
163+
} else {
164+
process.exit(0);
159165
}
160166
});
161167
}

0 commit comments

Comments
 (0)