Skip to content

Commit

Permalink
[es/healthcheck] determine enabled scripting langs in healthcheck (#1…
Browse files Browse the repository at this point in the history
…2454)

* [es/healthcheck] determine enabled scripting langs in healthcheck

* add test for "latestHealthCheckResults" exports
  • Loading branch information
spalger authored Jun 21, 2017
1 parent 6d605ec commit a533455
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
36 changes: 35 additions & 1 deletion src/core_plugins/elasticsearch/lib/__tests__/health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ 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 * as determineEnabledScriptingLangsNS from '../determine_enabled_scripting_langs';
import { determineEnabledScriptingLangs } from '../determine_enabled_scripting_langs';

const esPort = esTestServerUrlParts.port;
const esUrl = url.format(esTestServerUrlParts);
Expand All @@ -20,6 +22,7 @@ describe('plugins/elasticsearch', () => {
let health;
let plugin;
let cluster;
let server;

beforeEach(() => {
const COMPATIBLE_VERSION_NUMBER = '5.0.0';
Expand Down Expand Up @@ -52,6 +55,8 @@ describe('plugins/elasticsearch', () => {
}
}));

sinon.stub(determineEnabledScriptingLangsNS, 'determineEnabledScriptingLangs').returns(Promise.resolve([]));

// setup the config().get()/.set() stubs
const get = sinon.stub();
get.withArgs('elasticsearch.url').returns(esUrl);
Expand All @@ -60,10 +65,11 @@ describe('plugins/elasticsearch', () => {
const set = sinon.stub();

// Setup the server mock
const server = {
server = {
log: sinon.stub(),
info: { port: 5601 },
config: function () { return { get, set }; },
expose: sinon.stub(),
plugins: {
elasticsearch: {
getCluster: sinon.stub().returns(cluster)
Expand All @@ -76,6 +82,7 @@ describe('plugins/elasticsearch', () => {

afterEach(() => {
kibanaVersion.get.restore();
determineEnabledScriptingLangs.restore();
});

it('should set the cluster green if everything is ready', function () {
Expand Down Expand Up @@ -170,6 +177,33 @@ describe('plugins/elasticsearch', () => {
});
});

describe('latestHealthCheckResults', () => {
it('exports an object when the health check completes', () => {
cluster.callWithInternalUser.withArgs('ping').returns(Promise.resolve());
cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any).returns(
Promise.resolve({ timed_out: false, status: 'green' })
);
determineEnabledScriptingLangs.returns(Promise.resolve([
'foo',
'bar'
]));

return health.run()
.then(function () {
sinon.assert.calledOnce(server.expose);
expect(server.expose.firstCall.args).to.eql([
'latestHealthCheckResults',
{
enabledScriptingLangs: [
'foo',
'bar'
]
}
]);
});
});
});

describe('#waitUntilReady', function () {
it('polls health until index is ready', function () {
const clusterHealth = cluster.callWithInternalUser.withArgs('cluster.health', sinon.match.any);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { get, pick, omit, keys } from 'lodash';

export function determineEnabledScriptingLangs(callDataAsKibanaUser) {
return callDataAsKibanaUser('cluster.getSettings', {
include_defaults: true,
filter_path: '**.script.engine.*.inline'
})
.then((esResponse) => {
const langs = get(esResponse, 'defaults.script.engine', {});
const inlineLangs = pick(langs, lang => lang.inline === 'true');
const supportedLangs = omit(inlineLangs, 'mustache');
return keys(supportedLangs);
});
}
7 changes: 7 additions & 0 deletions src/core_plugins/elasticsearch/lib/health_check.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import kibanaVersion from './kibana_version';
import { ensureEsVersion } from './ensure_es_version';
import { ensureNotTribe } from './ensure_not_tribe';
import { ensureAllowExplicitIndex } from './ensure_allow_explicit_index';
import { determineEnabledScriptingLangs } from './determine_enabled_scripting_langs';

const NoConnections = elasticsearch.errors.NoConnections;
import util from 'util';
Expand Down Expand Up @@ -92,13 +93,18 @@ module.exports = function (plugin, server, { mappings }) {
}

function check() {
const results = {};

const healthCheck =
waitForPong(callAdminAsKibanaUser, config.get('elasticsearch.url'))
.then(waitForEsVersion)
.then(() => ensureNotTribe(callAdminAsKibanaUser))
.then(() => ensureAllowExplicitIndex(callAdminAsKibanaUser, config))
.then(waitForShards)
.then(_.partial(migrateConfig, server, { mappings }))
.then(async () => {
results.enabledScriptingLangs = await determineEnabledScriptingLangs(callDataAsKibanaUser);
})
.then(() => {
const tribeUrl = config.get('elasticsearch.tribe.url');
if (tribeUrl) {
Expand All @@ -108,6 +114,7 @@ module.exports = function (plugin, server, { mappings }) {
});

return healthCheck
.then(() => server.expose('latestHealthCheckResults', results))
.then(setGreenStatus)
.catch(err => plugin.status.red(err));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import _ from 'lodash';
import handleESError from '../../../lib/handle_es_error';

export function registerLanguages(server) {
server.route({
path: '/api/kibana/scripts/languages',
method: 'GET',
handler: function (request, reply) {
const { callWithRequest } = server.plugins.elasticsearch.getCluster('data');

return callWithRequest(request, 'cluster.getSettings', {
include_defaults: true,
filter_path: '**.script.engine.*.inline'
})
.then((esResponse) => {
const langs = _.get(esResponse, 'defaults.script.engine', {});
const inlineLangs = _.pick(langs, (lang) => lang.inline === 'true');
const supportedLangs = _.omit(inlineLangs, 'mustache');
return _.keys(supportedLangs);
})
.then(reply)
.catch((error) => {
reply(handleESError(error));
});
const { latestHealthCheckResults = {} } = server.plugins.elasticsearch;
reply(latestHealthCheckResults.enabledScriptingLangs || []);
}
});
}

0 comments on commit a533455

Please sign in to comment.