Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate Kibana and Elasticsearch launch logic in tests #22328

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 10 additions & 10 deletions src/core_plugins/kibana/server/lib/__tests__/manage_uuid.js
Expand Up @@ -19,32 +19,32 @@

import expect from 'expect.js';
import sinon from 'sinon';
import * as kbnTestServer from '../../../../../test_utils/kbn_server.js';
import { startTestServers } from '../../../../../test_utils/kbn_server.js';
import manageUuid from '../manage_uuid';

describe('core_plugins/kibana/server/lib', function () {
describe('manage_uuid', function () {
const testUuid = 'c4add484-0cba-4e05-86fe-4baa112d9e53';
let kbnServer;
let config;
let servers;

before(async function () {
this.timeout(60000); // sometimes waiting for server takes longer than 10

kbnServer = kbnTestServer.createServerWithCorePlugins();

await kbnServer.ready();
servers = await startTestServers({
adjustTimeout: (t) => {
this.timeout(t);
},
});
kbnServer = servers.kbnServer;
});

// clear uuid stuff from previous test runs
// Clear uuid stuff from previous test runs
beforeEach(function () {
kbnServer.server.log = sinon.stub();
config = kbnServer.server.config();
});

after(async function () {
await kbnServer.close();
});
after(() => servers.stop());

it('ensure config uuid is validated as a guid', async function () {
config.set('server.uuid', testUuid);
Expand Down
53 changes: 51 additions & 2 deletions src/test_utils/kbn_server.js
Expand Up @@ -20,8 +20,9 @@
import { resolve } from 'path';
import { defaultsDeep, set } from 'lodash';
import { header as basicAuthHeader } from './base_auth';
import { esTestConfig, kibanaTestUser, kibanaServerTestUser } from '@kbn/test';
import { createEsTestCluster, esTestConfig, kibanaTestUser, kibanaServerTestUser } from '@kbn/test';
import KbnServer from '../../src/server/kbn_server';
import { ToolingLog } from '@kbn/dev-utils';

const DEFAULTS_SETTINGS = {
server: {
Expand Down Expand Up @@ -66,11 +67,59 @@ export function createServer(settings = {}) {
return new KbnServer(defaultsDeep({}, settings, DEFAULTS_SETTINGS));
}

/**
* Creates an instance of KbnServer, including all of the core plugins,
* with default configuration tailored for unit tests, and starts es.
*
* @param {Object} options
* @prop {Object} settings Any config overrides for this instance
* @prop {function} adjustTimeout A function(t) => this.timeout(t) that adjust the timeout of a test,
* ensuring the test properly waits for the server to boot without timing out.
* @return {KbnServer}
*/
export async function startTestServers({ adjustTimeout, settings = {} }) {
if (!adjustTimeout) {
throw new Error('adjustTimeout is required in order to avoid flaky tests');
}

const log = new ToolingLog({
level: 'debug',
writeTo: process.stdout
});

log.indent(6);
log.info('starting elasticsearch');
log.indent(4);

const es = createEsTestCluster({ log });

log.indent(-4);

adjustTimeout(es.getStartTimeout());

await es.start();

const kbnServer = createServerWithCorePlugins(settings);

await kbnServer.ready();
await kbnServer.server.plugins.elasticsearch.waitUntilReady();

return {
kbnServer,
es,

async stop() {
await this.kbnServer.close();
await es.cleanup();
},
};
}

/**
* Creates an instance of KbnServer, including all of the core plugins,
* with default configuration tailored for unit tests
*
* @param {Object} [settings={}]
* @param {Object} [settings={}] Any config overrides for this instance
* @return {KbnServer}
*/
export function createServerWithCorePlugins(settings = {}) {
Expand Down
Expand Up @@ -20,41 +20,19 @@
import sinon from 'sinon';
import expect from 'expect.js';

import { createEsTestCluster } from '@kbn/test';
import { createServerWithCorePlugins } from '../../../../test_utils/kbn_server';
import { ToolingLog } from '@kbn/dev-utils';
import { startTestServers } from '../../../../test_utils/kbn_server';
import { createOrUpgradeSavedConfig } from '../create_or_upgrade_saved_config';

describe('createOrUpgradeSavedConfig()', () => {
let savedObjectsClient;
let kbnServer;
const cleanup = [];
let servers;

before(async function () {
const log = new ToolingLog({
level: 'debug',
writeTo: process.stdout
});
log.indent(6);

log.info('starting elasticsearch');
log.indent(4);

const es = createEsTestCluster({ log });
this.timeout(es.getStartTimeout());

log.indent(-4);
cleanup.push(async () => await es.cleanup());

await es.start();

kbnServer = createServerWithCorePlugins();
await kbnServer.ready();
cleanup.push(async () => {
await kbnServer.close();
kbnServer = null;
savedObjectsClient = null;
servers = await startTestServers({
adjustTimeout: (t) => this.timeout(t),
});
kbnServer = servers.kbnServer;

await kbnServer.server.plugins.elasticsearch.waitUntilReady();

Expand Down Expand Up @@ -89,10 +67,7 @@ describe('createOrUpgradeSavedConfig()', () => {
]);
});

after(async () => {
await Promise.all(cleanup.map(fn => fn()));
cleanup.length = 0;
});
after(() => servers.stop());

it('upgrades the previous version on each increment', async function () {
this.timeout(30000);
Expand Down
51 changes: 15 additions & 36 deletions src/ui/ui_settings/routes/__tests__/lib/servers.js
Expand Up @@ -17,47 +17,32 @@
* under the License.
*/

import { createEsTestCluster } from '@kbn/test';
import { ToolingLog } from '@kbn/dev-utils';
import * as kbnTestServer from '../../../../../test_utils/kbn_server';
import { startTestServers } from '../../../../../test_utils/kbn_server';

let kbnServer;
let services;
let es;
let servers;

export async function startServers() {
const log = new ToolingLog({
level: 'debug',
writeTo: process.stdout
});
log.indent(6);

log.info('starting elasticsearch');
log.indent(4);

es = createEsTestCluster({ log });
this.timeout(es.getStartTimeout());

log.indent(-4);
await es.start();

kbnServer = kbnTestServer.createServerWithCorePlugins({
uiSettings: {
overrides: {
foo: 'bar',
}
servers = await startTestServers({
adjustTimeout: (t) => this.timeout(t),
settings: {
uiSettings: {
overrides: {
foo: 'bar',
}
},
}
});
await kbnServer.ready();
await kbnServer.server.plugins.elasticsearch.waitUntilReady();
kbnServer = servers.kbnServer;
}

export function getServices() {
if (services) {
return services;
}

const callCluster = es.getCallCluster();
const callCluster = servers.es.getCallCluster();

const savedObjects = kbnServer.server.savedObjects;
const savedObjectsClient = savedObjects.getScopedSavedObjectsClient();
Expand All @@ -78,14 +63,8 @@ export function getServices() {

export async function stopServers() {
services = null;

if (kbnServer) {
await kbnServer.close();
kbnServer = null;
}

if (es) {
await es.cleanup();
es = null;
kbnServer = null;
if (servers) {
await servers.stop();
}
}