[7.6] [ML] Fixes bucket span estimators loading of max_buckets setting (#59639) (#59662)

* [ML] Fixes bucket span estimators loading of max_buckets setting (#59639)

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>

* fixing test

* reverting test fix

* disabling test

* disabling tests for error text

* removing tests

Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
James Gowdy 2020-03-10 15:23:26 +00:00 committed by GitHub
parent 78508b856d
commit 918cdd47ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 139 deletions

View file

@ -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();
});
});

View file

@ -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(