Skip to content

Commit

Permalink
feat(jobs): verify test exists before creating/updating job (#29)
Browse files Browse the repository at this point in the history
* feat(jobs): verify test exists before creating/updating job

re #26

* style(jobs): changing nock match function

* feat(jobs): verify test exists before creating/updating job

re #26

* style(jobs): changing nock match function

* refactor(jobs): refactor of job verifier to use error handler

* test(reporter): integrate new job validations into reporter tests
  • Loading branch information
enudler authored and NivLipetz committed Feb 4, 2019
1 parent c1802b1 commit dd62517
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 120 deletions.
45 changes: 17 additions & 28 deletions src/jobs/helpers/jobVerifier.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';
let request = require('request-promise-native');
let config = require('../../config/serviceConfig');
let uuid = require('uuid/v4');
let testsManager = require('../../tests/models/manager');
module.exports.verifyJobBody = (req, res, next) => {
let jobBody = req.body;
if (!(jobBody.run_immediately || jobBody.cron_expression)) {
Expand All @@ -11,30 +9,21 @@ module.exports.verifyJobBody = (req, res, next) => {
next();
};

// Todo rewrite
module.exports.verifyTestExists = async (req, res, next) => {
return next();
// let jobBody = req.body;
// if (jobBody.test_id) {
// try {
// await request.get({
// url: config.testsApiUrl + '/v1/tests/' + jobBody.test_id,
// json: true,
// forever: true,
// headers: {'x-zooz-request-id': uuid()}
// });
//
// if (!req.context) {
// req.context = {};
// }
//
// } catch (error) {
// if (error.statusCode === 404) {
// return res.status(400).json({message: `test with id: ${jobBody.test_id} does not exist`});
// } else {
// return res.status(500).json({message: error.message});
// }
// }
// }
// next();
let errorToThrow;
let jobBody = req.body;
if (jobBody.test_id) {
try {
await testsManager.getTest(jobBody.test_id);
} catch (error) {
if (error.statusCode === 404) {
errorToThrow = new Error(`test with id: ${jobBody.test_id} does not exist`);
errorToThrow.statusCode = 400;
} else {
errorToThrow = new Error(error.message);
errorToThrow.statusCode = 500;
}
}
}
next(errorToThrow);
};
3 changes: 1 addition & 2 deletions tests/integration-tests/jobs/createJobGlobal-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ describe('Create job global tests', () => {
});
});

// todo waiting for tests api integration
it.skip('Create a job with non existing test_id', () => {
it('Create a job with non existing test_id', () => {
let illegalBody = {
test_id: '56ccc314-8c92-4002-839d-8424909ff475',
arrival_rate: 1,
Expand Down
46 changes: 28 additions & 18 deletions tests/integration-tests/jobs/createJobKubernetes-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
let should = require('should');
let uuid = require('uuid');
let schedulerRequestCreator = require('./helpers/requestCreator');
let testsRequestCreator = require('../tests/helpers/requestCreator');

let nock = require('nock');
let serviceConfig = require('../../../src/config/serviceConfig');
let kubernetesConfig = require('../../../src/config/kubernetesConfig');

describe('Create job specific kubernetes tests', () => {
let testId;

before(async () => {
await schedulerRequestCreator.init();
await testsRequestCreator.init();

let requestBody = require('../../testExamples/Custom_test');
let response = await testsRequestCreator.createTest(requestBody, {});
should(response.statusCode).eql(201);
should(response.body).have.key('id');
testId = response.body.id;
});

beforeEach(async () => {
Expand All @@ -18,14 +29,6 @@ describe('Create job specific kubernetes tests', () => {
describe('Kubernetes', () => {
describe('Good requests', () => {
let jobId;
let testId = '56ccc314-8c92-4002-839d-8424909ff475';
let expectedResult = {
environment: 'test',
test_id: testId,
duration: 1,
arrival_rate: 1
};

describe('Create two jobs, one is one time, second one is cron and get them', () => {
let createJobResponse;
let getJobsFromService;
Expand Down Expand Up @@ -83,7 +86,6 @@ describe('Create job specific kubernetes tests', () => {
let relevantJobs = getJobsFromService = getJobsFromService.body.filter(job => job.id === cronJobId || job.id === oneTimeJobId);
should(relevantJobs.length).eql(1);
should(relevantJobs[0].id).eql(cronJobId);

});

it('Get the jobs, with one_time query param, two jobs should be returned', async () => {
Expand Down Expand Up @@ -122,15 +124,23 @@ describe('Create job specific kubernetes tests', () => {
describe('Create one time job, should create job with the right parameters and run it, finally stop and delete it', () => {
let createJobResponse;
let getJobsFromService;
let validBody = {
test_id: testId,
arrival_rate: 1,
duration: 1,
environment: 'test',
run_immediately: true
};

let expectedResult;
it('Create the job', async () => {
let validBody = {
test_id: testId,
arrival_rate: 1,
duration: 1,
environment: 'test',
run_immediately: true
};

expectedResult = {
environment: 'test',
test_id: testId,
duration: 1,
arrival_rate: 1
};

nock(kubernetesConfig.kubernetesUrl).post(`/apis/batch/v1/namespaces/${kubernetesConfig.kubernetesNamespace}/jobs`)
.reply(200, {
metadata: { name: 'jobName', uid: 'uid' },
Expand Down Expand Up @@ -199,7 +209,7 @@ describe('Create job specific kubernetes tests', () => {

it('Create the job, then get the runs, then get the job from kubernetes and service', async () => {
date = new Date();
date.setSeconds(date.getSeconds() + 3);
date.setSeconds(date.getSeconds() + 2);
let validBody = {
test_id: testId,
arrival_rate: 1,
Expand Down
55 changes: 34 additions & 21 deletions tests/integration-tests/jobs/createJobMetronome-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
let should = require('should');
let uuid = require('uuid');
let schedulerRequestCreator = require('./helpers/requestCreator');
let testsRequestCreator = require('../tests/helpers/requestCreator');
let nock = require('nock');
let serviceConfig = require('../../../src/config/serviceConfig');
let metronomeConfig = require('../../../src/config/metronomeConfig');

describe('Create job specific metronome tests', () => {
let testId;
let expectedResult;
before(async () => {
await schedulerRequestCreator.init();
await testsRequestCreator.init();

let requestBody = require('../../testExamples/Custom_test');
let response = await testsRequestCreator.createTest(requestBody, {});
should(response.statusCode).eql(201);
should(response.body).have.key('id');
testId = response.body.id;

expectedResult = {
environment: 'test',
test_id: testId,
duration: 1,
arrival_rate: 1
};
});

beforeEach(async () => {
Expand All @@ -18,27 +35,21 @@ describe('Create job specific metronome tests', () => {
describe('Metronome', () => {
describe('Good requests', () => {
let jobId;
let testId = '56ccc314-8c92-4002-839d-8424909ff475';
let expectedResult = {
environment: 'test',
test_id: testId,
duration: 1,
arrival_rate: 1
};

describe('Create one time job, job not yet exists, should create job with the right parameters and run it, finally stop and delete it', () => {
let createJobResponse;
let getJobsFromService;
let jobResponseBody;
let validBody = {
test_id: testId,
arrival_rate: 1,
duration: 1,
environment: 'test',
run_immediately: true
};

it('Create the job', async () => {
let validBody = {
test_id: testId,
arrival_rate: 1,
duration: 1,
environment: 'test',
run_immediately: true
};

nock(metronomeConfig.metronomeUrl)
.get(url => {
return url.startsWith('/v1/jobs/predator.');
Expand Down Expand Up @@ -104,15 +115,17 @@ describe('Create job specific metronome tests', () => {
let createJobResponse;
let getJobsFromService;
let jobResponseBody;
let validBody = {
test_id: testId,
arrival_rate: 1,
duration: 1,
environment: 'test',
run_immediately: true
};

it('Create the job', async () => {

let validBody = {
test_id: testId,
arrival_rate: 1,
duration: 1,
environment: 'test',
run_immediately: true
};

nock(metronomeConfig.metronomeUrl)
.get(url => {
return url.startsWith('/v1/jobs/predator.');
Expand Down
46 changes: 32 additions & 14 deletions tests/integration-tests/jobs/updateJob-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
'use strict';

let schedulerRequestCreator = require('./helpers/requestCreator');
let testsRequestCreator = require('../tests/helpers/requestCreator');

let should = require('should');
let nock = require('nock');
let kubernetesConfig = require('../../../src/config/kubernetesConfig');

describe('Update scheduled job', function () {
this.timeout(5000);
let testId = '56ccc314-8c92-4002-839d-8424909ff475';
let testId;
let updatedTestId;

before(async () => {
await schedulerRequestCreator.init();
await testsRequestCreator.init();

let requestBody = require('../../testExamples/Custom_test');
let response = await testsRequestCreator.createTest(requestBody, {});
should(response.statusCode).eql(201);
should(response.body).have.key('id');
testId = response.body.id;

response = await testsRequestCreator.createTest(requestBody, {});
should(response.statusCode).eql(201);
should(response.body).have.key('id');
updatedTestId = response.body.id;
});

describe('Update test id', () => {
Expand All @@ -20,11 +34,11 @@ describe('Update scheduled job', function () {

let jobId;
let runsWithUpdatedTestId = 0;
let updatedTestId = '56ccc314-8c92-4002-839d-8424909ff476';

before(async () => {
nock(kubernetesConfig.kubernetesUrl).post(`/apis/batch/v1/namespaces/${kubernetesConfig.kubernetesNamespace}/jobs`, body => {
return body.spec.template.spec.containers['0'].env.find(o => o.name === 'TEST_ID').value === updatedTestId;
let isMatch = body.spec.template.spec.containers['0'].env.find(o => o.name === 'TEST_ID').value === updatedTestId;
return isMatch;
}).reply(200,
() => {
runsWithUpdatedTestId++;
Expand Down Expand Up @@ -72,8 +86,8 @@ describe('Update scheduled job', function () {
getJobResponseAfterUpdate.body.should.eql(expectedResponseBodyAfterUpdate);
});

it('Wait for 4 seconds', (done) => {
setTimeout(done, 4000);
it('Wait for 5 seconds', (done) => {
setTimeout(done, 5000);
});

it('Validate test updated', async () => {
Expand All @@ -95,7 +109,8 @@ describe('Update scheduled job', function () {

before(async () => {
nock(kubernetesConfig.kubernetesUrl).post(`/apis/batch/v1/namespaces/${kubernetesConfig.kubernetesNamespace}/jobs`, body => {
return body.spec.template.spec.containers['0'].env.find(o => o.name === 'TEST_ID').value === testId;
let isMatch = body.spec.template.spec.containers['0'].env.find(o => o.name === 'TEST_ID').value === testId;
return isMatch;
}).reply(200,
() => {
runsWithTestId++;
Expand Down Expand Up @@ -175,11 +190,13 @@ describe('Update scheduled job', function () {
updatedCronExpression = `* ${date.getHours()} * * * * *`;

let updateJobResponse = await schedulerRequestCreator.updateJob(jobId,
{ cron_expression: updatedCronExpression,
{
cron_expression: updatedCronExpression,
arrival_rate: 10,
ramp_to: 20,
duration: 30,
environment: 'updated env' },
environment: 'updated env'
},
{
'Content-Type': 'application/json'
});
Expand All @@ -191,15 +208,17 @@ describe('Update scheduled job', function () {
'Content-Type': 'application/json'
});

should(getJobResponseAfterUpdate.body).eql({ id: jobId,
test_id: '56ccc314-8c92-4002-839d-8424909ff475',
should(getJobResponseAfterUpdate.body).eql({
id: jobId,
test_id: testId,
cron_expression: updatedCronExpression,
webhooks: ['a@webhooks.com'],
emails: ['b@emails.com'],
ramp_to: 20,
arrival_rate: 10,
duration: 30,
environment: 'updated env' });
environment: 'updated env'
});
});

it('Delete job', async () => {
Expand Down Expand Up @@ -235,8 +254,7 @@ describe('Update scheduled job', function () {
updateJobResponse.statusCode.should.eql(200);
});

// todo waiting for tests api integration
it.skip('Update job with non existing test id', async () => {
it('Update job with non existing test id', async () => {
let nonExistingTestId = '56ccc314-8c92-4002-839d-8424909ff475';
let updateJobResponse = await schedulerRequestCreator.updateJob(jobId, { test_id: nonExistingTestId }, {
'Content-Type': 'application/json'
Expand Down

0 comments on commit dd62517

Please sign in to comment.