-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestSuiteBootstrap.fs
268 lines (229 loc) · 10.5 KB
/
TestSuiteBootstrap.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information
module Tests.YamlRunner.TestSuiteBootstrap
open System
open System.Linq
open Elastic.Transport
open Elasticsearch.Net.Specification.CatApi
open Elasticsearch.Net.Specification.ClusterApi
open Elasticsearch.Net.Specification.IndicesApi
open Tests.YamlRunner.Models
open System.Collections.Generic
let DefaultSetup : Operation list = [Actions("Setup", fun (client, suite) ->
let firstFailure (responses:DynamicResponse seq) =
responses
|> Seq.filter (fun r -> not r.Success && r.HttpStatusCode <> Nullable.op_Implicit 404)
|> Seq.tryHead
let isXPackName name =
match name with
| ".watches"
| "logstash-index-template"
| ".logstash-management"
| "security_audit_log"
| ".slm-history"
| ".async-search"
| "saml-service-provider"
| "ilm-history"
| "logs"
| "logs-settings"
| "logs-mappings"
| "metrics"
| "data-streams-mappings"
| "metrics-settings"
| "metrics-mappings"
| "metrics-mappings"
| "synthetics"
| "synthetics-settings"
| "synthetics-mappings"
| ".snapshot-blob-cache"
| ".deprecation-indexing-template" -> false
| ".deprecation-indexing-mappings" -> false
| ".deprecation-indexing-settings" -> false
| s when s.StartsWith(".monitoring") -> false
| s when s.StartsWith(".watch") -> false
| s when s.StartsWith(".data-frame") -> false
| s when s.StartsWith(".ml-") -> false
| s when s.StartsWith(".transform-") -> false
| _ -> true
let getAndDeleteFilter (setup:_ -> DynamicResponse) (delete:(_ -> DynamicResponse)) filter =
setup().Body
|> Seq.map (fun kv ->
match filter with
| Some filter ->
match filter kv.Key kv.Value with
| false -> Some <| delete(kv.Key, kv.Value)
| _ -> None
| None -> Some <| delete(kv.Key, kv.Value)
)
|> Seq.choose id
|> Seq.toList
let getAndDelete setup delete = getAndDeleteFilter setup delete None
let wipeRollupJobs () =
let getJobs = client.Rollup.GetJob<DynamicResponse> "_all"
let deleteJobs =
getJobs.Get<string[]> "jobs.config.id"
|> Seq.collect (fun jobId -> [
client.Rollup.StopJob<DynamicResponse>(jobId)
client.Rollup.DeleteJob<DynamicResponse>(jobId)
])
|> Seq.toList
[getJobs] @ deleteJobs
let waitForPendingTasks (filter:string) =
let call () =
client.Cat.Tasks<StringResponse>(CatTasksRequestParameters(Detailed=true)).Body.Split("\n")
|> Array.filter (fun l -> l.Contains(filter, StringComparison.OrdinalIgnoreCase))
|> Array.length
let start = DateTime.UtcNow
let e = start.AddSeconds(30.)
while (call() > 0 && DateTime.UtcNow < e) do ignore()
let waitForPendingRollupTasks () = waitForPendingTasks "xpack/rollup/job"
let deleteAllSLMPolicies () =
getAndDelete
(fun _ -> client.SnapshotLifecycleManagement.GetSnapshotLifecycle<DynamicResponse>())
(fun (name, _) -> client.SnapshotLifecycleManagement.DeleteSnapshotLifecycle<DynamicResponse> name)
let wipeSnapshots () =
getAndDelete
(fun _ -> client.Snapshot.GetRepository<DynamicResponse>())
(fun (name, value) ->
if value.Get<string> "type" = "fs" then
client.Snapshot.Delete<DynamicResponse> (name, "*") |> ignore
client.Snapshot.DeleteRepository<DynamicResponse> name
)
let wipeDataStreams () =
client.Indices.DeleteDataStreamForAll<DynamicResponse> "*"
let wipeAllIndices () =
let dp = DeleteIndexRequestParameters()
dp.SetQueryString("expand_wildcards", "all")
client.Indices.Delete<DynamicResponse>("*,-.ds-.watcher-history-*,-.ds-ilm-history-*", dp)
let deleteTemplates () =
client.Cat.Templates<StringResponse>("*", CatTemplatesRequestParameters(Headers=["name"].ToArray()))
.Body.Split("\n")
|> Seq.map(fun line -> line.Split(" ", StringSplitOptions.RemoveEmptyEntries))
|> Seq.filter(fun line -> line.Length = 1)
|> Seq.map(fun tokens -> tokens.[0].Trim())
|> Seq.filter isXPackName
//TODO template does not accept comma separated list but is documented as such
|> Seq.map(fun template ->
let result = client.Indices.DeleteTemplateForAll<DynamicResponse>(template)
match result.Success with
| true -> result
| false -> client.Indices.DeleteTemplateV2ForAll<DynamicResponse>(template)
)
|> Seq.toList
let deleteComponentTemplates () =
let result = client.Cluster.GetComponentTemplate<DynamicResponse>()
let names = result.Get<string[]>("component_templates.name")
names
|> Seq.filter isXPackName
|> Seq.map client.Cluster.DeleteComponentTemplate<DynamicResponse>
|> Seq.toList
let wipeTemplateForXPack () = deleteTemplates() @ deleteComponentTemplates()
let wipeClusterSettings () =
let settings = client.Cluster.GetSettings<DynamicResponse>(ClusterGetSettingsRequestParameters(FlatSettings=true))
let payload =
[
"transient", dict [ for v in settings.Get<DynamicDictionary>("transient").Keys -> v, null ];
"persistent", dict [ for v in settings.Get<DynamicDictionary>("persistent").Keys -> v, null ];
]
|> dict
if payload.["transient"].Values.Count > 0 || payload.["transient"].Values.Count > 0 then
client.Cluster.PutSettings<DynamicResponse>(PostData.Serializable(payload))
else
settings
let deleteAllILMPolicies () =
let preserved = [
"ilm-history-ilm-policy";
"slm-history-ilm-policy";
"watch-history-ilm-policy";
"ml-size-based-ilm-policy";
"logs";
"metrics"
]
getAndDeleteFilter
(fun _ -> client.Snapshot.GetRepository<DynamicResponse>())
(fun (name, value) ->
if value.Get<string> "type" = "fs" then
client.Snapshot.Delete<DynamicResponse> (name, "*") |> ignore
client.Snapshot.DeleteRepository<DynamicResponse> name
)
(Some (fun name value -> preserved |> List.contains name ))
let deleteAllAutoFollowPatterns () =
let result = client.CrossClusterReplication.GetAutoFollowPattern<DynamicResponse>()
let names = result.Get<string[]>("patterns.name")
names
|> Seq.map client.CrossClusterReplication.DeleteAutoFollowPattern<DynamicResponse>
|> Seq.toList
let deleteAllTasks () =
let getJobs = client.Tasks.List<DynamicResponse> ()
let cancelJobs =
getJobs.Get<DynamicDictionary> "nodes"
|> Seq.collect(fun kv -> kv.Value.Get<DynamicDictionary> "tasks")
|> Seq.map (fun kv ->
match kv.Value.Get<bool> "cancellable" with
| true -> Some <| client.Tasks.Cancel<DynamicResponse>(kv.Key)
| _ -> None
)
|> Seq.choose id
|> Seq.toList
[getJobs] @ cancelJobs
let stopTransforms () =
let transforms = client.Transform.Get<DynamicResponse> "_all"
let stopTransforms =
transforms.Get<string[]> "transforms.id"
|> Seq.collect (fun id -> [
client.Transform.Stop<DynamicResponse> id
client.Transform.Delete<DynamicResponse> id
])
|> Seq.toList
[transforms] @ stopTransforms
let closeMLJobs () =
let closeJobs = client.MachineLearning.CloseJob<DynamicResponse>("_all", PostData.Empty)
let getJobs = client.MachineLearning.GetJobs<DynamicResponse> "_all"
let deleteJobs =
getJobs.Get<string[]> "jobs.job_id"
|> Seq.map (fun jobId -> client.MachineLearning.DeleteJob<DynamicResponse>(jobId))
|> Seq.toList
[closeJobs; getJobs] @ deleteJobs
let deleteMLDatafeeds () =
let stopFeeds = client.MachineLearning.StopDatafeed<DynamicResponse>("_all", null)
let getFeeds = client.MachineLearning.GetDatafeeds<DynamicResponse> ()
let deleteFeeds =
getFeeds.Get<string[]> "datafeeds.datafeed_id"
|> Seq.map (fun jobId -> client.MachineLearning.DeleteDatafeed<DynamicResponse>(jobId))
|> Seq.toList
[stopFeeds; getFeeds] @ deleteFeeds
let waitForClusterStateUpdatesToFinish () =
let call () =
client.Cluster.PendingTasks<DynamicResponse>().Get<string[]>("tasks.source")
|> Array.length
let start = DateTime.UtcNow
let e = start.AddSeconds(30.)
while (call() > 0 && DateTime.UtcNow < e) do ignore()
firstFailure <| seq {
if suite = Platinum then
yield! wipeRollupJobs()
waitForPendingRollupTasks()
yield! deleteAllSLMPolicies()
if suite = Platinum then
yield wipeDataStreams()
yield wipeAllIndices()
yield! wipeSnapshots()
yield! wipeTemplateForXPack()
yield wipeClusterSettings()
if suite = Platinum then
yield! deleteAllILMPolicies()
yield! deleteAllAutoFollowPatterns()
yield! deleteAllTasks()
// deleting feeds before jobs is important
yield! deleteMLDatafeeds()
yield client.IndexLifecycleManagement.RemovePolicy<DynamicResponse>("_all")
yield! closeMLJobs()
yield! deleteAllTasks()
yield! stopTransforms()
if suite = Platinum then
let data = PostData.String @"{""password"":""x-pack-test-password"", ""roles"":[""superuser""]}"
yield client.Security.PutUser<DynamicResponse>("x_pack_rest_user", data)
waitForClusterStateUpdatesToFinish()
}
)]