Skip to content

Commit 62d4a7d

Browse files
bsubbalootic
andauthored
TSERV-861:Error exit code (#29)
TSERV-861 adding status code 1 if testjob fails or the command could not be understood. Added a test script together with resources to make it easier to test status codes and see if something is broken. Co-authored-by: Jonathan Lundholm <jonathan.lundholm@smartbear.com>
1 parent a67feb4 commit 62d4a7d

17 files changed

Lines changed: 610 additions & 208 deletions

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ When a config file is present, it is possible to override the values using the c
3939
There is currently no support for encrypting the config file but normal file system security should make it possible to
4040
restrict reading it to the user running the tool.
4141

42+
TestEngine-CLI will return with exit status code 0 if it successfully carried out the operation. It will return with
43+
exit status code 1 if it failed to execute the command or parts of, including if a test job fails when it tries to run
44+
a project or some user couldn't be added while importing.
45+
4246
## Test Jobs
4347
The tool can submit test jobs, list jobs which has been submitted (only admins can see other users' jobs) and purge old
4448
jobs from the server.

bin/auditlog_functions.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@ const request = require('superagent');
44
const config = require('./config').config;
55
const sprintf = require('sprintf-js').sprintf;
66
const util = require('./shared_utils');
7+
const process = require('process');
78

89
module.exports.dispatcher = function (args) {
9-
if (args.length === 0)
10-
return printModuleHelp();
10+
if (args.length === 0) {
11+
printModuleHelp();
12+
process.exit(1);
13+
}
1114

1215
switch (args[0].toLowerCase()) {
1316
case 'dump':
1417
if (args.length < 1) {
1518
printModuleHelp();
19+
process.exit(1);
1620
} else {
1721
let options = util.optionsFromArgs(args.splice(1), [
1822
'format', 'date', 'user', 'limit', '=iso']);
@@ -24,8 +28,7 @@ module.exports.dispatcher = function (args) {
2428
printModuleHelp();
2529
break;
2630
default:
27-
util.error("Unknown operatation");
28-
break;
31+
util.printErrorAndExit("Unknown operation");
2932
}
3033
};
3134

@@ -61,18 +64,7 @@ function dumpAuditLog(options) {
6164
.type('application/json')
6265
.send()
6366
.end((err, result) => {
64-
if (err !== null) {
65-
if ('code' in err) {
66-
if (err.code === 'ECONNREFUSED') {
67-
util.error(sprintf("Connection refused: %s:%d", err.address, err.port));
68-
} else {
69-
util.error(sprintf("Error: %s:%s", err.code, err.message));
70-
}
71-
} else {
72-
util.output(err.status + ': ' + err.message);
73-
}
74-
return 1
75-
}
67+
util.handleError(err);
7668
if (format === 'json') {
7769
if ('limit' in options) {
7870
util.output(JSON.stringify(result.body.slice(0, options['limit'])));
@@ -82,7 +74,7 @@ function dumpAuditLog(options) {
8274
} else {
8375
printAuditLogHeader(format);
8476
let counter = 0;
85-
let maxItems = 'limit' in options?options['limit']:1e10;
77+
let maxItems = 'limit' in options ? options['limit'] : 1e10;
8678
let isoTime = 'iso' in options;
8779
for (let line of result.body) {
8880
printAuditLogLine(line, format, isoTime);

bin/jobs_functions.js

Lines changed: 44 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ const request = require('superagent');
55
const config = require('./config').config;
66
const sprintf = require('sprintf-js').sprintf;
77
const fs = require('fs');
8+
const process = require('process');
89

910
module.exports = {
1011
dispatcher: function (args) {
11-
if (args.length === 0)
12-
return printModuleHelp();
12+
if (args.length === 0) {
13+
printModuleHelp();
14+
process.exit(1);
15+
}
1316

1417
switch (args[0].toLowerCase()) {
1518
case 'list': {
@@ -21,6 +24,7 @@ module.exports = {
2124
case 'cancel': {
2225
if (args.length < 2) {
2326
printModuleHelp();
27+
process.exit(1);
2428
} else {
2529
terminateTestJob(args[1]);
2630
}
@@ -29,6 +33,7 @@ module.exports = {
2933
case 'delete': {
3034
if (args.length < 2) {
3135
printModuleHelp();
36+
process.exit(1);
3237
} else {
3338
deleteTestJob(args[1]);
3439
}
@@ -37,14 +42,16 @@ module.exports = {
3742
case 'status': {
3843
if (args.length < 2) {
3944
printModuleHelp();
45+
process.exit(1);
4046
} else {
4147
reportForTestJob(args[1]);
4248
}
4349
break;
4450
}
4551
case 'report': {
46-
if (args.length < 3) {
52+
if (args.length < 4) {
4753
printModuleHelp();
54+
process.exit(1);
4855
} else {
4956
let jobId = args[args.length - 1];
5057
let options = util.optionsFromArgs(args.splice(1), [
@@ -56,8 +63,9 @@ module.exports = {
5663
case 'printreport': {
5764
if (args.length < 2) {
5865
printModuleHelp();
66+
process.exit(1);
5967
} else {
60-
const testJobId = args[ args.length - 1 ];
68+
const testJobId = args[args.length - 1];
6169
printReport(testJobId);
6270
}
6371
break;
@@ -76,8 +84,7 @@ module.exports = {
7684
printModuleHelp();
7785
break;
7886
default:
79-
util.error("Unknown operatation");
80-
break;
87+
util.printErrorAndExit("Unknown operation");
8188
}
8289
},
8390
reportForTestJob: reportForTestJob
@@ -97,25 +104,18 @@ function printModuleHelp() {
97104
}
98105

99106
function terminateTestJob(testjobId) {
100-
let url = config.server + '/api/v1/testjobs'+ '/' + testjobId;
101-
util.output('Canceling job: '+testjobId);
107+
let url = config.server + '/api/v1/testjobs' + '/' + testjobId;
108+
util.output('Canceling job: ' + testjobId);
102109
request.delete(url)
103110
.auth(config.username, config.password)
104111
.accept('application/junit+xml')
105112
.send()
106113
.end((err, result) => {
107114
if (err !== null) {
108115
if (('status' in err) && ('message' in result.body)) {
109-
switch (err['status']) {
110-
case 403:
111-
util.error(err['status'] + ': ' + result.body['message']);
112-
break;
113-
default:
114-
util.error(err['status'] + ': ' + result.body['message']);
115-
return;
116-
}
116+
util.printErrorAndExit(err['status'] + ': ' + result.body['message']);
117117
} else {
118-
util.error(err);
118+
util.printErrorAndExit(err);
119119
}
120120
} else {
121121
util.output('Successfully canceled job');
@@ -124,8 +124,8 @@ function terminateTestJob(testjobId) {
124124
}
125125

126126
function deleteTestJob(testjobId) {
127-
let url = config.server + '/api/v1/testjobs'+ '/' + testjobId + '/delete';
128-
util.output('Deleting job: '+testjobId);
127+
let url = config.server + '/api/v1/testjobs' + '/' + testjobId + '/delete';
128+
util.output('Deleting job: ' + testjobId);
129129
request.delete(url)
130130
.auth(config.username, config.password)
131131
.accept('application/junit+xml')
@@ -134,26 +134,22 @@ function deleteTestJob(testjobId) {
134134
if (err !== null) {
135135
if (('status' in err) && ('message' in result.body)) {
136136
switch (err['status']) {
137-
case 403:
138-
util.error(err['status'] + ': ' + result.body['message']);
139-
break;
140137
case 404:
141-
util.output(`${err['status']}: Testjob not found`);
138+
util.printErrorAndExit(`${err['status']}: Testjob not found`);
142139
break;
143140
default:
144-
util.error(err['status'] + ': ' + result.body['message']);
145-
return;
141+
util.printErrorAndExit(err['status'] + ': ' + result.body['message']);
146142
}
147143
} else {
148-
util.error(err);
144+
util.printErrorAndExit(err);
149145
}
150146
} else {
151147
util.output('Successfully deleted job');
152148
}
153149
})
154150
}
155151

156-
function printReport (testjobId) {
152+
function printReport(testjobId) {
157153
const endPoint = config.server + '/api/v1/testjobs';
158154
const url = endPoint + '/' + testjobId + '/report';
159155
util.output(`Printing report for ${testjobId} ...`);
@@ -162,16 +158,17 @@ function printReport (testjobId) {
162158
.accept('application/json')
163159
.send()
164160
.end((err, res) => {
165-
const jsonReport = res.body;
166161
if (err !== null) {
167162
if (err.status === 404) {
168-
util.output(`Testjob with id ${testjobId} not found`);
163+
util.printErrorAndExit(`Testjob with id ${testjobId} not found`);
169164
} else {
170-
util.output(err.status + ': ' + err.message);
165+
util.printErrorAndExit(err.status + ': ' + err.message);
171166
}
172-
return 1
173167
}
174-
util.output(utility.inspect(jsonReport, { showHidden: false, depth: null}));
168+
if (res) {
169+
const jsonReport = res.body;
170+
util.output(utility.inspect(jsonReport, {showHidden: false, depth: null}));
171+
}
175172
});
176173
}
177174

@@ -209,17 +206,13 @@ function reportForTestJob(testjobId, outputFolder, fileName, format) {
209206
reportFilename += '.pdf';
210207
break;
211208
default:
212-
util.error("Invalid format: " + format);
213-
contentType = '';
214-
break;
209+
util.printErrorAndExit("Invalid format: " + format);
215210
}
216211
} else {
217-
util.error("Output folder exists but is not a directory");
218-
return;
212+
util.printErrorAndExit("Output folder exists but is not a directory");
219213
}
220214
}
221215
if (contentType !== '') {
222-
let success = true;
223216
let url = endPoint + '/' + testjobId + '/report';
224217
let stream;
225218
let reportFileName;
@@ -232,10 +225,13 @@ function reportForTestJob(testjobId, outputFolder, fileName, format) {
232225
.accept(contentType)
233226
.on('response', function (response) {
234227
if (response.status !== 200) {
235-
success = false;
236228
if (reportFileName) {
237229
fs.unlinkSync(reportFileName)
238230
}
231+
util.printErrorAndExit(`Status code: ${response.status}`);
232+
} else {
233+
util.output('Report created successfully');
234+
process.exit(0);
239235
}
240236
}).send();
241237
if (stream) {
@@ -246,16 +242,9 @@ function reportForTestJob(testjobId, outputFolder, fileName, format) {
246242
util.output('Status of job ' + testjobId + ': ' + result.body.status);
247243
} else {
248244
if (('status' in err) && ('message' in result.body)) {
249-
switch (err['status']) {
250-
case 403:
251-
util.error(err['status'] + ': ' + result.body['message']);
252-
break;
253-
default:
254-
util.error(err['status'] + ': ' + result.body['message']);
255-
return;
256-
}
245+
util.printErrorAndExit(err['status'] + ': ' + result.body['message']);
257246
} else {
258-
util.error(err);
247+
util.printErrorAndExit(err);
259248
}
260249
}
261250
})
@@ -272,8 +261,7 @@ function listJobs(options) {
272261
.send()
273262
.end((err, res) => {
274263
if (err !== null) {
275-
util.output(err.status + ': ' + err.message);
276-
return 1
264+
util.printErrorAndExit(err.status + ': ' + err.message);
277265
}
278266
let dataFromServer = res.body;
279267
if (Array.isArray(dataFromServer) && 'status' in options) {
@@ -294,8 +282,7 @@ function listJobs(options) {
294282
dumpArrayAsJson(dataFromServer);
295283
break;
296284
default:
297-
util.error('Unrecognized format');
298-
break;
285+
util.printErrorAndExit('Unrecognized format');
299286
}
300287
});
301288
}
@@ -309,7 +296,7 @@ function pruneJobs(options) {
309296
let date = new Date(parseInt(matchResult[1]), parseInt(matchResult[2]) - 1, parseInt(matchResult[3]));
310297
let tmpDate = date.toISOString();
311298
tmpDate = tmpDate.replace('.000Z', 'Z');
312-
url += '?before='+encodeURIComponent(tmpDate);
299+
url += '?before=' + encodeURIComponent(tmpDate);
313300
}
314301
}
315302
request.delete(url)
@@ -320,17 +307,16 @@ function pruneJobs(options) {
320307
if (err !== null) {
321308
if ('code' in err) {
322309
if (err.code === 'ECONNREFUSED') {
323-
util.error(sprintf("Connection refused: %s:%d", err.address, err.port));
310+
util.printErrorAndExit(sprintf("Connection refused: %s:%d", err.address, err.port));
324311
} else {
325-
util.error(sprintf("Error: %s:%s", err.code, err.message));
312+
util.printErrorAndExit(sprintf("Error: %s:%s", err.code, err.message));
326313
}
327314
} else {
328315
if ('message' in res.body)
329-
util.output(res.body['message']);
316+
util.printErrorAndExit(res.body['message']);
330317
else
331-
util.output(err.status + ': ' + err.message);
318+
util.printErrorAndExit(err.status + ': ' + err.message);
332319
}
333-
return 1
334320
}
335321
let jobsPruned = JSON.parse(res.request.response.body);
336322
util.output("Pruned " + jobsPruned + " jobs from the database.")

0 commit comments

Comments
 (0)