forked from breser/git2consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git2consul_webhook_and_polling_test.js
346 lines (295 loc) · 12.5 KB
/
git2consul_webhook_and_polling_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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
var should = require('should');
var _ = require('underscore');
var request = require('request');
// We want this above any git2consul module to make sure logging gets configured
var bootstrap = require('./git2consul_bootstrap_test.js');
var consul_utils = require('./utils/consul_utils.js');
var Repo = require('../lib/git/repo.js');
var git = require('../lib/git');
var git_commands = require('../lib/git/commands.js');
var git_utils = require('./utils/git_utils.js');
var logger = require('../lib/logging.js');
/**
* Test webhooks and polling. NOTE: This test needs to run last because the polling test case will
* cause all sorts of race conditions for subsequent tests, and we didn't bother mocking up a way to
* reach in and disable a polling hook once it starts running.
*/
// Test failing webhook configs
[[
undefined
],[{
'type': 'github',
'err': 'Hook configuration failed due to Invalid webhook port undefined'
}],[{
'type': 'stash',
'port': 5252,
'err': 'Hook configuration failed due to No config url provided'
}],[{
'type': 'stash',
'port': 5252,
'url': '/happy',
'err': 'Hook configuration failed due to A webhook is already listening on 5252, /poke'
},{
'type': 'github',
'port': 5252,
'url': '/poke'
},{
'type': 'github',
'port': 5252,
'url': '/poke'
}]].forEach(function(hook_config) {
describe('webhook config validation', function() {
it ('should reject invalid webhook config', function(done) {
git_commands.init(git_utils.TEST_REMOTE_REPO, function(err) {
if (err) return done(err);
// When we create a repo, we need it to have an initial commit. The call to addFile provides that.
git_utils.addFileToGitRepo("readme.md", "Stub file to give us something to commit.", "Init webhook config repo.", function(err) {
if (err) return done(err);
var repo_config = git_utils.createRepoConfig();
repo_config.hooks = hook_config;
var repo = new Repo(repo_config);
repo.init(function(err) {
if (hook_config[0]) {
err[0].should.equal(hook_config[0].err);
} else {
err[0].should.startWith('Hook configuration failed due to');
}
done();
});
});
});
});
});
});
// Give us a mechanism to report on which set of hook params we're testing.
var test_counter = 0;
var test_descriptions = [
"valid updates",
"changes to untracked branches",
"changes to non-HEAD refs"
];
[
// Test webhooks sharing a port
[{
'type': 'github',
'url': '/githubpoke',
'port': 5252,
'body': { ref: "refs/heads/master", head_commit: {id: 12345} },
'fqurl': 'http://localhost:5252/githubpoke'
},{
'type': 'stash',
'url': '/stashpoke',
'port': 5252,
'body': { refChanges: [{refId: "refs/heads/master", toHash: "0"}, {refId: "refs/heads/master", toHash: "0"}] },
'fqurl': 'http://localhost:5252/stashpoke'
},{
'type': 'bitbucket',
'url': '/bitbucketpoke',
'port': 5252,
// Seriously, their default POST hook is this ugly
'body': '%7B%22repository%22%3A+%7B%22website%22%3A+null%2C+%22fork%22%3A+false%2C+%22name%22%3A+%22configuration%22%2C+%22scm%22%3A+%22git%22%2C+%22owner%22%3A+%22ryanbreen%22%2C+%22absolute_url%22%3A+%22%2Fryanbreen%2Fconfiguration%2F%22%2C+%22slug%22%3A+%22configuration%22%2C+%22is_private%22%3A+true%7D%2C+%22truncated%22%3A+false%2C+%22commits%22%3A+%5B%7B%22node%22%3A+%226f086f3d3de7%22%2C+%22files%22%3A+%5B%7B%22type%22%3A+%22modified%22%2C+%22file%22%3A+%22jobs_service%2Fauspice%2Fdemo-consumer-key%22%7D%5D%2C+%22raw_author%22%3A+%22Ryan+Breen+%3Crbreen%40vistaprint.com%3E%22%2C+%22utctimestamp%22%3A+%222014-10-18+23%3A20%3A47%2B00%3A00%22%2C+%22author%22%3A+%22Ryan+Breen%22%2C+%22timestamp%22%3A+%222014-10-19+01%3A20%3A47%22%2C+%22raw_node%22%3A+%226f086f3d3de724b9007934408e023b628a59ea15%22%2C+%22parents%22%3A+%5B%2230c88d68d029%22%5D%2C+%22branch%22%3A+%22master%22%2C+%22message%22%3A+%22Adding+bitbucket+test.%5Cn%22%2C+%22revision%22%3A+null%2C+%22size%22%3A+-1%7D%5D%2C+%22canon_url%22%3A+%22https%3A%2F%2Fbitbucket.org%22%2C+%22user%22%3A+%22ryanbreen%22%7D',
'fqurl': 'http://localhost:5252/bitbucketpoke'
},{
'type': 'gitlab',
'url': '/gitlabpoke',
'port': 5252,
'body': { after: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", ref: "refs/heads/master"},
'fqurl': 'http://localhost:5252/gitlabpoke',
'no_change_expected': true
}],
// Test no-op changes to an incorrect branch
[{
'type': 'github',
'url': '/githubpoke_bogus_branch',
'port': 5253,
'body': { ref: "refs/heads/bogus_branch", head_commit: {id: 12345} },
'fqurl': 'http://localhost:5253/githubpoke_bogus_branch',
'no_change_expected': true
},{
'type': 'stash',
'url': '/stashpoke_bogus_branch',
'port': 5253,
'body': { refChanges: [{refId: "refs/heads/bogus_branch", toHash: "deadbeef"}, {refId: "refs/heads/boguser_branch", toHash: "deadbeef"}] },
'fqurl': 'http://localhost:5253/stashpoke_bogus_branch',
'no_change_expected': true
},{
'type': 'bitbucket',
'url': '/bitbucket_bogus_branch',
'port': 5253,
'body': '%7B%22repository%22%3A+%7B%22website%22%3A+null%2C+%22fork%22%3A+false%2C+%22name%22%3A+%22configuration%22%2C+%22scm%22%3A+%22git%22%2C+%22owner%22%3A+%22ryanbreen%22%2C+%22absolute_url%22%3A+%22%2Fryanbreen%2Fconfiguration%2F%22%2C+%22slug%22%3A+%22configuration%22%2C+%22is_private%22%3A+true%7D%2C+%22truncated%22%3A+false%2C+%22commits%22%3A+%5B%7B%22node%22%3A+%226f086f3d3de7%22%2C+%22files%22%3A+%5B%7B%22type%22%3A+%22modified%22%2C+%22file%22%3A+%22jobs_service%2Fauspice%2Fdemo-consumer-key%22%7D%5D%2C+%22raw_author%22%3A+%22Ryan+Breen+%3Crbreen%40vistaprint.com%3E%22%2C+%22utctimestamp%22%3A+%222014-10-18+23%3A20%3A47%2B00%3A00%22%2C+%22author%22%3A+%22Ryan+Breen%22%2C+%22timestamp%22%3A+%222014-10-19+01%3A20%3A47%22%2C+%22raw_node%22%3A+%226f086f3d3de724b9007934408e023b628a59ea15%22%2C+%22parents%22%3A+%5B%2230c88d68d029%22%5D%2C+%22branch%22%3A+%22dev%22%2C+%22message%22%3A+%22Adding+bitbucket+test.%5Cn%22%2C+%22revision%22%3A+null%2C+%22size%22%3A+-1%7D%5D%2C+%22canon_url%22%3A+%22https%3A%2F%2Fbitbucket.org%22%2C+%22user%22%3A+%22ryanbreen%22%7D',
'fqurl': 'http://localhost:5253/bitbucket_bogus_branch',
'no_change_expected': true
},{
'type': 'gitlab',
'url': '/gitlab_bogus_branch',
'port': 5253,
'body': { after: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", ref: "refs/heads/bogus_branch"},
'fqurl': 'http://localhost:5253/gitlab_bogus_branch',
'no_change_expected': true
}],
// Test no-op changes with non-HEAD refs
[{
'type': 'github',
'url': '/githubpoke_bogus_ref',
'port': 5254,
'body': { ref: "refs/remotes/origin/master", head_commit: {id: 12345} },
'fqurl': 'http://localhost:5254/githubpoke_bogus_ref',
'no_change_expected': true
},{
'type': 'stash',
'url': '/stashpoke_bogus_ref',
'port': 5254,
'body': { refChanges: [{refId: "refs/remotes/origin/master", toHash: "0"}] },
'fqurl': 'http://localhost:5254/stashpoke_bogus_ref',
'no_change_expected': true
},{
'type': 'bitbucket',
'url': '/bitbucket_bogus_ref',
'port': 5254,
'body': '',
'fqurl': 'http://localhost:5254/bitbucket_bogus_ref',
'no_change_expected': true
},{
'type': 'gitlab',
'url': '/gitlab_bogus_ref',
'port': 5254,
'body': { after: "da1560886d4f094c3e6c9ef40349f7d38b5d27d7", ref: "refs/remotes/origin/master"},
'fqurl': 'http://localhost:5254/gitlab_bogus_ref',
'no_change_expected': true
}]
].forEach(function(hook_config) {
describe('webhook', function() {
// Add a file for the config, commit it, and validate that it has populated properly.
var test_hook_req = function(config, cb) {
var sample_key = 'webhook_key_' + config.type;
var sample_value = config.type + ' test data';
git_utils.addFileToGitRepo(sample_key, sample_value, "Webhook.", function(err) {
if (err) return cb(err);
var req_conf = { url: config.fqurl, method: 'POST' };
if (config.type === 'bitbucket') {
req_conf.form = { payload: decodeURIComponent(config.body).replace(/\+/g, ' ') };
} else req_conf.json = config.body
if (config.type === 'stash') req_conf.headers = {'content-encoding':'UTF-8'};
request(req_conf, function(err) {
setTimeout(function() {
if (err) return cb(err);
// If this is a test that won't trigger an update, such as a req specifying an untracked branch,
// short-circuit here and don't test for a KV update.
if (config.no_change_expected) return cb();
consul_utils.waitForValue('test_repo/master/' + sample_key, function(err) {
if (err) return cb(err);
cb();
});
}, 500);
});
});
};
it ('should handle inbound requests with ' + test_descriptions[test_counter], function(done) {
var repo_config = git_utils.createRepoConfig();
repo_config.hooks = hook_config;
git_utils.initRepo(repo_config, function(err, repo) {
if (err) return done(err);
// Test each hook config, 1 at a time. Since they are updating the same repo, having all webhooks
// fire in parallel would lead to undefined results.
var test_config = function() {
config = hook_config.pop();
test_hook_req(config, function(err) {
if (err) return done(err);
if (hook_config.length > 0) return test_config();
done();
});
};
test_config();
});
});
++test_counter;
});
});
// Test failing webhook configs
[[
undefined
],[{
'type': 'polling'
}],[{
'type': 'polling',
'interval': -1
}],[{
'type': 'polling',
'interval': 1.1
}]].forEach(function(hook_config) {
describe('polling config validation', function() {
it ('should reject invalid polling config', function(done) {
var repo_config = git_utils.createRepoConfig();
repo_config.hooks = hook_config;
git_utils.initRepo(repo_config, function(err, repo) {
if (hook_config[0]) {
err[0].should.equal('Hook configuration failed due to Polling intervals must be positive integers');
} else {
err[0].should.startWith('Hook configuration failed due to');
}
done();
});
});
});
});
describe('polling hook', function() {
// Create a repo with a polling hook and validate that the KV is updated on a change to the remote
// repo contents.
it ('should handle polling updates', function(done) {
var repo_config = git_utils.createRepoConfig();
repo_config.hooks = [{
'type': 'polling',
'interval': '1'
}];
repo_config.name = "polling_test";
git_utils.initRepo(repo_config, function(err, repo) {
if (err) return done(err);
repo.hooks_active.should.equal(true);
var sample_key = 'sample_key';
var sample_value = 'stash test data';
git_utils.addFileToGitRepo(sample_key, sample_value, "Polling hook.", function(err) {
if (err) return done(err);
consul_utils.waitForValue('polling_test/master/sample_key', function(err) {
if (err) return done(err);
done();
});
});
});
});
});
describe('no daemon mode', function() {
// No daemon mode works by disabling hooks. Validate that no_daemon mode is enabled by attempting to start
// a repo with hooks and checking that no hooks are actually running.
it ('should disable hooks', function(done) {
var repo_config = git_utils.createRepoConfig();
repo_config.hooks = [{
'type': 'polling',
'interval': '1'
},{
'type': 'stash',
'url': '/stashpoke_bogus_branch',
'port': 5253
}];
repo_config.name = "you_shall_not_hook";
git_commands.init(git_utils.TEST_REMOTE_REPO, function(err) {
if (err) return done(err);
var sample_key = 'readme.md';
var sample_value = 'stub data';
git_utils.addFileToGitRepo(sample_key, sample_value, "Stub commit.", function(err) {
if (err) return done(err);
var config = {
repos: [repo_config],
local_store: git_utils.TEST_WORKING_DIR,
no_daemon: true
};
// Now, create a repo with hooks. The hooks should not be active due to no_daemon mode.
git.createRepos(config, function(err) {
(undefined === err).should.equal(true);
var repo = git.repos['you_shall_not_hook'];
(undefined === repo.hooks_active).should.equal(true);
done();
});
});
});
});
});