forked from LeisureLink/consul-kv-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
213 lines (190 loc) · 5.9 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
const exec = require('child_process').exec;
const expect = require('chai').expect;
const _ = require('lodash');
const Promise = require('bluebird');
const consul = require('consul');
const execute = (commandLine) => {
return new Promise((resolve, reject) => {
exec(commandLine, {
cwd: __dirname,
env: process.env
}, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({
stdout: stdout,
stderr: stderr
});
}
});
});
};
describe('consul-kv-sync', () => {
const config = {
host: process.env.CONSUL_HOST || 'consul.service.consul',
port: process.env.CONSUL_PORT || '8500',
secure: process.env.CONSUL_SECURE === 'true'
};
const client = consul(config);
Promise.promisifyAll(client.kv);
after(() => {
return client.kv.delAsync({
key: 'service',
recurse: true
})
.then(() => {
return client.kv.delAsync({
key: 'service2',
recurse: true
});
})
.then(() => {
return client.kv.delAsync({
key: 'other_service',
recurse: true
});
});
});
describe('#validation', () => {
it('should return an error when an empty config file is added', () => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json ./empty.json')
.then(() => {
throw new Error('Expected failure, got success');
})
.catch((err) => {
expect(err.code).to.eql(99);
});
});
it('should return an error when a config file with multiple top-level nodes is added', () => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json ./full.json')
.then(() => {
throw new Error('Expected failure, got success');
})
.catch((err) => {
expect(err.code).to.eql(99);
});
});
it('should return an error when an different service\'s config file is added', () => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json ./other.json')
.then(() => {
throw new Error('Expected failure, got success');
})
.catch((err) => {
expect(err.code).to.eql(99);
});
});
});
describe('#run', () => {
let response;
describe('first run scenario', () => {
before(() => {
return client.kv.delAsync({
key: 'service',
recurse: true
})
.then(() => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json');
}).then(() => {
return client.kv.getAsync({
key: 'service',
recurse: true
});
}).then((res) => {
response = res;
});
});
it('should set value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/two';
});
expect(item).to.be.ok;
expect(item.Value).to.eql('value 2');
});
it('should set overridden value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/one';
});
expect(item).to.be.ok;
expect(item.Value).to.eql('value from file two');
});
it('should set array parameters correctly', () => {
let items = _.filter(response, (item) => {
return /^service\/arrayparam/.test(item.Key);
});
expect(items.length).to.eql(4);
expect(items[0].Key).to.eql('service/arrayparam/0');
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');
});
});
describe('second run scenario', () => {
before(() => {
return client.kv.setAsync({
key: 'service/four',
value: 'value for removal'
}).then(() => {
return client.kv.setAsync({
key: 'service2/item',
value: 'this value should stay'
});
}).then(() => {
return client.kv.setAsync({
key: 'service/one',
value: 'this value should be changed'
});
}).then(() => {
return execute('node ../consul-kv-sync.js ./one.json ./two.json');
}).then(() => {
return client.kv.getAsync({
key: 'service',
recurse: true
});
}).then(function(result) {
response = result;
});
});
it('should set value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/two';
});
expect(item).to.be.ok;
expect(item.Value).to.eql('value 2');
});
it('should set overridden value to correct value', () => {
let item = response.find((item) => {
return item.Key === 'service/one';
});
expect(item).to.be.ok;
expect(item.Value).to.eql('value from file two');
});
it('should set array parameters correctly', () => {
let items = _.filter(response, (item) => {
return /^service\/arrayparam/.test(item.Key);
});
expect(items.length).to.eql(4);
expect(items[0].Key).to.eql('service/arrayparam/0');
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 remove existing keys that are not in config file', () => {
let items = _.filter(response, (item) => {
return item.Key === 'service/four';
});
expect(items.length).to.eql(0);
});
it('should not impact keys for a different service', () => {
return client.kv.getAsync({
key: 'service2/item'
}).then((item) => {
expect(item.Value).to.eql('this value should stay');
});
});
});
});
});