Files
MaxScale/maxctrl/test_utils.js
Markus Mäkelä e76dc80e47 MXS-1300: Reorganize and refactor tests
The tests are now sorted by command type. This allows for more consistent
test creation as tests can be done per source code file instead of per
object type.

Added helper functions for common testing operations. The two main ones
are doCommand, which executes a MaxCtrl command and returns a result, and
verifyCommand, that executes a command and verifies the result via the
REST API.
2017-07-23 08:21:00 +03:00

61 lines
1.9 KiB
JavaScript

var child_process = require("child_process")
module.exports = function() {
if (process.env.MAXSCALE_DIR == null) {
throw new Error("MAXSCALE_DIR is not set");
}
this.request = require("request-promise-native")
this.chai = require("chai")
this.assert = require("assert")
this.chaiAsPromised = require("chai-as-promised")
chai.use(chaiAsPromised)
this.should = chai.should()
this.expect = chai.expect
this.host = 'http://localhost:8989/v1/'
// Start MaxScale, this should be called in the `before` handler of each test unit
this.startMaxScale = function() {
return new Promise(function(resolve, reject) {
child_process.execFile("./start_maxscale.sh", function(err, stdout, stderr) {
if (err) {
reject()
} else {
resolve()
}
})
})
};
// Stop MaxScale, this should be called in the `after` handler of each test unit
this.stopMaxScale = function() {
return new Promise(function(resolve, reject) {
child_process.execFile("./stop_maxscale.sh", function(err, stdout, stderr) {
if (err) {
reject()
} else {
resolve()
}
})
})
};
// Execute a single MaxCtrl command, returns a Promise
this.doCommand = function(command) {
var ctrl = require('./lib/core.js')
var opts = { extra_args: [ '--quiet'] }
return ctrl.execute(command.split(' '), opts)
}
// Execute a single MaxCtrl command and request a resource via the REST API,
// returns a Promise with the JSON format resource as an argument
this.verifyCommand = function(command, resource) {
return doCommand(command)
.then(function() {
return request.get(host + resource, {json: true})
})
};
}