-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuildLogsController.unit.js
More file actions
362 lines (347 loc) · 12.5 KB
/
buildLogsController.unit.js
File metadata and controls
362 lines (347 loc) · 12.5 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
'use strict';
var $controller;
var $scope;
var $rootScope;
var $q;
var $timeout;
var EventEmitter = require('events').EventEmitter;
describe('BuildLogsController'.bold.underline.blue, function () {
var mockStreamingLog;
var mockPrimus;
var mockInstance;
var mockStream;
var mockStreamingLogContents;
var BLC;
var mockCreateDebugContainer;
var mockDebugContainer;
var mockDockerfile;
var mockErrs;
var isRunnabotPartOfOrgStub;
var fetchRepoDockerfileStub;
function setup(useInstance, dontIncludeBuildDockerContainer) {
mockInstance = {
build: 'This is a test build!',
status: sinon.stub().returns('building'),
hasDockerfileMirroring: sinon.stub().returns(true),
on: sinon.spy(),
off: sinon.spy(),
id: sinon.stub().returns('instanceID'),
getContainerHostname: sinon.spy(),
attrs: {
contextVersion: {
_id: 'ctxId',
build: {
dockerContainer: dontIncludeBuildDockerContainer ? undefined : 'asdsdfsd'
}
}
},
getRepoAndBranchName: sinon.stub().returns('test/master'),
mirroredDockerfile: {
attrs: {
path: '/',
name: 'Dockerfile'
}
}
};
mockDebugContainer = {
id: sinon.stub().returns('debugContainerId'),
attrs: {
contextVersion: '12345',
layerId: 'Layer ID'
}
};
mockStreamingLogContents = {
destroy: sinon.stub(),
logs: []
};
mockStreamingLog = sinon.stub().returns(mockStreamingLogContents);
mockDockerfile = {message: 'Not Found'};
mockPrimus = {
createBuildStream: sinon.spy(function () {
mockStream = new EventEmitter();
sinon.spy(mockStream, 'on');
return mockStream;
}),
createBuildStreamFromContextVersionId: sinon.spy(function () {
mockStream = new EventEmitter();
sinon.spy(mockStream, 'on');
return mockStream;
})
};
mockErrs = {
handler: sinon.spy(),
report: sinon.spy()
};
angular.mock.module('app', function ($provide) {
$provide.value('streamingLog', mockStreamingLog);
$provide.value('primus', mockPrimus);
$provide.value('errs', mockErrs);
$provide.factory('createDebugContainer', function ($q) {
mockCreateDebugContainer = sinon.stub().returns($q.when(mockDebugContainer));
return mockCreateDebugContainer;
});
$provide.factory('fetchRepoDockerfile', function ($q) {
fetchRepoDockerfileStub = sinon.stub().returns($q.when(mockDockerfile));
return fetchRepoDockerfileStub;
});
$provide.factory('isRunnabotPartOfOrg', function ($q) {
isRunnabotPartOfOrgStub = sinon.stub().returns($q.when());
return isRunnabotPartOfOrgStub;
});
});
angular.mock.inject(function (
_$controller_,
_$rootScope_,
_$q_,
_$timeout_
) {
$controller = _$controller_;
$scope = _$rootScope_.$new();
$rootScope = _$rootScope_;
$q = _$q_;
$timeout = _$timeout_;
});
var laterController = $controller('BuildLogsController', {
$scope: $scope
}, true);
if (useInstance) {
laterController.instance.instance = mockInstance;
} else {
laterController.instance.debugContainer = mockDebugContainer;
}
BLC = laterController();
$scope.BLC = BLC;
}
describe('with an instance without a ready build', function () {
beforeEach(function () {
setup(true, true);
$rootScope.$digest();
});
it('shouldn\'t create the build stream until the dockerContainer is populated', function () {
sinon.assert.notCalled(mockPrimus.createBuildStream);
sinon.assert.notCalled(mockStreamingLog);
sinon.assert.notCalled(mockPrimus.createBuildStream);
sinon.assert.notCalled(mockStreamingLog);
BLC.instance.attrs.contextVersion.build.dockerContainer = 'asdasd';
$rootScope.$digest();
sinon.assert.calledWith(mockPrimus.createBuildStream, mockInstance.build);
sinon.assert.calledOnce(mockStreamingLog);
sinon.assert.calledOnce(mockPrimus.createBuildStream);
sinon.assert.calledWith(mockStreamingLog, mockStream);
});
});
describe('with an instance', function () {
beforeEach(function () {
setup(true);
$rootScope.$digest();
});
it('should create the build stream and send it to streamingLog', function () {
sinon.assert.calledWith(mockPrimus.createBuildStream, mockInstance.build);
sinon.assert.calledOnce(mockStreamingLog);
sinon.assert.calledOnce(mockPrimus.createBuildStream);
sinon.assert.calledWith(mockStreamingLog, mockStream);
});
it('should handle destroy events', function () {
$scope.$destroy();
sinon.assert.calledOnce(mockStreamingLogContents.destroy);
});
it('should destroy the old stream, and create a new one', function () {
sinon.assert.calledWith(mockPrimus.createBuildStream, mockInstance.build);
sinon.assert.calledOnce(mockStreamingLog);
sinon.assert.calledOnce(mockPrimus.createBuildStream);
sinon.assert.calledWith(mockStreamingLog, mockStream);
sinon.assert.calledOnce(BLC.instance.off);
sinon.assert.calledOnce(BLC.instance.on);
BLC.instance.off.reset();
BLC.instance.on.reset();
BLC.instance.attrs.contextVersion.build.dockerContainer = 'asd3e3rdfafads';
$scope.$digest();
sinon.assert.calledTwice(BLC.instance.off);
sinon.assert.calledOnce(BLC.instance.on);
});
describe('handleUpdate', function () {
it('should handle build logs running status', function () {
mockInstance.on.lastCall.args[1]();
expect(BLC.buildLogsRunning).to.be.ok;
mockInstance.status.returns('started');
mockInstance.on.lastCall.args[1]();
expect(BLC.buildLogsRunning).to.not.be.ok;
});
it('should set error message', function () {
var testErr = 'thatsDope';
mockInstance.attrs.contextVersion.build.error = {
message: testErr
};
mockInstance.status.returns('buildFailed');
mockInstance.on.lastCall.args[1]();
expect(BLC.failReason).to.equal(testErr);
expect(BLC.buildStatus).to.equal('failed');
});
it('should set failed if no error message', function () {
mockInstance.status.returns('buildFailed');
mockInstance.on.lastCall.args[1]();
expect(BLC.failReason).to.equal('failed');
expect(BLC.buildStatus).to.equal('failed');
});
it('should handle toggling debug mode', function () {
mockInstance.on.lastCall.args[1]();
expect(BLC.showDebug).to.not.be.ok;
mockInstance.status.returns('buildFailed');
mockInstance.on.lastCall.args[1]();
expect(BLC.showDebug).to.be.ok;
});
it('should display a dockerfile error if absent', function () {
mockInstance.status.returns('buildFailed');
mockInstance.on.lastCall.args[1]();
$scope.$digest();
expect(BLC.showNoDockerfileError).to.equal(true);
})
});
describe('actions', function () {
describe('launchDebugContainer', function () {
it('should create a debug container', function () {
var event = {
stopPropagation: sinon.spy()
};
var command = {
imageId: 12345
};
var newWindow = {
location: ''
};
sinon.stub(window, 'open').returns(newWindow);
expect(BLC.generatingDebug).to.not.be.ok;
BLC.actions.launchDebugContainer(event, command);
expect(BLC.generatingDebug).to.be.ok;
$scope.$digest();
expect(BLC.generatingDebug).to.not.be.ok;
expect(newWindow.location).to.equal('/debug/' + mockDebugContainer.id());
sinon.assert.calledOnce(mockCreateDebugContainer);
sinon.assert.calledWith(mockCreateDebugContainer, mockInstance.id(), mockInstance.attrs.contextVersion._id, command.imageId);
window.open.restore();
});
it('should do nothing if we are already a debug container', function () {
var event = {
stopPropagation: sinon.spy()
};
var command = {
imageId: 12345
};
BLC.debugContainer = {};
BLC.actions.launchDebugContainer(event, command);
sinon.assert.notCalled(mockCreateDebugContainer);
});
it('should do nothing if we are already generating a debug container', function () {
var event = {
stopPropagation: sinon.spy()
};
var command = {
imageId: 12345
};
BLC.generatingDebug = true;
BLC.actions.launchDebugContainer(event, command);
sinon.assert.notCalled(mockCreateDebugContainer);
});
it('should handle errors creating deubg containers', function () {
var event = {
stopPropagation: sinon.spy()
};
var command = {
imageId: 12345
};
var newWindow = {
location: '',
close: sinon.stub()
};
mockCreateDebugContainer.returns($q.reject('REJECTION!'));
sinon.stub(window, 'open').returns(newWindow);
expect(BLC.generatingDebug, 'generatingDebug before').to.not.be.ok;
BLC.actions.launchDebugContainer(event, command);
expect(BLC.generatingDebug, 'generatingDebug before promise resolved').to.be.ok;
$scope.$digest();
expect(BLC.generatingDebug, 'generatingDebug after').to.not.be.ok;
sinon.assert.calledOnce(mockCreateDebugContainer);
sinon.assert.calledWith(mockCreateDebugContainer, mockInstance.id(), mockInstance.attrs.contextVersion._id, command.imageId);
sinon.assert.calledOnce(mockErrs.handler);
sinon.assert.calledWith(mockErrs.handler, 'REJECTION!');
window.open.restore();
});
});
});
describe('on stream end', function () {
it('should retry if there was no data sent then fail', function () {
for (var i=0;i<16;i++) {
mockStream.emit('end');
$scope.$digest();
$timeout.flush();
}
expect(BLC.streamFailure).to.be.ok;
expect(BLC.buildLogsRunning).to.not.be.ok;
});
it('should finish if we had data', function () {
mockStream.emit('data', {});
mockStream.emit('end');
$scope.$digest();
expect(BLC.streamFailure).to.not.be.ok;
expect(BLC.buildLogsRunning).to.not.be.ok;
});
});
describe('on stream disconnect', function () {
it('should reload everything', function () {
sinon.assert.calledOnce(mockPrimus.createBuildStream);
mockStream.emit('disconnection');
$scope.$digest();
sinon.assert.calledTwice(mockPrimus.createBuildStream);
});
it('should not create 2 streams accidentally', function () {
sinon.assert.calledOnce(mockPrimus.createBuildStream);
delete BLC.instance.attrs.contextVersion.build.dockerContainer;
mockPrimus.createBuildStream.reset();
mockStream.emit('disconnection');
$scope.$digest();
mockStream.emit('disconnection');
$scope.$digest();
BLC.instance.attrs.contextVersion.build.dockerContainer = 'asdasdasda';
$scope.$digest();
sinon.assert.calledOnce(mockPrimus.createBuildStream);
});
});
});
describe('with a debug container', function () {
beforeEach(function () {
setup();
});
describe('setupStream', function () {
it('should createBuildStreamFromContextVersionId', function () {
sinon.assert.calledOnce(mockPrimus.createBuildStreamFromContextVersionId);
sinon.assert.calledWith(mockPrimus.createBuildStreamFromContextVersionId, mockDebugContainer.attrs.contextVersion);
sinon.assert.calledOnce(mockStreamingLog);
sinon.assert.calledWith(mockStreamingLog, mockStream);
});
});
describe('getBuildLogs', function () {
it('should filter the build logs to only show logs until the one we are debugging', function () {
BLC.buildLogs = [
{
imageId: '1'
},
{
imageId: '2'
},
{
imageId: 'Layer ID'
},
{
imageId: '3'
}
];
var newBuildLogs = BLC.getBuildLogs();
expect(newBuildLogs.length).to.equal(3);
var foundThird = newBuildLogs.find(function (log) {
return log.imageId === '3';
});
expect(foundThird).to.not.be.ok;
});
});
});
});