nci/test/distributor.js

153 lines
4.1 KiB
JavaScript
Raw Normal View History

2015-04-09 18:55:29 +00:00
'use strict';
var Distributor = require('../lib/distributor').Distributor,
Node = require('../lib/node').Node,
expect = require('expect.js'),
EventEmitter = require('events').EventEmitter,
sinon = require('sinon')
2015-04-09 18:55:29 +00:00
describe('Distributor', function() {
var distributor,
projects = [{name: 'project1'}];
2015-04-09 18:55:29 +00:00
2015-04-09 19:39:22 +00:00
var createNodeMock = function(executorRun) {
return function(params) {
var node = new Node(params);
node._createExecutor = function() {
var executor = new EventEmitter();
executor.run = executorRun;
return executor;
2015-04-09 19:39:22 +00:00
};
return node;
};
};
var expectUpdateBuild = function(distributor, build, number, conditionsHash) {
var conditions = conditionsHash[number];
expect(distributor.queue).length(conditions.queue.length);
expect(build.status).equal(conditions.build.status);
if (build.status === 'error') {
2015-05-23 17:47:17 +00:00
expect(build.error).eql(conditions.build.error.message);
2015-04-09 19:39:22 +00:00
}
};
describe('with success project', function() {
2015-04-09 18:55:29 +00:00
before(function() {
var executorRun = function(params, callback) {
setTimeout(callback, 1);
};
sinon.stub(Distributor.prototype, '_createNode', createNodeMock(
executorRun
));
2015-04-09 19:39:22 +00:00
});
var updateBuildSpy;
2015-04-09 19:39:22 +00:00
it('instance should be created without errors', function() {
2015-04-09 19:39:22 +00:00
distributor = new Distributor({
2015-06-13 18:59:32 +00:00
projects: projects,
nodes: [{type: 'local', maxExecutorsCount: 1}]
2015-04-09 19:39:22 +00:00
});
updateBuildSpy = sinon.spy(distributor, '_updateBuild');
2015-04-09 19:39:22 +00:00
});
it('should run without errors', function(done) {
2015-06-13 19:08:13 +00:00
distributor.run({projectName: 'project1'}, function(err) {
2015-04-09 19:39:22 +00:00
expect(err).not.ok();
done();
2015-04-09 19:39:22 +00:00
});
});
it('build should be queued', function() {
var changes = updateBuildSpy.getCall(0).args[1];
expect(changes).only.have.keys(
2015-06-14 14:21:04 +00:00
'project', 'initiator', 'params', 'createDate', 'status',
'completed'
);
expect(changes.status).equal('queued');
expect(changes.completed).equal(false);
});
2015-06-14 23:27:58 +00:00
it('build should be in-progress', function() {
var changes = updateBuildSpy.getCall(1).args[1];
expect(changes).only.have.keys('startDate', 'status', 'waitReason');
expect(changes.status).equal('in-progress');
expect(changes.waitReason).equal('');
});
it('build should be done', function() {
var changes = updateBuildSpy.getCall(2).args[1];
expect(changes).only.have.keys(
'endDate', 'status', 'completed', 'error'
);
expect(changes.status).equal('done');
expect(changes.completed).equal(true);
expect(changes.error).equal(null);
});
it('update build called 3 times in total', function() {
expect(updateBuildSpy.callCount).equal(3);
2015-04-09 19:39:22 +00:00
});
after(function() {
Distributor.prototype._createNode.restore();
2015-04-09 19:39:22 +00:00
});
});
describe('with fail project', function() {
before(function() {
var executorRun = function(params, callback) {
setTimeout(function() {
callback(new Error('Some error'));
}, 1);
};
sinon.stub(Distributor.prototype, '_createNode', createNodeMock(
executorRun
));
2015-04-09 18:55:29 +00:00
});
var updateBuildSpy;
2015-04-09 19:39:22 +00:00
it('instance should be created without errors', function() {
2015-04-09 18:55:29 +00:00
distributor = new Distributor({
2015-06-13 18:59:32 +00:00
projects: projects,
nodes: [{type: 'local', maxExecutorsCount: 1}]
2015-04-09 18:55:29 +00:00
});
updateBuildSpy = sinon.spy(distributor, '_updateBuild');
2015-04-09 18:55:29 +00:00
});
it('should run without errors', function(done) {
2015-06-13 19:08:13 +00:00
distributor.run({projectName: 'project1'}, function(err) {
2015-04-09 18:55:29 +00:00
expect(err).not.ok();
done();
2015-04-09 18:55:29 +00:00
});
});
it('build should be queued', function() {
var changes = updateBuildSpy.getCall(0).args[1];
expect(changes.status).equal('queued');
});
2015-06-14 23:27:58 +00:00
it('build should be in-progress', function() {
var changes = updateBuildSpy.getCall(1).args[1];
expect(changes.status).equal('in-progress');
});
it('build should be fail', function() {
var changes = updateBuildSpy.getCall(2).args[1];
expect(changes.status).equal('error');
expect(changes.completed).equal(true);
expect(changes.error).equal('Some error');
});
it('update build called 3 times in total', function() {
expect(updateBuildSpy.callCount).equal(3);
2015-04-09 18:55:29 +00:00
});
after(function() {
Distributor.prototype._createNode.restore();
2015-04-09 18:55:29 +00:00
});
});
});