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

add more rule_registry unit tests #120323

Merged
merged 8 commits into from
Dec 7, 2021
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
@@ -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(),
installIndexLevelResources: jest.fn(),
installAndUpdateNamespaceLevelResources: jest.fn(),
};
};

export const resourceInstallerMock: {
create: () => ResourceInstallerMock;
} = {
create: createResourceInstallerMock,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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';
import { RuleDataClient } from '../rule_data_client/rule_data_client';
import { createRuleDataClientMock as mockCreateRuleDataClient } from '../rule_data_client/rule_data_client.mock';

jest.mock('../rule_data_client/rule_data_client', () => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually this means we don't use any dependency injection model :)
Apart from this, the tests look good :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ersin-erdal Exactly. I had troubles mocking constructors like RuleDataClient or ResourceInstaller in the rule_data_plugin_service.test. I was thinking to refactor using dependency injection instead of directly importing the dependencies. Let's discuss about this during our today's meeting

RuleDataClient: jest.fn().mockImplementation(() => mockCreateRuleDataClient()),
}));

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('initializeIndex', () => {
it('calls RuleDataClient', 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,
});
const indexOptions = {
feature: AlertConsumers.LOGS,
registrationContext: 'observability.logs',
dataset: Dataset.alerts,
componentTemplateRefs: [],
componentTemplates: [
{
name: 'mappings',
},
],
};
await ruleDataService.initializeService();
await ruleDataService.initializeIndex(indexOptions);
expect(RuleDataClient).toHaveBeenCalled();
expect(RuleDataClient).toHaveBeenCalledWith(
expect.objectContaining({
indexInfo: expect.objectContaining({ baseName: '.alerts-observability.logs.alerts' }),
})
);
});
});
});