@@ -193,6 +193,7 @@ func (g *Generator) genFileHeader() {
193
193
import (
194
194
encjson "encoding/json"
195
195
encyaml "gopkg.in/yaml.v2"
196
+ "context"
196
197
"crypto/tls"
197
198
"testing"
198
199
"time"
@@ -289,14 +290,37 @@ func (g *Generator) genCommonSetup() {
289
290
commonSetup := func() {
290
291
var res *esapi.Response
291
292
293
+ {
294
+ res, _ = es.Cluster.Health(es.Cluster.Health.WithWaitForNoInitializingShards(true))
295
+ if res != nil && res.Body != nil {
296
+ defer res.Body.Close()
297
+ }
298
+ }
299
+
292
300
{
293
301
res, _ = es.Indices.Delete([]string{"_all"})
294
302
if res != nil && res.Body != nil { defer res.Body.Close() }
295
303
}
296
304
297
305
{
298
- res, _ = es.Indices.DeleteTemplate("*")
299
- if res != nil && res.Body != nil { defer res.Body.Close() }
306
+ var r map[string]interface{}
307
+ res, _ = es.Indices.GetTemplate()
308
+ if res != nil && res.Body != nil {
309
+ defer res.Body.Close()
310
+ json.NewDecoder(res.Body).Decode(&r)
311
+ for templateName, _ := range r {
312
+ if strings.HasPrefix(templateName, ".") {
313
+ continue
314
+ }
315
+ if templateName == "security_audit_log" {
316
+ continue
317
+ }
318
+ if templateName == "logstash-index-template" {
319
+ continue
320
+ }
321
+ es.Indices.DeleteTemplate(templateName)
322
+ }
323
+ }
300
324
}
301
325
302
326
{
@@ -327,6 +351,13 @@ func (g *Generator) genCommonSetup() {
327
351
}
328
352
}
329
353
}
354
+
355
+ {
356
+ res, _ = es.Cluster.Health(es.Cluster.Health.WithWaitForStatus("yellow"))
357
+ if res != nil && res.Body != nil {
358
+ defer res.Body.Close()
359
+ }
360
+ }
330
361
}
331
362
332
363
` )
@@ -338,6 +369,21 @@ func (g *Generator) genXPackSetup() {
338
369
xpackSetup := func() {
339
370
var res *esapi.Response
340
371
372
+ {
373
+ var r map[string]interface{}
374
+ res, _ = es.Indices.GetTemplate()
375
+ if res != nil && res.Body != nil {
376
+ defer res.Body.Close()
377
+ json.NewDecoder(res.Body).Decode(&r)
378
+ for templateName, _ := range r {
379
+ if strings.HasPrefix(templateName, ".") {
380
+ continue
381
+ }
382
+ es.Indices.DeleteTemplate(templateName)
383
+ }
384
+ }
385
+ }
386
+
341
387
{
342
388
res, _ = es.Watcher.DeleteWatch("my_watch")
343
389
if res != nil && res.Body != nil {
@@ -395,8 +441,10 @@ func (g *Generator) genXPackSetup() {
395
441
396
442
{
397
443
var r map[string]interface{}
398
- es.ML.StopDatafeed("_all")
399
- res, _ = es.ML.GetDatafeeds(es.ML.GetDatafeeds.WithAllowNoDatafeeds(true))
444
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
445
+ defer cancel()
446
+ es.ML.StopDatafeed("_all", es.ML.StopDatafeed.WithContext(ctx))
447
+ res, _ = es.ML.GetDatafeeds()
400
448
if res != nil && res.Body != nil {
401
449
defer res.Body.Close()
402
450
json.NewDecoder(res.Body).Decode(&r)
@@ -412,13 +460,15 @@ func (g *Generator) genXPackSetup() {
412
460
413
461
{
414
462
var r map[string]interface{}
415
- es.ML.CloseJob("_all", es.ML.CloseJob.WithForce(true))
416
- res, _ = es.ML.GetJobs(es.ML.GetJobs.WithAllowNoJobs(true))
463
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
464
+ defer cancel()
465
+ es.ML.CloseJob("_all", es.ML.CloseJob.WithContext(ctx))
466
+ res, _ = es.ML.GetJobs()
417
467
if res != nil && res.Body != nil {
418
468
defer res.Body.Close()
419
469
json.NewDecoder(res.Body).Decode(&r)
420
470
for _, v := range r["jobs"].([]interface{}) {
421
- jobID, ok := v.(map[string]interface{})["datafeed_id "]
471
+ jobID, ok := v.(map[string]interface{})["job_id "]
422
472
if !ok {
423
473
continue
424
474
}
@@ -438,7 +488,7 @@ func (g *Generator) genXPackSetup() {
438
488
if !ok {
439
489
continue
440
490
}
441
- es.Rollup.StopJob(jobID.(string))
491
+ es.Rollup.StopJob(jobID.(string), es.Rollup.StopJob.WithWaitForCompletion(true) )
442
492
es.Rollup.DeleteJob(jobID.(string))
443
493
}
444
494
}
@@ -457,12 +507,45 @@ func (g *Generator) genXPackSetup() {
457
507
continue
458
508
}
459
509
taskID := fmt.Sprintf("%v:%v", v.(map[string]interface{})["node"], v.(map[string]interface{})["id"])
460
- es.Tasks.Cancel(es.Tasks.Cancel.WithTaskID(taskID))
510
+ ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
511
+ defer cancel()
512
+ es.Tasks.Cancel(es.Tasks.Cancel.WithTaskID(taskID), es.Tasks.Cancel.WithContext(ctx))
513
+ }
514
+ }
515
+ }
516
+ }
517
+
518
+ {
519
+ var r map[string]interface{}
520
+ res, _ = es.Snapshot.GetRepository()
521
+ if res != nil && res.Body != nil {
522
+ defer res.Body.Close()
523
+ json.NewDecoder(res.Body).Decode(&r)
524
+ for repositoryID, _ := range r {
525
+ var r map[string]interface{}
526
+ res, _ = es.Snapshot.Get(repositoryID, []string{"_all"})
527
+ json.NewDecoder(res.Body).Decode(&r)
528
+ for _, vv := range r["responses"].([]interface{}) {
529
+ for _, v := range vv.(map[string]interface{})["snapshots"].([]interface{}) {
530
+ snapshotID, ok := v.(map[string]interface{})["snapshot"]
531
+ if !ok {
532
+ continue
533
+ }
534
+ es.Snapshot.Delete(repositoryID, fmt.Sprintf("%s", snapshotID))
535
+ }
461
536
}
537
+ es.Snapshot.DeleteRepository([]string{fmt.Sprintf("%s", repositoryID)})
462
538
}
463
539
}
464
540
}
465
541
542
+ {
543
+ res, _ = es.ILM.RemovePolicy(es.ILM.RemovePolicy.WithIndex("_all"))
544
+ if res != nil && res.Body != nil {
545
+ defer res.Body.Close()
546
+ }
547
+ }
548
+
466
549
{
467
550
res, _ = es.Cluster.Health(es.Cluster.Health.WithWaitForStatus("yellow"))
468
551
if res != nil && res.Body != nil {
@@ -478,7 +561,7 @@ func (g *Generator) genXPackSetup() {
478
561
}
479
562
480
563
{
481
- res, _ = es.Indices.Refresh(es.Indices.Refresh.WithIndex(".security* "))
564
+ res, _ = es.Indices.Refresh(es.Indices.Refresh.WithIndex("_all "))
482
565
if res != nil && res.Body != nil {
483
566
defer res.Body.Close()
484
567
}
@@ -490,6 +573,26 @@ func (g *Generator) genXPackSetup() {
490
573
defer res.Body.Close()
491
574
}
492
575
}
576
+
577
+ {
578
+ var i int
579
+ for {
580
+ i++
581
+ var r map[string]interface{}
582
+ res, _ = es.Cluster.PendingTasks()
583
+ if res != nil && res.Body != nil {
584
+ defer res.Body.Close()
585
+ json.NewDecoder(res.Body).Decode(&r)
586
+ if len(r["tasks"].([]interface{})) < 1 {
587
+ break
588
+ }
589
+ }
590
+ if i > 30 {
591
+ break
592
+ }
593
+ time.Sleep(time.Second)
594
+ }
595
+ }
493
596
}
494
597
495
598
` )
0 commit comments