Skip to content

Commit

Permalink
feat: add verify method to router (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
pritamstyz4ever authored Jun 29, 2021
1 parent bcaddbb commit 8d8d86d
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 3 deletions.
18 changes: 18 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
119 changes: 116 additions & 3 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit 8d8d86d

Please sign in to comment.