[ML] Add ML plugin contract mocks (#96265)
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
parent
2582522c66
commit
955c46ba5e
|
@ -0,0 +1,17 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
import { ML_APP_URL_GENERATOR } from '../../../common/constants/ml_url_generator';
|
||||
import { UrlGeneratorContract } from '../../../../../../src/plugins/share/public';
|
||||
|
||||
export const createMlUrlGeneratorMock = () =>
|
||||
({
|
||||
id: ML_APP_URL_GENERATOR,
|
||||
isDeprecated: false,
|
||||
createUrl: jest.fn(),
|
||||
migrate: jest.fn(),
|
||||
} as jest.Mocked<UrlGeneratorContract<typeof ML_APP_URL_GENERATOR>>);
|
24
x-pack/plugins/ml/public/mocks.ts
Normal file
24
x-pack/plugins/ml/public/mocks.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
import { createMlUrlGeneratorMock } from './ml_url_generator/__mocks__/ml_url_generator';
|
||||
import { MlPluginSetup, MlPluginStart } from './plugin';
|
||||
const createSetupContract = (): jest.Mocked<MlPluginSetup> => {
|
||||
return {
|
||||
urlGenerator: createMlUrlGeneratorMock(),
|
||||
};
|
||||
};
|
||||
|
||||
const createStartContract = (): jest.Mocked<MlPluginStart> => {
|
||||
return {
|
||||
urlGenerator: createMlUrlGeneratorMock(),
|
||||
};
|
||||
};
|
||||
|
||||
export const mlPluginMock = {
|
||||
createSetupContract,
|
||||
createStartContract,
|
||||
};
|
30
x-pack/plugins/ml/server/mocks.ts
Normal file
30
x-pack/plugins/ml/server/mocks.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
import { createJobServiceProviderMock } from './shared_services/providers/__mocks__/jobs_service';
|
||||
import { createAnomalyDetectorsProviderMock } from './shared_services/providers/__mocks__/anomaly_detectors';
|
||||
import { createMockMlSystemProvider } from './shared_services/providers/__mocks__/system';
|
||||
import { createModulesProviderMock } from './shared_services/providers/__mocks__/modules';
|
||||
import { createResultsServiceProviderMock } from './shared_services/providers/__mocks__/results_service';
|
||||
import { createAlertingServiceProviderMock } from './shared_services/providers/__mocks__/alerting_service';
|
||||
import { MlPluginSetup } from './plugin';
|
||||
|
||||
const createSetupContract = () =>
|
||||
(({
|
||||
jobServiceProvider: createJobServiceProviderMock(),
|
||||
anomalyDetectorsProvider: createAnomalyDetectorsProviderMock(),
|
||||
mlSystemProvider: createMockMlSystemProvider(),
|
||||
modulesProvider: createModulesProviderMock(),
|
||||
resultsServiceProvider: createResultsServiceProviderMock(),
|
||||
alertingServiceProvider: createAlertingServiceProviderMock(),
|
||||
} as unknown) as jest.Mocked<MlPluginSetup>);
|
||||
|
||||
const createStartContract = () => jest.fn();
|
||||
|
||||
export const mlPluginServerMock = {
|
||||
createSetupContract,
|
||||
createStartContract,
|
||||
};
|
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
export const createAlertingServiceProviderMock = () =>
|
||||
jest.fn(() => ({
|
||||
preview: jest.fn(),
|
||||
execute: jest.fn(),
|
||||
}));
|
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
export const createAnomalyDetectorsProviderMock = () =>
|
||||
jest.fn(() => ({
|
||||
jobs: jest.fn(),
|
||||
jobStats: jest.fn(),
|
||||
datafeeds: jest.fn(),
|
||||
datafeedStats: jest.fn(),
|
||||
}));
|
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
export const createJobServiceProviderMock = () =>
|
||||
jest.fn(() => ({
|
||||
jobsSummary: jest.fn(),
|
||||
}));
|
|
@ -0,0 +1,14 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
export const createModulesProviderMock = () =>
|
||||
jest.fn(() => ({
|
||||
recognize: jest.fn(),
|
||||
getModule: jest.fn(),
|
||||
listModules: jest.fn(),
|
||||
setup: jest.fn(),
|
||||
}));
|
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
export const createResultsServiceProviderMock = () =>
|
||||
jest.fn(() => ({
|
||||
getAnomaliesTableData: jest.fn(),
|
||||
}));
|
|
@ -0,0 +1,13 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License
|
||||
* 2.0; you may not use this file except in compliance with the Elastic License
|
||||
* 2.0.
|
||||
*/
|
||||
|
||||
export const createMockMlSystemProvider = () =>
|
||||
jest.fn(() => ({
|
||||
mlCapabilities: jest.fn(),
|
||||
mlInfo: jest.fn(),
|
||||
mlAnomalySearch: jest.fn(),
|
||||
}));
|
|
@ -27,12 +27,12 @@ jest.mock('../../../machine_learning/authz', () => mockMlAuthzFactory.create());
|
|||
describe('create_rules_bulk', () => {
|
||||
let server: ReturnType<typeof serverMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
({ clients, context } = requestContextMock.createTools());
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.clusterClient.callAsCurrentUser.mockResolvedValue(getNonEmptyIndex()); // index exists
|
||||
clients.alertsClient.find.mockResolvedValue(getEmptyFindResult()); // no existing rules
|
||||
|
|
|
@ -29,12 +29,12 @@ jest.mock('../../../machine_learning/authz', () => mockMlAuthzFactory.create());
|
|||
describe('create_rules', () => {
|
||||
let server: ReturnType<typeof serverMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
({ clients, context } = requestContextMock.createTools());
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.clusterClient.callAsCurrentUser.mockResolvedValue(getNonEmptyIndex()); // index exists
|
||||
clients.alertsClient.find.mockResolvedValue(getEmptyFindResult()); // no current rules
|
||||
|
|
|
@ -34,7 +34,7 @@ describe('import_rules_route', () => {
|
|||
let server: ReturnType<typeof serverMock.create>;
|
||||
let request: ReturnType<typeof requestMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
|
@ -42,7 +42,7 @@ describe('import_rules_route', () => {
|
|||
config = createMockConfig();
|
||||
const hapiStream = buildHapiStream(ruleIdsToNdJsonString(['rule-1']));
|
||||
request = getImportRulesRequest(hapiStream);
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.clusterClient.callAsCurrentUser.mockResolvedValue(getNonEmptyIndex()); // index exists
|
||||
clients.alertsClient.find.mockResolvedValue(getEmptyFindResult()); // no extant rules
|
||||
|
|
|
@ -24,12 +24,12 @@ jest.mock('../../../machine_learning/authz', () => mockMlAuthzFactory.create());
|
|||
describe('patch_rules_bulk', () => {
|
||||
let server: ReturnType<typeof serverMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
({ clients, context } = requestContextMock.createTools());
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.alertsClient.find.mockResolvedValue(getFindResultWithSingleHit()); // rule exists
|
||||
clients.alertsClient.update.mockResolvedValue(getResult()); // update succeeds
|
||||
|
|
|
@ -26,12 +26,12 @@ jest.mock('../../../machine_learning/authz', () => mockMlAuthzFactory.create());
|
|||
describe('patch_rules', () => {
|
||||
let server: ReturnType<typeof serverMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
({ clients, context } = requestContextMock.createTools());
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.alertsClient.get.mockResolvedValue(getResult()); // existing rule
|
||||
clients.alertsClient.find.mockResolvedValue(getFindResultWithSingleHit()); // existing rule
|
||||
|
|
|
@ -26,12 +26,12 @@ jest.mock('../../../machine_learning/authz', () => mockMlAuthzFactory.create());
|
|||
describe('update_rules_bulk', () => {
|
||||
let server: ReturnType<typeof serverMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
({ clients, context } = requestContextMock.createTools());
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.alertsClient.find.mockResolvedValue(getFindResultWithSingleHit());
|
||||
clients.alertsClient.update.mockResolvedValue(getResult());
|
||||
|
|
|
@ -28,12 +28,12 @@ jest.mock('../../rules/update_rules_notifications');
|
|||
describe('update_rules', () => {
|
||||
let server: ReturnType<typeof serverMock.create>;
|
||||
let { clients, context } = requestContextMock.createTools();
|
||||
let ml: ReturnType<typeof mlServicesMock.create>;
|
||||
let ml: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
beforeEach(() => {
|
||||
server = serverMock.create();
|
||||
({ clients, context } = requestContextMock.createTools());
|
||||
ml = mlServicesMock.create();
|
||||
ml = mlServicesMock.createSetupContract();
|
||||
|
||||
clients.alertsClient.get.mockResolvedValue(getResult()); // existing rule
|
||||
clients.alertsClient.find.mockResolvedValue(getFindResultWithSingleHit()); // rule exists
|
||||
|
|
|
@ -16,7 +16,7 @@ jest.mock('../../../common/machine_learning/has_ml_admin_permissions');
|
|||
|
||||
describe('isMlAdmin', () => {
|
||||
it('returns true if hasMlAdminPermissions is true', async () => {
|
||||
const mockMl = mlServicesMock.create();
|
||||
const mockMl = mlServicesMock.createSetupContract();
|
||||
const request = httpServerMock.createKibanaRequest();
|
||||
const savedObjectsClient = savedObjectsClientMock.create();
|
||||
(hasMlAdminPermissions as jest.Mock).mockReturnValue(true);
|
||||
|
@ -25,7 +25,7 @@ describe('isMlAdmin', () => {
|
|||
});
|
||||
|
||||
it('returns false if hasMlAdminPermissions is false', async () => {
|
||||
const mockMl = mlServicesMock.create();
|
||||
const mockMl = mlServicesMock.createSetupContract();
|
||||
const request = httpServerMock.createKibanaRequest();
|
||||
const savedObjectsClient = savedObjectsClientMock.create();
|
||||
(hasMlAdminPermissions as jest.Mock).mockReturnValue(false);
|
||||
|
@ -56,13 +56,13 @@ describe('hasMlLicense', () => {
|
|||
|
||||
describe('mlAuthz', () => {
|
||||
let licenseMock: ReturnType<typeof licensingMock.createLicenseMock>;
|
||||
let mlMock: ReturnType<typeof mlServicesMock.create>;
|
||||
let mlMock: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
let request: KibanaRequest;
|
||||
let savedObjectsClient: SavedObjectsClientContract;
|
||||
|
||||
beforeEach(() => {
|
||||
licenseMock = licensingMock.createLicenseMock();
|
||||
mlMock = mlServicesMock.create();
|
||||
mlMock = mlServicesMock.createSetupContract();
|
||||
request = httpServerMock.createKibanaRequest();
|
||||
savedObjectsClient = savedObjectsClientMock.create();
|
||||
});
|
||||
|
|
|
@ -5,25 +5,9 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { MlPluginSetup } from '../../../../ml/server';
|
||||
import { elasticsearchServiceMock } from '../../../../../../src/core/server/mocks';
|
||||
import { mlPluginServerMock } from '../../../../ml/server/mocks';
|
||||
|
||||
const createMockClient = () => elasticsearchServiceMock.createLegacyClusterClient();
|
||||
const createMockMlSystemProvider = () =>
|
||||
jest.fn(() => ({
|
||||
mlCapabilities: jest.fn(),
|
||||
}));
|
||||
|
||||
export const mlServicesMock = {
|
||||
create: () =>
|
||||
(({
|
||||
modulesProvider: jest.fn(),
|
||||
jobServiceProvider: jest.fn(),
|
||||
anomalyDetectorsProvider: jest.fn(),
|
||||
mlSystemProvider: createMockMlSystemProvider(),
|
||||
mlClient: createMockClient(),
|
||||
} as unknown) as jest.Mocked<MlPluginSetup>),
|
||||
};
|
||||
export const mlServicesMock = mlPluginServerMock;
|
||||
|
||||
const mockValidateRuleType = jest.fn().mockResolvedValue({ valid: true, message: undefined });
|
||||
const createBuildMlAuthzMock = () =>
|
||||
|
|
|
@ -21,12 +21,12 @@ import { fetchDetectionsUsage, fetchDetectionsMetrics } from './index';
|
|||
describe('Detections Usage and Metrics', () => {
|
||||
let esClientMock: jest.Mocked<ElasticsearchClient>;
|
||||
let savedObjectsClientMock: jest.Mocked<SavedObjectsClientContract>;
|
||||
let mlMock: ReturnType<typeof mlServicesMock.create>;
|
||||
let mlMock: ReturnType<typeof mlServicesMock.createSetupContract>;
|
||||
|
||||
describe('fetchDetectionsUsage()', () => {
|
||||
beforeEach(() => {
|
||||
esClientMock = elasticsearchServiceMock.createClusterClient().asInternalUser;
|
||||
mlMock = mlServicesMock.create();
|
||||
mlMock = mlServicesMock.createSetupContract();
|
||||
});
|
||||
|
||||
it('returns zeroed counts if both calls are empty', async () => {
|
||||
|
@ -108,7 +108,7 @@ describe('Detections Usage and Metrics', () => {
|
|||
|
||||
describe('fetchDetectionsMetrics()', () => {
|
||||
beforeEach(() => {
|
||||
mlMock = mlServicesMock.create();
|
||||
mlMock = mlServicesMock.createSetupContract();
|
||||
});
|
||||
|
||||
it('returns an empty array if there is no data', async () => {
|
||||
|
|
Loading…
Reference in a new issue