From f422e92496b44dd7cc4bada3f5ed8525305a367f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Mon, 7 Aug 2017 09:05:15 +0300 Subject: [PATCH] MXS-1300: Return output instead of printing it The `execute` command now returns the output of the command instead of printint it. This allows the tests to actually test the output of the commands instead manually verifying that it is correct. It also allows the library part to be used as an actual library that only returns data. --- maxctrl/lib/call.js | 2 +- maxctrl/lib/cluster.js | 19 +++++++++++-------- maxctrl/lib/common.js | 29 ++++++++++++++--------------- maxctrl/test/unknown.js | 1 + 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/maxctrl/lib/call.js b/maxctrl/lib/call.js index 3f9344cbe..5d6a86323 100644 --- a/maxctrl/lib/call.js +++ b/maxctrl/lib/call.js @@ -33,7 +33,7 @@ exports.builder = function(yargs) { return doAsyncRequest(host, 'maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'), function(resp) { - logger.log(JSON.stringify(resp, null, 4)) + return JSON.stringify(resp, null, 4) }, { method: verb }) }) }) diff --git a/maxctrl/lib/cluster.js b/maxctrl/lib/cluster.js index b4ee9cc3e..2d0342147 100644 --- a/maxctrl/lib/cluster.js +++ b/maxctrl/lib/cluster.js @@ -110,6 +110,7 @@ exports.builder = function(yargs) { maxctrl(argv, function(host) { return getDiffs(host, argv.target) .then(function(diffs) { + var output = [] var src = diffs[0] var dest = diffs[1] @@ -119,16 +120,16 @@ exports.builder = function(yargs) { var changed = getChangedObjects(src[i].data, dest[i].data) if (newObj.length) { - logger.log("New:", i) - logger.log(colors.green(JSON.stringify(newObj, null, 4))) + output.push("New:", i) + output.push(colors.green(JSON.stringify(newObj, null, 4))) } if (oldObj.length) { - logger.log("Deleted:", i) - logger.log(colors.red(JSON.stringify(oldObj, null, 4))) + output.push("Deleted:", i) + output.push(colors.red(JSON.stringify(oldObj, null, 4))) } if (changed.length) { - logger.log("Changed:", i) - logger.log(colors.yellow(JSON.stringify(changed, null, 4))) + output.push("Changed:", i) + output.push(colors.yellow(JSON.stringify(changed, null, 4))) } }) endpoints.forEach(function(i) { @@ -136,10 +137,12 @@ exports.builder = function(yargs) { // to compare individual resources and resource collections var changed = getChangedObjects([src[i].data], [dest[i].data]) if (changed.length) { - logger.log("Changed:", i) - logger.log(colors.yellow(JSON.stringify(changed, null, 4))) + output.push("Changed:", i) + output.push(colors.yellow(JSON.stringify(changed, null, 4))) } }) + + return output.join(require('os').EOL) }) }) }) diff --git a/maxctrl/lib/common.js b/maxctrl/lib/common.js index ccc4e210d..8ab16e147 100644 --- a/maxctrl/lib/common.js +++ b/maxctrl/lib/common.js @@ -15,24 +15,17 @@ var request = require('request-promise-native'); var colors = require('colors/safe'); var Table = require('cli-table'); var consoleLib = require('console') -var fs = require('fs') +var os = require('os') module.exports = function() { this._ = require('lodash-getpath') - this.logger = console - // The main entry point into the library. This function is used to do // cluster health checks and to propagate the commands to multiple // servers. this.maxctrl = function(argv, cb) { - if (argv.quiet) { - this.logger = new consoleLib.Console(fs.createWriteStream('/dev/null'), - fs.createWriteStream('/dev/null')) - } - this.argv = argv if (!argv.hosts || argv.hosts.length < 1) { @@ -42,14 +35,21 @@ module.exports = function() { return pingCluster(argv.hosts) .then(function() { var promises = [] + var rval = [] argv.hosts.forEach(function(i) { - promises.push(cb(i)) + promises.push(cb(i) + .then(function(output) { + if (argv.hosts.length > 1) { + rval.push(colors.yellow(i)) + } + rval.push(output) + })) }) return Promise.all(promises) .then(function() { - argv.resolve() + argv.resolve(argv.quiet ? undefined : rval.join(os.EOL)) }, function(err) { argv.reject(err) }) @@ -86,7 +86,7 @@ module.exports = function() { table.push(row) }) - logger.log(table.toString()) + return table.toString() }) } @@ -119,7 +119,7 @@ module.exports = function() { table.push(row) }) - logger.log(table.toString()) + return table.toString() }) } @@ -145,7 +145,7 @@ module.exports = function() { table.push(o) }) - logger.log(table.toString()) + return table.toString() }) } @@ -190,8 +190,7 @@ module.exports = function() { return cb(res) } else { // Request OK, no data or data is ignored - logger.log(colors.green('OK')) - return Promise.resolve() + return Promise.resolve(colors.green('OK')) } }, function(err) { if (err.response && err.response.body) { diff --git a/maxctrl/test/unknown.js b/maxctrl/test/unknown.js index edcf8d893..7f818095a 100644 --- a/maxctrl/test/unknown.js +++ b/maxctrl/test/unknown.js @@ -19,6 +19,7 @@ describe("Unknown Commands", function() { 'alter', 'rotate', 'call', + 'cluster' ] endpoints.forEach(function (i) {