From 8f73619ba1f9bb683e37ba14aeca75d4648e8b5f Mon Sep 17 00:00:00 2001 From: Varchar38 Date: Tue, 28 Jul 2015 17:09:09 +0200 Subject: [PATCH] Add tests on job and schedule --- src/job.spec.js | 18 +++-- src/schedule.spec.js | 178 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+), 7 deletions(-) create mode 100644 src/schedule.spec.js diff --git a/src/job.spec.js b/src/job.spec.js index b07c7ec01..605b52908 100644 --- a/src/job.spec.js +++ b/src/job.spec.js @@ -6,7 +6,7 @@ import expect from 'must' // =================================================================== import {getConfig, getMainConnection, getVmXoTestPvId} from './util' -import {map, find} from 'lodash' +import {map} from 'lodash' import eventToPromise from 'event-to-promise' // =================================================================== @@ -81,9 +81,7 @@ describe('job', function () { } async function getJob (id) { - // const job = await xo.call('job.get', {id: id}) - const jobs = await xo.call('job.getAll') - const job = find(jobs, {id: id}) + const job = await xo.call('job.get', {id: id}) return job } @@ -153,7 +151,7 @@ describe('job', function () { beforeEach(async function () { jobId = createJobTest() }) - it.only('modifies an existing job', async function () { + it.skip('modifies an existing job', async function () { await xo.call('job.set', { job: { id: jobId, @@ -190,8 +188,14 @@ describe('job', function () { }) it('delete an existing job', async function () { await xo.call('job.delete', {id: jobId}) - const job = await getJob(jobId) - expect(job).to.be.undefined() + await getJob(jobId).then( + function () { + throw new Error('schedule.delete() should have thrown') + }, + function (error) { + expect(error.message).to.match(/no such object/) + } + ) jobIds = [] }) }) diff --git a/src/schedule.spec.js b/src/schedule.spec.js new file mode 100644 index 000000000..832154c60 --- /dev/null +++ b/src/schedule.spec.js @@ -0,0 +1,178 @@ +/* eslint-env mocha */ + +// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must +import expect from 'must' + +// =================================================================== + +import {getMainConnection, getConfig, getVmXoTestPvId} from './util' +import eventToPromise from 'event-to-promise' +import {map} from 'lodash' + +// =================================================================== + +describe('schedule', function () { + + let xo + let serverId + let scheduleIds = [] + let jobId + + before(async function () { + this.timeout(10e3) + let config + ;[xo, config] = await Promise.all([ + getMainConnection(), + getConfig() + ]) + + serverId = await xo.call('server.add', config.xenServer1).catch(() => {}) + await eventToPromise(xo.objects, 'finish') + + jobId = await createJob() + }) + + // ----------------------------------------------------------------- + + after(async function () { + await Promise.all([ + xo.call('job.delete', {id: jobId}), + xo.call('server.remove', {id: serverId}) + ]) + }) + + // ----------------------------------------------------------------- + + afterEach(async function () { + await Promise.all(map( + scheduleIds, + scheduleId => xo.call('schedule.delete', {id: scheduleId}) + )) + scheduleIds = [] + }) + + // ----------------------------------------------------------------- + + async function createJob () { + const vmId = await getVmXoTestPvId(xo) + const id = await xo.call('job.create', { + job: { + type: 'call', + key: 'snapshot', + method: 'vm.snapshot', + paramsVector: { + type: 'cross product', + items: [ + { + type: 'set', + values: [{ + id: vmId, + name: 'snapshot' + }] + } + ] + } + } + }) + return id + } + + async function createScedule (params) { + const schedule = await xo.call('schedule.create', params) + scheduleIds.push(schedule.id) + return schedule + } + + async function createSceduleTest () { + const schedule = await createScedule({ + jobId: jobId, + cron: '******', + enabled: false + }) + return schedule + } + + async function getSchedule (id) { + const schedule = xo.call('schedule.get', {id: id}) + return schedule + } + + // ================================================================= + + describe('.getAll()', function () { + it('gets all existing schedules', async function () { + const schedules = await xo.call('schedule.getAll') + expect(schedules).to.be.an.array() + }) + }) + + // ----------------------------------------------------------------- + + describe('.get()', function () { + let scheduleId + before(async function () { + scheduleId = (await createSceduleTest()).id + }) + + it('gets an existing schedule', async function () { + const schedule = await xo.call('schedule.get', {id: scheduleId}) + expect(schedule.job).to.be.equal(jobId) + expect(schedule.cron).to.be.equal('******') + expect(schedule.enabled).to.be.false() + }) + }) + + // ----------------------------------------------------------------- + + describe('.create()', function () { + it.only('creates a new schedule', async function () { + const schedule = await createScedule({ + jobId: jobId, + cron: '******', + enabled: false + }) + expect(schedule.job).to.be.equal(jobId) + expect(schedule.cron).to.be.equal('******') + expect(schedule.enabled).to.be.false() + }) + }) + + // ----------------------------------------------------------------- + + describe('.set()', function () { + let scheduleId + before(async function () { + scheduleId = (await createSceduleTest()).id + }) + it('modifies an existing schedule', async function () { + await xo.call('schedule.set', { + id: scheduleId, + cron: '2*****' + }) + + const schedule = await getSchedule(scheduleId) + expect(schedule.cron).to.be.equal('2*****') + }) + }) + + // ----------------------------------------------------------------- + + describe('.delete()', function () { + let scheduleId + beforeEach(async function () { + scheduleId = (await createSceduleTest()).id + }) + it('deletes an existing schedule', async function () { + await xo.call('schedule.delete', {id: scheduleId}) + await getSchedule(scheduleId).then( + function () { + throw new Error('schedule.delete() should have thrown') + }, + function (error) { + expect(error.message).to.match(/no such object/) + } + ) + scheduleIds = [] + }) + }) +})