Skip to content

Commit 5b5fd8a

Browse files
Merge pull request #130 from LambdaTest/dev
2.4.4
2 parents 9e13e3d + 358aa2b commit 5b5fd8a

File tree

8 files changed

+153
-25
lines changed

8 files changed

+153
-25
lines changed

commands/build_stop.js

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const request = require("request");
22
const constants = require("./utils/constants.js");
33
const process = require("process");
4-
5-
function stop_build(args) {
4+
const fs = require("fs");
5+
function stop_session(args) {
66
return new Promise(function (resolve, reject) {
77
var username = "";
88
var access_key = "";
@@ -25,13 +25,34 @@ function stop_build(args) {
2525
} else {
2626
reject("Access Key not provided");
2727
}
28-
29-
if (
30-
!("buildId" in args) ||
31-
args["buildid"] == "" ||
32-
args["buildid"] == undefined
33-
) {
34-
reject("Please provide a build ID");
28+
if ("stop_last_session" in args) {
29+
const file_path = "lambdatest_run.json";
30+
if (fs.existsSync(file_path)) {
31+
let lambda_run = fs.readFileSync(file_path);
32+
try {
33+
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");
36+
}
37+
args.session_id = lambda_run_obj.session_id;
38+
} catch (e) {
39+
reject(
40+
"Error!! lambdatest_run.json file is tampered Err: " + e.message
41+
);
42+
}
43+
} else {
44+
reject(
45+
"Error!! Last session details not found, lambdatest_run.json file not present!!"
46+
);
47+
}
48+
} else {
49+
if (
50+
!("session_id" in args) ||
51+
args["session_id"] == "" ||
52+
args["session_id"] == undefined
53+
) {
54+
reject("Error!! Please provide a Session ID");
55+
}
3556
}
3657
var env = "prod";
3758
if ("env" in args) {
@@ -45,7 +66,7 @@ function stop_build(args) {
4566
}
4667

4768
let options = {
48-
url: constants[env].BUILD_STOP_URL + args.buildId,
69+
url: constants[env].BUILD_STOP_URL + args.session_id,
4970
headers: {
5071
Authorization: "Token " + access_key,
5172
Username: username,
@@ -69,15 +90,21 @@ function stop_build(args) {
6990
reject("error", responseData);
7091
}
7192
} else {
72-
resolve("Build Stopped successfully");
93+
if (responseData.length == 0) {
94+
resolve("No tests to stop in session " + args.session_id);
95+
}
96+
resolve(
97+
"Session Stopped successfully, No. of tests stopped are: " +
98+
responseData.length
99+
);
73100
}
74101
}
75102
});
76103
});
77104
}
78105

