Skip to content

Commit

Permalink
Merge pull request #8 from screwdriver-cd/fix
Browse files Browse the repository at this point in the history
fix: implement _stop() function
  • Loading branch information
minzcmu authored Jun 26, 2017
2 parents b290223 + 6484201 commit afbd253
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 5 deletions.
20 changes: 18 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class ExecutorRouter extends Executor {

/**
* Starts a new build in an executor
* @method start
* @method _start
* @param {Object} config Configuration
* @param {Object} [config.annotations] Optional key/value object
* @param {String} config.apiUri Screwdriver's API
Expand All @@ -67,13 +67,29 @@ class ExecutorRouter extends Executor {
* @param {String} config.token JWT to act on behalf of the build
* @return {Promise}
*/
start(config) {
_start(config) {
const annotations = config.annotations || {};
const executorType = annotations[ANNOTATION_EXECUTOR_TYPE];
const executor = this[executorType] || this.defaultExecutor; // Route to executor (based on annotations) or use default executor

return executor.start(config);
}

/**
* Stop a running or finished build
* @method _stop
* @param {Object} config Configuration
* @param {Object} [config.annotations] Optional key/value object
* @param {String} config.buildId Unique ID for a build
* @return {Promise}
*/
_stop(config) {
const annotations = config.annotations || {};
const executorType = annotations[ANNOTATION_EXECUTOR_TYPE];
const executor = this[executorType] || this.defaultExecutor; // Route to executor (based on annotations) or use default executor

return executor.stop(config);
}
}

module.exports = ExecutorRouter;
93 changes: 90 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ describe('index test', () => {
.returns('api_key');

k8sExecutorMock = {
_start: sinon.stub()
_start: sinon.stub(),
_stop: sinon.stub()
};
exampleExecutorMock = {
_start: sinon.stub()
_start: sinon.stub(),
_stop: sinon.stub()
};

mockery.registerMock('fs', fsMock);
Expand Down Expand Up @@ -255,7 +257,7 @@ describe('index test', () => {
});
});

describe('start', () => {
describe('_start', () => {
it('default executor when no annotation is given', () => {
executor = new Executor({
defaultPlugin: 'example',
Expand Down Expand Up @@ -354,4 +356,89 @@ describe('index test', () => {
});
});
});

describe('_stop', () => {
it('default executor when no annotation is given', () => {
executor = new Executor({
defaultPlugin: 'example',
ecosystem,
executor: [
{
name: 'k8s',
options: k8sPluginOptions
},
{
name: 'example',
options: examplePluginOptions
}
]
});
exampleExecutorMock._stop.resolves('exampleExecutorMockResult');

return executor.stop({
buildId: 920
}).then((result) => {
assert.strictEqual(result, 'exampleExecutorMockResult');
});
});

it('default executor is the first one when given no executor annotation', () => {
k8sExecutorMock._stop.resolves('k8sExecutorResult');

return executor.stop({
buildId: 920
}).then((result) => {
assert.strictEqual(result, 'k8sExecutorResult');
assert.calledOnce(k8sExecutorMock._stop);
assert.notCalled(exampleExecutorMock._stop);
});
});

it('default executor is the first one when given an invalid executor annotation', () => {
k8sExecutorMock._stop.resolves('k8sExecutorResult');
exampleExecutorMock._stop.rejects();

return executor.stop({
annotations: {
'beta.screwdriver.cd/executor': 'darrenIsSometimesRight'
},
buildId: 920
}).then((result) => {
assert.strictEqual(result, 'k8sExecutorResult');
assert.calledOnce(k8sExecutorMock._stop);
assert.notCalled(exampleExecutorMock._stop);
});
});

it('uses an annotation to determine which executor to call', () => {
k8sExecutorMock._stop.rejects();
exampleExecutorMock._stop.resolves('exampleExecutorResult');

return executor.stop({
annotations: {
'beta.screwdriver.cd/executor': 'example'
},
buildId: 920
}).then((result) => {
assert.strictEqual(result, 'exampleExecutorResult');
assert.calledOnce(exampleExecutorMock._stop);
assert.notCalled(k8sExecutorMock._stop);
});
});

it('propogates the failure from initiating a start', () => {
const testError = new Error('triggeredError');

k8sExecutorMock._stop.rejects(testError);

return executor.stop({
annotations: {
'beta.screwdriver.cd/executor': 'screwdriver-executor-k8s'
},
buildId: 920
}).then(assert.fail, (err) => {
assert.deepEqual(err, testError);
});
});
});
});

0 comments on commit afbd253

Please sign in to comment.