From b0061b88adc4ac5c61dfacfa2be7530a05e3a8b9 Mon Sep 17 00:00:00 2001 From: Shruti Ranganathan Jothi Date: Wed, 8 Mar 2017 16:28:28 -0800 Subject: [PATCH] ability to write logs of test run to a file for nodejs tests --- README | 3 ++ server/tests/js_tests/libs/utils.js | 52 +++++++++++++++++-- server/tests/js_tests/package.json | 2 + .../js_tests/tests/discoverServerUnderTest.js | 7 ++- .../tests/hideApplicationInHiddenState.js | 20 ++++++- 5 files changed, 77 insertions(+), 7 deletions(-) diff --git a/README b/README index cd96646..82ca389 100644 --- a/README +++ b/README @@ -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. diff --git a/server/tests/js_tests/libs/utils.js b/server/tests/js_tests/libs/utils.js index 7d132ef..1d746d9 100644 --- a/server/tests/js_tests/libs/utils.js +++ b/server/tests/js_tests/libs/utils.js @@ -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; diff --git a/server/tests/js_tests/package.json b/server/tests/js_tests/package.json index 95070c5..942c565 100644 --- a/server/tests/js_tests/package.json +++ b/server/tests/js_tests/package.json @@ -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" } } diff --git a/server/tests/js_tests/tests/discoverServerUnderTest.js b/server/tests/js_tests/tests/discoverServerUnderTest.js index 4432b15..c63490a 100644 --- a/server/tests/js_tests/tests/discoverServerUnderTest.js +++ b/server/tests/js_tests/tests/discoverServerUnderTest.js @@ -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)); } }) diff --git a/server/tests/js_tests/tests/hideApplicationInHiddenState.js b/server/tests/js_tests/tests/hideApplicationInHiddenState.js index 73c15b9..9b1d73b 100644 --- a/server/tests/js_tests/tests/hideApplicationInHiddenState.js +++ b/server/tests/js_tests/tests/hideApplicationInHiddenState.js @@ -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)); }