ability to write logs of test run to a file for nodejs tests

This commit is contained in:
Shruti Ranganathan Jothi
2017-03-08 16:28:28 -08:00
parent 11e4eaf2ac
commit b0061b88ad
5 changed files with 77 additions and 7 deletions

3
README
View File

@@ -119,3 +119,6 @@ Options:
running [string] [required]
--application, --app Application to test [string] [required]
--help, -h Show help [boolean]
Log file of test run is written in log.txt in the server/tests/js_tests/tests
folder.

View File

@@ -2,19 +2,63 @@
const colors = require("colors/safe");
const sprintf = require("sprintf-js").sprintf;
const winston = require("winston");
const moment = require("moment");
function printTestInfo(test, msg) {
return console.log(sprintf("%-20s : %-s\n%-20s : %-s", "Test", test, "Description", msg));
const levels = { error: 0, warn: 1, info: 2, verbose: 3, debug: 4 };
const transports = [
new (winston.transports.Console)({
level: "info",
formatter: consoleFormatter
}),
new (winston.transports.File)({
level: "debug",
name: "log_file",
filename: "log.txt",
json: false,
formatter: fileFormatter,
options: { flags: "w" }
}),
];
winston.configure({ levels: levels, transports: transports });
function consoleFormatter(options) {
let str = options.message;
switch (options.level) {
case "error": return colors.red(str);
case "warn": return colors.yellow(str);
case "debug": return colors.blue(str);
case "info":
default: return str;
}
}
function fileFormatter(options) {
let str = options.message;
return str;
}
function printTestInfo(testFile, description) {
winston.info(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "TEST", testFile));
winston.info(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "DESCRIPTION", description));
}
function printTestSuccess() {
return console.log(colors.green("TEST PASSED\n"));
winston.info(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "RESULT", "TEST PASSED\n"));
}
function printTestFailure(err) {
return console.log(colors.red(sprintf("%-20s : %-s\n", "TEST FAILED", err)));
winston.error(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "RESULT", "TEST FAILED " + err + "\n"));
}
function printDebug(msg) {
winston.debug(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "DEBUG", msg));
}
module.exports.printTestInfo = printTestInfo;
module.exports.printTestSuccess = printTestSuccess;
module.exports.printTestFailure = printTestFailure;
module.exports.printDebug = printDebug;

View File

@@ -12,10 +12,12 @@
"license": "BSD-2-Clause",
"dependencies": {
"colors": "^1.1.2",
"moment": "^2.17.1",
"node-ssdp": "^3.2.0",
"q": "~1.4.1",
"request": "^2.78.0",
"sprintf-js": "~1.0.3",
"winston": "^2.3.1",
"yargs": "^6.3.0"
}
}

View File

@@ -26,15 +26,20 @@ function test() {
.then(function () {
utils.printTestInfo(__filename.slice(__dirname.length + 1) , "Perform DIAL discovery and ensure that the server under test is discovered");
})
.then(dial.discover)
.then(function discover() {
utils.printDebug("Performing discovery ..");
return dial.discover();
})
.then(function findServerInList(servers) {
var found = false;
servers.forEach(function (server) {
if(server.host === testServer) {
utils.printDebug("Found " + server.host + " in discovered list of servers");
found = true;
}
});
if(!found) {
utils.printDebug("Did not find " + testServer + " in discovered list of servers");
return Q.reject(new Error("DIAL client was not able to discover the server under test : " + testServer));
}
})

View File

@@ -39,25 +39,36 @@ function test() {
if(!result || !result.state) {
return Q.reject(new Error("Could not retrieve current " + app + " application state"));
}
utils.printDebug("Application status is " + result.state);
if(result.state !== "hidden") {
if(result.state === "stopped") {
// Launch and hide app
return dial.launchApplication(host, app)
.then(function () {
utils.printDebug("Requested server to launch application ..");
})
.delay(timeToWaitForStateChange)
.then(dial.getApplicationStatus.bind(null, host, app))
.then(function checkAppStatus(result) {
if(!result || !result.state) {
return Q.reject(new Error("Could not retrieve current " + app + " application state"));
}
utils.printDebug("Application status is " + result.state);
if(result.state !== "running") {
return Q.reject(new Error("Expected " + app + " app status to be running but the state was " + result.state));
}
console.log("app was running");
})
.then(function () {
utils.printDebug("Hide application ..");
})
.then(dial.hideApplication.bind(null, host, app))
.delay(timeToWaitForStateChange);
}
return dial.hideApplication(host, app)
return new Q()
.then(function () {
utils.printDebug("Hide application ..");
return dial.hideApplication(host, app);
})
.delay(timeToWaitForStateChange);
}
})
@@ -68,11 +79,15 @@ function test() {
if(!result || !result.state) {
return Q.reject(new Error("Could not retrieve current " + app + " application state"));
}
utils.printDebug("Application status is now " + result.state);
if(result.state !== "hidden") {
return Q.reject(new Error("Expected " + app + " app status to be hidden but the state was " + result.state));
}
})
.then(function () {
utils.printDebug("Hide application ..");
})
.then(dial.hideApplication.bind(null, host, app))
.then(function (response) {
if(response.statusCode !== 200) {
@@ -85,6 +100,7 @@ function test() {
if(!result || !result.state) {
return Q.reject(new Error("Could not retrieve current " + app + " application state"));
}
utils.printDebug("Application status is " + result.state);
if(result.state !== "hidden") {
return Q.reject(new Error("Expected " + app + " app status to be hidden but the state was " + result.state));
}