Skip to content

Commit

Permalink
chore(xo-server-test): cleanup and rewrite user tests (#8048)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-m-dev authored Oct 22, 2024
1 parent a3cb9d5 commit a71e78f
Show file tree
Hide file tree
Showing 36 changed files with 471 additions and 331 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.unreleased.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- xo-server minor
- xo-server-audit patch
- xo-server-netbox patch
- xo-server-test patch
- xo-web minor

<!--packages-end-->
10 changes: 0 additions & 10 deletions packages/xo-server-test/.babelrc.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/xo-server-test/.eslintrc.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import appConf from 'app-conf'
import path from 'path'

/* eslint-env jest */
import { before } from 'node:test'

let config
export { config as default }

beforeAll(async () => {
before(async () => {
config = await appConf.load('xo-server-test', {
appDir: path.join(__dirname, '..'),
appDir: path.join(process.cwd(), '..'),
})
})
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
/* eslint-env jest */
import Xo from 'xo-lib'
import { Collection as XoCollection } from 'xo-collection'
import { decorateWith } from '@vates/decorate-with'
import { defaultsDeep, find, forOwn, iteratee, pick } from 'lodash'
import { decorateClass } from '@vates/decorate-with'
import defaultsDeep from 'lodash/defaultsDeep.js'
import find from 'lodash/find.js'
import forOwn from 'lodash/forOwn.js'
import iteratee from 'lodash/iteratee.js'
import pick from 'lodash/pick.js'
import { defer } from 'golike-defer'
import { fromEvent } from 'promise-toolbox'
import { parseDuration } from '@vates/parse-duration'
// import { parseDuration } from '@vates/parse-duration'
// import {before, after, afterEach} from 'node:test'

import config from './_config'
import { getDefaultName } from './_defaultValues'
import config from './_config.mjs'
import { getDefaultName } from './_defaultValues.mjs'

class XoConnection extends Xo {
export class XoConnection extends Xo.default {
constructor(opts) {
super(opts)

Expand Down Expand Up @@ -69,7 +74,6 @@ class XoConnection extends Xo {
return this.waitObject(id)
}

@decorateWith(defer)
async connect($defer, credentials = pick(config.xoConnection, 'email', 'password')) {
await this.open()
$defer.onFailure(() => this.close())
Expand Down Expand Up @@ -108,6 +112,7 @@ class XoConnection extends Xo {
}

async createTempResourceSet(params) {
const xo = this // getConnection();
const { id } = await xo.call('resourceSet.create', params)
this._tempResourceDisposers.push('resourceSet.delete', { id })
return id
Expand Down Expand Up @@ -221,6 +226,7 @@ class XoConnection extends Xo {
}

async runBackupJob(jobId, scheduleId, { remotes, nExecutions = 1 }) {
const xo = this // getConnection();
for (let i = 0; i < nExecutions; i++) {
await xo.call('backupNg.runJob', { id: jobId, schedule: scheduleId })
}
Expand Down Expand Up @@ -287,23 +293,27 @@ const getConnection = credentials => {
return xo.connect(credentials)
}

let xo
beforeAll(async () => {
/*
before(async () => {
console.log('before xoconnection')
xo = new XoConnection({ url: config.xoConnection.url })
console.log('set')
// TOFIX: stop tests if the connection is not established properly and show the error
xo = await getConnection()
await getConnection()
console.log('connected')
})
afterAll(async () => {
after(async () => {
console.log('after xoconnection')
await xo.deleteDurableResources()
await xo.close()
xo = null
})
afterEach(async () => {
console.log('afterEach xoconnection')
jest.setTimeout(parseDuration(config.deleteTempResourcesTimeout))
await xo.deleteTempResources()
})

export { xo as default }
*/

export const testConnection = ({ credentials }) => getConnection(credentials).then(connection => connection.close())

Expand All @@ -312,3 +322,7 @@ export const testWithOtherConnection = defer(async ($defer, credentials, functio
$defer(() => xoUser.close())
await functionToExecute(xoUser)
})

decorateClass(XoConnection, {
connect: defer,
})
20 changes: 20 additions & 0 deletions packages/xo-server-test/connect.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Xo from 'xo-lib'
const XoConnection = Xo.default

const SERVER_URL = process.env.SERVER_URL || 'http://127.0.0.1:80'

/**
* @param params Contains {url, email, password} as the optionnal server url, and the email/password of the user
* @return Xo A Xo object connection
**/
export async function connect({ url = SERVER_URL, email, password }) {
const xo = new XoConnection({ url })
await xo.open()
try {
await xo.signIn({ email, password })
} catch (err) {
xo.close()
throw err
}
return xo
}
2 changes: 2 additions & 0 deletions packages/xo-server-test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @TODO
// Index.js will be the main entry point as defined in package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
/* eslint-env jest */
// 'use strict'

/*
import forOwn from 'lodash/forOwn.js'
import { noSuchObject } from 'xo-common/api-errors'
import { noSuchObject } from 'xo-common/api-errors.js'
import config from '../_config'
import randomId from '../_randomId'
import xo from '../_xoConnection'
import { getDefaultName, getDefaultSchedule } from '../_defaultValues'
import config from '../_config.js'
import randomId from '../_randomId.js'
import xo from '../_xoConnection.js'
import { getDefaultName, getDefaultSchedule } from '../_defaultValues.js'
import assert from 'node:assert'
const validateBackupJob = (jobInput, jobOutput, createdSchedule) => {
const expectedObj = {
Expand All @@ -24,7 +27,7 @@ const validateBackupJob = (jobInput, jobOutput, createdSchedule) => {
const schedules = jobInput.schedules
if (schedules !== undefined) {
const scheduleTmpId = Object.keys(schedules)[0]
expect(createdSchedule).toEqual({
assert.deepEqual(createdSchedule, {
...schedules[scheduleTmpId],
enabled: false,
id: expect.any(String),
Expand Down Expand Up @@ -567,3 +570,4 @@ describe('backupNg', () => {
})
}, 200e3)
})
*/
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* eslint-env jest */

// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
// eslint-disable-next-line n/no-missing-import
import expect from 'must'

// ===================================================================

import fromEvent from 'promise-toolbox/fromEvent'
import { getConfig, getMainConnection, getSrId, waitObjectState } from './util'
import { getConfig, getMainConnection, getSrId, waitObjectState } from '../util.mjs'
import map from 'lodash/map.js'

// ===================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { find, map } from 'lodash'

// eslint-disable-next-line n/no-missing-import
import { createUser, deleteUsers, getUser, xo } from './util.js'

// ===================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must

// eslint-disable-next-line n/no-missing-import
import expect from 'must'
import fromEvent from 'promise-toolbox/fromEvent'
// eslint-disable-next-line n/no-missing-import
import { getAllHosts, getConfig, getMainConnection, getVmToMigrateId, waitObjectState } from './util'
import { find, forEach } from 'lodash'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { parseDuration } from '@vates/parse-duration'

import config from '../_config'
import xo from '../_xoConnection'
import config from '../../_config.mjs'
import xo from '../../_xoConnection.mjs'

describe('issue', () => {
test('4507', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* eslint-env jest */

import { difference, keyBy } from 'lodash'
import config from '../../_config.mjs'
import { describe, it, afterEach } from 'node:test'
import { XoConnection as xo, testWithOtherConnection } from '../../_xoConnection.mjs'

import config from '../_config'
import xo, { testWithOtherConnection } from '../_xoConnection'
const { difference, keyBy } = 'lodash'

const ADMIN_USER = {
email: '[email protected]',
Expand All @@ -14,33 +15,33 @@ const ADMIN_USER = {
describe('job', () => {
let defaultJob

beforeAll(() => {
defaultJob = {
name: 'jobTest',
timeout: 2000,
type: 'call',
key: 'snapshot',
method: 'vm.snapshot',
paramsVector: {
type: 'crossProduct',
items: [
{
type: 'set',
values: [
{
id: config.vms.default,
name: 'test-snapshot',
},
],
},
],
},
}
})
// beforeAll(() => {
// defaultJob = {
// name: 'jobTest',
// timeout: 2000,
// type: 'call',
// key: 'snapshot',
// method: 'vm.snapshot',
// paramsVector: {
// type: 'crossProduct',
// items: [
// {
// type: 'set',
// values: [
// {
// id: config.vms.default,
// name: 'test-snapshot',
// },
// ],
// },
// ],
// },
// }
// })

describe('.create() :', () => {
it('creates a new job', async () => {
jest.setTimeout(6e3)
// jest.setTimeout(6e3)
const userId = await xo.createTempUser(ADMIN_USER)
const { email, password } = ADMIN_USER
await testWithOtherConnection({ email, password }, async xo => {
Expand Down Expand Up @@ -201,7 +202,7 @@ describe('job', () => {
})

it('runs a job', async () => {
jest.setTimeout(7e4)
// jest.setTimeout(7e4)
await xo.createTempServer(config.servers.default)
const jobId = await xo.createTempJob(defaultJob)
const snapshots = xo.objects.all[config.vms.default].snapshots
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-env jest */

// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
// eslint-disable-next-line n/no-missing-import
import expect from 'must'

// ===================================================================

import fromEvent from 'promise-toolbox/fromEvent'
// eslint-disable-next-line n/no-missing-import
import { getConfig, getMainConnection, waitObjectState } from './util'
import find from 'lodash/find.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-env jest */

// eslint-disable-next-line n/no-missing-import
import { xo } from './util'

// ===================================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-env jest */

// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
// eslint-disable-next-line n/no-missing-import
import expect from 'must'

// ===================================================================

import fromEvent from 'promise-toolbox/fromEvent'
// eslint-disable-next-line n/no-missing-import
import { getConfig, getMainConnection, getSchedule, jobTest, scheduleTest } from './util'
import map from 'lodash/map.js'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-env jest */

// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
// eslint-disable-next-line n/no-missing-import
import expect from 'must'

// ===================================================================

import fromEvent from 'promise-toolbox/fromEvent'
// eslint-disable-next-line n/no-missing-import
import { jobTest, scheduleTest, getConfig, getMainConnection, getSchedule } from './util'

// ===================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { find, map } from 'lodash'

// eslint-disable-next-line n/no-missing-import
import { config, rejectionOf, xo } from './util'

// ===================================================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { defer } from 'golike-defer'
import map from 'lodash/map.js'

import { getConnection, rejectionOf, testConnection, xo } from './util.js'
import { getConnection, rejectionOf, testConnection, xo } from '../util.mjs'

// ===================================================================

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint-env jest */

// Doc: https://github.com/moll/js-must/blob/master/doc/API.md#must
// eslint-disable-next-line n/no-missing-import
import expect from 'must'

// ===================================================================

import fromEvent from 'promise-toolbox/fromEvent'
// eslint-disable-next-line n/no-missing-import
import { getConfig, getMainConnection, getVmXoTestPvId, getOneHost, waitObjectState } from './util'
import map from 'lodash/map.js'

Expand Down
Loading

0 comments on commit a71e78f

Please sign in to comment.