-
Notifications
You must be signed in to change notification settings - Fork 8.2k
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
[EPM] Conditionally generate ES index pattern name based on dataset_is_prefix #89870
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9fb8dfa
Explicitly generate ES index pattern name.
skh 0d45453
Adjust tests.
skh f5b4dcc
Adjust and reenable tests.
skh 0642673
Set template priority based on dataset_is_prefix
skh 764b414
Refactor indexPatternName -> templateIndexPattern
skh 47f7556
Add unit tests.
skh 890e2de
Use more realistic index pattern in test.
skh 4e543cb
Fix unit test.
skh 0367454
Add unit test for installTemplate().
skh c7887ba
Merge branch 'master' into 88307-use-dataset-is-prefix
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
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
110 changes: 110 additions & 0 deletions
110
x-pack/plugins/fleet/server/services/epm/elasticsearch/template/install.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,110 @@ | ||
/* | ||
* 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 { RegistryDataStream } from '../../../../types'; | ||
import { Field } from '../../fields/field'; | ||
|
||
import { elasticsearchServiceMock } from 'src/core/server/mocks'; | ||
import { installTemplate } from './install'; | ||
|
||
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix not set', async () => { | ||
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser; | ||
const fields: Field[] = []; | ||
const dataStreamDatasetIsPrefixUnset = { | ||
type: 'metrics', | ||
dataset: 'package.dataset', | ||
title: 'test data stream', | ||
release: 'experimental', | ||
package: 'package', | ||
path: 'path', | ||
ingest_pipeline: 'default', | ||
} as RegistryDataStream; | ||
const pkg = { | ||
name: 'package', | ||
version: '0.0.1', | ||
}; | ||
const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*'; | ||
const templatePriorityDatasetIsPrefixUnset = 200; | ||
await installTemplate({ | ||
callCluster, | ||
fields, | ||
dataStream: dataStreamDatasetIsPrefixUnset, | ||
packageVersion: pkg.version, | ||
packageName: pkg.name, | ||
}); | ||
// @ts-ignore | ||
const sentTemplate = callCluster.mock.calls[0][1].body; | ||
expect(sentTemplate).toBeDefined(); | ||
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixUnset); | ||
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixUnset]); | ||
}); | ||
|
||
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to false', async () => { | ||
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser; | ||
const fields: Field[] = []; | ||
const dataStreamDatasetIsPrefixFalse = { | ||
type: 'metrics', | ||
dataset: 'package.dataset', | ||
title: 'test data stream', | ||
release: 'experimental', | ||
package: 'package', | ||
path: 'path', | ||
ingest_pipeline: 'default', | ||
dataset_is_prefix: false, | ||
} as RegistryDataStream; | ||
const pkg = { | ||
name: 'package', | ||
version: '0.0.1', | ||
}; | ||
const templateIndexPatternDatasetIsPrefixFalse = 'metrics-package.dataset-*'; | ||
const templatePriorityDatasetIsPrefixFalse = 200; | ||
await installTemplate({ | ||
callCluster, | ||
fields, | ||
dataStream: dataStreamDatasetIsPrefixFalse, | ||
packageVersion: pkg.version, | ||
packageName: pkg.name, | ||
}); | ||
// @ts-ignore | ||
const sentTemplate = callCluster.mock.calls[0][1].body; | ||
expect(sentTemplate).toBeDefined(); | ||
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixFalse); | ||
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixFalse]); | ||
}); | ||
|
||
test('tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to true', async () => { | ||
const callCluster = elasticsearchServiceMock.createLegacyScopedClusterClient().callAsCurrentUser; | ||
const fields: Field[] = []; | ||
const dataStreamDatasetIsPrefixTrue = { | ||
type: 'metrics', | ||
dataset: 'package.dataset', | ||
title: 'test data stream', | ||
release: 'experimental', | ||
package: 'package', | ||
path: 'path', | ||
ingest_pipeline: 'default', | ||
dataset_is_prefix: true, | ||
} as RegistryDataStream; | ||
const pkg = { | ||
name: 'package', | ||
version: '0.0.1', | ||
}; | ||
const templateIndexPatternDatasetIsPrefixTrue = 'metrics-package.dataset.*-*'; | ||
const templatePriorityDatasetIsPrefixTrue = 150; | ||
await installTemplate({ | ||
callCluster, | ||
fields, | ||
dataStream: dataStreamDatasetIsPrefixTrue, | ||
packageVersion: pkg.version, | ||
packageName: pkg.name, | ||
}); | ||
// @ts-ignore | ||
const sentTemplate = callCluster.mock.calls[0][1].body; | ||
expect(sentTemplate).toBeDefined(); | ||
expect(sentTemplate.priority).toBe(templatePriorityDatasetIsPrefixTrue); | ||
expect(sentTemplate.index_patterns).toEqual([templateIndexPatternDatasetIsPrefixTrue]); | ||
}); |
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
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
Oops, something went wrong.
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.
it would be great to have a unit test for
installTemplate
since that's the method that generates the index pattern and priority based on data stream information. I don't see any tests here that actually test on thedataset_is_prefix
propertyThere 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.
Good point. Is there an established way how to mock calls to elasticsearch in unit tests?
I've added unit tests for
generateTemplateIndexPattern()
andgetTemplatePriority()
as a start in 8b58f46There 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.
it looks like Kibana core does export some ES mocks, here's an example: https:/elastic/kibana/blob/master/x-pack/plugins/fleet/server/services/epm/packages/_install_package.test.ts#L38
I see that
installTemplate
passescallCluster
to other methods, so I guess you might have to mock ES return values in order of whenevercallCluster
is called further down the chain. the mocked ES client returns ajest.fn()
so mocking the responses could be done like this: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.
Thanks for the pointer, I've added a unit test for
installTemplate()
.