diff --git a/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/__tests__/bucket_span_estimator.js b/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/__tests__/bucket_span_estimator.js deleted file mode 100644 index c003d3117132f..0000000000000 --- a/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/__tests__/bucket_span_estimator.js +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import sinon from 'sinon'; -import expect from '@kbn/expect'; -import { estimateBucketSpanFactory } from '../bucket_span_estimator'; - -// Mock callWithRequest with the ability to simulate returning different -// permission settings. On each call using `ml.privilegeCheck` we retrieve -// the last value from `permissions` and pass that to one of the permission -// settings. The tests call `ml.privilegeCheck` two times, the first time -// sufficient permissions should be returned, the second time insufficient -// permissions. -const permissions = [false, true]; -const callWithRequest = method => { - return new Promise(resolve => { - if (method === 'ml.privilegeCheck') { - resolve({ - cluster: { - 'cluster:monitor/xpack/ml/job/get': true, - 'cluster:monitor/xpack/ml/job/stats/get': true, - 'cluster:monitor/xpack/ml/datafeeds/get': true, - 'cluster:monitor/xpack/ml/datafeeds/stats/get': permissions.pop(), - }, - }); - return; - } - resolve({}); - }); -}; - -// mock callWithInternalUserFactory -// we replace the return value of the factory with the above mocked callWithRequest -import * as mockModule from '../../../client/call_with_internal_user_factory'; - -// mock xpack_main plugin -function mockXpackMainPluginFactory(isEnabled = false, licenseType = 'platinum') { - return { - info: { - isAvailable: () => true, - feature: () => ({ - isEnabled: () => isEnabled, - }), - license: { - getType: () => licenseType, - }, - }, - }; -} - -const mockElasticsearchPlugin = {}; -// mock configuration to be passed to the estimator -const formConfig = { - aggTypes: ['count'], - duration: {}, - fields: [null], - index: '', - query: { - bool: { - must: [{ match_all: {} }], - must_not: [], - }, - }, -}; - -describe('ML - BucketSpanEstimator', () => { - let mockCallWithInternalUserFactory; - - beforeEach(() => { - mockCallWithInternalUserFactory = sinon.mock(mockModule); - mockCallWithInternalUserFactory - .expects('callWithInternalUserFactory') - .once() - .returns(callWithRequest); - }); - - it('call factory', () => { - expect(function() { - estimateBucketSpanFactory(callWithRequest); - mockCallWithInternalUserFactory.verify(); - }).to.not.throwError('Not initialized.'); - }); - - it('call factory and estimator with security disabled', done => { - expect(function() { - const estimateBucketSpan = estimateBucketSpanFactory( - callWithRequest, - mockElasticsearchPlugin, - mockXpackMainPluginFactory() - ); - - estimateBucketSpan(formConfig).catch(catchData => { - expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets'); - mockCallWithInternalUserFactory.verify(); - done(); - }); - }).to.not.throwError('Not initialized.'); - }); - - it('call factory and estimator with security enabled and sufficient permissions.', done => { - expect(function() { - const estimateBucketSpan = estimateBucketSpanFactory( - callWithRequest, - mockElasticsearchPlugin, - mockXpackMainPluginFactory(true) - ); - estimateBucketSpan(formConfig).catch(catchData => { - expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets'); - mockCallWithInternalUserFactory.verify(); - done(); - }); - }).to.not.throwError('Not initialized.'); - }); - - it('call factory and estimator with security enabled and insufficient permissions.', done => { - expect(function() { - const estimateBucketSpan = estimateBucketSpanFactory( - callWithRequest, - mockElasticsearchPlugin, - mockXpackMainPluginFactory(true) - ); - - estimateBucketSpan(formConfig).catch(catchData => { - expect(catchData).to.be('Insufficient permissions to call bucket span estimation.'); - mockCallWithInternalUserFactory.verify(); - done(); - }); - }).to.not.throwError('Not initialized.'); - }); - - afterEach(() => { - mockCallWithInternalUserFactory.restore(); - }); -}); diff --git a/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js b/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js index c0edcb5eeb537..8c9e0a9421701 100644 --- a/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js +++ b/x-pack/legacy/plugins/ml/server/models/bucket_span_estimator/bucket_span_estimator.js @@ -343,11 +343,21 @@ export function estimateBucketSpanFactory(callWithRequest, elasticsearchPlugin, filterPath: '*.*max_buckets', }) .then(settings => { - if (typeof settings !== 'object' || typeof settings.defaults !== 'object') { + if (typeof settings !== 'object') { + reject('Unable to retrieve cluster settings'); + } + + // search.max_buckets could exist in default, persistent or transient cluster settings + const maxBucketsSetting = (settings.defaults || + settings.persistent || + settings.transient || + {})['search.max_buckets']; + + if (maxBucketsSetting === undefined) { reject('Unable to retrieve cluster setting search.max_buckets'); } - const maxBuckets = parseInt(settings.defaults['search.max_buckets']); + const maxBuckets = parseInt(maxBucketsSetting); const runEstimator = (splitFieldValues = []) => { const bucketSpanEstimator = new BucketSpanEstimator(