MXS-1300: Add test for cluster command internals

The test tests the internals of the cluster diff generation
algorithms. This should guarantee that the diff calculation and the
resulting synchronization will work as expected without having to test it
on a live cluster.
This commit is contained in:
Markus Mäkelä
2017-08-01 13:48:08 +03:00
parent 8fb4532468
commit 10bfa072d9
2 changed files with 163 additions and 7 deletions

View File

@ -36,6 +36,13 @@ function getChangedObjects(a, b) {
return _.differenceWith(ours, theirs, equalResources) return _.differenceWith(ours, theirs, equalResources)
} }
// Check if the diffs add or delete services
function haveExtraServices(src, dest) {
var newObj = getDifference(src.services.data, dest.services.data)
var oldObj = getDifference(dest.services.data, src.services.data)
return newObj.length > 0 || oldObj.length > 0
}
// Resource collections // Resource collections
const collections = [ const collections = [
'servers', 'servers',
@ -80,13 +87,9 @@ function getDiffs(a, b) {
}) })
} }
// Check if the diffs add or delete services exports.haveExtraServices = haveExtraServices
function haveExtraServices(src, dest) { exports.getDifference = getDifference
var newObj = getDifference(src.services.data, dest.services.data) exports.getChangedObjects = getChangedObjects
var oldObj = getDifference(dest.services.data, src.services.data)
return newObj.length > 0 || oldObj.length > 0
}
exports.command = 'cluster <command>' exports.command = 'cluster <command>'
exports.desc = 'Cluster objects' exports.desc = 'Cluster objects'
exports.handler = function() {} exports.handler = function() {}

153
maxctrl/test/cluster.js Normal file
View File

@ -0,0 +1,153 @@
require('../test_utils.js')()
var cluster = require('../lib/cluster.js')
describe('Cluster Commands', function() {
before(startMaxScale)
it('detect added and removed objects', function() {
var a = [
{
'id': 'server1',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3000
}
}
}
]
var b = [
{
'id': 'server1',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3000
}
}
},
{
'id': 'server2',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3001
}
}
}
]
var c = [
{
'id': 'server1',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3000
}
}
},
{
'id': 'server3',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3002
}
}
}
]
cluster.getDifference(b, a)[0].id.should.equal('server2')
cluster.getDifference(c, a)[0].id.should.equal('server3')
cluster.getDifference(a, b).should.be.empty
cluster.getDifference(a, c).should.be.empty
cluster.getDifference(b, c)[0].id.should.equal('server2')
cluster.getDifference(c, b)[0].id.should.equal('server3')
cluster.getDifference(a, a).should.be.empty
cluster.getDifference(b, b).should.be.empty
cluster.getDifference(c, c).should.be.empty
})
it('detect changes in objects', function() {
var a = [
{
'id': 'server1',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3000
}
}
}
]
var b = [
{
'id': 'server1',
'type': 'servers',
'attributes': {
'parameters': {
'address': '127.0.0.1',
'port': 3001
}
}
}
]
cluster.getDifference(a, b).should.be.empty
cluster.getDifference(b, a).should.be.empty
cluster.getDifference(a, a).should.be.empty
cluster.getDifference(b, b).should.be.empty
var obj = cluster.getChangedObjects(a, b)[0]
obj.id.should.equal('server1')
obj.attributes.parameters.port.should.equal(3000)
})
it('detect extra services', function() {
var a = {
'services': {
data: [
{
'id': 'CLI',
'type': 'services',
'attributes': {
'parameters': {}
},
'relationships': {},
}
]
}
}
var b = {
'services': {
data: [
a.services.data[0],
{
'id': 'CLI2',
'type': 'services',
'attributes': {
'parameters': {
},
},
'relationships': {},
}
]
}
}
cluster.haveExtraServices(a, b).should.be.true
cluster.haveExtraServices(a, a).should.be.false
cluster.haveExtraServices(b, b).should.be.false
})
after(stopMaxScale)
});