Adding tests around upgradeConfig

This commit is contained in:
Chris Cowan 2015-03-02 16:51:49 -07:00
parent 5d3438d738
commit a731c78e2b
7 changed files with 170 additions and 45 deletions

View file

@ -96,6 +96,8 @@
"requirejs": "~2.1.14",
"rjs-build-analysis": "0.0.3",
"simple-git": "^0.11.0",
"sinon": "^1.12.2",
"sinon-as-promised": "^2.0.3",
"tar": "^1.0.1"
}
}

View file

@ -1,30 +1,31 @@
var config = require('../config');
var upgrade = require('./upgradeConfig');
var client = require('./elasticsearch_client');
module.exports = function () {
var options = {
index: config.kibana.kibana_index,
type: 'config',
body: {
size: 1000,
sort: [ { buildNum: { order: 'desc' } } ],
query: {
filtered: {
filter: {
bool: {
must_not: [ { query: { match: { _id: '@@version' } } } ]
module.exports = function (client) {
return function () {
var options = {
index: config.kibana.kibana_index,
type: 'config',
body: {
size: 1000,
sort: [ { buildNum: { order: 'desc' } } ],
query: {
filtered: {
filter: {
bool: {
must_not: [ { query: { match: { _id: '@@version' } } } ]
}
}
}
}
}
}
};
};
return client.search(options)
.then(upgrade)
.catch(function (err) {
if (!/SearchParseException.+mapping.+\[buildNum\]|^IndexMissingException/.test(err.message)) throw err;
});
return client.search(options)
.then(upgrade(client))
.catch(function (err) {
if (!/SearchParseException.+mapping.+\[buildNum\]|^IndexMissingException/.test(err.message)) throw err;
});
};
};

View file

@ -1,9 +1,10 @@
var Promise = require('bluebird');
var waitForEs = require('./waitForEs');
var migrateConfig = require('./migrateConfig');
var client = require('./elasticsearch_client');
module.exports = function () {
return waitForEs().then(function () {
return migrateConfig();
return migrateConfig(client);
});
};

View file

@ -3,32 +3,31 @@ var isUpgradeable = require('./isUpgradeable');
var config = require('../config');
var _ = require('lodash');
var client = require('./elasticsearch_client');
module.exports = function (response) {
var newConfig = {};
// Check to see if there are any doc. If not then we can assume
// nothing needs to be done
if (response.hits.hits.length === 0) return Promise.resolve();
module.exports = function (client) {
return function (response) {
var newConfig = {};
// Check to see if there are any doc. If not then we can assume
// nothing needs to be done
if (response.hits.hits.length === 0) return Promise.resolve();
// if we already have a the current version in the index then we need to stop
if (_.find(response.hits.hits, { _id: config.package.version })) return Promise.resolve();
// if we already have a the current version in the index then we need to stop
if (_.find(response.hits.hits, { _id: config.package.version })) return Promise.resolve();
// Look for upgradeable configs. If none of them are upgradeable
// then resolve with null.
var body = _.find(response.hits.hits, isUpgradeable);
if (!body) return Promise.resolve();
// Look for upgradeable configs. If none of them are upgradeable
// then resolve with null.
var body = _.find(response.hits.hits, isUpgradeable);
if (!body) return Promise.resolve();
// if the build number is still the template string (which it wil be in development)
// then we need to set it to the max interger. Otherwise we will set it to the build num
body._source.buildNum = (/^@@/.test(config.buildNum)) ? Math.pow(2, 53) - 1 : parseInt(config.buildNum, 10);
return client.create({
index: config.kibana.kibana_index,
type: 'config',
body: body._source,
id: config.package.version
}).catch(function (err) {
// Ignore document already exists exceptions for beta and snapshot upgrades.
if (/DocumentAlreadyExistsException/.test(err.message) && /beta|snapshot/.test(config.package.version)) return;
throw err;
});
// if the build number is still the template string (which it wil be in development)
// then we need to set it to the max interger. Otherwise we will set it to the build num
body._source.buildNum = (/^@@/.test(config.buildNum)) ? Math.pow(2, 53) - 1 : parseInt(config.buildNum, 10);
return client.create({
index: config.kibana.kibana_index,
type: 'config',
body: body._source,
id: config.package.version
});
};
};

View file

@ -0,0 +1,35 @@
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "4.0.1-snapshot",
"_score": 1,
"_source": {
"buildNum": 5921,
"defaultIndex": "logstash-*"
}
},
{
"_index": ".kibana",
"_type": "config",
"_id": "4.0.0",
"_score": 1,
"_source": {
"buildNum": 5888,
"defaultIndex": "logstash-*"
}
}
]
}
}

View file

@ -0,0 +1,35 @@
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": ".kibana",
"_type": "config",
"_id": "4.0.1",
"_score": 1,
"_source": {
"buildNum": 5921,
"defaultIndex": "logstash-*"
}
},
{
"_index": ".kibana",
"_type": "config",
"_id": "4.0.0",
"_score": 1,
"_source": {
"buildNum": 5888,
"defaultIndex": "logstash-*"
}
}
]
}
}

View file

@ -0,0 +1,52 @@
var root = require('requirefrom')('');
var upgradeConfig = root('src/server/lib/upgradeConfig');
var expect = require('expect.js');
var sinon = require('sinon');
var sinonAsPromised = require('sinon-as-promised')(require('bluebird'));
var util = require('util');
var package = root('package.json');
var Promise = require('bluebird');
var config = root('src/server/config');
var upgradeFrom4_0_0_to_4_0_1 = root('test/unit/fixtures/config_upgrade_from_4.0.0_to_4.0.1.json');
var upgradeFrom4_0_0_to_4_0_1_snapshot = root('test/unit/fixtures/config_upgrade_from_4.0.0_to_4.0.1-snapshot.json');
describe('lib/upgradeConfig', function () {
var client, oldPackageVersion;
beforeEach(function () {
oldPackageVersion = config.package.version;
client = { create: sinon.stub() };
});
afterEach(function () {
config.package.version = oldPackageVersion;
});
it('should not upgrade if the current version of the config exits', function () {
config.package.version = '4.0.1';
var fn = upgradeConfig(client);
client.create.rejects(new Error('DocumentAlreadyExistsException'));
return fn(upgradeFrom4_0_0_to_4_0_1).finally(function () {
sinon.assert.notCalled(client.create);
});
});
it('should not upgrade if there are no hits', function () {
config.package.version = '4.0.1';
var fn = upgradeConfig(client);
return fn({ hits: { hits: [] } }).finally(function () {
sinon.assert.notCalled(client.create);
});
});
it('should not upgrade if there are no hits', function () {
config.package.version = '4.0.1-snapshot';
client.create.rejects(new Error('DocumentAlreadyExistsException'));
var fn = upgradeConfig(client);
return fn(upgradeFrom4_0_0_to_4_0_1_snapshot).finally(function () {
sinon.assert.notCalled(client.create);
});
});
});