Skip to content

Commit

Permalink
Implement esTestCluster test util (#13099)
Browse files Browse the repository at this point in the history
* [es/tests] remove unused module

* [testUtil/es] add utility for starting es nodes in tests

* [ftr/tests] use esTestCluster util to start es

* [es/tests/routes] use esTestCluster to start own es

* [testUtils/kbnServer] disable uiSettings unless plugins are enabled

* [testUtils/esTestCluster] use standard test es port by default

* [server/http/tests] add esTestCluster to setup

* [test/config] unify es test config into a single module

* [testUtils/esTestCluster] directory is no longer configurable

* [testUtils/esTestCluster] throw when es.start() is called again without es.stop()

* [testUtils/esTestCluster] is* checks should not mutate state

(cherry picked from commit 6748b22)
  • Loading branch information
spalger committed Jul 28, 2017
1 parent 270d544 commit 3e74f40
Show file tree
Hide file tree
Showing 21 changed files with 235 additions and 165 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@
"ncp": "2.0.0",
"nock": "8.0.0",
"node-sass": "3.8.0",
"portscanner": "1.0.0",
"proxyquire": "1.7.10",
"sass-loader": "4.0.0",
"simple-git": "1.37.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import _ from 'lodash';
import Promise from 'bluebird';
import sinon from 'sinon';
import expect from 'expect.js';
import url from 'url';

import { esTestServerUrlParts } from '../../../../../test/es_test_server_url_parts';
import { esTestConfig } from '../../../../test_utils/es';
import { ensureEsVersion } from '../ensure_es_version';

describe('plugins/elasticsearch', () => {
Expand All @@ -22,7 +21,7 @@ describe('plugins/elasticsearch', () => {
status: {
red: sinon.stub()
},
url: url.format(esTestServerUrlParts)
url: esTestConfig.getUrl()
}
}
};
Expand Down
13 changes: 0 additions & 13 deletions src/core_plugins/elasticsearch/lib/__tests__/find_port.js

This file was deleted.

7 changes: 3 additions & 4 deletions src/core_plugins/elasticsearch/lib/__tests__/health_check.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import Promise from 'bluebird';
import sinon from 'sinon';
import expect from 'expect.js';
import url from 'url';

const NoConnections = require('elasticsearch').errors.NoConnections;

import mappings from './fixtures/mappings';
import healthCheck from '../health_check';
import kibanaVersion from '../kibana_version';
import { esTestServerUrlParts } from '../../../../../test/es_test_server_url_parts';
import { esTestConfig } from '../../../../test_utils/es';
import * as patchKibanaIndexNS from '../patch_kibana_index';
import * as migrateConfigNS from '../migrate_config';

const esPort = esTestServerUrlParts.port;
const esUrl = url.format(esTestServerUrlParts);
const esPort = esTestConfig.getPort();
const esUrl = esTestConfig.getUrl();

describe('plugins/elasticsearch', () => {
describe('lib/health_check', function () {
Expand Down
16 changes: 9 additions & 7 deletions src/core_plugins/elasticsearch/lib/__tests__/routes.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
import { format } from 'util';

import * as kbnTestServer from '../../../../test_utils/kbn_server';
import { esTestCluster } from '../../../../test_utils/es';

describe('plugins/elasticsearch', function () {
describe('routes', function () {
let kbnServer;
const es = esTestCluster.use({
name: 'core_plugins/es/routes',
});

before(async function () {
// Sometimes waiting for server takes longer than 10s.
// NOTE: This can't be a fat-arrow function because `this` needs to refer to the execution
// context, not to the parent context.
this.timeout(60000);
this.timeout(es.getStartTimeout());
await es.start();

kbnServer = kbnTestServer.createServerWithCorePlugins();

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

after(function () {
return kbnServer.close();
after(async function () {
await kbnServer.close();
await es.stop();
});

function testRoute(options, statusCode = 200) {
Expand Down
14 changes: 8 additions & 6 deletions src/core_plugins/tests_bundle/tests_entry_template.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { esTestConfig } from '../../test_utils/es';

export default function ({ env, bundle }) {

const pluginSlug = env.pluginInfo.sort()
Expand Down Expand Up @@ -26,22 +28,22 @@ window.__KBN__ = {
vars: {
kbnIndex: '.kibana',
esShardTimeout: 1500,
esApiVersion: 'master',
esApiVersion: ${JSON.stringify(esTestConfig.getBranch())},
esRequestTimeout: '300000',
tilemapsConfig: {
deprecated: {
isOverridden: false,
config: {
options: {
config: {
options: {
}
}
}
}
}
},
regionmapsConfig: {
layers: []
},
mapConfig: {
manifestServiceUrl: 'https://geo.elastic.co/v1/manifest'
manifestServiceUrl: 'https://geo.elastic.co/v1/manifest'
}
},
uiSettings: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ import { format as formatUrl } from 'url';

import { readConfigFile } from '../../lib';
import { createToolingLog, createReduceStream } from '../../../utils';
import { startupEs, startupKibana } from '../lib';
import { esTestCluster } from '../../../test_utils/es';
import { startupKibana } from '../lib';

const SCRIPT = resolve(__dirname, '../../../../scripts/functional_test_runner.js');
const CONFIG = resolve(__dirname, '../fixtures/with_es_archiver/config.js');

describe('single test that uses esArchiver', function () {
this.timeout(3 * 60 * 1000);

describe('single test that uses esArchiver', () => {
let log;
const cleanupWork = [];

before(async () => {
before(async function () {
log = createToolingLog('debug');
log.pipe(process.stdout);
log.indent(6);
Expand All @@ -24,11 +23,17 @@ describe('single test that uses esArchiver', function () {

log.info('starting elasticsearch');
log.indent(2);
const es = await startupEs({
log,
port: config.get('servers.elasticsearch.port'),
fresh: false

const es = esTestCluster.use({
log: msg => log.debug(msg),
name: 'ftr/withEsArchiver',
port: config.get('servers.elasticsearch.port')
});
cleanupWork.unshift(() => es.stop());

this.timeout(es.getStartTimeout());
await es.start();

log.indent(-2);

log.info('starting kibana');
Expand All @@ -39,8 +44,7 @@ describe('single test that uses esArchiver', function () {
});
log.indent(-2);

cleanupWork.push(() => kibana.close());
cleanupWork.push(() => es.shutdown());
cleanupWork.unshift(() => kibana.close());
});

it('test', async () => {
Expand Down
43 changes: 0 additions & 43 deletions src/functional_test_runner/__tests__/lib/es.js

This file was deleted.

1 change: 0 additions & 1 deletion src/functional_test_runner/__tests__/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { startupEs } from './es';
export { startupKibana } from './kibana';
23 changes: 15 additions & 8 deletions src/server/http/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import expect from 'expect.js';
import * as kbnTestServer from '../../../test_utils/kbn_server';
import { esTestCluster } from '../../../test_utils/es';

describe('routes', function () {
this.slow(10000);
this.timeout(60000);

describe('routes', () => {
let kbnServer;
beforeEach(function () {
const es = esTestCluster.use({
name: 'server/http',
});

before(async function () {
this.timeout(es.getStartTimeout());
await es.start();
kbnServer = kbnTestServer.createServerWithCorePlugins();
return kbnServer.ready();
await kbnServer.ready();
await kbnServer.server.plugins.elasticsearch.waitUntilReady();
});
afterEach(function () {
return kbnServer.close();

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

describe('cookie validation', function () {
Expand Down
103 changes: 103 additions & 0 deletions src/test_utils/es/es_test_cluster.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { resolve } from 'path';
import { esTestConfig } from './es_test_config';

import libesvm from 'libesvm';

const ESVM_DIR = resolve(__dirname, '../../../esvm/test_utils/es_test_cluster');

export class EsTestCluster {
_branchesDownloaded = [];

use(options = {}) {
const {
name,
log = console.log,
port = esTestConfig.getPort(),
branch = esTestConfig.getBranch(),
} = options;

if (!name) {
throw new Error('esTestCluster.use() requires { name }');
}

// assigned in use.start(), reassigned in use.stop()
let cluster;

return {
getStartTimeout: () => {
return esTestConfig.getLibesvmStartTimeout();
},

start: async () => {
const download = this._isDownloadNeeded(branch);

if (cluster) {
throw new Error(`
EsTestCluster[${name}] is already started, call and await es.stop()
before calling es.start() again.
`);
}

cluster = libesvm.createCluster({
fresh: download,
purge: !download,
directory: ESVM_DIR,
branch,
config: {
http: {
port,
},
cluster: {
name,
},
discovery: {
zen: {
ping: {
unicast: {
hosts: [ `localhost:${port}` ]
}
}
}
}
}
});

cluster.on('log', (event) => {
log(`EsTestCluster[${name}]: ${event.type} - ${event.message}`);
});

await cluster.install();

if (download) {
// track the branches that have successfully downloaded
// after cluster.install() resolves
this._branchesDownloaded.push(branch);
}

await cluster.start();
},

stop: async () => {
if (cluster) {
const c = cluster;
cluster = null;
await c.shutdown();
}
}
};
}

_isDownloadNeeded(branch) {
if (process.env.ESVM_NO_FRESH || process.argv.includes('--esvm-no-fresh')) {
return false;
}

if (this._branchesDownloaded.includes(branch)) {
return false;
}

return true;
}
}

export const esTestCluster = new EsTestCluster();
Loading

0 comments on commit 3e74f40

Please sign in to comment.