Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ML] Migrate server side Mocha tests to Jest. #65651

Merged
merged 6 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* you may not use this file except in compliance with the Elastic License.
*/

import expect from '@kbn/expect';
import { estimateBucketSpanFactory } from '../bucket_span_estimator';
import { APICaller } from 'kibana/server';

import { ES_AGGREGATION } from '../../../common/constants/aggregation_types';

import { estimateBucketSpanFactory, BucketSpanEstimatorData } from './bucket_span_estimator';

// Mock callWithRequest with the ability to simulate returning different
// permission settings. On each call using `ml.privilegeCheck` we retrieve
Expand All @@ -14,7 +17,7 @@ import { estimateBucketSpanFactory } from '../bucket_span_estimator';
// sufficient permissions should be returned, the second time insufficient
// permissions.
const permissions = [false, true];
const callWithRequest = method => {
const callWithRequest: APICaller = (method: string) => {
return new Promise(resolve => {
if (method === 'ml.privilegeCheck') {
resolve({
Expand All @@ -28,34 +31,19 @@ const callWithRequest = method => {
return;
}
resolve({});
});
}) as Promise<any>;
};

const callWithInternalUser = () => {
const callWithInternalUser: APICaller = () => {
return new Promise(resolve => {
resolve({});
});
}) as Promise<any>;
};

// mock xpack_main plugin
function mockXpackMainPluginFactory(isEnabled = false, licenseType = 'platinum') {
return {
info: {
isAvailable: () => true,
feature: () => ({
isEnabled: () => isEnabled,
}),
license: {
getType: () => licenseType,
},
},
};
}

// mock configuration to be passed to the estimator
const formConfig = {
aggTypes: ['count'],
duration: {},
const formConfig: BucketSpanEstimatorData = {
aggTypes: [ES_AGGREGATION.COUNT],
duration: { start: 0, end: 1 },
fields: [null],
index: '',
query: {
Expand All @@ -64,58 +52,45 @@ const formConfig = {
must_not: [],
},
},
splitField: undefined,
timeField: undefined,
};

describe('ML - BucketSpanEstimator', () => {
it('call factory', () => {
expect(function() {
estimateBucketSpanFactory(callWithRequest, callWithInternalUser);
}).to.not.throwError('Not initialized.');
estimateBucketSpanFactory(callWithRequest, callWithInternalUser, false);
}).not.toThrow('Not initialized.');
});

it('call factory and estimator with security disabled', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
callWithInternalUser,
mockXpackMainPluginFactory()
true
);

estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets');

done();
});
}).to.not.throwError('Not initialized.');
}).not.toThrow('Not initialized.');
});

it('call factory and estimator with security enabled and sufficient permissions.', done => {
it('call factory and estimator with security enabled.', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
callWithInternalUser,
mockXpackMainPluginFactory(true)
false
);
estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Unable to retrieve cluster setting search.max_buckets');
expect(catchData).toBe('Unable to retrieve cluster setting search.max_buckets');

done();
});
}).to.not.throwError('Not initialized.');
});

it('call factory and estimator with security enabled and insufficient permissions.', done => {
expect(function() {
const estimateBucketSpan = estimateBucketSpanFactory(
callWithRequest,
callWithInternalUser,
mockXpackMainPluginFactory(true)
);

estimateBucketSpan(formConfig).catch(catchData => {
expect(catchData).to.be('Insufficient permissions to call bucket span estimation.');
done();
});
}).to.not.throwError('Not initialized.');
}).not.toThrow('Not initialized.');
});
});
17 changes: 11 additions & 6 deletions x-pack/plugins/ml/server/models/job_validation/job_validation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@

import { APICaller } from 'kibana/server';
import { TypeOf } from '@kbn/config-schema';

import { DeepPartial } from '../../../common/types/common';

import { validateJobSchema } from '../../routes/schemas/job_validation_schema';

type ValidateJobPayload = TypeOf<typeof validateJobSchema>;
import { ValidationMessage } from './messages';

export type ValidateJobPayload = TypeOf<typeof validateJobSchema>;

export function validateJob(
callAsCurrentUser: APICaller,
payload: ValidateJobPayload,
kbnVersion: string,
callAsInternalUser: APICaller,
isSecurityDisabled: boolean
): string[];
payload?: DeepPartial<ValidateJobPayload>,
kbnVersion?: string,
callAsInternalUser?: APICaller,
isSecurityDisabled?: boolean
): Promise<ValidationMessage[]>;
Loading