From 8396b9043620aac260ca431c6978e7b6060325f9 Mon Sep 17 00:00:00 2001 From: Jesse Hansen Date: Tue, 10 Nov 2015 10:38:21 -0700 Subject: [PATCH] Added tests for config file validation --- consul-kv-sync.js | 8 ++--- test/empty.json | 1 + test/full.json | 1 + test/other.json | 5 ++++ test/test.js | 76 ++++++++++++++++++++++++++++++++++------------- 5 files changed, 66 insertions(+), 25 deletions(-) create mode 100644 test/empty.json create mode 100644 test/full.json create mode 100644 test/other.json diff --git a/consul-kv-sync.js b/consul-kv-sync.js index bd02e2d..b76d740 100755 --- a/consul-kv-sync.js +++ b/consul-kv-sync.js @@ -24,11 +24,11 @@ function readFragments(fileName) { } var pointers = jptr.list(contents); if (firstFragmentId) { - if (pointers[0].fragmentId != firstFragmentId) { - return Promise.reject(new Error('Each file must have the same top-level node. Expected "' + fileName + '" to have "' + firstFragmentId + '", but it has "' + pointers[0].fragmentId + '".')); + if (pointers[1].fragmentId != firstFragmentId) { + return Promise.reject(new Error('Each file must have the same top-level node. Expected "' + fileName + '" to have top-level node "' + firstFragmentId.substring(2) + '", but it has "' + pointers[1].fragmentId.substring(2) + '".')); } } else { - firstFragmentId = pointers[0].fragmentId; + firstFragmentId = pointers[1].fragmentId; } return Promise.resolve(pointers); }); @@ -101,7 +101,7 @@ Promise.all(_.map(program.ca, readFile)).then(function(certificates) { _existing = results; }).catch(function(err) { error(err); - process.exit(1); + process.exit(99); }).then(function() { _reduced = _.reduce(_.filter(_flattened, function(x) { return _.isString(x.value) || _.isFinite(x.value); diff --git a/test/empty.json b/test/empty.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/test/empty.json @@ -0,0 +1 @@ +{} diff --git a/test/full.json b/test/full.json new file mode 100644 index 0000000..552228d --- /dev/null +++ b/test/full.json @@ -0,0 +1 @@ +{"service1": {}, "service2": {}} diff --git a/test/other.json b/test/other.json new file mode 100644 index 0000000..928fdbf --- /dev/null +++ b/test/other.json @@ -0,0 +1,5 @@ +{ + "other_service": { + "config": "values" + } +} diff --git a/test/test.js b/test/test.js index a72c346..922c5e6 100644 --- a/test/test.js +++ b/test/test.js @@ -4,6 +4,17 @@ var _ = require('lodash'); var Promise = require('bluebird'); var consul = require('consul'); +function execute(commandLine) { + return new Promise(function(resolve) { + var proc = exec(commandLine, { + cwd: __dirname + }); + proc.on('exit', function(exitCode) { + resolve(exitCode); + }); + }); +} + describe('consul-kv-sync', function() { var config = { host: process.env.CONSUL_HOST || 'consul.service.consul', @@ -12,38 +23,61 @@ describe('consul-kv-sync', function() { }; var _client = consul(config); + var _exitCode; Promise.promisifyAll(_client.kv); + describe('#validation', function() { + it('should return an error when an empty config file is added', function() { + return execute('node ../consul-kv-sync.js ./one.json ./two.json ./empty.json') + .then(function(exitCode) { + expect(exitCode).to.eql(99); + }); + }); + it('should return an error when a config file with multiple top-level nodes is added', function() { + return execute('node ../consul-kv-sync.js ./one.json ./two.json ./full.json') + .then(function(exitCode) { + expect(exitCode).to.eql(99); + }); + }); + it('should return an error when an different service\'s config file is added', function() { + return execute('node ../consul-kv-sync.js ./one.json ./two.json ./other.json') + .then(function(exitCode) { + expect(exitCode).to.eql(99); + }); + }); + }); + describe('#run', function() { - var response; - before(function(done) { + var _response; + before(function() { return _client.kv.setAsync({ - key:'service/four', + key: 'service/four', value: 'value for removal' - }).then(function(){ + }).then(function() { return _client.kv.setAsync({ key: 'service2/item', value: 'this value should stay' - }) + }); }) .then(function() { - var proc = exec('node ../consul-kv-sync.js ./one.json ./two.json', { - cwd: __dirname - }); - proc.on('exit', function() { - _client.kv.getAsync({ - key:'service', - recurse: true - }).then(function(result) { - response = result; - done(); - }).catch(done); + return execute('node ../consul-kv-sync.js ./one.json ./two.json'); + }).then(function(exitCode) { + _exitCode = exitCode; + return _client.kv.getAsync({ + key: 'service', + recurse: true }); + }).then(function(result) { + _response = result; }); }); + it('should set exit code to 0', function() { + expect(_exitCode).to.eql(0); + }); + it('should set value to correct value', function() { - var item = response.find(function(item) { + var item = _response.find(function(item) { return item.Key == 'service/two'; }); @@ -52,7 +86,7 @@ describe('consul-kv-sync', function() { }); it('should set overridden value to correct value', function() { - var item = response.find(function(item) { + var item = _response.find(function(item) { return item.Key == 'service/one'; }); @@ -61,7 +95,7 @@ describe('consul-kv-sync', function() { }); it('should set array parameters correctly', function() { - var items = _.filter(response, function(item) { + var items = _.filter(_response, function(item) { return /^service\/arrayparam/.test(item.Key); }); @@ -74,7 +108,7 @@ describe('consul-kv-sync', function() { }); it('should remove existing keys that are not in config file', function() { - var items = _.filter(response, function(item) { + var items = _.filter(_response, function(item) { return item.Key == 'service/four'; }); @@ -84,7 +118,7 @@ describe('consul-kv-sync', function() { it('should not impact keys for a different service', function() { return _client.kv.getAsync({ key: 'service2/item' - }).then(function(item){ + }).then(function(item) { expect(item.Value).to.eql('this value should stay'); }); });