-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetRepositories.js
More file actions
324 lines (295 loc) · 9.4 KB
/
getRepositories.js
File metadata and controls
324 lines (295 loc) · 9.4 KB
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
'use strict';
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var expect = require('code').expect;
var childProcess = require('child_process');
var path = require('path');
var fs = require('fs');
var sinon = require('sinon');
var lockfile = require('lockfile');
var steps = require('../../lib/steps');
lab.experiment('getRepositories', function () {
var requiredEnvVars = {
RUNNABLE_REPO: 'git@github.com:bkendall/flaming-octo-nemesis',
RUNNABLE_COMMITISH: '34a728c59e713b7fbf5b0d6ed3a8e4f4e2c695c5',
RUNNABLE_KEYS_BUCKET: 'runnable.image-builder',
RUNNABLE_DEPLOYKEY: 'flaming-octo-nemesis.key'
};
lab.beforeEach(function (done) {
Object.keys(requiredEnvVars).forEach(function (key) {
process.env[key] = requiredEnvVars[key];
});
steps.dirs = {};
steps.dirs.dockerContext = '/tmp/rnnbl.XXXXXXXXXXXXXXXXXXXX';
steps.dirs.keyDirectory = '/tmp/rnnbl.key.XXXXXXXXXXXXXXXXXXXX';
steps.logs = {};
steps.logs.dockerBuild = '/tmp/rnnbl.log.XXXXXXXXXXXXXXXXXXXX';
steps.logs.stdout = '/tmp/rnnbl.ib.stdout.XXXXXXXXXXXXXXXXXXXX';
steps.logs.stderr = '/tmp/rnnbl.ib.stderr.XXXXXXXXXXXXXXXXXXXX';
done();
});
lab.beforeEach(function (done) {
sinon.stub(childProcess, 'execFile')
.yields(null, new Buffer(''), new Buffer(''));
sinon.stub(steps, 'saveToLogs', function (cb) {
return function (err, stdout, stderr) {
cb(
err,
stdout ? stdout.toString() : '', stderr ? stderr.toString() : ''
);
};
});
sinon.stub(lockfile, 'lock').yields(null, true);
sinon.stub(lockfile, 'unlock').yields(null);
done();
});
lab.afterEach(function (done) {
childProcess.execFile.restore();
steps.saveToLogs.restore();
lockfile.lock.restore();
lockfile.unlock.restore();
done();
});
lab.experiment('succeeds', function () {
lab.experiment('when there is a repo', function () {
lab.beforeEach(function (done) {
childProcess.execFile.yieldsAsync(null, new Buffer(''), new Buffer(''));
done();
});
lab.test('to download the repo', function (done) {
// no lock, so it returns true
steps.getRepositories(function (err) {
if (err) { return done(err); }
sinon.assert.calledWith(
childProcess.execFile,
'git',
[
'clone',
'-q',
'--depth=1',
'git@github.com:bkendall/flaming-octo-nemesis',
'/cache/bkendall/flaming-octo-nemesis'
]
);
sinon.assert.calledWith(
childProcess.execFile,
'git',
[
'checkout',
'-q',
'34a728c59e713b7fbf5b0d6ed3a8e4f4e2c695c5'
]
);
sinon.assert.calledWith(
childProcess.execFile,
'cp',
[
'-p',
'-r',
'-d',
sinon.match.string,
sinon.match.string
]
);
sinon.assert.calledOnce(lockfile.lock);
sinon.assert.calledOnce(lockfile.unlock);
done();
});
});
});
lab.experiment('when there is no repo', function () {
lab.beforeEach(function (done) {
delete process.env.RUNNABLE_REPO;
done();
});
lab.afterEach(function (done) {
process.env.RUNNABLE_REPO = requiredEnvVars.RUNNABLE_REPO;
done();
});
lab.it('just continues', function (done) {
steps.getRepositories(function (err) {
if (err) { return done(err); }
// TODO look for no repos
done();
});
});
});
lab.experiment('when a lock already exists for the repo', function () {
lab.beforeEach(function (done) {
// cannot get the lock => false
lockfile.lock.yields(new Error());
done();
});
lab.test('to download the repo', function (done) {
steps.getRepositories(function (err) {
if (err) { return done(err); }
sinon.assert.calledWith(
childProcess.execFile,
'git',
[
'clone',
'-q',
'--depth=1',
sinon.match.string,
sinon.match.string
]
);
sinon.assert.calledWith(
childProcess.execFile,
'git',
[
'checkout',
'-q',
sinon.match.string
]
);
expect(childProcess.execFile.calledWithMatch('cp')).to.be.false();
done();
});
});
});
lab.experiment('when the repo has already been cached', function () {
var repoGitDir = path.join(
'/cache',
'bkendall/flaming-octo-nemesis',
'.git');
lab.beforeEach(function (done) {
childProcess.execFile.yieldsAsync(null, new Buffer(''), new Buffer(''));
sinon.stub(fs, 'existsSync').withArgs(repoGitDir).returns(true);
done();
});
lab.afterEach(function (done) {
fs.existsSync.restore();
done();
});
lab.test('to still complete', function (done) {
steps.getRepositories(function (err) {
if (err) { return done(err); }
expect(fs.existsSync.calledWithExactly(repoGitDir)).to.be.true();
expect(childProcess.execFile.calledWithMatch('git',
['clone', '-q', sinon.match.string]))
.to.be.false();
expect(childProcess.execFile.calledWithMatch('git',
['fetch', '--all']))
.to.be.true();
expect(childProcess.execFile.calledWithMatch('cp')).to.be.true();
expect(lockfile.lock.calledOnce).to.be.true();
done();
});
});
lab.experiment('but needing to be updated', function () {
lab.beforeEach(function (done) {
childProcess.execFile
.withArgs('git', ['rev-parse', 'HEAD'])
.yields(
null,
new Buffer('04d07787dd44b4f2167e26532e95471871a9b233'),
new Buffer(''));
done();
});
lab.test('to updated and complete', function (done) {
steps.getRepositories(function (err) {
if (err) { return done(err); }
sinon.assert.calledWith(
childProcess.execFile,
'git',
[
'fetch',
'--all'
]
);
sinon.assert.calledWith(
childProcess.execFile,
'git',
[
'checkout',
'-q',
'34a728c59e713b7fbf5b0d6ed3a8e4f4e2c695c5'
]
);
done();
});
});
});
});
});
lab.experiment('fails', function () {
lab.experiment('when there is a repo, but no commitish', function () {
lab.beforeEach(function (done) {
delete process.env.RUNNABLE_COMMITISH;
done();
});
lab.afterEach(function (done) {
process.env.RUNNABLE_COMMITISH = requiredEnvVars.RUNNABLE_COMMITISH;
done();
});
lab.it('just continues', function (done) {
steps.getRepositories(function (err) {
expect(err).to.exist();
expect(err.message).to.match(/COMMITISH is missing/);
done();
});
});
});
lab.experiment('to make sure the cache directory exists', function () {
lab.beforeEach(function (done) {
childProcess.execFile
.withArgs('mkdir', ['-p', '/cache'])
.callsArgWith(2, new Error('Command failed'), '', '');
done();
});
lab.it('returns an error', function (done) {
steps.getRepositories(function (err) {
expect(err).to.exist();
expect(err.message).to.match(/Command failed/);
done();
});
});
});
lab.experiment('to remove all the ssh keys from agent', function () {
lab.beforeEach(function (done) {
childProcess.execFile
.withArgs('ssh-add', ['-D'])
.callsArgWith(2, new Error('Command failed'), '', '');
done();
});
lab.it('returns an error', function (done) {
steps.getRepositories(function (err) {
expect(err).to.exist();
expect(err.message).to.match(/Command failed/);
done();
});
});
});
lab.experiment('to make the lock file directory', function () {
lab.beforeEach(function (done) {
childProcess.execFile
.withArgs('mkdir', ['-p', '/cache/bkendall'])
.callsArgWith(2, new Error('Command failed'), '', '');
done();
});
lab.it('returns an error', function (done) {
steps.getRepositories(function (err) {
expect(err).to.exist();
expect(err.message).to.match(/Command failed/);
done();
});
});
});
lab.experiment('to release the lock', function () {
lab.beforeEach(function (done) {
lockfile.unlock.yields(new Error('could not unlock'));
done();
});
lab.it('returns an error', function (done) {
steps.getRepositories(function (err) {
expect(err).to.exist();
expect(err.message).to.match(/could not unlock/);
// should have tried to unlock it twice
sinon.assert.calledTwice(lockfile.unlock);
done();
});
});
});
});
});