79106
module.exports = function (args) {
80-
stop_build(args)
107+
stop_session(args)
81108
.then(function (resp) {
82109
console.log(resp);
83110
})

commands/utils/constants.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ module.exports = {
1010
LAMBDA_CONFIG: "./lambdatest-config.json",
1111
SUPPORTED_CYPRESS_VERSIONS: ["5", "6"],
1212
BUILD_END_STATES: "&status=running,queued,created,initiated,pqueued",
13+
BUILD_ERROR_STATES: "&status=error,lambda error,failed",
1314
CYPRESS_ENV_FILE_PATH: "cypress.env.json",
1415
ENVS: ["stage", "beta", "prod", "preprod"],
1516
prod: {
1617
INTEGRATION_BASE_URL: "https://api.lambdatest.com/liis",
1718
BUILD_BASE_URL: "https://api.lambdatest.com/automation/api/v1/builds/",
18-
BUILD_STOP_URL: "https://beta-api.lambdatest.com/api/v1/test/stop?buildId=",
19+
BUILD_STOP_URL:
20+
"https://beta-api.lambdatest.com/api/v1/test/stop?sessionId=",
1921
SESSION_URL:
2022
"https://api.lambdatest.com/automation/api/v1/sessions?limit=200&session_id=",
2123
REPORT_URL:
@@ -26,7 +28,7 @@ module.exports = {
2628
BUILD_BASE_URL:
2729
"https://api.cypress-v3.dev.lambdatest.io/automation/api/v1/builds/",
2830
BUILD_STOP_URL:
29-
"https://api.cypress-v3.dev.lambdatest.io/api/v1/test/stop?buildId=",
31+
"https://api.cypress-v3.dev.lambdatest.io/api/v1/test/stop?sessionId=",
3032
SESSION_URL:
3133
"https://api.cypress-v3.dev.lambdatest.io/automation/api/v1/sessions?limit=200&session_id=",
3234
REPORT_URL:
@@ -37,7 +39,7 @@ module.exports = {
3739
BUILD_BASE_URL:
3840
"https://stage-api.lambdatest.com/automation/api/v1/builds/",
3941
BUILD_STOP_URL:
40-
"https://stage-api.lambdatest.com/api/v1/test/stop?buildId=",
42+
"https://stage-api.lambdatest.com/api/v1/test/stop?sessionId=",
4143
SESSION_URL:
4244
"https://stage-api.lambdatest.com/automation/api/v1/sessions?limit=200&session_id=",
4345
REPORT_URL:
@@ -48,7 +50,7 @@ module.exports = {
4850
BUILD_BASE_URL:
4951
"https://preprod-api.lambdatest.com/automation/api/v1/builds/",
5052
BUILD_STOP_URL:
51-
"https://preprod-api.lambdatest.com/api/v1/test/stop?buildId=",
53+
"https://preprod-api.lambdatest.com/api/v1/test/stop?sessionId=",
5254
SESSION_URL:
5355
"https://preprod-api.lambdatest.com/automation/api/v1/sessions?limit=200&session_id=",
5456
REPORT_URL:

commands/utils/poller/build.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const constants = require("../constants");
2+
const request = require("request");
3+
//https://api.cypress-v3.dev.lambdatest.io/api/v1/test/stop/?sessionId=4a7434b9-1905-4aaf-a178-9167acb00c5d
4+
function stop_cypress_session(lt_config, session_id, env) {
5+
return new Promise(function (resolve, reject) {
6+
request(
7+
constants[env].BUILD_STOP_URL + session_id,
8+
{
9+
auth: {
10+
username: lt_config["lambdatest_auth"]["username"],
11+
password: lt_config["lambdatest_auth"]["access_key"],
12+
},
13+
method: "PUT",
14+
},
15+
(err, res, body) => {
16+
if (err) {
17+
console.log("Error occured while stopping session", err);
18+
reject(err);
19+
}
20+
if (res.statusCode == "401") {
21+
console.log("Error Occured: Unauthorized access to session-stop");
22+
reject("Unauthorized");
23+
} else if (res.statusCode == "200") {
24+
console.log("Session stopped successfully");
25+
resolve(JSON.parse(body));
26+
} else {
27+
console.log(body);
28+
reject("No response for session stop");
29+
}
30+
}
31+
);
32+
});
33+
}
34+
35+
module.exports = {
36+
stop_cypress_session: stop_cypress_session,
37+
};

commands/utils/poller/build_stats.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const constants = require("../constants");
22
const request = require("request");
3+
const builds = require("./build");
34
//const poller=require("./poller.js")
45

56
function get_completed_build_info(lt_config, session_id, env) {
@@ -38,7 +39,7 @@ function get_build_info(lt_config, session_id, env, update_status, callback) {
3839
password: lt_config["lambdatest_auth"]["access_key"],
3940
},
4041
},
41-
(err, res, body) => {
42+
async (err, res, body) => {
4243
if (err) {
4344
//reject(err);
4445
update_status(false);
@@ -54,7 +55,13 @@ function get_build_info(lt_config, session_id, env, update_status, callback) {
5455
update_status(false);
5556
return callback(null, JSON.parse(body));
5657
}
57-
//console.log(resp)
58+
//Stop the tests if stop on failure is enabled and we get an errored/failed/lambda errored test
59+
if (lt_config.run_settings.stop_on_failure == true) {
60+
let response = await get_error_state(lt_config, session_id, env);
61+
if (response.count > 0) {
62+
await builds.stop_cypress_session(lt_config, session_id, env);
63+
}
64+
}
5865
return setTimeout(callback, 5000, null);
5966
} else {
6067
update_status(false);
@@ -64,6 +71,39 @@ function get_build_info(lt_config, session_id, env, update_status, callback) {
6471
);
6572
}
6673

74+
function get_error_state(lt_config, session_id, env) {
75+
return new Promise(function (resolve, reject) {
76+
request(
77+
constants[env].SESSION_URL + session_id + constants.BUILD_ERROR_STATES,
78+
{
79+
auth: {
80+
username: lt_config["lambdatest_auth"]["username"],
81+
password: lt_config["lambdatest_auth"]["access_key"],
82+
},
83+
},
84+
(err, res, body) => {
85+
let response = { err: null, res_code: null, count: 0 };
86+
if (err) {
87+
console.log(err);
88+
response.err = err;
89+
response.res_code = 500;
90+
resolve(response);
91+
}
92+
response.res_code = res.statusCode;
93+
if (res.statusCode == "401") {
94+
response.err = "Unauthorized";
95+
resolve(response);
96+
} else if (res.statusCode == "200") {
97+
resp = JSON.parse(body);
98+
response.count = resp["Meta"]["result_set"]["count"];
99+
resolve(response);
100+
} else {
101+
resolve(response);
102+
}
103+
}
104+
);
105+
});
106+
}
67107
module.exports = {
68108
get_build_info: get_build_info,
69109
get_completed_build_info: get_completed_build_info,

commands/utils/set_args.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,12 @@ function sync_args_from_cmd(args) {
274274
} else if (!lt_config["run_settings"]["geo_location"]) {
275275
lt_config["run_settings"]["geo_location"] = "";
276276
}
277+
//Check for stop on failure location
278+
if ("stop_on_failure" in args) {
279+
lt_config["run_settings"]["stop_on_failure"] = true;
280+
} else if (!lt_config["run_settings"]["stop_on_failure"]) {
281+
lt_config["run_settings"]["stop_on_failure"] = false;
282+
}
277283

278284
//get specs from current directory if specs are not passed in config or cli
279285
if (

commands/utils/validate.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ module.exports = validate_config = function (lt_config, validation_configs) {
236236
}
237237
}
238238
}
239+
240+
if (
241+
lt_config.run_settings.stop_on_failure &&
242+
typeof lt_config.run_settings.stop_on_failure != "boolean"
243+
) {
244+
reject("Type of stop_on_failure flag is not bool");
245+
}
239246
resolve("Validated the Config");
240247
});
241248
};

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ const argv = require("yargs")
111111
alias: "geo_location",
112112
describe: "Pass Geo Country Code",
113113
type: "string",
114+
})
115+
.option("sof", {
116+
alias: "stop_on_failure",
117+
describe: "Stop other tests if any test in session gets errored out",
118+
type: "bool",
114119
});
115120
},
116121
function (argv) {
@@ -154,25 +159,29 @@ const argv = require("yargs")
154159
function (yargs) {
155160
return yargs
156161
.option("id", {
157-
alias: "build-id",
158-
describe: "Build Identifier",
162+
alias: "session_id",
163+
describe: "Session Identifier",
159164
type: "string",
160-
demandOption: true,
161165
})
162166
.option("user", {
163-
alias: "user",
167+
alias: "username",
164168
describe: "username",
165169
type: "string",
166170
})
167-
.option("access_key", {
171+
.option("ak", {
168172
alias: "access_key",
169173
describe: "Access Key",
170174
type: "string",
171175
})
172176
.option("env", {
173-
alias: "env",
177+
alias: "environment",
174178
describe: "environment",
175179
type: "string",
180+
})
181+
.option("sls", {
182+
alias: "stop_last_session",
183+
describe: "stop last session",
184+
type: "bool",
176185
});
177186
},
178187
function (argv) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lambdatest-cypress-cli",
3-
"version": "2.4.3",
3+
"version": "2.4.4",
44
"description": "The lambdatest-cypress-cli is LambdaTest's command-line interface (CLI) aimed to help you run your Cypress tests on LambdaTest platform.",
55
"homepage": "https://github.com/LambdaTest/lambdatest-cypress-cli",
66
"author": "LambdaTest <[email protected]>",

0 commit comments

Comments
 (0)