forked from breser/git2consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git2consul_expand_keys_test.js
185 lines (148 loc) · 7.35 KB
/
git2consul_expand_keys_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
var should = require('should');
var _ = require('underscore');
var mkdirp = require('mkdirp');
// We want this above any git2consul module to make sure logging gets configured
require('./git2consul_bootstrap_test.js');
var repo = require('../lib/git/repo.js');
var git_utils = require('./utils/git_utils.js');
var consul_utils = require('./utils/consul_utils.js');
var git_commands = require('../lib/git/commands.js');
describe('Expand keys', function() {
// The current copy of the git master branch. This is initialized before each test in the suite.
var branch;
beforeEach(function(done) {
// Each of these tests needs a working repo instance, so create it here and expose it to the suite
// namespace. These are all tests of expand_keys mode, so set that here.
git_utils.initRepo(_.extend(git_utils.createRepoConfig(), {'expand_keys': true}), function(err, repo) {
if (err) return done(err);
// The default repo created by initRepo has a single branch, master.
branch = repo.branches['master'];
done();
});
});
it ('should handle additions of new JSON files', function(done) {
var sample_key = 'simple.json';
var sample_value = '{ "first_level" : { "second_level": "is_all_we_need" } }';
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Add a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/simple.json/first_level/second_level', 'is_all_we_need', function(err, value) {
if (err) return done(err);
done();
});
});
});
});
it ('should handle changing JSON files', function(done) {
var sample_key = 'changeme.json';
var sample_value = '{ "first_level" : "is_all_we_need" }';
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Add a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/changeme.json/first_level', 'is_all_we_need', function(err, value) {
if (err) return done(err);
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, '{ "first_level" : "is super different" }', "Change a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/changeme.json/first_level', 'is super different', function(err, value) {
if (err) return done(err);
done();
});
});
});
});
});
});
});
it ('should handle busted JSON files', function(done) {
var sample_key = 'busted.json';
var sample_value = '{ "busted" ; "json" }';
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Add a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/busted.json', sample_value, function(err, value) {
if (err) return done(err);
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, '{ "not_busted" : "json" }', "Change a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/busted.json/not_busted', 'json', function(err, value) {
if (err) return done(err);
done();
});
});
});
});
});
});
});
it ('should handle JSON files comingled with non-JSON files', function(done) {
var json_key = 'happy.json';
var json_value = '{ "happy" : "json" }';
var sample_key = 'not_a_json_key';
var sample_value = 'password: calvin12345';
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(json_key, json_value, "Add a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, sample_value, "Add another file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/happy.json/happy', 'json', function(err, value) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/not_a_json_key', sample_value, function(err, value) {
if (err) return done(err);
done();
});
});
});
});
});
});
});
it ('should handle JSON files with special characters', function(done) {
var sample_key = 'special.json';
var sample_value = {
"fuzzy" : {
"second level": "ain\'t no one got time for that",
"second/level": {
"ok?": "yes"
}
}
};
// Add the file, call branch.handleRef to sync the commit, then validate that consul contains the correct info.
git_utils.addFileToGitRepo(sample_key, JSON.stringify(sample_value), "Add a file.", function(err) {
if (err) return done(err);
branch.handleRefChange(0, function(err) {
if (err) return done(err);
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/special.json/fuzzy/second%20level', sample_value['fuzzy']['second level'], function(err, value) {
// At this point, the repo should have populated consul with our sample_key
consul_utils.validateValue('test_repo/master/special.json/fuzzy/second%2Flevel/ok%3F', sample_value['fuzzy']['second/level']['ok?'], function(err, value) {
if (err) return done(err);
done();
});
});
});
});
});
});