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.
This commit is contained in:
Markus Mäkelä
2017-08-07 09:05:15 +03:00
parent 9a04dd0311
commit f422e92496
4 changed files with 27 additions and 24 deletions

View File

@ -33,7 +33,7 @@ exports.builder = function(yargs) {
return doAsyncRequest(host, 'maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'), return doAsyncRequest(host, 'maxscale/modules/' + argv.module + '/' + argv.command + '?' + argv.parameters.join('&'),
function(resp) { function(resp) {
logger.log(JSON.stringify(resp, null, 4)) return JSON.stringify(resp, null, 4)
}, { method: verb }) }, { method: verb })
}) })
}) })

View File

@ -110,6 +110,7 @@ exports.builder = function(yargs) {
maxctrl(argv, function(host) { maxctrl(argv, function(host) {
return getDiffs(host, argv.target) return getDiffs(host, argv.target)
.then(function(diffs) { .then(function(diffs) {
var output = []
var src = diffs[0] var src = diffs[0]
var dest = diffs[1] var dest = diffs[1]
@ -119,16 +120,16 @@ exports.builder = function(yargs) {
var changed = getChangedObjects(src[i].data, dest[i].data) var changed = getChangedObjects(src[i].data, dest[i].data)
if (newObj.length) { if (newObj.length) {
logger.log("New:", i) output.push("New:", i)
logger.log(colors.green(JSON.stringify(newObj, null, 4))) output.push(colors.green(JSON.stringify(newObj, null, 4)))
} }
if (oldObj.length) { if (oldObj.length) {
logger.log("Deleted:", i) output.push("Deleted:", i)
logger.log(colors.red(JSON.stringify(oldObj, null, 4))) output.push(colors.red(JSON.stringify(oldObj, null, 4)))
} }
if (changed.length) { if (changed.length) {
logger.log("Changed:", i) output.push("Changed:", i)
logger.log(colors.yellow(JSON.stringify(changed, null, 4))) output.push(colors.yellow(JSON.stringify(changed, null, 4)))
} }
}) })
endpoints.forEach(function(i) { endpoints.forEach(function(i) {
@ -136,10 +137,12 @@ exports.builder = function(yargs) {
// to compare individual resources and resource collections // to compare individual resources and resource collections
var changed = getChangedObjects([src[i].data], [dest[i].data]) var changed = getChangedObjects([src[i].data], [dest[i].data])
if (changed.length) { if (changed.length) {
logger.log("Changed:", i) output.push("Changed:", i)
logger.log(colors.yellow(JSON.stringify(changed, null, 4))) output.push(colors.yellow(JSON.stringify(changed, null, 4)))
} }
}) })
return output.join(require('os').EOL)
}) })
}) })
}) })

View File

@ -15,24 +15,17 @@ var request = require('request-promise-native');
var colors = require('colors/safe'); var colors = require('colors/safe');
var Table = require('cli-table'); var Table = require('cli-table');
var consoleLib = require('console') var consoleLib = require('console')
var fs = require('fs') var os = require('os')
module.exports = function() { module.exports = function() {
this._ = require('lodash-getpath') this._ = require('lodash-getpath')
this.logger = console
// The main entry point into the library. This function is used to do // The main entry point into the library. This function is used to do
// cluster health checks and to propagate the commands to multiple // cluster health checks and to propagate the commands to multiple
// servers. // servers.
this.maxctrl = function(argv, cb) { this.maxctrl = function(argv, cb) {
if (argv.quiet) {
this.logger = new consoleLib.Console(fs.createWriteStream('/dev/null'),
fs.createWriteStream('/dev/null'))
}
this.argv = argv this.argv = argv
if (!argv.hosts || argv.hosts.length < 1) { if (!argv.hosts || argv.hosts.length < 1) {
@ -42,14 +35,21 @@ module.exports = function() {
return pingCluster(argv.hosts) return pingCluster(argv.hosts)
.then(function() { .then(function() {
var promises = [] var promises = []
var rval = []
argv.hosts.forEach(function(i) { 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) return Promise.all(promises)
.then(function() { .then(function() {
argv.resolve() argv.resolve(argv.quiet ? undefined : rval.join(os.EOL))
}, function(err) { }, function(err) {
argv.reject(err) argv.reject(err)
}) })
@ -86,7 +86,7 @@ module.exports = function() {
table.push(row) table.push(row)
}) })
logger.log(table.toString()) return table.toString()
}) })
} }
@ -119,7 +119,7 @@ module.exports = function() {
table.push(row) table.push(row)
}) })
logger.log(table.toString()) return table.toString()
}) })
} }
@ -145,7 +145,7 @@ module.exports = function() {
table.push(o) table.push(o)
}) })
logger.log(table.toString()) return table.toString()
}) })
} }
@ -190,8 +190,7 @@ module.exports = function() {
return cb(res) return cb(res)
} else { } else {
// Request OK, no data or data is ignored // Request OK, no data or data is ignored
logger.log(colors.green('OK')) return Promise.resolve(colors.green('OK'))
return Promise.resolve()
} }
}, function(err) { }, function(err) {
if (err.response && err.response.body) { if (err.response && err.response.body) {

View File

@ -19,6 +19,7 @@ describe("Unknown Commands", function() {
'alter', 'alter',
'rotate', 'rotate',
'call', 'call',
'cluster'
] ]
endpoints.forEach(function (i) { endpoints.forEach(function (i) {