Skip to content

Commit dea1631

Browse files
delvedorgithub-actions[bot]
authored andcommitted
Updated integration test cleanup code (#1356)
1 parent e9ad12b commit dea1631

File tree

2 files changed

+138
-126
lines changed

2 files changed

+138
-126
lines changed

test/integration/helper.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,45 @@ function to (promise) {
4949

5050
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
5151

52-
module.exports = { runInParallel, delve, to, sleep }
52+
function isXPackTemplate (name) {
53+
if (name.startsWith('.monitoring-')) {
54+
return true
55+
}
56+
if (name.startsWith('.watch') || name.startsWith('.triggered_watches')) {
57+
return true
58+
}
59+
if (name.startsWith('.data-frame-')) {
60+
return true
61+
}
62+
if (name.startsWith('.ml-')) {
63+
return true
64+
}
65+
if (name.startsWith('.transform-')) {
66+
return true
67+
}
68+
switch (name) {
69+
case '.watches':
70+
case 'logstash-index-template':
71+
case '.logstash-management':
72+
case 'security_audit_log':
73+
case '.slm-history':
74+
case '.async-search':
75+
case 'saml-service-provider':
76+
case 'ilm-history':
77+
case 'logs':
78+
case 'logs-settings':
79+
case 'logs-mappings':
80+
case 'metrics':
81+
case 'metrics-settings':
82+
case 'metrics-mappings':
83+
case 'synthetics':
84+
case 'synthetics-settings':
85+
case 'synthetics-mappings':
86+
case '.snapshot-blob-cache':
87+
case '.deprecation-indexing-template':
88+
return true
89+
}
90+
return false
91+
}
92+
93+
module.exports = { runInParallel, delve, to, sleep, isXPackTemplate }

test/integration/test-runner.js

Lines changed: 96 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const helper = require('./helper')
2727
const deepEqual = require('fast-deep-equal')
2828
const { ConfigurationError } = require('../../lib/errors')
2929

30-
const { delve, to } = helper
30+
const { delve, to, isXPackTemplate, sleep } = helper
3131

3232
const supportedFeatures = [
3333
'gtelte',
@@ -52,135 +52,115 @@ function build (opts = {}) {
5252
* Runs a cleanup, removes all indices, aliases, templates, and snapshots
5353
* @returns {Promise}
5454
*/
55-
async function cleanup () {
55+
async function cleanup (isXPack) {
5656
response = null
5757
stash.clear()
5858

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] })
6681
}
6782

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] })
7588
}
7689

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: '*' })
8193
}
8294

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+
}
88107
}
89-
} catch (err) {
90-
assert.ifError(err, 'should not error: snapshot.delete / snapshot.deleteRepository')
91-
}
92-
}
93108

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: '*' })
101118

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: '*' })
112121

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: '*' })
122124
}
123125

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+
}
138135
}
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 })
150138
}
151139

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+
}
163152

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+
}
178158

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)
182162
.reduce((acc, node) => {
183-
const { tasks } = body.nodes[node]
163+
const { tasks } = nodesTask.nodes[node]
184164
Object.keys(tasks).forEach(id => {
185165
if (tasks[id].cancellable) acc.push(id)
186166
})
@@ -191,21 +171,14 @@ function build (opts = {}) {
191171
client, 'tasks.cancel',
192172
tasks.map(id => ({ taskId: id }))
193173
)
194-
} catch (err) {
195-
assert.ifError(err, 'should not error: tasks cleanup')
196174
}
197175

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)
209182
}
210183
}
211184

@@ -253,9 +226,7 @@ function build (opts = {}) {
253226

254227
if (teardown) await exec('Teardown', teardown, stats, junit)
255228

256-
if (isXPack) await cleanupXPack()
257-
258-
await cleanup()
229+
await cleanup(isXPack)
259230
}
260231

261232
/**

0 commit comments

Comments
 (0)