From 9c3344ee2af35b3d9e6804170027e58ed5f8bfeb Mon Sep 17 00:00:00 2001 From: mgiota Date: Mon, 29 Nov 2021 13:57:23 +0100 Subject: [PATCH] add resource installer unit tests --- .../resource_installer.test.ts | 215 ++++++++++++++++++ 1 file changed, 215 insertions(+) create mode 100644 x-pack/plugins/rule_registry/server/rule_data_plugin_service/resource_installer.test.ts diff --git a/x-pack/plugins/rule_registry/server/rule_data_plugin_service/resource_installer.test.ts b/x-pack/plugins/rule_registry/server/rule_data_plugin_service/resource_installer.test.ts new file mode 100644 index 0000000000000..fdce04a6b1f3d --- /dev/null +++ b/x-pack/plugins/rule_registry/server/rule_data_plugin_service/resource_installer.test.ts @@ -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' }) + ); + }); + }); + }); +});