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('&'),
function(resp) {
logger.log(JSON.stringify(resp, null, 4))
return JSON.stringify(resp, null, 4)
}, { method: verb })
})
})

View File

@ -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)
})
})
})

View File

@ -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) {

View File

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