@@ -27,7 +27,7 @@ const helper = require('./helper')
27
27
const deepEqual = require ( 'fast-deep-equal' )
28
28
const { ConfigurationError } = require ( '../../lib/errors' )
29
29
30
- const { delve, to } = helper
30
+ const { delve, to, isXPackTemplate , sleep } = helper
31
31
32
32
const supportedFeatures = [
33
33
'gtelte' ,
@@ -52,135 +52,115 @@ function build (opts = {}) {
52
52
* Runs a cleanup, removes all indices, aliases, templates, and snapshots
53
53
* @returns {Promise }
54
54
*/
55
- async function cleanup ( ) {
55
+ async function cleanup ( isXPack ) {
56
56
response = null
57
57
stash . clear ( )
58
58
59
- try {
60
- await client . indices . deleteAlias ( {
61
- index : '_all' ,
62
- name : '_all'
63
- } , { ignore : [ 404 ] } )
64
- } catch ( err ) {
65
- assert . ifError ( err , 'should not error: indices.deleteAlias' )
59
+ if ( isXPack ) {
60
+ // wipe rollup jobs
61
+ const { body : jobsList } = await client . rollup . getJobs ( { id : '_all' } )
62
+ const jobsIds = jobsList . jobs . map ( j => j . config . id )
63
+ await helper . runInParallel (
64
+ client , 'rollup.stopJob' ,
65
+ jobsIds . map ( j => ( { id : j , waitForCompletion : true } ) )
66
+ )
67
+ await helper . runInParallel (
68
+ client , 'rollup.deleteJob' ,
69
+ jobsIds . map ( j => ( { id : j } ) )
70
+ )
71
+
72
+ // delete slm policies
73
+ const { body : policies } = await client . slm . getLifecycle ( )
74
+ await helper . runInParallel (
75
+ client , 'slm.deleteLifecycle' ,
76
+ Object . keys ( policies ) . map ( p => ( { policy_id : p } ) )
77
+ )
78
+
79
+ // remove 'x_pack_rest_user', used in some xpack test
80
+ await client . security . deleteUser ( { username : 'x_pack_rest_user' } , { ignore : [ 404 ] } )
66
81
}
67
82
68
- try {
69
- await client . indices . delete ( {
70
- index : '_all' ,
71
- expand_wildcards : 'open,closed,hidden'
72
- } , { ignore : [ 404 ] } )
73
- } catch ( err ) {
74
- assert . ifError ( err , 'should not error: indices.delete' )
83
+ // clean snapshots
84
+ const { body : repositories } = await client . snapshot . getRepository ( )
85
+ for ( const repository of Object . keys ( repositories ) ) {
86
+ await client . snapshot . delete ( { repository, snapshot : '*' } , { ignore : [ 404 ] } )
87
+ await client . snapshot . deleteRepository ( { repository } , { ignore : [ 404 ] } )
75
88
}
76
89
77
- try {
78
- await client . indices . deleteTemplate ( { name : '*' } )
79
- } catch ( err ) {
80
- assert . ifError ( err , 'should not error: indices.deleteTemplate' )
90
+ if ( isXPack ) {
91
+ // clean data streams
92
+ await client . indices . deleteDataStream ( { name : '*' } )
81
93
}
82
94
83
- try {
84
- const { body : repositories } = await client . snapshot . getRepository ( )
85
- for ( const repository of Object . keys ( repositories ) ) {
86
- await client . snapshot . delete ( { repository, snapshot : '*' } , { ignore : [ 404 ] } )
87
- await client . snapshot . deleteRepository ( { repository } , { ignore : [ 404 ] } )
95
+ // clean all indices
96
+ await client . indices . delete ( { index : '*,-.ds-ilm-history-*' , expand_wildcards : 'open,closed,hidden' } , { ignore : [ 404 ] } )
97
+
98
+ if ( isXPack ) {
99
+ // delete templates
100
+ const { body : templates } = await client . cat . templates ( { h : 'name' } )
101
+ for ( const template of templates . split ( '\n' ) . filter ( Boolean ) ) {
102
+ if ( isXPackTemplate ( template ) ) continue
103
+ const { body } = await client . indices . deleteTemplate ( { name : template } , { ignore : [ 404 ] } )
104
+ if ( JSON . stringify ( body ) . includes ( `index_template [${ template } ] missing` ) ) {
105
+ await client . indices . deleteIndexTemplate ( { name : template } , { ignore : [ 404 ] } )
106
+ }
88
107
}
89
- } catch ( err ) {
90
- assert . ifError ( err , 'should not error: snapshot.delete / snapshot.deleteRepository' )
91
- }
92
- }
93
108
94
- /**
95
- * Runs some additional API calls to prepare ES for the xpack test,
96
- * This set of calls should be executed before the final clenup.
97
- * @returns {Promise }
98
- */
99
- async function cleanupXPack ( ) {
100
- // tap.comment('XPack Cleanup')
109
+ // delete component template
110
+ const { body } = await client . cluster . getComponentTemplate ( )
111
+ const components = body . component_templates . filter ( c => ! isXPackTemplate ( c . name ) ) . map ( c => c . name )
112
+ if ( components . length > 0 ) {
113
+ await client . cluster . deleteComponentTemplate ( { name : components . join ( ',' ) } , { ignore : [ 404 ] } )
114
+ }
115
+ } else {
116
+ // clean all templates
117
+ await client . indices . deleteTemplate ( { name : '*' } )
101
118
102
- try {
103
- const { body } = await client . security . getRole ( )
104
- const roles = Object . keys ( body ) . filter ( n => ! body [ n ] . metadata . _reserved )
105
- await helper . runInParallel (
106
- client , 'security.deleteRole' ,
107
- roles . map ( r => ( { name : r } ) )
108
- )
109
- } catch ( err ) {
110
- assert . ifError ( err , 'should not error: security role cleanup' )
111
- }
119
+ // clean all templates
120
+ await client . indices . deleteIndexTemplate ( { name : '*' } )
112
121
113
- try {
114
- const { body } = await client . security . getUser ( )
115
- const users = Object . keys ( body ) . filter ( n => ! body [ n ] . metadata . _reserved )
116
- await helper . runInParallel (
117
- client , 'security.deleteUser' ,
118
- users . map ( r => ( { username : r } ) )
119
- )
120
- } catch ( err ) {
121
- assert . ifError ( err , 'should not error: security user cleanup' )
122
+ // clean all templates
123
+ await client . cluster . deleteComponentTemplate ( { name : '*' } )
122
124
}
123
125
124
- try {
125
- const { body } = await client . security . getPrivileges ( )
126
- const privileges = [ ]
127
- Object . keys ( body ) . forEach ( app => {
128
- Object . keys ( body [ app ] ) . forEach ( priv => {
129
- privileges . push ( {
130
- name : body [ app ] [ priv ] . name ,
131
- application : body [ app ] [ priv ] . application
132
- } )
133
- } )
134
- } )
135
- await helper . runInParallel ( client , 'security.deletePrivileges' , privileges )
136
- } catch ( err ) {
137
- assert . ifError ( err , 'should not error: security privileges cleanup' )
126
+ // Remove any cluster setting
127
+ const { body : settings } = await client . cluster . getSettings ( )
128
+ const newSettings = { }
129
+ for ( const setting in settings ) {
130
+ if ( Object . keys ( settings [ setting ] ) . length === 0 ) continue
131
+ newSettings [ setting ] = { }
132
+ for ( const key in settings [ setting ] ) {
133
+ newSettings [ setting ] [ `${ key } .*` ] = null
134
+ }
138
135
}
139
-
140
- try {
141
- await client . ml . stopDatafeed ( { datafeedId : '*' , force : true } )
142
- const { body } = await client . ml . getDatafeeds ( { datafeedId : '*' } )
143
- const feeds = body . datafeeds . map ( f => f . datafeed_id )
144
- await helper . runInParallel (
145
- client , 'ml.deleteDatafeed' ,
146
- feeds . map ( f => ( { datafeedId : f } ) )
147
- )
148
- } catch ( err ) {
149
- assert . ifError ( err , 'should error: not ml datafeed cleanup' )
136
+ if ( Object . keys ( newSettings ) . length > 0 ) {
137
+ await client . cluster . putSettings ( { body : newSettings } )
150
138
}
151
139
152
- try {
153
- await client . ml . closeJob ( { jobId : '*' , force : true } )
154
- const { body } = await client . ml . getJobs ( { jobId : '*' } )
155
- const jobs = body . jobs . map ( j => j . job_id )
156
- await helper . runInParallel (
157
- client , 'ml.deleteJob' ,
158
- jobs . map ( j => ( { jobId : j , waitForCompletion : true , force : true } ) )
159
- )
160
- } catch ( err ) {
161
- assert . ifError ( err , 'should not error: ml job cleanup' )
162
- }
140
+ if ( isXPack ) {
141
+ // delete ilm policies
142
+ const preserveIlmPolicies = [
143
+ 'ilm-history-ilm-policy' , 'slm-history-ilm-policy' ,
144
+ 'watch-history-ilm-policy' , 'ml-size-based-ilm-policy' ,
145
+ 'logs' , 'metrics'
146
+ ]
147
+ const { body : policies } = await client . ilm . getLifecycle ( )
148
+ for ( const policy in policies ) {
149
+ if ( preserveIlmPolicies . includes ( policy ) ) continue
150
+ await client . ilm . deleteLifecycle ( { policy } )
151
+ }
163
152
164
- try {
165
- const { body } = await client . rollup . getJobs ( { id : '_all' } )
166
- const jobs = body . jobs . map ( j => j . config . id )
167
- await helper . runInParallel (
168
- client , 'rollup.stopJob' ,
169
- jobs . map ( j => ( { id : j , waitForCompletion : true } ) )
170
- )
171
- await helper . runInParallel (
172
- client , 'rollup.deleteJob' ,
173
- jobs . map ( j => ( { id : j } ) )
174
- )
175
- } catch ( err ) {
176
- assert . ifError ( err , 'should not error: rollup jobs cleanup' )
177
- }
153
+ // delete autofollow patterns
154
+ const { body : patterns } = await client . ccr . getAutoFollowPattern ( )
155
+ for ( const { name } of patterns . patterns ) {
156
+ await client . ccr . deleteAutoFollowPattern ( { name } )
157
+ }
178
158
179
- try {
180
- const { body } = await client . tasks . list ( )
181
- const tasks = Object . keys ( body . nodes )
159
+ // delete all tasks
160
+ const { body : nodesTask } = await client . tasks . list ( )
161
+ const tasks = Object . keys ( nodesTask . nodes )
182
162
. reduce ( ( acc , node ) => {
183
- const { tasks } = body . nodes [ node ]
163
+ const { tasks } = nodesTask . nodes [ node ]
184
164
Object . keys ( tasks ) . forEach ( id => {
185
165
if ( tasks [ id ] . cancellable ) acc . push ( id )
186
166
} )
@@ -191,21 +171,14 @@ function build (opts = {}) {
191
171
client , 'tasks.cancel' ,
192
172
tasks . map ( id => ( { taskId : id } ) )
193
173
)
194
- } catch ( err ) {
195
- assert . ifError ( err , 'should not error: tasks cleanup' )
196
174
}
197
175
198
- try {
199
- await client . ilm . removePolicy ( { index : '_all' } )
200
- } catch ( err ) {
201
- assert . ifError ( err , 'should not error: ilm.removePolicy' )
202
- }
203
-
204
- // refresh the all indexes
205
- try {
206
- await client . indices . refresh ( { index : '_all' } )
207
- } catch ( err ) {
208
- assert . ifError ( err , 'should not error: indices.refresh' )
176
+ // wait for pending task before resolving the promise
177
+ await sleep ( 100 )
178
+ while ( true ) {
179
+ const { body } = await client . cluster . pendingTasks ( )
180
+ if ( body . tasks . length === 0 ) break
181
+ await sleep ( 500 )
209
182
}
210
183
}
211
184
@@ -253,9 +226,7 @@ function build (opts = {}) {
253
226
254
227
if ( teardown ) await exec ( 'Teardown' , teardown , stats , junit )
255
228
256
- if ( isXPack ) await cleanupXPack ( )
257
-
258
- await cleanup ( )
229
+ await cleanup ( isXPack )
259
230
}
260
231
261
232
/**
0 commit comments