@@ -9,6 +9,8 @@ import { FtrProviderContext } from '@kbn/ftr-common-functional-services';
9
9
10
10
export const elasticAssetCheckerFactory = ( getService : FtrProviderContext [ 'getService' ] ) => {
11
11
const es = getService ( 'es' ) ;
12
+ const retry = getService ( 'retry' ) ;
13
+ const log = getService ( 'log' ) ;
12
14
13
15
const expectTransformExists = async ( transformId : string ) => {
14
16
return expectTransformStatus ( transformId , true ) ;
@@ -18,45 +20,43 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
18
20
return expectTransformStatus ( transformId , false ) ;
19
21
} ;
20
22
21
- const expectTransformStatus = async (
22
- transformId : string ,
23
- exists : boolean ,
24
- attempts : number = 5 ,
25
- delayMs : number = 2000
26
- ) => {
27
- let currentAttempt = 1 ;
28
- while ( currentAttempt <= attempts ) {
29
- try {
30
- await es . transform . getTransform ( { transform_id : transformId } ) ;
31
- if ( ! exists ) {
32
- throw new Error ( `Expected transform ${ transformId } to not exist, but it does` ) ;
23
+ const expectTransformStatus = async ( transformId : string , exists : boolean ) => {
24
+ await retry . waitForWithTimeout (
25
+ `transform ${ transformId } to ${ exists ? 'exist' : 'not exist' } ` ,
26
+ 10_000 ,
27
+ async ( ) => {
28
+ try {
29
+ await es . transform . getTransform ( { transform_id : transformId } ) ;
30
+ return exists ;
31
+ } catch ( e ) {
32
+ log . debug ( `Transform ${ transformId } not found: ${ e } ` ) ;
33
+ return ! exists ;
33
34
}
34
- return ; // Transform exists, exit the loop
35
- } catch ( e ) {
36
- if ( currentAttempt === attempts ) {
37
- if ( exists ) {
38
- throw new Error ( `Expected transform ${ transformId } to exist, but it does not: ${ e } ` ) ;
39
- } else {
40
- return ; // Transform does not exist, exit the loop
41
- }
42
- }
43
- await new Promise ( ( resolve ) => setTimeout ( resolve , delayMs ) ) ;
44
- currentAttempt ++ ;
45
35
}
46
- }
36
+ ) ;
47
37
} ;
48
38
49
39
const expectEnrichPolicyStatus = async ( policyId : string , exists : boolean ) => {
50
- try {
51
- await es . enrich . getPolicy ( { name : policyId } ) ;
52
- if ( ! exists ) {
53
- throw new Error ( `Expected enrich policy ${ policyId } to not exist, but it does` ) ;
54
- }
55
- } catch ( e ) {
56
- if ( exists ) {
57
- throw new Error ( `Expected enrich policy ${ policyId } to exist, but it does not: ${ e } ` ) ;
40
+ await retry . waitForWithTimeout (
41
+ `enrich policy ${ policyId } to ${ exists ? 'exist' : 'not exist' } ` ,
42
+ 20_000 ,
43
+ async ( ) => {
44
+ try {
45
+ const res = await es . enrich . getPolicy ( { name : policyId } ) ;
46
+ const policy = res . policies ?. [ 0 ] ;
47
+ if ( policy ) {
48
+ log . debug ( `Enrich policy ${ policyId } found: ${ JSON . stringify ( res ) } ` ) ;
49
+ return exists ;
50
+ } else {
51
+ log . debug ( `Enrich policy ${ policyId } not found: ${ JSON . stringify ( res ) } ` ) ;
52
+ return ! exists ;
53
+ }
54
+ } catch ( e ) {
55
+ log . debug ( `Enrich policy ${ policyId } not found: ${ e } ` ) ;
56
+ return ! exists ;
57
+ }
58
58
}
59
- }
59
+ ) ;
60
60
} ;
61
61
62
62
const expectEnrichPolicyExists = async ( policyId : string ) =>
@@ -66,18 +66,19 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
66
66
expectEnrichPolicyStatus ( policyId , false ) ;
67
67
68
68
const expectComponentTemplatStatus = async ( templateName : string , exists : boolean ) => {
69
- try {
70
- await es . cluster . getComponentTemplate ( { name : templateName } ) ;
71
- if ( ! exists ) {
72
- throw new Error ( `Expected component template ${ templateName } to not exist, but it does` ) ;
73
- }
74
- } catch ( e ) {
75
- if ( exists ) {
76
- throw new Error (
77
- `Expected component template ${ templateName } to exist, but it does not: ${ e } `
78
- ) ;
69
+ await retry . waitForWithTimeout (
70
+ `component template ${ templateName } to ${ exists ? 'exist' : 'not exist' } ` ,
71
+ 10_000 ,
72
+ async ( ) => {
73
+ try {
74
+ await es . cluster . getComponentTemplate ( { name : templateName } ) ;
75
+ return exists ; // Component template exists
76
+ } catch ( e ) {
77
+ log . debug ( `Component template ${ templateName } not found: ${ e } ` ) ;
78
+ return ! exists ; // Component template does not exist
79
+ }
79
80
}
80
- }
81
+ ) ;
81
82
} ;
82
83
83
84
const expectComponentTemplateExists = async ( templateName : string ) =>
@@ -87,23 +88,45 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
87
88
expectComponentTemplatStatus ( templateName , false ) ;
88
89
89
90
const expectIngestPipelineStatus = async ( pipelineId : string , exists : boolean ) => {
91
+ await retry . waitForWithTimeout (
92
+ `ingest pipeline ${ pipelineId } to ${ exists ? 'exist' : 'not exist' } ` ,
93
+ 10_000 ,
94
+ async ( ) => {
95
+ try {
96
+ await es . ingest . getPipeline ( { id : pipelineId } ) ;
97
+ return exists ; // Ingest pipeline exists
98
+ } catch ( e ) {
99
+ log . debug ( `Ingest pipeline ${ pipelineId } not found: ${ e } ` ) ;
100
+ return ! exists ; // Ingest pipeline does not exist
101
+ }
102
+ }
103
+ ) ;
104
+ } ;
105
+
106
+ const expectIngestPipelineExists = async ( pipelineId : string ) =>
107
+ expectIngestPipelineStatus ( pipelineId , true ) ;
108
+
109
+ const expectIngestPipelineNotFound = async ( pipelineId : string ) =>
110
+ expectIngestPipelineStatus ( pipelineId , false ) ;
111
+
112
+ const expectIndexStatus = async ( indexName : string , exists : boolean ) => {
90
113
try {
91
- await es . ingest . getPipeline ( { id : pipelineId } ) ;
114
+ await es . indices . get ( { index : indexName } ) ;
92
115
if ( ! exists ) {
93
- throw new Error ( `Expected ingest pipeline ${ pipelineId } to not exist, but it does` ) ;
116
+ throw new Error ( `Expected index ${ indexName } to not exist, but it does` ) ;
94
117
}
95
118
} catch ( e ) {
96
119
if ( exists ) {
97
- throw new Error ( `Expected ingest pipeline ${ pipelineId } to exist, but it does not: ${ e } ` ) ;
120
+ throw new Error ( `Expected index ${ indexName } to exist, but it does not: ${ e } ` ) ;
98
121
}
99
122
}
100
123
} ;
101
124
102
- const expectIngestPipelineExists = async ( pipelineId : string ) =>
103
- expectIngestPipelineStatus ( pipelineId , true ) ;
125
+ const expectEntitiesIndexExists = async ( entityType : string , namespace : string ) =>
126
+ expectIndexStatus ( `.entities.v1.latest.security_ ${ entityType } _ ${ namespace } ` , true ) ;
104
127
105
- const expectIngestPipelineNotFound = async ( pipelineId : string ) =>
106
- expectIngestPipelineStatus ( pipelineId , false ) ;
128
+ const expectEntitiesIndexNotFound = async ( entityType : string , namespace : string ) =>
129
+ expectIndexStatus ( `.entities.v1.latest.security_ ${ entityType } _ ${ namespace } ` , false ) ;
107
130
108
131
return {
109
132
expectComponentTemplateExists,
@@ -112,6 +135,8 @@ export const elasticAssetCheckerFactory = (getService: FtrProviderContext['getSe
112
135
expectEnrichPolicyNotFound,
113
136
expectIngestPipelineExists,
114
137
expectIngestPipelineNotFound,
138
+ expectEntitiesIndexExists,
139
+ expectEntitiesIndexNotFound,
115
140
expectTransformExists,
116
141
expectTransformNotFound,
117
142
} ;
0 commit comments