Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ When a config file is present, it is possible to override the values using the c
There is currently no support for encrypting the config file but normal file system security should make it possible to
restrict reading it to the user running the tool.

TestEngine-CLI will return with exit status code 0 if it successfully carried out the operation. It will return with
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
a project or some user couldn't be added while importing.

## Test Jobs
The tool can submit test jobs, list jobs which has been submitted (only admins can see other users' jobs) and purge old
jobs from the server.
Expand Down
26 changes: 9 additions & 17 deletions bin/auditlog_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@ const request = require('superagent');
const config = require('./config').config;
const sprintf = require('sprintf-js').sprintf;
const util = require('./shared_utils');
const process = require('process');

module.exports.dispatcher = function (args) {
if (args.length === 0)
return printModuleHelp();
if (args.length === 0) {
printModuleHelp();
process.exit(1);
}

switch (args[0].toLowerCase()) {
case 'dump':
if (args.length < 1) {
printModuleHelp();
process.exit(1);
} else {
let options = util.optionsFromArgs(args.splice(1), [
'format', 'date', 'user', 'limit', '=iso']);
Expand All @@ -24,8 +28,7 @@ module.exports.dispatcher = function (args) {
printModuleHelp();
break;
default:
util.error("Unknown operatation");
break;
util.printErrorAndExit("Unknown operation");
}
};

Expand Down Expand Up @@ -61,18 +64,7 @@ function dumpAuditLog(options) {
.type('application/json')
.send()
.end((err, result) => {
if (err !== null) {
if ('code' in err) {
if (err.code === 'ECONNREFUSED') {
util.error(sprintf("Connection refused: %s:%d", err.address, err.port));
} else {
util.error(sprintf("Error: %s:%s", err.code, err.message));
}
} else {
util.output(err.status + ': ' + err.message);
}
return 1
}
util.handleError(err);
if (format === 'json') {
if ('limit' in options) {
util.output(JSON.stringify(result.body.slice(0, options['limit'])));
Expand All @@ -82,7 +74,7 @@ function dumpAuditLog(options) {
} else {
printAuditLogHeader(format);
let counter = 0;
let maxItems = 'limit' in options?options['limit']:1e10;
let maxItems = 'limit' in options ? options['limit'] : 1e10;
let isoTime = 'iso' in options;
for (let line of result.body) {
printAuditLogLine(line, format, isoTime);
Expand Down
102 changes: 44 additions & 58 deletions bin/jobs_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ const request = require('superagent');
const config = require('./config').config;
const sprintf = require('sprintf-js').sprintf;
const fs = require('fs');
const process = require('process');

module.exports = {
dispatcher: function (args) {
if (args.length === 0)
return printModuleHelp();
if (args.length === 0) {
printModuleHelp();
process.exit(1);
}

switch (args[0].toLowerCase()) {
case 'list': {
Expand All @@ -21,6 +24,7 @@ module.exports = {
case 'cancel': {
if (args.length < 2) {
printModuleHelp();
process.exit(1);
} else {
terminateTestJob(args[1]);
}
Expand All @@ -29,6 +33,7 @@ module.exports = {
case 'delete': {
if (args.length < 2) {
printModuleHelp();
process.exit(1);
} else {
deleteTestJob(args[1]);
}
Expand All @@ -37,14 +42,16 @@ module.exports = {
case 'status': {
if (args.length < 2) {
printModuleHelp();
process.exit(1);
} else {
reportForTestJob(args[1]);
}
break;
}
case 'report': {
if (args.length < 3) {
if (args.length < 4) {
printModuleHelp();
process.exit(1);
} else {
let jobId = args[args.length - 1];
let options = util.optionsFromArgs(args.splice(1), [
Expand All @@ -56,8 +63,9 @@ module.exports = {
case 'printreport': {
if (args.length < 2) {
printModuleHelp();
process.exit(1);
} else {
const testJobId = args[ args.length - 1 ];
const testJobId = args[args.length - 1];
printReport(testJobId);
}
break;
Expand All @@ -76,8 +84,7 @@ module.exports = {
printModuleHelp();
break;
default:
util.error("Unknown operatation");
break;
util.printErrorAndExit("Unknown operation");
}
},
reportForTestJob: reportForTestJob
Expand All @@ -96,25 +103,18 @@ function printModuleHelp() {
}

function terminateTestJob(testjobId) {
let url = config.server + '/api/v1/testjobs'+ '/' + testjobId;
util.output('Canceling job: '+testjobId);
let url = config.server + '/api/v1/testjobs' + '/' + testjobId;
util.output('Canceling job: ' + testjobId);
request.delete(url)
.auth(config.username, config.password)
.accept('application/junit+xml')
.send()
.end((err, result) => {
if (err !== null) {
if (('status' in err) && ('message' in result.body)) {
switch (err['status']) {
case 403:
util.error(err['status'] + ': ' + result.body['message']);
break;
default:
util.error(err['status'] + ': ' + result.body['message']);
return;
}
util.printErrorAndExit(err['status'] + ': ' + result.body['message']);
} else {
util.error(err);
util.printErrorAndExit(err);
}
} else {
util.output('Successfully canceled job');
Expand All @@ -123,8 +123,8 @@ function terminateTestJob(testjobId) {
}

function deleteTestJob(testjobId) {
let url = config.server + '/api/v1/testjobs'+ '/' + testjobId + '/delete';
util.output('Deleting job: '+testjobId);
let url = config.server + '/api/v1/testjobs' + '/' + testjobId + '/delete';
util.output('Deleting job: ' + testjobId);
request.delete(url)
.auth(config.username, config.password)
.accept('application/junit+xml')
Expand All @@ -133,26 +133,22 @@ function deleteTestJob(testjobId) {
if (err !== null) {
if (('status' in err) && ('message' in result.body)) {
switch (err['status']) {
case 403:
util.error(err['status'] + ': ' + result.body['message']);
break;
case 404:
util.output(`${err['status']}: Testjob not found`);
util.printErrorAndExit(`${err['status']}: Testjob not found`);
break;
default:
util.error(err['status'] + ': ' + result.body['message']);
return;
util.printErrorAndExit(err['status'] + ': ' + result.body['message']);
}
} else {
util.error(err);
util.printErrorAndExit(err);
}
} else {
util.output('Successfully deleted job');
}
})
}

function printReport (testjobId) {
function printReport(testjobId) {
const endPoint = config.server + '/api/v1/testjobs';
const url = endPoint + '/' + testjobId + '/report';
util.output(`Printing report for ${testjobId} ...`);
Expand All @@ -161,16 +157,17 @@ function printReport (testjobId) {
.accept('application/json')
.send()
.end((err, res) => {
const jsonReport = res.body;
if (err !== null) {
if (err.status === 404) {
util.output(`Testjob with id ${testjobId} not found`);
util.printErrorAndExit(`Testjob with id ${testjobId} not found`);
} else {
util.output(err.status + ': ' + err.message);
util.printErrorAndExit(err.status + ': ' + err.message);
}
return 1
}
util.output(utility.inspect(jsonReport, { showHidden: false, depth: null}));
if (res) {
const jsonReport = res.body;
util.output(utility.inspect(jsonReport, {showHidden: false, depth: null}));
}
});
}

Expand Down Expand Up @@ -208,17 +205,13 @@ function reportForTestJob(testjobId, outputFolder, fileName, format) {
reportFilename += '.pdf';
break;
default:
util.error("Invalid format: " + format);
contentType = '';
break;
util.printErrorAndExit("Invalid format: " + format);
}
} else {
util.error("Output folder exists but is not a directory");
return;
util.printErrorAndExit("Output folder exists but is not a directory");
}
}
if (contentType !== '') {
let success = true;
let url = endPoint + '/' + testjobId + '/report';
let stream;
let reportFileName;
Expand All @@ -231,10 +224,13 @@ function reportForTestJob(testjobId, outputFolder, fileName, format) {
.accept(contentType)
.on('response', function (response) {
if (response.status !== 200) {
success = false;
if (reportFileName) {
fs.unlinkSync(reportFileName)
}
util.printErrorAndExit(`Status code: ${response.status}`);
} else {
util.output('Report created successfully');
process.exit(0);
}
}).send();
if (stream) {
Expand All @@ -245,16 +241,9 @@ function reportForTestJob(testjobId, outputFolder, fileName, format) {
util.output('Status of job ' + testjobId + ': ' + result.body.status);
} else {
if (('status' in err) && ('message' in result.body)) {
switch (err['status']) {
case 403:
util.error(err['status'] + ': ' + result.body['message']);
break;
default:
util.error(err['status'] + ': ' + result.body['message']);
return;
}
util.printErrorAndExit(err['status'] + ': ' + result.body['message']);
} else {
util.error(err);
util.printErrorAndExit(err);
}
}
})
Expand All @@ -271,8 +260,7 @@ function listJobs(options) {
.send()
.end((err, res) => {
if (err !== null) {
util.output(err.status + ': ' + err.message);
return 1
util.printErrorAndExit(err.status + ': ' + err.message);
}
let dataFromServer = res.body;
if (Array.isArray(dataFromServer) && 'status' in options) {
Expand All @@ -293,8 +281,7 @@ function listJobs(options) {
dumpArrayAsJson(dataFromServer);
break;
default:
util.error('Unrecognized format');
break;
util.printErrorAndExit('Unrecognized format');
}
});
}
Expand All @@ -308,7 +295,7 @@ function pruneJobs(options) {
let date = new Date(parseInt(matchResult[1]), parseInt(matchResult[2]) - 1, parseInt(matchResult[3]));
let tmpDate = date.toISOString();
tmpDate = tmpDate.replace('.000Z', 'Z');
url += '?before='+encodeURIComponent(tmpDate);
url += '?before=' + encodeURIComponent(tmpDate);
}
}
request.delete(url)
Expand All @@ -319,17 +306,16 @@ function pruneJobs(options) {
if (err !== null) {
if ('code' in err) {
if (err.code === 'ECONNREFUSED') {
util.error(sprintf("Connection refused: %s:%d", err.address, err.port));
util.printErrorAndExit(sprintf("Connection refused: %s:%d", err.address, err.port));
} else {
util.error(sprintf("Error: %s:%s", err.code, err.message));
util.printErrorAndExit(sprintf("Error: %s:%s", err.code, err.message));
}
} else {
if ('message' in res.body)
util.output(res.body['message']);
util.printErrorAndExit(res.body['message']);
else
util.output(err.status + ': ' + err.message);
util.printErrorAndExit(err.status + ': ' + err.message);
}
return 1
}
let jobsPruned = JSON.parse(res.request.response.body);
util.output("Pruned " + jobsPruned + " jobs from the database.")
Expand Down
Loading