Skip to content

Commit

Permalink
Make tests use consul client as well
Browse files Browse the repository at this point in the history
  • Loading branch information
jessehansen committed Nov 10, 2015
1 parent 382b01f commit 9f3f1e9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 18 deletions.
2 changes: 1 addition & 1 deletion consul-kv-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Promise.all(_.map(program.ca, readFile)).then(function(certificates) {
var config = {
host: program.host || process.env.CONSUL_HOST || 'consul.service.consul',
port: program.port || process.env.CONSUL_PORT || 8500,
secure: !!program.secure,
secure: program.secure || process.env.CONSUL_SECURE === 'true',
ca: certificates
};
debug('Config:');
Expand Down
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@
"eslint": "^1.9.0",
"eslint-watch": "^2.1.3",
"mocha": "^2.2.5",
"onchange": "^2.0.0",
"request-promise": "^1.0.2",
"js-base64": "^2.1.9"
"onchange": "^2.0.0"
}
}
5 changes: 5 additions & 0 deletions test/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"env": {
"mocha": true
}
}
37 changes: 23 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
var exec = require('child_process').exec;
var expect = require('chai').expect;
var request = require('request-promise');
var base64 = require('js-base64').Base64;
var _ = require('lodash');
var Promise = require('bluebird');
var consul = require('consul');

describe('consul-kv-sync', function() {
var config = {
host: process.env.CONSUL_HOST || 'consul.service.consul',
port: process.env.CONSUL_PORT || '8500',
secure: process.env.CONSUL_SECURE === 'true'
};

var _client = consul(config);
Promise.promisifyAll(_client.kv);

describe('#run', function() {
var response;
before(function(done) {
request.put('http://' + process.env.CONSUL_HOST + ':' + (process.env.CONSUL_PORT || 8500) + '/v1/kv/service/four', {
body: 'value for removal',
json: true
return _client.kv.setAsync({
key:'service/four',
value: 'value for removal'
}).then(function() {
var proc = exec('node ../consul-kv-sync.js ./one.json ./two.json', {
cwd: __dirname
});
proc.on('exit', function() {
request.get('http://' + process.env.CONSUL_HOST + ':' + (process.env.CONSUL_PORT || 8500) + '/v1/kv/service?recurse=1', {
json: true
_client.kv.getAsync({
key:'service/',
recurse: true
}).then(function(result) {
response = result;
done();
Expand All @@ -33,7 +42,7 @@ describe('consul-kv-sync', function() {
});

expect(item).to.be.ok;
expect(base64.decode(item.Value)).to.eql('value 2');
expect(item.Value).to.eql('value 2');
});

it('should set overridden value to correct value', function() {
Expand All @@ -42,7 +51,7 @@ describe('consul-kv-sync', function() {
});

expect(item).to.be.ok;
expect(base64.decode(item.Value)).to.eql('value from file two');
expect(item.Value).to.eql('value from file two');
});

it('should set array parameters correctly', function() {
Expand All @@ -52,15 +61,15 @@ describe('consul-kv-sync', function() {

expect(items.length).to.eql(4);
expect(items[0].Key).to.eql('service/arrayparam/0');
expect(base64.decode(items[0].Value)).to.eql('1');
expect(base64.decode(items[1].Value)).to.eql('2');
expect(base64.decode(items[2].Value)).to.eql('3');
expect(base64.decode(items[3].Value)).to.eql('4');
expect(items[0].Value).to.eql('1');
expect(items[1].Value).to.eql('2');
expect(items[2].Value).to.eql('3');
expect(items[3].Value).to.eql('4');
});

it('should set remove existing keys that are not in config file', function() {
var items = _.filter(response, function(item) {
return item.Key == 'service/four';
return item.key == 'service/four';
});

expect(items.length).to.eql(0);
Expand Down

0 comments on commit 9f3f1e9

Please sign in to comment.