-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/rule_registry/server/rule_data_plugin_service/resource_installer.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 type { PublicMethodsOf } from '@kbn/utility-types'; | ||
import { ResourceInstaller } from './resource_installer'; | ||
|
||
type Schema = PublicMethodsOf<ResourceInstaller>; | ||
export type ResourceInstallerMock = jest.Mocked<Schema>; | ||
const createResourceInstallerMock = () => { | ||
return { | ||
installCommonResources: jest.fn(() => Promise.resolve()), | ||
installIndexLevelResources: jest.fn(), | ||
installAndUpdateNamespaceLevelResources: jest.fn(), | ||
}; | ||
}; | ||
|
||
export const resourceInstallerMock: { | ||
create: () => ResourceInstallerMock; | ||
} = { | ||
create: createResourceInstallerMock, | ||
}; |
124 changes: 124 additions & 0 deletions
124
...ck/plugins/rule_registry/server/rule_data_plugin_service/rule_data_plugin_service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* 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 { loggerMock } from '@kbn/logging/mocks'; | ||
import { RuleDataService } from './rule_data_plugin_service'; | ||
import { elasticsearchServiceMock } from 'src/core/server/mocks'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils/alerts_as_data_rbac'; | ||
|
||
import { Dataset } from './index_options'; | ||
jest.mock('../rule_data_client/rule_data_client'); | ||
|
||
describe('ruleDataPluginService', () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
describe('isRegistrationContextDisabled', () => { | ||
it('should return true', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
const ruleDataService = new RuleDataService({ | ||
logger: loggerMock.create(), | ||
getClusterClient, | ||
kibanaVersion: '8.1.0', | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
isWriterCacheEnabled: true, | ||
}); | ||
expect(ruleDataService.isRegistrationContextDisabled('observability.logs')).toBe(true); | ||
}); | ||
|
||
it('should return false', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
const ruleDataService = new RuleDataService({ | ||
logger: loggerMock.create(), | ||
getClusterClient, | ||
kibanaVersion: '8.1.0', | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
isWriterCacheEnabled: true, | ||
}); | ||
expect(ruleDataService.isRegistrationContextDisabled('observability.apm')).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isWriteEnabled', () => { | ||
it('should return true', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
const ruleDataService = new RuleDataService({ | ||
logger: loggerMock.create(), | ||
getClusterClient, | ||
kibanaVersion: '8.1.0', | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
isWriterCacheEnabled: true, | ||
}); | ||
|
||
expect(ruleDataService.isWriteEnabled('observability.logs')).toBe(false); | ||
}); | ||
}); | ||
describe('initializeService', () => { | ||
it('calls ResourceInstaller', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
new RuleDataService({ | ||
logger: loggerMock.create(), | ||
getClusterClient, | ||
kibanaVersion: '8.1.0', | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
isWriterCacheEnabled: true, | ||
}); | ||
|
||
expect(jest.requireMock('./resource_installer').ResourceInstaller).toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('initializeIndex', () => { | ||
it('calls RuleDataClient', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
jest.mock('./resource_installer', () => { | ||
return function () { | ||
return { installCommonResources: () => {} }; | ||
}; | ||
}); | ||
|
||
const ruleDataService = new RuleDataService({ | ||
logger: loggerMock.create(), | ||
getClusterClient, | ||
kibanaVersion: '8.1.0', | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
isWriterCacheEnabled: true, | ||
}); | ||
const indexOptions = { | ||
feature: AlertConsumers.LOGS, | ||
registrationContext: 'observability.logs', | ||
dataset: Dataset.alerts, | ||
componentTemplateRefs: [], | ||
componentTemplates: [ | ||
{ | ||
name: 'mappings', | ||
}, | ||
], | ||
}; | ||
await ruleDataService.initializeService(); | ||
await ruleDataService.initializeIndex(indexOptions); | ||
expect( | ||
jest.requireMock('../rule_data_client/rule_data_client').RuleDataClient | ||
).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |