-
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 1 commit
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(() => 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(); | ||
}); | ||
}); | ||
}); |
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.
@weltenwort Before I open this PR for review, I would like your input regarding what's the recommended way to mock a class constructor. Later in my test I do
and it works. I am just wondering if there's a better way.
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.
Looks like there already is a mock implementation of the rule data client in
kibana/x-pack/plugins/rule_registry/server/rule_data_client/rule_data_client.mock.ts
Lines 21 to 43 in bfa8b3c
And further down I saw you're calling
jest.mock
again. Instead I would recommend to just import theRuleDataClient
normally. The jest mocking mechanism will make sure it's replaced with the mock.Similarly, it looks like you're calling
jest.mock('./resource_installer', () => {
inside of a test case. I think it's safer to only calljest.mock
on the top level of the file as it needs to be evaluated before any import.