Skip to content

Commit e2a54ca

Browse files
Japneet Singh ChawlaJapneet Singh Chawla
authored andcommitted
build stop feature added
1 parent 9e13e3d commit e2a54ca

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

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 != "bool"
243+
) {
244+
reject("Type of stop_on_failure flag is not bool");
245+
}
239246
resolve("Validated the Config");
240247
});
241248
};

index.js

Lines changed: 5 additions & 0 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) {

0 commit comments

Comments
 (0)