-
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
1 changed file
with
215 additions
and
0 deletions.
There are no files selected for viewing
215 changes: 215 additions & 0 deletions
215
x-pack/plugins/rule_registry/server/rule_data_plugin_service/resource_installer.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,215 @@ | ||
/* | ||
* 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 { ResourceInstaller } from './resource_installer'; | ||
import { loggerMock } from '@kbn/logging/mocks'; | ||
import { AlertConsumers } from '@kbn/rule-data-utils/alerts_as_data_rbac'; | ||
|
||
import { Dataset } from './index_options'; | ||
import { IndexInfo } from './index_info'; | ||
import { elasticsearchServiceMock } from 'src/core/server/mocks'; | ||
import { | ||
DEFAULT_ILM_POLICY_ID, | ||
ECS_COMPONENT_TEMPLATE_NAME, | ||
TECHNICAL_COMPONENT_TEMPLATE_NAME, | ||
} from '../../common/assets'; | ||
|
||
describe('resourceInstaller', () => { | ||
describe('if write is disabled', () => { | ||
it('should not install common resources', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
const installer = new ResourceInstaller({ | ||
logger: loggerMock.create(), | ||
isWriteEnabled: false, | ||
disabledRegistrationContexts: [], | ||
getResourceName: jest.fn(), | ||
getClusterClient, | ||
}); | ||
installer.installCommonResources(); | ||
expect(getClusterClient).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not install index level resources', () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
const installer = new ResourceInstaller({ | ||
logger: loggerMock.create(), | ||
isWriteEnabled: false, | ||
disabledRegistrationContexts: [], | ||
getResourceName: jest.fn(), | ||
getClusterClient, | ||
}); | ||
const indexOptions = { | ||
feature: AlertConsumers.LOGS, | ||
registrationContext: 'observability.logs', | ||
dataset: Dataset.alerts, | ||
componentTemplateRefs: [], | ||
componentTemplates: [ | ||
{ | ||
name: 'mappings', | ||
}, | ||
], | ||
}; | ||
const indexInfo = new IndexInfo({ indexOptions, kibanaVersion: '8.1.0' }); | ||
|
||
installer.installIndexLevelResources(indexInfo); | ||
expect(mockClusterClient.cluster.putComponentTemplate).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
||
describe('if write is enabled', () => { | ||
it('should install common resources', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
const getResourceNameMock = jest | ||
.fn() | ||
.mockReturnValueOnce(DEFAULT_ILM_POLICY_ID) | ||
.mockReturnValueOnce(TECHNICAL_COMPONENT_TEMPLATE_NAME) | ||
.mockReturnValueOnce(ECS_COMPONENT_TEMPLATE_NAME); | ||
const installer = new ResourceInstaller({ | ||
logger: loggerMock.create(), | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: [], | ||
getResourceName: getResourceNameMock, | ||
getClusterClient, | ||
}); | ||
|
||
await installer.installCommonResources(); | ||
|
||
expect(mockClusterClient.ilm.putLifecycle).toHaveBeenCalled(); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(2); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ name: TECHNICAL_COMPONENT_TEMPLATE_NAME }) | ||
); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith( | ||
2, | ||
expect.objectContaining({ name: ECS_COMPONENT_TEMPLATE_NAME }) | ||
); | ||
}); | ||
it('should install index level resources', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
const installer = new ResourceInstaller({ | ||
logger: loggerMock.create(), | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: [], | ||
getResourceName: jest.fn(), | ||
getClusterClient, | ||
}); | ||
|
||
const indexOptions = { | ||
feature: AlertConsumers.LOGS, | ||
registrationContext: 'observability.logs', | ||
dataset: Dataset.alerts, | ||
componentTemplateRefs: [], | ||
componentTemplates: [ | ||
{ | ||
name: 'mappings', | ||
}, | ||
], | ||
}; | ||
const indexInfo = new IndexInfo({ indexOptions, kibanaVersion: '8.1.0' }); | ||
|
||
await installer.installIndexLevelResources(indexInfo); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledWith( | ||
expect.objectContaining({ name: '.alerts-observability.logs.alerts-mappings' }) | ||
); | ||
}); | ||
it('should install namespace level resources', async () => {}); | ||
|
||
describe('and writing to observability.logs registration is disabled', () => { | ||
it('should install common resources', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
const logger = loggerMock.create(); | ||
const getResourceNameMock = jest | ||
.fn() | ||
.mockReturnValueOnce(DEFAULT_ILM_POLICY_ID) | ||
.mockReturnValueOnce(TECHNICAL_COMPONENT_TEMPLATE_NAME) | ||
.mockReturnValueOnce(ECS_COMPONENT_TEMPLATE_NAME); | ||
const installer = new ResourceInstaller({ | ||
logger, | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
getResourceName: getResourceNameMock, | ||
getClusterClient, | ||
}); | ||
await installer.installCommonResources(); | ||
|
||
expect(mockClusterClient.ilm.putLifecycle).toHaveBeenCalled(); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledTimes(2); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ name: TECHNICAL_COMPONENT_TEMPLATE_NAME }) | ||
); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenNthCalledWith( | ||
2, | ||
expect.objectContaining({ name: ECS_COMPONENT_TEMPLATE_NAME }) | ||
); | ||
}); | ||
it('should not install observability.logs index level resources', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
const installer = new ResourceInstaller({ | ||
logger: loggerMock.create(), | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
getResourceName: jest.fn(), | ||
getClusterClient, | ||
}); | ||
const indexOptions = { | ||
feature: AlertConsumers.LOGS, | ||
registrationContext: 'observability.logs', | ||
dataset: Dataset.alerts, | ||
componentTemplateRefs: [], | ||
componentTemplates: [ | ||
{ | ||
name: 'mappings', | ||
}, | ||
], | ||
}; | ||
const indexInfo = new IndexInfo({ indexOptions, kibanaVersion: '8.1.0' }); | ||
|
||
installer.installIndexLevelResources(indexInfo); | ||
expect(mockClusterClient.cluster.putComponentTemplate).not.toHaveBeenCalled(); | ||
}); | ||
it('should install observability.apm index level resources', async () => { | ||
const mockClusterClient = elasticsearchServiceMock.createElasticsearchClient(); | ||
const getClusterClient = jest.fn(() => Promise.resolve(mockClusterClient)); | ||
|
||
const installer = new ResourceInstaller({ | ||
logger: loggerMock.create(), | ||
isWriteEnabled: true, | ||
disabledRegistrationContexts: ['observability.logs'], | ||
getResourceName: jest.fn(), | ||
getClusterClient, | ||
}); | ||
const indexOptions = { | ||
feature: AlertConsumers.APM, | ||
registrationContext: 'observability.apm', | ||
dataset: Dataset.alerts, | ||
componentTemplateRefs: [], | ||
componentTemplates: [ | ||
{ | ||
name: 'mappings', | ||
}, | ||
], | ||
}; | ||
const indexInfo = new IndexInfo({ indexOptions, kibanaVersion: '8.1.0' }); | ||
|
||
await installer.installIndexLevelResources(indexInfo); | ||
expect(mockClusterClient.cluster.putComponentTemplate).toHaveBeenCalledWith( | ||
expect.objectContaining({ name: '.alerts-observability.apm.alerts-mappings' }) | ||
); | ||
}); | ||
}); | ||
}); | ||
}); |