Skip to content

Commit

Permalink
Added tests for config file validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jessehansen committed Nov 10, 2015
1 parent ff5d5ee commit 8396b90
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 25 deletions.
8 changes: 4 additions & 4 deletions consul-kv-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions test/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/full.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"service1": {}, "service2": {}}
5 changes: 5 additions & 0 deletions test/other.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"other_service": {
"config": "values"
}
}
76 changes: 55 additions & 21 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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';
});

Expand All @@ -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';
});

Expand All @@ -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);
});

Expand All @@ -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';
});

Expand All @@ -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');
});
});
Expand Down

0 comments on commit 8396b90

Please sign in to comment.