Skip to content

Commit

Permalink
[testUtils/esTestCluster] use more standard api style (#13197)
Browse files Browse the repository at this point in the history
(cherry picked from commit d36080b)
  • Loading branch information
spalger committed Jul 28, 2017
1 parent 7ee7cd7 commit f3cbc4d
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/core_plugins/elasticsearch/lib/__tests__/routes.js
Original file line number Diff line number Diff line change
@@ -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',
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions src/server/http/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -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',
});

Expand Down
150 changes: 73 additions & 77 deletions src/test_utils/es/es_test_cluster.js
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion src/test_utils/es/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { esTestCluster } from './es_test_cluster';
export { esTestConfig } from './es_test_config';
export { createEsTestCluster } from './es_test_cluster';

0 comments on commit f3cbc4d

Please sign in to comment.