From 8d8d86dd35afcb60d173f8fb8b8c6fa1e50cc79e Mon Sep 17 00:00:00 2001 From: Pritam Paul Date: Tue, 29 Jun 2021 15:33:42 -0400 Subject: [PATCH] feat: add verify method to router (#34) --- index.js | 18 +++++++ test/index.test.js | 119 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 134 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 78dbe2a..036e477 100644 --- a/index.js +++ b/index.js @@ -180,6 +180,24 @@ class ExecutorRouter extends Executor { return executor.stop(config); } + + /** + * Verifies the status of a build in an executor + * @method _start + * @param {Object} config Configuration + * @param {Object} [config.annotations] Optional key/value object + * @param {String} config.apiUri Screwdriver's API + * @param {Object} [config.build] Build object + * @param {String} config.buildId Unique ID for a build + * @param {String} config.container Container for the build to run in + * @param {String} config.token JWT to act on behalf of the build + * @return {Promise} + */ + _verify(config) { + const executor = this.getExecutor(config); + + return executor.verify(config); + } } module.exports = ExecutorRouter; diff --git a/test/index.test.js b/test/index.test.js index ff8d398..54a047a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -66,15 +66,18 @@ describe('index test', () => { k8sExecutorMock = { _start: sinon.stub(), - _stop: sinon.stub() + _stop: sinon.stub(), + _verify: sinon.stub() }; exampleExecutorMock = { _start: sinon.stub(), - _stop: sinon.stub() + _stop: sinon.stub(), + _verify: sinon.stub() }; k8sVmExecutorMock = { _start: sinon.stub(), - _stop: sinon.stub() + _stop: sinon.stub(), + _verify: sinon.stub() }; mockery.registerMock('fs', fsMock); mockery.registerMock('screwdriver-executor-k8s', testExecutor(k8sExecutorMock)); @@ -501,6 +504,116 @@ describe('index test', () => { }); }); + describe('_verify', () => { + it('default executor when no annotation is given', () => { + executor = new Executor({ + defaultPlugin: 'example', + ecosystem, + executor: [ + { + name: 'k8s', + options: k8sPluginOptions + }, + { + name: 'example', + options: examplePluginOptions + } + ] + }); + exampleExecutorMock._verify.resolves('exampleExecutorMockResult'); + + return executor + .verify({ + buildId: 920, + container: 'node:4', + apiUri: 'http://api.com', + token: 'asdf' + }) + .then(result => { + assert.strictEqual(result, 'exampleExecutorMockResult'); + }); + }); + + it('default executor is the first one when given no executor annotation', () => { + k8sExecutorMock._verify.resolves('k8sExecutorResult'); + + return executor + .verify({ + buildId: 920, + container: 'node:4', + apiUri: 'http://api.com', + token: 'asdf' + }) + .then(result => { + assert.strictEqual(result, 'k8sExecutorResult'); + assert.calledOnce(k8sExecutorMock._verify); + assert.notCalled(exampleExecutorMock._verify); + }); + }); + + it('default executor is the first one when given an invalid executor annotation', () => { + k8sExecutorMock._verify.resolves('k8sExecutorResult'); + exampleExecutorMock._verify.rejects(); + + return executor + .verify({ + annotations: { + 'beta.screwdriver.cd/executor': 'darrenIsSometimesRight' + }, + buildId: 920, + container: 'node:4', + apiUri: 'http://api.com', + token: 'asdf' + }) + .then(result => { + assert.strictEqual(result, 'k8sExecutorResult'); + assert.calledOnce(k8sExecutorMock._verify); + assert.notCalled(exampleExecutorMock._verify); + }); + }); + + it('uses an annotation to determine which executor to call', () => { + k8sExecutorMock._verify.rejects(); + exampleExecutorMock._verify.resolves('exampleExecutorResult'); + + return executor + .verify({ + annotations: { + 'beta.screwdriver.cd/executor': 'example' + }, + buildId: 920, + container: 'node:4', + apiUri: 'http://api.com', + token: 'asdf' + }) + .then(result => { + assert.strictEqual(result, 'exampleExecutorResult'); + assert.calledOnce(exampleExecutorMock._verify); + assert.notCalled(k8sExecutorMock._verify); + }); + }); + + it('propogates the failure from initiating a start', () => { + const testError = new Error('triggeredError'); + + k8sExecutorMock._verify.rejects(testError); + + return executor + .verify({ + annotations: { + 'beta.screwdriver.cd/executor': 'k8s' + }, + buildId: 920, + container: 'node:4', + apiUri: 'http://api.com', + token: 'asdf' + }) + .then(assert.fail, err => { + assert.deepEqual(err, testError); + }); + }); + }); + describe('Executor config with weightage', () => { beforeEach(() => { executor = new Executor({