-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
895d992
add more rule_registry unit tests
mgiota 595ab98
delete initializeService test
mgiota 38ac6a6
refactor and use the existing ruleDataClient mock
mgiota 9354f1e
remove resource installer mock
mgiota 4e3764f
check ruleDataClient has been called with correct arguments
mgiota 84a6eac
resource installer attempt
mgiota 9b136d0
remove failing tests
mgiota fcf257f
Merge branch 'main' into 120322_rule_registry_unit_tests
kibanamachine File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(), | ||
installIndexLevelResources: jest.fn(), | ||
installAndUpdateNamespaceLevelResources: jest.fn(), | ||
}; | ||
}; | ||
|
||
export const resourceInstallerMock: { | ||
create: () => ResourceInstallerMock; | ||
} = { | ||
create: createResourceInstallerMock, | ||
}; |
109 changes: 109 additions & 0 deletions
109
...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,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', () => ({ | ||
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' }), | ||
}) | ||
); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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