Add tests and move functions to util

This commit is contained in:
Varchar38
2015-07-29 15:25:33 +02:00
parent 8f73619ba1
commit 3c71a20bb2
5 changed files with 150 additions and 78 deletions

View File

@@ -5,7 +5,7 @@ import expect from 'must'
// ===================================================================
import {getConfig, getMainConnection, getVmXoTestPvId} from './util'
import {JobTest, getConfig, getMainConnection, getVmXoTestPvId} from './util'
import {map} from 'lodash'
import eventToPromise from 'event-to-promise'
@@ -58,26 +58,9 @@ describe('job', function () {
}
async function createJobTest () {
const id = await createJob({
job: {
type: 'call',
key: 'snapshot',
method: 'vm.snapshot',
paramsVector: {
type: 'cross product',
items: [
{
type: 'set',
values: [{
id: vmId,
name: 'snapshot'
}]
}
]
}
}
})
return id
const jobId = await JobTest(xo)
jobIds.push(jobId)
return jobId
}
async function getJob (id) {
@@ -151,7 +134,7 @@ describe('job', function () {
beforeEach(async function () {
jobId = createJobTest()
})
it.skip('modifies an existing job', async function () {
it.only('modifies an existing job', async function () {
await xo.call('job.set', {
job: {
id: jobId,
@@ -190,7 +173,7 @@ describe('job', function () {
await xo.call('job.delete', {id: jobId})
await getJob(jobId).then(
function () {
throw new Error('schedule.delete() should have thrown')
throw new Error('getJob() should have thrown')
},
function (error) {
expect(error.message).to.match(/no such object/)

View File

@@ -52,7 +52,7 @@ describe('pool', function () {
afterEach(async function () {
await xo.call('pool.set', {
id: poolId,
name_label: '',
name_label: config.pool.name_label,
name_description: ''
})
})

View File

@@ -5,7 +5,7 @@ import expect from 'must'
// ===================================================================
import {getMainConnection, getConfig, getVmXoTestPvId} from './util'
import {jobTest, scheduleTest, getMainConnection, getConfig, getSchedule, getVmXoTestPvId, waitObjectState} from './util'
import eventToPromise from 'event-to-promise'
import {map} from 'lodash'
@@ -29,7 +29,7 @@ describe('schedule', function () {
serverId = await xo.call('server.add', config.xenServer1).catch(() => {})
await eventToPromise(xo.objects, 'finish')
jobId = await createJob()
jobId = await jobTest(xo)
})
// -----------------------------------------------------------------
@@ -53,47 +53,15 @@ describe('schedule', function () {
// -----------------------------------------------------------------
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) {
async function createSchedule (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})
async function createScheduleTest () {
const schedule = await scheduleTest(xo, jobId)
scheduleIds.push(schedule.id)
return schedule
}
@@ -111,13 +79,13 @@ describe('schedule', function () {
describe('.get()', function () {
let scheduleId
before(async function () {
scheduleId = (await createSceduleTest()).id
scheduleId = (await createScheduleTest()).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.cron).to.be.equal('* * * * * *')
expect(schedule.enabled).to.be.false()
})
})
@@ -125,15 +93,15 @@ describe('schedule', function () {
// -----------------------------------------------------------------
describe('.create()', function () {
it.only('creates a new schedule', async function () {
const schedule = await createScedule({
it('creates a new schedule', async function () {
const schedule = await createSchedule({
jobId: jobId,
cron: '******',
enabled: false
cron: '* * * * * *',
enabled: true
})
expect(schedule.job).to.be.equal(jobId)
expect(schedule.cron).to.be.equal('******')
expect(schedule.enabled).to.be.false()
expect(schedule.cron).to.be.equal('* * * * * *')
expect(schedule.enabled).to.be.true()
})
})
@@ -142,16 +110,16 @@ describe('schedule', function () {
describe('.set()', function () {
let scheduleId
before(async function () {
scheduleId = (await createSceduleTest()).id
scheduleId = (await createScheduleTest()).id
})
it('modifies an existing schedule', async function () {
await xo.call('schedule.set', {
id: scheduleId,
cron: '2*****'
cron: '2 * * * * *'
})
const schedule = await getSchedule(scheduleId)
expect(schedule.cron).to.be.equal('2*****')
const schedule = await getSchedule(xo, scheduleId)
expect(schedule.cron).to.be.equal('2 * * * * *')
})
})
@@ -160,13 +128,13 @@ describe('schedule', function () {
describe('.delete()', function () {
let scheduleId
beforeEach(async function () {
scheduleId = (await createSceduleTest()).id
scheduleId = (await createScheduleTest()).id
})
it('deletes an existing schedule', async function () {
await xo.call('schedule.delete', {id: scheduleId})
await getSchedule(scheduleId).then(
await getSchedule(xo, scheduleId).then(
function () {
throw new Error('schedule.delete() should have thrown')
throw new Error('getSchedule() should have thrown')
},
function (error) {
expect(error.message).to.match(/no such object/)

81
src/scheduler.spec.js Normal file
View File

@@ -0,0 +1,81 @@
/* eslint-env mocha */
// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
import expect from 'must'
// ===================================================================
import {jobTest, scheduleTest, getConfig, getMainConnection, getSchedule} from './util'
import eventToPromise from 'event-to-promise'
// ===================================================================
describe('scheduler', function () {
let xo
let serverId
let jobId
let scheduleId
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 jobTest(xo)
scheduleId = (await scheduleTest(xo, jobId)).id
})
// -----------------------------------------------------------------
after(async function () {
await Promise.all([
xo.call('schedule.delete', {id: scheduleId}),
xo.call('job.delete', {id: jobId}),
xo.call('server.remove', {id: serverId})
])
})
// =================================================================
describe('.enable()', function () {
afterEach(async function () {
await xo.call('scheduler.disable', {id: scheduleId})
})
it.skip('enables a schedule to run it\'s job as scheduled', async function () {
await xo.call('scheduler.enable', {id: scheduleId})
const schedule = await getSchedule(xo, scheduleId)
expect(schedule.enabled).to.be.true()
})
})
// -----------------------------------------------------------------
describe('.disable()', function () {
beforeEach(async function () {
await xo.call('schedule.enable', {id: scheduleId})
})
it.skip('disables a schedule', async function () {
await xo.call('schedule.disable', {id: scheduleId})
const schedule = await getSchedule(xo, scheduleId)
expect(schedule.enabled).to.be.false()
})
})
// -----------------------------------------------------------------
describe('.getScheduleTable()', function () {
it('get a map of existing schedules', async function () {
const table = await xo.call('scheduler.getScheduleTable')
console.log(table)
expect(table).to.be.an.object()
expect(table).to.match(scheduleId)
})
})
})

View File

@@ -32,7 +32,7 @@ export async function getConfig () {
name_label: 'Windows7Ultimate.iso'
},
pool: {
name_label: ''
name_label: 'lab3'
}
}
}
@@ -163,6 +163,46 @@ export async function getVmXoTestPvId (xo) {
// ==================================================================
export async function jobTest (xo) {
const vmId = await getVmXoTestPvId(xo)
const jobId = 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 jobId
}
export async function scheduleTest (xo, jobId) {
const schedule = await xo.call('schedule.create', {
jobId: jobId,
cron: '* * * * * *',
enabled: false
})
return schedule
}
export async function getSchedule (xo, id) {
const schedule = xo.call('schedule.get', {id: id})
return schedule
}
// ==================================================================
export function deepDelete (obj, path) {
const lastIndex = path.length - 1
for (let i = 0; i < lastIndex; i++) {