5
5
6
6
import { SearchResponse } from '../../../opensearch' ;
7
7
import { opensearchClientMock } from '../../../opensearch/client/mocks' ;
8
- import { DYNAMIC_APP_CONFIG_ALIAS } from '../../utils/constants' ;
8
+ import { DYNAMIC_APP_CONFIG_ALIAS , DYNAMIC_APP_CONFIG_INDEX_PREFIX } from '../../utils/constants' ;
9
9
import { OpenSearchConfigStoreClient } from './opensearch_config_store_client' ;
10
10
import { ConfigDocument } from './types' ;
11
11
import _ from 'lodash' ;
12
12
import { ConfigBlob } from '../../types' ;
13
- import { BulkOperationContainer } from '@opensearch-project/opensearch/api/types' ;
13
+ import {
14
+ BulkOperationContainer ,
15
+ CatIndicesResponse ,
16
+ IndicesGetAliasResponse ,
17
+ } from '@opensearch-project/opensearch/api/types' ;
14
18
import { getDynamicConfigIndexName } from '../../utils/utils' ;
15
19
16
20
describe ( 'OpenSearchConfigStoreClient' , ( ) => {
@@ -32,10 +36,12 @@ describe('OpenSearchConfigStoreClient', () => {
32
36
isListConfig : boolean ;
33
37
configDocuments : ConfigDocument [ ] ;
34
38
existsAliasResult : boolean ;
39
+ getAliasIndicesResult : IndicesGetAliasResponse ;
40
+ catIndicesResult : CatIndicesResponse ;
35
41
}
36
42
37
43
/**
38
- * Creates a new OpenSearch client mock complete with a mock for existsAlias() and search() results
44
+ * Creates a new OpenSearch client mock complete with a mock for existsAlias(), cat.indices(), and search() results
39
45
*
40
46
* @param param0
41
47
* @returns
@@ -44,6 +50,8 @@ describe('OpenSearchConfigStoreClient', () => {
44
50
isListConfig,
45
51
configDocuments,
46
52
existsAliasResult,
53
+ getAliasIndicesResult,
54
+ catIndicesResult,
47
55
} : OpenSearchClientMockProps ) => {
48
56
const mockClient = opensearchClientMock . createOpenSearchClient ( ) ;
49
57
@@ -53,6 +61,18 @@ describe('OpenSearchConfigStoreClient', () => {
53
61
} )
54
62
) ;
55
63
64
+ mockClient . cat . indices . mockResolvedValue (
65
+ opensearchClientMock . createApiResponse < CatIndicesResponse > ( {
66
+ body : catIndicesResult ,
67
+ } )
68
+ ) ;
69
+
70
+ mockClient . indices . getAlias . mockResolvedValue (
71
+ opensearchClientMock . createApiResponse < IndicesGetAliasResponse > ( {
72
+ body : getAliasIndicesResult ,
73
+ } )
74
+ ) ;
75
+
56
76
// @ts -expect-error
57
77
mockClient . search . mockImplementation ( ( request , options ) => {
58
78
// Filters out results when the request is for getting/bulk getting configs
@@ -83,6 +103,49 @@ describe('OpenSearchConfigStoreClient', () => {
83
103
return mockClient ;
84
104
} ;
85
105
106
+ const noDynamicConfigIndexResults : CatIndicesResponse = [
107
+ {
108
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _` ,
109
+ } ,
110
+ {
111
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _foo` ,
112
+ } ,
113
+ {
114
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _foo_2` ,
115
+ } ,
116
+ ] ;
117
+
118
+ const oneDynamicConfigIndexResult : CatIndicesResponse = [
119
+ {
120
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _1` ,
121
+ } ,
122
+ ] ;
123
+
124
+ const multipleDynamicConfigIndexResults : CatIndicesResponse = [
125
+ {
126
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _2` ,
127
+ } ,
128
+ {
129
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _4` ,
130
+ } ,
131
+ {
132
+ index : `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _800` ,
133
+ } ,
134
+ ] ;
135
+
136
+ const validAliasIndicesResponse : IndicesGetAliasResponse = {
137
+ [ `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _4` ] : { aliases : { DYNAMIC_APP_CONFIG_ALIAS : { } } } ,
138
+ } ;
139
+
140
+ const multipleAliasIndicesResponse : IndicesGetAliasResponse = {
141
+ [ `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _4` ] : { aliases : { DYNAMIC_APP_CONFIG_ALIAS : { } } } ,
142
+ [ `${ DYNAMIC_APP_CONFIG_INDEX_PREFIX } _2` ] : { aliases : { DYNAMIC_APP_CONFIG_ALIAS : { } } } ,
143
+ } ;
144
+
145
+ const invalidAliasIndicesResponse : IndicesGetAliasResponse = {
146
+ [ `.some_random_index_8` ] : { aliases : { DYNAMIC_APP_CONFIG_ALIAS : { } } } ,
147
+ } ;
148
+
86
149
const configDocument : ConfigDocument = {
87
150
config_name : 'some_config_name' ,
88
151
config_blob : {
@@ -143,26 +206,88 @@ describe('OpenSearchConfigStoreClient', () => {
143
206
it . each ( [
144
207
{
145
208
existsAliasResult : false ,
209
+ catIndicesResult : noDynamicConfigIndexResults ,
210
+ getAliasIndicesResult : validAliasIndicesResponse ,
146
211
numCreateCalls : 1 ,
212
+ numUpdateCalls : 0 ,
213
+ errorThrown : false ,
214
+ } ,
215
+ {
216
+ existsAliasResult : false ,
217
+ catIndicesResult : multipleDynamicConfigIndexResults ,
218
+ getAliasIndicesResult : validAliasIndicesResponse ,
219
+ numCreateCalls : 0 ,
220
+ numUpdateCalls : 1 ,
221
+ errorThrown : false ,
222
+ } ,
223
+ {
224
+ existsAliasResult : false ,
225
+ catIndicesResult : oneDynamicConfigIndexResult ,
226
+ getAliasIndicesResult : validAliasIndicesResponse ,
227
+ numCreateCalls : 0 ,
228
+ numUpdateCalls : 1 ,
229
+ errorThrown : false ,
147
230
} ,
148
231
{
149
232
existsAliasResult : true ,
233
+ catIndicesResult : multipleDynamicConfigIndexResults ,
234
+ getAliasIndicesResult : { } ,
150
235
numCreateCalls : 0 ,
236
+ numUpdateCalls : 0 ,
237
+ errorThrown : true ,
238
+ } ,
239
+ {
240
+ existsAliasResult : true ,
241
+ catIndicesResult : multipleDynamicConfigIndexResults ,
242
+ getAliasIndicesResult : multipleAliasIndicesResponse ,
243
+ numCreateCalls : 0 ,
244
+ numUpdateCalls : 0 ,
245
+ errorThrown : true ,
246
+ } ,
247
+ {
248
+ existsAliasResult : true ,
249
+ catIndicesResult : multipleDynamicConfigIndexResults ,
250
+ getAliasIndicesResult : invalidAliasIndicesResponse ,
251
+ numCreateCalls : 0 ,
252
+ numUpdateCalls : 0 ,
253
+ errorThrown : true ,
254
+ } ,
255
+ {
256
+ existsAliasResult : true ,
257
+ catIndicesResult : multipleDynamicConfigIndexResults ,
258
+ getAliasIndicesResult : validAliasIndicesResponse ,
259
+ numCreateCalls : 0 ,
260
+ numUpdateCalls : 0 ,
261
+ errorThrown : false ,
151
262
} ,
152
263
] ) (
153
- 'should create config index $numCreateCalls times when existsAlias() is $existsAliasResult' ,
154
- async ( { existsAliasResult, numCreateCalls } ) => {
264
+ 'should throw error should be $errorThrown, create() should be called $numCreateCalls times, and update() should be called $numUpdateCalls times' ,
265
+ async ( {
266
+ existsAliasResult,
267
+ catIndicesResult,
268
+ getAliasIndicesResult,
269
+ numCreateCalls,
270
+ numUpdateCalls,
271
+ errorThrown,
272
+ } ) => {
155
273
const mockClient = createOpenSearchClientMock ( {
156
274
isListConfig : false ,
157
275
configDocuments : [ ] ,
158
276
existsAliasResult,
277
+ getAliasIndicesResult,
278
+ catIndicesResult,
159
279
} ) ;
160
280
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
161
- await configStoreClient . createDynamicConfigIndex ( ) ;
281
+
282
+ if ( errorThrown ) {
283
+ expect ( configStoreClient . createDynamicConfigIndex ( ) ) . rejects . toThrowError ( ) ;
284
+ } else {
285
+ await configStoreClient . createDynamicConfigIndex ( ) ;
286
+ }
162
287
163
288
expect ( mockClient . indices . existsAlias ) . toBeCalled ( ) ;
164
289
expect ( mockClient . indices . create ) . toBeCalledTimes ( numCreateCalls ) ;
165
- expect ( mockClient . indices . updateAliases ) . toBeCalledTimes ( numCreateCalls ) ;
290
+ expect ( mockClient . indices . updateAliases ) . toBeCalledTimes ( numUpdateCalls ) ;
166
291
}
167
292
) ;
168
293
} ) ;
@@ -175,6 +300,8 @@ describe('OpenSearchConfigStoreClient', () => {
175
300
isListConfig : false ,
176
301
configDocuments : [ configDocument ] ,
177
302
existsAliasResult : false ,
303
+ catIndicesResult : oneDynamicConfigIndexResult ,
304
+ getAliasIndicesResult : validAliasIndicesResponse ,
178
305
} ) ;
179
306
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
180
307
@@ -210,6 +337,8 @@ describe('OpenSearchConfigStoreClient', () => {
210
337
isListConfig : false ,
211
338
configDocuments,
212
339
existsAliasResult : false ,
340
+ catIndicesResult : oneDynamicConfigIndexResult ,
341
+ getAliasIndicesResult : validAliasIndicesResponse ,
213
342
} ) ;
214
343
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
215
344
@@ -274,6 +403,8 @@ describe('OpenSearchConfigStoreClient', () => {
274
403
isListConfig : true ,
275
404
configDocuments : allConfigDocuments ,
276
405
existsAliasResult : false ,
406
+ catIndicesResult : oneDynamicConfigIndexResult ,
407
+ getAliasIndicesResult : validAliasIndicesResponse ,
277
408
} ) ;
278
409
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
279
410
const actualMap = await configStoreClient . listConfigs ( ) ;
@@ -342,6 +473,8 @@ describe('OpenSearchConfigStoreClient', () => {
342
473
isListConfig : false ,
343
474
configDocuments : newConfigDocuments ,
344
475
existsAliasResult : false ,
476
+ catIndicesResult : oneDynamicConfigIndexResult ,
477
+ getAliasIndicesResult : validAliasIndicesResponse ,
345
478
} ) ;
346
479
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
347
480
await configStoreClient . createConfig ( {
@@ -540,6 +673,8 @@ describe('OpenSearchConfigStoreClient', () => {
540
673
isListConfig : false ,
541
674
configDocuments : existingConfigs ,
542
675
existsAliasResult : false ,
676
+ catIndicesResult : oneDynamicConfigIndexResult ,
677
+ getAliasIndicesResult : validAliasIndicesResponse ,
543
678
} ) ;
544
679
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
545
680
await configStoreClient . bulkCreateConfigs ( {
@@ -565,6 +700,8 @@ describe('OpenSearchConfigStoreClient', () => {
565
700
isListConfig : false ,
566
701
configDocuments : [ ] ,
567
702
existsAliasResult : false ,
703
+ catIndicesResult : oneDynamicConfigIndexResult ,
704
+ getAliasIndicesResult : validAliasIndicesResponse ,
568
705
} ) ;
569
706
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
570
707
await configStoreClient . deleteConfig ( { name : 'some_config_name' } ) ;
@@ -604,6 +741,8 @@ describe('OpenSearchConfigStoreClient', () => {
604
741
isListConfig : false ,
605
742
configDocuments : [ ] ,
606
743
existsAliasResult : false ,
744
+ catIndicesResult : oneDynamicConfigIndexResult ,
745
+ getAliasIndicesResult : validAliasIndicesResponse ,
607
746
} ) ;
608
747
const configStoreClient = new OpenSearchConfigStoreClient ( mockClient ) ;
609
748
await configStoreClient . bulkDeleteConfigs ( {
0 commit comments