Skip to content

Commit 4906f6d

Browse files
authored
Fix yaml tests not deleting newly defined hidden indices (#4447)
* Fix yaml tests not deleting newly defined hidden indices Before we were not deleting system indices only `.ml-*` However this started to become an issue because it left closed indices around. This might be related to .ml indices now being hidden. Warrants more investigation as the new solution deleting all indices all the time is definitely a bit slower * update refresh and delete to include hidden indices
1 parent 3eeeffb commit 4906f6d

File tree

4 files changed

+160
-159
lines changed

4 files changed

+160
-159
lines changed

tests/Tests.YamlRunner/Commands.fs

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ open ShellProgressBar
55
open Tests.YamlRunner.AsyncExtensions
66
open Tests.YamlRunner.TestsLocator
77
open Tests.YamlRunner.TestsReader
8-
open Elasticsearch.Net
98
open Tests.YamlRunner
109

1110
let private barOptions =
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
module Tests.YamlRunner.TestSuiteBootstrap
2+
3+
open System
4+
open System.Linq
5+
6+
open Elasticsearch.Net
7+
open Elasticsearch.Net.Specification.CatApi
8+
open Elasticsearch.Net.Specification.ClusterApi
9+
open Elasticsearch.Net.Specification.IndicesApi
10+
open Tests.YamlRunner.Models
11+
12+
let DefaultSetup : Operation list = [Actions("Setup", fun (client, suite) ->
13+
let firstFailure (responses:DynamicResponse seq) =
14+
responses
15+
|> Seq.filter (fun r -> not r.Success && r.HttpStatusCode <> Nullable.op_Implicit 404)
16+
|> Seq.tryHead
17+
18+
match suite with
19+
| Oss ->
20+
let deleteAll = client.Indices.Delete<DynamicResponse>("*")
21+
let templates =
22+
client.Cat.Templates<StringResponse>("*", CatTemplatesRequestParameters(Headers=["name"].ToArray()))
23+
.Body.Split("\n")
24+
|> Seq.filter(fun f -> not(String.IsNullOrWhiteSpace(f)) && not(f.StartsWith(".")) && f <> "security-audit-log")
25+
//TODO template does not accept comma separated list but is documented as such
26+
|> Seq.map(fun template -> client.Indices.DeleteTemplateForAll<DynamicResponse>(template))
27+
|> Seq.toList
28+
firstFailure <| [deleteAll] @ templates
29+
30+
| XPack ->
31+
firstFailure <| seq {
32+
//delete all templates
33+
let templates =
34+
client.Cat.Templates<StringResponse>("*", CatTemplatesRequestParameters(Headers=["name"].ToArray()))
35+
.Body.Split("\n")
36+
|> Seq.filter(fun f -> not(String.IsNullOrWhiteSpace(f)) && not(f.StartsWith(".")) && f <> "security-audit-log")
37+
//TODO template does not accept comma separated list but is documented as such
38+
|> Seq.map(fun template -> client.Indices.DeleteTemplateForAll<DynamicResponse>(template))
39+
40+
yield! templates
41+
42+
yield client.Watcher.Delete<DynamicResponse>("my_watch")
43+
44+
let deleteNonReserved (setup:_ -> DynamicResponse) (delete:(_ -> DynamicResponse)) =
45+
setup().Dictionary.GetKeyValues()
46+
|> Seq.map (fun kv ->
47+
match kv.Value.Get<bool> "metadata._reserved" with
48+
| false -> Some <| delete(kv.Key)
49+
| _ -> None
50+
)
51+
|> Seq.choose id
52+
|> Seq.toList
53+
54+
yield! //roles
55+
deleteNonReserved
56+
(fun _ -> client.Security.GetRole<DynamicResponse>())
57+
(fun role -> client.Security.DeleteRole<DynamicResponse> role)
58+
59+
yield! //users
60+
deleteNonReserved
61+
(fun _ -> client.Security.GetUser<DynamicResponse>())
62+
(fun user -> client.Security.DeleteUser<DynamicResponse> user)
63+
64+
yield! //privileges
65+
deleteNonReserved
66+
(fun _ -> client.Security.GetPrivileges<DynamicResponse>())
67+
(fun priv -> client.Security.DeletePrivileges<DynamicResponse>(priv, "_all"))
68+
69+
// deleting feeds before jobs is important
70+
let mlDataFeeds =
71+
let stopFeeds = client.MachineLearning.StopDatafeed<DynamicResponse>("_all")
72+
let getFeeds = client.MachineLearning.GetDatafeeds<DynamicResponse> ()
73+
let deleteFeeds =
74+
getFeeds.Get<string[]> "datafeeds.datafeed_id"
75+
|> Seq.map (fun jobId -> client.MachineLearning.DeleteDatafeed<DynamicResponse>(jobId))
76+
|> Seq.toList
77+
[stopFeeds; getFeeds] @ deleteFeeds
78+
yield! mlDataFeeds
79+
80+
yield client.IndexLifecycleManagement.RemovePolicy<DynamicResponse>("_all")
81+
82+
let mlJobs =
83+
let closeJobs = client.MachineLearning.CloseJob<DynamicResponse>("_all", PostData.Empty)
84+
let getJobs = client.MachineLearning.GetJobs<DynamicResponse> "_all"
85+
let deleteJobs =
86+
getJobs.Get<string[]> "jobs.job_id"
87+
|> Seq.map (fun jobId -> client.MachineLearning.DeleteJob<DynamicResponse>(jobId))
88+
|> Seq.toList
89+
[closeJobs; getJobs] @ deleteJobs
90+
yield! mlJobs
91+
92+
let rollupJobs =
93+
let getJobs = client.Rollup.GetJob<DynamicResponse> "_all"
94+
let deleteJobs =
95+
getJobs.Get<string[]> "jobs.config.id"
96+
|> Seq.collect (fun jobId -> [
97+
client.Rollup.StopJob<DynamicResponse>(jobId)
98+
client.Rollup.DeleteJob<DynamicResponse>(jobId)
99+
])
100+
|> Seq.toList
101+
[getJobs] @ deleteJobs
102+
yield! rollupJobs
103+
104+
let tasks =
105+
let getJobs = client.Tasks.List<DynamicResponse> ()
106+
let cancelJobs =
107+
let dict = getJobs.Get<DynamicDictionary> "nodes"
108+
dict.GetKeyValues()
109+
|> Seq.collect(fun kv ->
110+
let dict = kv.Value.Get<DynamicDictionary> "tasks"
111+
dict.GetKeyValues()
112+
)
113+
|> Seq.map (fun kv ->
114+
match kv.Value.Get<bool> "cancellable" with
115+
| true -> Some <| client.Tasks.Cancel<DynamicResponse>(kv.Key)
116+
| _ -> None
117+
)
118+
|> Seq.choose id
119+
|> Seq.toList
120+
121+
[getJobs] @ cancelJobs
122+
yield! tasks
123+
124+
let transforms =
125+
let transforms = client.Transform.Get<DynamicResponse> "_all"
126+
let stopTransforms =
127+
transforms.Get<string[]> "transforms.id"
128+
|> Seq.collect (fun id -> [
129+
client.Transform.Stop<DynamicResponse> id
130+
client.Transform.Delete<DynamicResponse> id
131+
])
132+
|> Seq.toList
133+
[transforms] @ stopTransforms
134+
yield! transforms
135+
136+
let yellowStatus = Nullable.op_Implicit WaitForStatus.Yellow
137+
yield client.Cluster.Health<DynamicResponse>(ClusterHealthRequestParameters(WaitForStatus=yellowStatus))
138+
139+
let indices =
140+
let dp = DeleteIndexRequestParameters()
141+
dp.SetQueryString("expand_wildcards", "open,closed,hidden")
142+
client.Indices.Delete<DynamicResponse>("*", dp)
143+
yield indices
144+
145+
let data = PostData.String @"{""password"":""x-pack-test-password"", ""roles"":[""superuser""]}"
146+
yield client.Security.PutUser<DynamicResponse>("x_pack_rest_user", data)
147+
148+
let refreshAll =
149+
let rp = RefreshRequestParameters()
150+
rp.SetQueryString("expand_wildcards", "open,closed,hidden")
151+
client.Indices.Refresh<DynamicResponse>( "_all", rp)
152+
153+
yield refreshAll
154+
155+
yield client.Cluster.Health<DynamicResponse>(ClusterHealthRequestParameters(WaitForStatus=yellowStatus))
156+
}
157+
)]
158+

tests/Tests.YamlRunner/Tests.YamlRunner.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<Compile Include="TestsDownloader.fs" />
2222
<Compile Include="TestsLocator.fs" />
2323
<Compile Include="OperationExecutor.fs" />
24+
<Compile Include="TestSuiteBootstrap.fs" />
2425
<Compile Include="TestsReader.fs" />
2526
<Compile Include="TestsRunner.fs" />
2627
<Compile Include="TestsExporter.fs" />

tests/Tests.YamlRunner/TestsReader.fs

+1-158
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,10 @@ open System.Text.RegularExpressions
66
open System.Linq
77

88
open System.Collections.Specialized
9-
open Elasticsearch.Net
10-
open Elasticsearch.Net
11-
open Elasticsearch.Net.Specification.CatApi
12-
open Elasticsearch.Net.Specification.ClusterApi
13-
open Elasticsearch.Net.Specification.MachineLearningApi
149
open System.IO
15-
open Tests.YamlRunner
16-
open Tests.YamlRunner.Models
1710
open Tests.YamlRunner.Models
1811
open Tests.YamlRunner.TestsLocator
1912

20-
2113
let private tryPick<'a> (map:YamlMap) key =
2214
let found, value = map.TryGetValue key
2315
if (found) then
@@ -211,160 +203,11 @@ type YamlTestDocument = {
211203
Tests: YamlTest list
212204
}
213205

214-
let private DefaultSetup : Operation list = [Actions("Setup", fun (client, suite) ->
215-
let firstFailure (responses:DynamicResponse seq) =
216-
responses
217-
|> Seq.filter (fun r -> not r.Success && r.HttpStatusCode <> Nullable.op_Implicit 404)
218-
|> Seq.tryHead
219-
220-
match suite with
221-
| Oss ->
222-
let deleteAll = client.Indices.Delete<DynamicResponse>("*")
223-
let templates =
224-
client.Cat.Templates<StringResponse>("*", CatTemplatesRequestParameters(Headers=["name"].ToArray()))
225-
.Body.Split("\n")
226-
|> Seq.filter(fun f -> not(String.IsNullOrWhiteSpace(f)) && not(f.StartsWith(".")) && f <> "security-audit-log")
227-
//TODO template does not accept comma separated list but is documented as such
228-
|> Seq.map(fun template -> client.Indices.DeleteTemplateForAll<DynamicResponse>(template))
229-
|> Seq.toList
230-
firstFailure <| [deleteAll] @ templates
231-
232-
| XPack ->
233-
firstFailure <| seq {
234-
//delete all templates
235-
let templates =
236-
client.Cat.Templates<StringResponse>("*", CatTemplatesRequestParameters(Headers=["name"].ToArray()))
237-
.Body.Split("\n")
238-
|> Seq.filter(fun f -> not(String.IsNullOrWhiteSpace(f)) && not(f.StartsWith(".")) && f <> "security-audit-log")
239-
//TODO template does not accept comma separated list but is documented as such
240-
|> Seq.map(fun template -> client.Indices.DeleteTemplateForAll<DynamicResponse>(template))
241-
242-
yield! templates
243-
244-
yield client.Watcher.Delete<DynamicResponse>("my_watch")
245-
246-
let deleteNonReserved (setup:_ -> DynamicResponse) (delete:(_ -> DynamicResponse)) =
247-
setup().Dictionary.GetKeyValues()
248-
|> Seq.map (fun kv ->
249-
match kv.Value.Get<bool> "metadata._reserved" with
250-
| false -> Some <| delete(kv.Key)
251-
| _ -> None
252-
)
253-
|> Seq.choose id
254-
|> Seq.toList
255-
256-
yield! //roles
257-
deleteNonReserved
258-
(fun _ -> client.Security.GetRole<DynamicResponse>())
259-
(fun role -> client.Security.DeleteRole<DynamicResponse> role)
260-
261-
yield! //users
262-
deleteNonReserved
263-
(fun _ -> client.Security.GetUser<DynamicResponse>())
264-
(fun user -> client.Security.DeleteUser<DynamicResponse> user)
265-
266-
yield! //privileges
267-
deleteNonReserved
268-
(fun _ -> client.Security.GetPrivileges<DynamicResponse>())
269-
(fun priv -> client.Security.DeletePrivileges<DynamicResponse>(priv, "_all"))
270-
271-
// deleting feeds before jobs is important
272-
let mlDataFeeds =
273-
let stopFeeds = client.MachineLearning.StopDatafeed<DynamicResponse>("_all")
274-
let getFeeds = client.MachineLearning.GetDatafeeds<DynamicResponse> ()
275-
let deleteFeeds =
276-
getFeeds.Get<string[]> "datafeeds.datafeed_id"
277-
|> Seq.map (fun jobId -> client.MachineLearning.DeleteDatafeed<DynamicResponse>(jobId))
278-
|> Seq.toList
279-
[stopFeeds; getFeeds] @ deleteFeeds
280-
yield! mlDataFeeds
281-
282-
yield client.IndexLifecycleManagement.RemovePolicy<DynamicResponse>("_all")
283-
284-
let mlJobs =
285-
let closeJobs = client.MachineLearning.CloseJob<DynamicResponse>("_all", PostData.Empty)
286-
let getJobs = client.MachineLearning.GetJobs<DynamicResponse> "_all"
287-
let deleteJobs =
288-
getJobs.Get<string[]> "jobs.job_id"
289-
|> Seq.map (fun jobId -> client.MachineLearning.DeleteJob<DynamicResponse>(jobId))
290-
|> Seq.toList
291-
[closeJobs; getJobs] @ deleteJobs
292-
yield! mlJobs
293-
294-
let rollupJobs =
295-
let getJobs = client.Rollup.GetJob<DynamicResponse> "_all"
296-
let deleteJobs =
297-
getJobs.Get<string[]> "jobs.config.id"
298-
|> Seq.collect (fun jobId -> [
299-
client.Rollup.StopJob<DynamicResponse>(jobId)
300-
client.Rollup.DeleteJob<DynamicResponse>(jobId)
301-
])
302-
|> Seq.toList
303-
[getJobs] @ deleteJobs
304-
yield! rollupJobs
305-
306-
let tasks =
307-
let getJobs = client.Tasks.List<DynamicResponse> ()
308-
let cancelJobs =
309-
let dict = getJobs.Get<DynamicDictionary> "nodes"
310-
dict.GetKeyValues()
311-
|> Seq.collect(fun kv ->
312-
let dict = kv.Value.Get<DynamicDictionary> "tasks"
313-
dict.GetKeyValues()
314-
)
315-
|> Seq.map (fun kv ->
316-
match kv.Value.Get<bool> "cancellable" with
317-
| true -> Some <| client.Tasks.Cancel<DynamicResponse>(kv.Key)
318-
| _ -> None
319-
)
320-
|> Seq.choose id
321-
|> Seq.toList
322-
323-
[getJobs] @ cancelJobs
324-
yield! tasks
325-
326-
let transforms =
327-
let transforms = client.Transform.Get<DynamicResponse> "_all"
328-
let stopTransforms =
329-
transforms.Get<string[]> "transforms.id"
330-
|> Seq.collect (fun id -> [
331-
client.Transform.Stop<DynamicResponse> id
332-
client.Transform.Delete<DynamicResponse> id
333-
])
334-
|> Seq.toList
335-
[transforms] @ stopTransforms
336-
yield! transforms
337-
338-
let yellowStatus = Nullable.op_Implicit WaitForStatus.Yellow
339-
yield client.Cluster.Health<DynamicResponse>(ClusterHealthRequestParameters(WaitForStatus=yellowStatus))
340-
341-
//make sure we don't delete system indices
342-
let indices =
343-
client.Cat.Indices<StringResponse>("*", CatIndicesRequestParameters(Headers=["index"].ToArray()))
344-
.Body.Split("\n")
345-
|> Seq.filter(fun f -> not(String.IsNullOrWhiteSpace(f)))
346-
|> Seq.filter(fun f -> not(f.StartsWith(".")) || f.StartsWith(".ml-"))
347-
|> String.concat ","
348-
|> function
349-
| s when String.IsNullOrEmpty(s) -> None
350-
| s -> Some <| client.Indices.Delete<DynamicResponse>(s)
351-
352-
match indices with Some r -> yield r | None -> ignore()
353-
354-
let data = PostData.String @"{""password"":""x-pack-test-password"", ""roles"":[""superuser""]}"
355-
yield client.Security.PutUser<DynamicResponse>("x_pack_rest_user", data)
356-
357-
yield client.Indices.Refresh<DynamicResponse> "_all"
358-
359-
yield client.Cluster.Health<DynamicResponse>(ClusterHealthRequestParameters(WaitForStatus=yellowStatus))
360-
}
361-
)]
362-
363206
let private toDocument (yamlInfo:YamlFileInfo) (sections:YamlTestSection list) =
364207
let setups = (sections |> List.tryPick (fun s -> match s with | Setup s -> Some s | _ -> None))
365208
{
366209
FileInfo = FileInfo yamlInfo.File
367-
Setup = Some <| (DefaultSetup @ (setups |> Option.defaultValue []))
210+
Setup = Some <| (TestSuiteBootstrap.DefaultSetup @ (setups |> Option.defaultValue []))
368211
Teardown = sections |> List.tryPick (fun s -> match s with | Teardown s -> Some s | _ -> None)
369212
Tests = sections |> List.map (fun s -> match s with | YamlTest s -> Some s | _ -> None) |> List.choose id
370213
}

0 commit comments

Comments
 (0)