diff --git a/server/tests/js_tests/libs/utils.js b/server/tests/js_tests/libs/utils.js index c71da24..c54cf1b 100644 --- a/server/tests/js_tests/libs/utils.js +++ b/server/tests/js_tests/libs/utils.js @@ -5,6 +5,7 @@ const sprintf = require("sprintf-js").sprintf; const winston = require("winston"); const moment = require("moment"); const keypress = require("keypress"); +const Q = require("q"); const levels = { error: 0, warn: 1, info: 2, verbose: 3, debug: 4 }; const transports = [ @@ -42,6 +43,18 @@ function fileFormatter(options) { return str; } +function setLogLevel(level) { + winston.remove(winston.transports.Console); + return winston.add(winston.transports.Console, { + level: "debug", + name: "log_file", + filename: "js_tests_log.txt", + json: false, + formatter: fileFormatter, + options: { flags: "w" } + }); +} + function ask(description) { return new Q.Promise(function (resolve, reject) { // make `process.stdin` begin emitting "keypress" events @@ -49,7 +62,7 @@ function ask(description) { process.stdin.setRawMode(true); process.stdin.resume(); - console.log(description); + winston.info(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "MANUAL STEP", description)); // listen for the "keypress" event process.stdin.on("keypress", function (ch, key) { @@ -82,7 +95,14 @@ function printDebug(msg) { winston.debug(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "DEBUG", msg)); } +function printInfo(msg) { + winston.info(sprintf("[%-s] %-20s : %-s", moment().format("YYYY-MM-DDTHH:mm:ssZ"), "INFO", msg)); +} + module.exports.printTestInfo = printTestInfo; module.exports.printTestSuccess = printTestSuccess; module.exports.printTestFailure = printTestFailure; module.exports.printDebug = printDebug; +module.exports.printInfo = printInfo; +module.exports.ask = ask; +module.exports.setLogLevel = setLogLevel; diff --git a/server/tests/js_tests/tests/launchHideStopStressTest.js b/server/tests/js_tests/tests/launchHideStopStressTest.js new file mode 100644 index 0000000..c6c4c7c --- /dev/null +++ b/server/tests/js_tests/tests/launchHideStopStressTest.js @@ -0,0 +1,137 @@ +"use strict"; + +var dial = require("../libs/dialClient.js"), + utils = require("../libs/utils.js"), + Q = require("q"); + +const argv = require("yargs") + .usage("\nUsage: node " + __filename.slice(__dirname.length + 1) + "[options]") + .option("host", { + describe: "IP address of host on which DIAL server under test is running", + type: "string", + demand: true + }) + .option("application", { + alias: "app", + describe: "Application to test", + type: "string", + demand: true + }) + .help("help").alias("help", "h").argv; + +function test() { + var host = argv.host; + var app = argv.application; + var timeToWaitForStateChange = argv.timeToWaitForStateChange || 5000; + var maxCount = 100; + var count = 0; + + + utils.printTestInfo("TEST " + __filename.slice(__dirname.length + 1), "Launch, hide and stop app cycle - stress testing"); + + function cycle() { + return new Q() + // Launch app + .then(function startApp(state) { + utils.printDebug("Launching application .."); + return dial.launchApplication(host, app) + .then(function (response) { + if(response.statusCode !== 201) { + return Q.reject(new Error("Error launching " + app + " application. Expected status code 201 from DIAL server but got " + response.statusCode)); + } + }); + }) + .then(function () { + utils.printDebug("Wait for " + timeToWaitForStateChange + " ms for state change to happen"); + }) + .delay(timeToWaitForStateChange) + .then(function () { + utils.printDebug("Querying application state .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function getCurrentAppState(result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "running") { + return Q.reject(new Error("Expected " + app + " application to be in running state, but querying state returned state as" + result.state)); + } + }) + + // Hide app + .then(function hideApp(state) { + utils.printDebug("Hiding application .."); + return dial.hideApplication(host, app) + .then(function (response) { + if(response.statusCode !== 200) { + return Q.reject(new Error("Error hiding " + app + " application. Expected status code 200 from DIAL server but got " + response.statusCode)); + } + }); + }) + .then(function () { + utils.printDebug("Wait for " + timeToWaitForStateChange + " ms for state change to happen"); + }) + .delay(timeToWaitForStateChange) + .then(function () { + utils.printDebug("Querying application state .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function getCurrentAppState(result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "hidden") { + return Q.reject(new Error("Expected " + app + " application to be in hidden state, but querying state returned state as" + result.state)); + } + }) + + // Stop app + .then(function stopApp(state) { + return dial.stopApplication(host, app) + .then(function (response) { + if(response.statusCode !== 200) { + return Q.reject(new Error("Error stopping " + app + " application. Expected status code 200 from DIAL server but got " + response.statusCode)); + } + }); + }) + .then(function () { + utils.printDebug("Wait for " + timeToWaitForStateChange + " ms for state change to happen"); + }) + .delay(timeToWaitForStateChange) + .then(function () { + utils.printDebug("Querying application state .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function getCurrentAppState(result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "stopped") { + return Q.reject(new Error("Expected " + app + " application to be in stopped state, but querying state returned state as" + result.state)); + } + }) + + .then(function () { + count++; + utils.printInfo("Executed cycle " + count + " times"); + if(count === maxCount) { + utils.printInfo("TEST END"); + } + else { + return cycle(); + } + }) + } + + return cycle(); +} + +module.exports.test = test; + +if (require.main === module) { + test() + .done(); +} diff --git a/server/tests/js_tests/tests/stateChangesWithoutDIAL.js b/server/tests/js_tests/tests/stateChangesWithoutDIAL.js new file mode 100644 index 0000000..0bd4790 --- /dev/null +++ b/server/tests/js_tests/tests/stateChangesWithoutDIAL.js @@ -0,0 +1,122 @@ +"use strict"; + +var dial = require("../libs/dialClient.js"), + utils = require("../libs/utils.js"), + Q = require("q"); + +const argv = require("yargs") + .usage("\nUsage: node " + __filename.slice(__dirname.length + 1) + "[options]") + .option("host", { + describe: "IP address of host on which DIAL server under test is running", + type: "string", + demand: true + }) + .option("application", { + alias: "app", + describe: "Application to test", + type: "string", + demand: true + }) + .help("help").alias("help", "h").argv; + +function test() { + var host = argv.host; + var app = argv.application; + + return new Q() + .then(function () { + utils.printTestInfo(__filename.slice(__dirname.length + 1), "Launch/Hide/Stop " + app + " without DIAL server and query application states"); + }) + .then(function () { + utils.printDebug("Querying application status .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function (result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + }) + + .then(function stopApp() { + return utils.ask("If application is running, stop it and press Enter once the application is stopped"); + }) + .then(function () { + utils.printDebug("Querying application status .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function (result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "stopped") { + return Q.reject(new Error("Application was expected to be in stopped state but was in " + result.state + " state")); + } + }) + + .then(function launchApp() { + return utils.ask("Launch the application manually and press Enter when the application is launched"); + }) + .then(function () { + utils.printDebug("Querying application status .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function (result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "running") { + return Q.reject(new Error("Application was expected to be in running state but was in " + result.state + " state")); + } + }) + + .then(function hideApp() { + return utils.ask("Hide the application manually and press Enter when the application is launched"); + }) + .then(function () { + utils.printDebug("Querying application status .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function (result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "hidden") { + return Q.reject(new Error("Application was expected to be in hidden state but was in " + result.state + " state")); + } + }) + + .then(function stopApp() { + return utils.ask("Stop the application manually and press Enter when the application is launched"); + }) + .then(function () { + utils.printDebug("Querying application status .."); + }) + .then(dial.getApplicationStatus.bind(null, host, app)) + .then(function (result) { + if(!result || !result.state) { + return Q.reject(new Error("Error retrieving current " + app + " application state")); + } + utils.printDebug("Application is in " + result.state + " state"); + if(result.state !== "stopped") { + return Q.reject(new Error("Application was expected to be in stopped state but was in " + result.state + " state")); + } + }) + + .then(function () { + utils.printTestSuccess() + }) + .fail(function handleError(err) { + utils.printTestFailure(err); + }); +} + +module.exports.test = test; + +if (require.main === module) { + test() + .done(); +}