From f3cbc4d2a415b35053920035104d03c1efeaa440 Mon Sep 17 00:00:00 2001 From: Spencer Date: Fri, 28 Jul 2017 15:50:15 -0700 Subject: [PATCH] [testUtils/esTestCluster] use more standard api style (#13197) (cherry picked from commit d36080bca837ba524345d010d978fc2b4580367b) --- .../elasticsearch/lib/__tests__/routes.js | 4 +- .../__tests__/integration/with_es_archiver.js | 4 +- src/server/http/__tests__/index.js | 4 +- src/test_utils/es/es_test_cluster.js | 150 +++++++++--------- src/test_utils/es/index.js | 2 +- 5 files changed, 80 insertions(+), 84 deletions(-) diff --git a/src/core_plugins/elasticsearch/lib/__tests__/routes.js b/src/core_plugins/elasticsearch/lib/__tests__/routes.js index 73b60561eae01..25ce5a2d8000c 100644 --- a/src/core_plugins/elasticsearch/lib/__tests__/routes.js +++ b/src/core_plugins/elasticsearch/lib/__tests__/routes.js @@ -1,12 +1,12 @@ import { format } from 'util'; import * as kbnTestServer from '../../../../test_utils/kbn_server'; -import { esTestCluster } from '../../../../test_utils/es'; +import { createEsTestCluster } from '../../../../test_utils/es'; describe('plugins/elasticsearch', function () { describe('routes', function () { let kbnServer; - const es = esTestCluster.use({ + const es = createEsTestCluster({ name: 'core_plugins/es/routes', }); diff --git a/src/functional_test_runner/__tests__/integration/with_es_archiver.js b/src/functional_test_runner/__tests__/integration/with_es_archiver.js index 5bfd1a9383832..48a1d168d0f00 100644 --- a/src/functional_test_runner/__tests__/integration/with_es_archiver.js +++ b/src/functional_test_runner/__tests__/integration/with_es_archiver.js @@ -4,7 +4,7 @@ import { format as formatUrl } from 'url'; import { readConfigFile } from '../../lib'; import { createToolingLog, createReduceStream } from '../../../utils'; -import { esTestCluster } from '../../../test_utils/es'; +import { createEsTestCluster } from '../../../test_utils/es'; import { startupKibana } from '../lib'; const SCRIPT = resolve(__dirname, '../../../../scripts/functional_test_runner.js'); @@ -24,7 +24,7 @@ describe('single test that uses esArchiver', () => { log.info('starting elasticsearch'); log.indent(2); - const es = esTestCluster.use({ + const es = createEsTestCluster({ log: msg => log.debug(msg), name: 'ftr/withEsArchiver', port: config.get('servers.elasticsearch.port') diff --git a/src/server/http/__tests__/index.js b/src/server/http/__tests__/index.js index e51d2e8634194..f6909114698e4 100644 --- a/src/server/http/__tests__/index.js +++ b/src/server/http/__tests__/index.js @@ -1,10 +1,10 @@ import expect from 'expect.js'; import * as kbnTestServer from '../../../test_utils/kbn_server'; -import { esTestCluster } from '../../../test_utils/es'; +import { createEsTestCluster } from '../../../test_utils/es'; describe('routes', () => { let kbnServer; - const es = esTestCluster.use({ + const es = createEsTestCluster({ name: 'server/http', }); diff --git a/src/test_utils/es/es_test_cluster.js b/src/test_utils/es/es_test_cluster.js index 9e96554a12e83..97ec418e1d831 100644 --- a/src/test_utils/es/es_test_cluster.js +++ b/src/test_utils/es/es_test_cluster.js @@ -1,103 +1,99 @@ import { resolve } from 'path'; -import { esTestConfig } from './es_test_config'; import libesvm from 'libesvm'; +import { esTestConfig } from './es_test_config'; + const ESVM_DIR = resolve(__dirname, '../../../esvm/test_utils/es_test_cluster'); +const BRANCHES_DOWNLOADED = []; -export class EsTestCluster { - _branchesDownloaded = []; +function isDownloadNeeded(branch) { + if (process.env.ESVM_NO_FRESH || process.argv.includes('--esvm-no-fresh')) { + return false; + } - use(options = {}) { - const { - name, - log = console.log, - port = esTestConfig.getPort(), - branch = esTestConfig.getBranch(), - } = options; + if (BRANCHES_DOWNLOADED.includes(branch)) { + return false; + } - if (!name) { - throw new Error('esTestCluster.use() requires { name }'); - } + return true; +} - // assigned in use.start(), reassigned in use.stop() - let cluster; +export function createEsTestCluster(options = {}) { + const { + name, + log = console.log, + port = esTestConfig.getPort(), + branch = esTestConfig.getBranch(), + } = options; - return { - getStartTimeout: () => { - return esTestConfig.getLibesvmStartTimeout(); - }, + if (!name) { + throw new Error('createEsTestCluster() requires { name }'); + } - start: async () => { - const download = this._isDownloadNeeded(branch); + // assigned in use.start(), reassigned in use.stop() + let cluster; - if (cluster) { - throw new Error(` - EsTestCluster[${name}] is already started, call and await es.stop() - before calling es.start() again. - `); - } + return new class EsTestCluster { + getStartTimeout() { + return esTestConfig.getLibesvmStartTimeout(); + } + + async start() { + const download = isDownloadNeeded(branch); - cluster = libesvm.createCluster({ - fresh: download, - purge: !download, - directory: ESVM_DIR, - branch, - config: { - http: { - port, - }, - cluster: { - name, - }, - discovery: { - zen: { - ping: { - unicast: { - hosts: [ `localhost:${port}` ] - } + 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(); - }, + cluster.on('log', (event) => { + log(`EsTestCluster[${name}]: ${event.type} - ${event.message}`); + }); - stop: async () => { - if (cluster) { - const c = cluster; - cluster = null; - await c.shutdown(); - } + await cluster.install(); + + if (download) { + // track the branches that have successfully downloaded + // after cluster.install() resolves + BRANCHES_DOWNLOADED.push(branch); } - }; - } - _isDownloadNeeded(branch) { - if (process.env.ESVM_NO_FRESH || process.argv.includes('--esvm-no-fresh')) { - return false; + await cluster.start(); } - if (this._branchesDownloaded.includes(branch)) { - return false; + async stop() { + if (cluster) { + const c = cluster; + cluster = null; + await c.shutdown(); + } } - - return true; - } + }; } - -export const esTestCluster = new EsTestCluster(); diff --git a/src/test_utils/es/index.js b/src/test_utils/es/index.js index 7230b751960c6..0409e204608eb 100644 --- a/src/test_utils/es/index.js +++ b/src/test_utils/es/index.js @@ -1,2 +1,2 @@ -export { esTestCluster } from './es_test_cluster'; export { esTestConfig } from './es_test_config'; +export { createEsTestCluster } from './es_test_cluster';