@@ -13,6 +13,12 @@ import { installTemplate } from './install';
1313
1414test ( 'tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix not set' , async ( ) => {
1515 const callCluster = elasticsearchServiceMock . createLegacyScopedClusterClient ( ) . callAsCurrentUser ;
16+ callCluster . mockImplementation ( ( _ , params ) => {
17+ if ( params . method === 'GET' && params . path === '/_index_template/metrics-package.dataset' ) {
18+ return { index_templates : [ ] } ;
19+ }
20+ } ) ;
21+
1622 const fields : Field [ ] = [ ] ;
1723 const dataStreamDatasetIsPrefixUnset = {
1824 type : 'metrics' ,
@@ -37,14 +43,20 @@ test('tests installPackage to use correct priority and index_patterns for data s
3743 packageName : pkg . name ,
3844 } ) ;
3945 // @ts -ignore
40- const sentTemplate = callCluster . mock . calls [ 0 ] [ 1 ] . body ;
46+ const sentTemplate = callCluster . mock . calls [ 1 ] [ 1 ] . body ;
4147 expect ( sentTemplate ) . toBeDefined ( ) ;
4248 expect ( sentTemplate . priority ) . toBe ( templatePriorityDatasetIsPrefixUnset ) ;
4349 expect ( sentTemplate . index_patterns ) . toEqual ( [ templateIndexPatternDatasetIsPrefixUnset ] ) ;
4450} ) ;
4551
4652test ( 'tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to false' , async ( ) => {
4753 const callCluster = elasticsearchServiceMock . createLegacyScopedClusterClient ( ) . callAsCurrentUser ;
54+ callCluster . mockImplementation ( ( _ , params ) => {
55+ if ( params . method === 'GET' && params . path === '/_index_template/metrics-package.dataset' ) {
56+ return { index_templates : [ ] } ;
57+ }
58+ } ) ;
59+
4860 const fields : Field [ ] = [ ] ;
4961 const dataStreamDatasetIsPrefixFalse = {
5062 type : 'metrics' ,
@@ -70,14 +82,20 @@ test('tests installPackage to use correct priority and index_patterns for data s
7082 packageName : pkg . name ,
7183 } ) ;
7284 // @ts -ignore
73- const sentTemplate = callCluster . mock . calls [ 0 ] [ 1 ] . body ;
85+ const sentTemplate = callCluster . mock . calls [ 1 ] [ 1 ] . body ;
7486 expect ( sentTemplate ) . toBeDefined ( ) ;
7587 expect ( sentTemplate . priority ) . toBe ( templatePriorityDatasetIsPrefixFalse ) ;
7688 expect ( sentTemplate . index_patterns ) . toEqual ( [ templateIndexPatternDatasetIsPrefixFalse ] ) ;
7789} ) ;
7890
7991test ( 'tests installPackage to use correct priority and index_patterns for data stream with dataset_is_prefix set to true' , async ( ) => {
8092 const callCluster = elasticsearchServiceMock . createLegacyScopedClusterClient ( ) . callAsCurrentUser ;
93+ callCluster . mockImplementation ( ( _ , params ) => {
94+ if ( params . method === 'GET' && params . path === '/_index_template/metrics-package.dataset' ) {
95+ return { index_templates : [ ] } ;
96+ }
97+ } ) ;
98+
8199 const fields : Field [ ] = [ ] ;
82100 const dataStreamDatasetIsPrefixTrue = {
83101 type : 'metrics' ,
@@ -103,8 +121,60 @@ test('tests installPackage to use correct priority and index_patterns for data s
103121 packageName : pkg . name ,
104122 } ) ;
105123 // @ts -ignore
106- const sentTemplate = callCluster . mock . calls [ 0 ] [ 1 ] . body ;
124+ const sentTemplate = callCluster . mock . calls [ 1 ] [ 1 ] . body ;
107125 expect ( sentTemplate ) . toBeDefined ( ) ;
108126 expect ( sentTemplate . priority ) . toBe ( templatePriorityDatasetIsPrefixTrue ) ;
109127 expect ( sentTemplate . index_patterns ) . toEqual ( [ templateIndexPatternDatasetIsPrefixTrue ] ) ;
110128} ) ;
129+
130+ test ( 'tests installPackage remove the aliases property if the property existed' , async ( ) => {
131+ const callCluster = elasticsearchServiceMock . createLegacyScopedClusterClient ( ) . callAsCurrentUser ;
132+ callCluster . mockImplementation ( ( _ , params ) => {
133+ if ( params . method === 'GET' && params . path === '/_index_template/metrics-package.dataset' ) {
134+ return {
135+ index_templates : [
136+ {
137+ name : 'metrics-package.dataset' ,
138+ index_template : {
139+ index_patterns : [ 'metrics-package.dataset-*' ] ,
140+ template : { aliases : { } } ,
141+ } ,
142+ } ,
143+ ] ,
144+ } ;
145+ }
146+ } ) ;
147+
148+ const fields : Field [ ] = [ ] ;
149+ const dataStreamDatasetIsPrefixUnset = {
150+ type : 'metrics' ,
151+ dataset : 'package.dataset' ,
152+ title : 'test data stream' ,
153+ release : 'experimental' ,
154+ package : 'package' ,
155+ path : 'path' ,
156+ ingest_pipeline : 'default' ,
157+ } as RegistryDataStream ;
158+ const pkg = {
159+ name : 'package' ,
160+ version : '0.0.1' ,
161+ } ;
162+ const templateIndexPatternDatasetIsPrefixUnset = 'metrics-package.dataset-*' ;
163+ const templatePriorityDatasetIsPrefixUnset = 200 ;
164+ await installTemplate ( {
165+ callCluster,
166+ fields,
167+ dataStream : dataStreamDatasetIsPrefixUnset ,
168+ packageVersion : pkg . version ,
169+ packageName : pkg . name ,
170+ } ) ;
171+
172+ // @ts -ignore
173+ const removeAliases = callCluster . mock . calls [ 1 ] [ 1 ] . body ;
174+ expect ( removeAliases . template . aliases ) . not . toBeDefined ( ) ;
175+ // @ts -ignore
176+ const sentTemplate = callCluster . mock . calls [ 2 ] [ 1 ] . body ;
177+ expect ( sentTemplate ) . toBeDefined ( ) ;
178+ expect ( sentTemplate . priority ) . toBe ( templatePriorityDatasetIsPrefixUnset ) ;
179+ expect ( sentTemplate . index_patterns ) . toEqual ( [ templateIndexPatternDatasetIsPrefixUnset ] ) ;
180+ } ) ;
0 commit comments