Add getConfig

This commit is contained in:
Varchar38
2015-06-24 17:26:44 +02:00
parent 766cdc9f59
commit d48ffdb14f
3 changed files with 85 additions and 54 deletions

View File

@@ -4,7 +4,7 @@
import expect from 'must'
import eventToPromise from 'event-to-promise'
import {getConnection} from './util'
import {getConnection, getConfig, getOneHost} from './util'
import {forEach} from 'lodash'
// ===================================================================
@@ -17,11 +17,8 @@ describe('host', function () {
xo = await getConnection()
serverId = await xo.call('server.add', {
host: '192.168.1.3',
username: 'root',
password: 'qwerty'
}).catch(() => {})
const config = await getConfig()
serverId = await xo.call('server.add', config.xenServer1).catch(() => {})
await eventToPromise(xo.objects, 'finish')
})
@@ -32,25 +29,12 @@ describe('host', function () {
})
})
function getAllHosts () {
return xo.objects.indexes.type.host
}
function getOneHost () {
const hosts = getAllHosts()
for (const id in hosts) {
return hosts[id]
}
throw new Error('no hosts found')
}
// ===================================================================
describe('.set()', function () {
this.timeout(20e3)
it('changes properties of the host', async function () {
let host = getOneHost()
let host = getOneHost(xo)
await xo.call('host.set', {
id: host.id,
@@ -98,7 +82,7 @@ describe('host', function () {
describe('.disable(), .enable()', function () {
this.timeout(20e3)
it('enable or disable to create VM on the host', async function () {
let host = getOneHost()
let host = getOneHost(xo)
await xo.call('host.disable', {
id: host.id
@@ -134,7 +118,7 @@ describe('host', function () {
describe('.stats()', function () {
it('returns an array with statistics of the host', async function () {
const host = getOneHost()
const host = getOneHost(xo)
const stats = await xo.call('host.stats', {
host: host.id
})

View File

@@ -2,16 +2,19 @@
// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
import expect from 'must'
import {getConnection} from './util'
import {map, find} from 'lodash'
import {getConnection, getConfig} from './util'
import {map, find, assign} from 'lodash'
// ===================================================================
describe('server', function () {
let xo
let serverIds = []
let config
before(async function () {
xo = await getConnection()
config = await getConfig()
})
afterEach(async function () {
@@ -79,12 +82,7 @@ describe('server', function () {
})
})
it('set autoConnect true by default', async function () {
const serverId = await addServer({
host: '192.168.1.3',
username: 'root',
password: 'qwerty'
})
const serverId = await addServer(config.xenServer1)
const server = await getServer(serverId)
expect(server.id).to.be.equal(serverId)
@@ -156,20 +154,15 @@ describe('server', function () {
describe('.connect()', function () {
this.timeout(30e3)
it('connects to a Xen server', async function () {
// 192.168.1.3 root:qwerty
const serverId = await addServer({
host: '192.168.1.3',
username: 'root',
password: 'qwerty',
autoConnect: false
})
const serverId = await addServer(assign(
{autoConnect: false}, config.xenServer1
))
await xo.call('server.connect', {
id: serverId
})
const server = await getServer(serverId)
expect(server).to.be.eql({
id: serverId,
host: '192.168.1.3',
@@ -182,14 +175,11 @@ describe('server', function () {
// -----------------------------------------------------------------
describe('.disconnect()', function () {
this.timeout(30000)
this.timeout(30e3)
it('disconnects to a Xen server', async function () {
const serverId = await addServer({
host: '192.168.1.3',
username: 'root',
password: 'qwerty',
autoConnect: false
})
const serverId = await addServer(assign(
{autoConnect: false}, config.xenServer1
))
await xo.call('server.connect', {
id: serverId

View File

@@ -1,16 +1,32 @@
import {Xo} from 'xo-lib'
import {find, forEach, map} from 'lodash'
import {find, forEach, map, cloneDeep} from 'lodash'
import expect from 'must'
export async function getConfig () {
return {
adminCredentials: {
email: 'admin@admin.net',
password: 'admin'
},
xoServerUrl: 'localhost:9000',
xenServer1: {
host: '192.168.1.3',
username: 'root',
password: 'qwerty'
}
}
}
export async function getConnection ({
credentials = {
// FIXME: sould be username
email: 'admin@admin.net',
password: 'admin'
}
credentials
} = {}) {
const xo = new Xo('localhost:9000')
await xo.signIn(credentials)
const config = await getConfig()
const xo = new Xo(config.xoServerUrl)
await xo.signIn(
credentials === undefined ?
config.adminCredentials :
credentials
)
// Injects waitObject()
//
@@ -46,6 +62,8 @@ export async function getConnection ({
return xo
}
// =================================================================
export async function getAllUsers (xo) {
return await xo.call('user.getAll')
}
@@ -67,3 +85,42 @@ export async function deleteUsers (xo, userIds) {
userId => xo.call('user.delete', {id: userId})
))
}
// ==================================================================
export function getAllHosts (xo) {
return xo.objects.indexes.type.host
}
export function getOneHost (xo) {
const hosts = getAllHosts(xo)
for (const id in hosts) {
return hosts[id]
}
throw new Error('no hosts found')
}
// ==================================================================
export function deepDelete (obj, path) {
const lastIndex = path.length - 1
for (let i = 0; i < lastIndex; i++) {
obj = obj[path[i]]
if (typeof obj !== 'object' || obj === null) {
return
}
}
delete obj[path[lastIndex]]
}
export function almostEqual (actual, expected, ignoredAttributes) {
const actualClone = cloneDeep(actual)
const expectedClone = cloneDeep(expected)
forEach(ignoredAttributes, (ignoredAttribute) => {
deepDelete(actualClone, ignoredAttribute.split('.'))
deepDelete(expectedClone, ignoredAttribute.split('.'))
})
expect(actualClone).to.be.eql(expectedClone)
}