forked from breser/git2consul
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgit2consul_config_validation_test.js
174 lines (150 loc) · 6.41 KB
/
git2consul_config_validation_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
var _ = require('underscore');
var should = require('should');
// We want this above any git2consul module to make sure logging gets configured
require('./git2consul_bootstrap_test.js');
var fs = require('fs');
var rimraf = require('rimraf');
var git = require('../lib/git/');
var git_commands = require('../lib/git/commands.js');
var Repo = require('../lib/git/repo.js');
var git_utils = require('./utils/git_utils.js');
describe('Config Validation', function() {
it ('should reject a repo with invalid config', function() {
try {
var repo = new Repo();
should.fail("Repo with no config should throw an exception");
} catch(e) {
e.message.should.equal('No configuration provided for repo');
}
[{}, {'local_store':'/tmp/test_workspace'}, {'local_store':'/tmp/test_workspace', 'name':'incomplete'},
{'local_store':'/tmp/test_workspace', 'name':'incomplete', 'branches': ['master']}].forEach(function(config) {
try {
var repo = new Repo(config);
should.fail("Repo with incomplete config should throw an exception");
} catch(e) {
e.message.should.equal('A repo must have a local_store, a url, a name, and a branch array.');
}
});
});
it ('should reject a config using an existing repo name', function(done) {
git_utils.initRepo(function(err, repo) {
if (err) return done(err);
var config = {
local_store: git_utils.TEST_WORKING_DIR,
repos: [git_utils.createRepoConfig(), git_utils.createRepoConfig()]
};
var callback_seen = false;
git.createRepos(config, function(err) {
err.should.equal("A repo with that name is already tracked.");
if (callback_seen) {
done();
} else callback_seen = true;
});
});
});
it ('should reject a repo with a bogus local_store', function(done) {
git_commands.init(git_utils.TEST_REMOTE_REPO, function(err) {
if (err) return done(err);
var config = {
local_store: "/var/permdenied",
repos: [git_utils.createRepoConfig()]
};
var callback_seen = false;
git.createRepos(config, function(err) {
err.should.startWith("Failed to create local store");
done();
});
});
});
it ('should reject a repo with a bogus mountpoint', function() {
try {
var repo = new Repo({'name': 'busted_mountpoint_repo', 'url': 'http://www.github.com/',
'local_store':'/tmp/', 'branches': ['master'], 'mountpoint': '/oops'});
should.fail("mountpoint must not start or end with /.");
} catch(e) {
e.message.should.equal('mountpoint must not start or end with /.');
}
try {
var repo = new Repo({'name': 'busted_mountpoint_repo', 'url': 'http://www.github.com/',
'local_store':'/tmp/', 'branches': ['master'], 'mountpoint': 'oops/'});
should.fail("mountpoint must not start or end with /.");
} catch(e) {
e.message.should.equal('mountpoint must not start or end with /.');
}
});
it ('should reject a repo with a non-existent local_store', function() {
try {
var repo = new Repo({'name': 'non_existent_local_store_repo', 'url': 'http://www.github.com/',
'local_store':'/var/i_dont_live_here', 'branches': ['master']});
should.fail("Repo with non-existent local_store should throw an exception");
} catch(e) {
e.message.should.equal('directory /var/i_dont_live_here does not exist');
}
});
it ('should reject a repo with a non-writeable local_store', function() {
try {
fs.writeFileSync('/tmp/not_a_directory', 'oops');
var repo = new Repo({'name': 'non_directory_local_store_repo', 'url': 'http://www.github.com/',
'local_store':'/tmp/not_a_directory', 'branches': ['master']});
should.fail("Repo with non-writeable local_store should throw an exception");
} catch(e) {
e.message.should.equal('/tmp/not_a_directory is not a directory');
}
try {
rimraf.sync('/tmp/test_directory');
fs.mkdirSync('/tmp/test_directory');
fs.chmodSync('/tmp/test_directory', parseInt(555, 8));
var repo = new Repo({'name': 'unwriteable_directory', 'url': 'http://www.github.com/',
'local_store':'/tmp/test_directory', 'branches': ['master']});
should.fail("Repo with non-writeable directory should throw an exception");
} catch(e) {
e.message.should.equal('/tmp/test_directory is not writeable');
} finally {
rimraf.sync('/tmp/test_directory');
}
});
it ('should reject a repo with duplicate branches', function() {
try {
var repo = new Repo({'name': 'busted_dupe_branch_repo', 'url': 'http://www.github.com/',
'local_store':'/tmp/test_workspace', 'branches': ['master', 'master', 'commander']});
should.fail("Repo with duplicate branches should throw an exception");
} catch(e) {
e.message.should.startWith('Duplicate name found in branches for repo busted_dupe_branch_repo');
}
});
it ('should reject a repo with no branches', function() {
try {
var repo = new Repo({'name': 'busted_empty_branch_repo', 'local_store':'/tmp/test_workspace',
'url': 'http://www.github.com/', 'branches': []});
should.fail("Repo with no branches should be denied.");
} catch(e) {
e.message.should.equal('No branches specified.');
}
});
it ('should reject a repo with a broken git url', function(done) {
var repo = new Repo(_.extend(git_utils.createRepoConfig(), { url: 'file:///tmp/nobody_home' }));
repo.init(function(err) {
err[0].message.should.containEql('does not appear to be a git repository');
done();
});
});
it ('should reject an invalid git hook type', 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 repo.", function(err) {
if (err) return cb(err);
var config = {
local_store: git_utils.TEST_WORKING_DIR,
repos: [git_utils.createRepoConfig()]
};
config.repos[0].hooks = [{'type':'unknown'}];
var callback_seen = false;
git.createRepos(config, function(err) {
err.should.startWith("Failed to load repo test_repo due to Invalid hook type unknown");
done();
});
});
});
});
});