ingest_get_pipeline_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2012-present Oliver Eilhard. All rights reserved.
  2. // Use of this source code is governed by a MIT-license.
  3. // See http://olivere.mit-license.org/license.txt for details.
  4. package elastic
  5. import (
  6. "context"
  7. "testing"
  8. )
  9. func TestIngestGetPipelineURL(t *testing.T) {
  10. client := setupTestClientAndCreateIndex(t)
  11. tests := []struct {
  12. Id []string
  13. Expected string
  14. }{
  15. {
  16. nil,
  17. "/_ingest/pipeline",
  18. },
  19. {
  20. []string{"my-pipeline-id"},
  21. "/_ingest/pipeline/my-pipeline-id",
  22. },
  23. {
  24. []string{"*"},
  25. "/_ingest/pipeline/%2A",
  26. },
  27. {
  28. []string{"pipeline-1", "pipeline-2"},
  29. "/_ingest/pipeline/pipeline-1%2Cpipeline-2",
  30. },
  31. }
  32. for _, test := range tests {
  33. path, _, err := client.IngestGetPipeline(test.Id...).buildURL()
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. if path != test.Expected {
  38. t.Errorf("expected %q; got: %q", test.Expected, path)
  39. }
  40. }
  41. }
  42. func TestIngestLifecycle(t *testing.T) {
  43. client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
  44. // With the new ES Docker images, XPack is already installed and returns a pipeline. So we cannot test for "no pipelines". Skipping for now.
  45. /*
  46. // Get all pipelines (returns 404 that indicates an error)
  47. getres, err := client.IngestGetPipeline().Do(context.TODO())
  48. if err == nil {
  49. t.Fatal(err)
  50. }
  51. if getres != nil {
  52. t.Fatalf("expected no response, got %v", getres)
  53. }
  54. //*/
  55. // Add a pipeline
  56. pipelineDef := `{
  57. "description" : "reset retweets",
  58. "processors" : [
  59. {
  60. "set" : {
  61. "field": "retweets",
  62. "value": 0
  63. }
  64. }
  65. ]
  66. }`
  67. putres, err := client.IngestPutPipeline("my-pipeline").BodyString(pipelineDef).Do(context.TODO())
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if putres == nil {
  72. t.Fatal("expected response, got nil")
  73. }
  74. if want, have := true, putres.Acknowledged; want != have {
  75. t.Fatalf("expected ack = %v, got %v", want, have)
  76. }
  77. // Get all pipelines again
  78. {
  79. getres, err := client.IngestGetPipeline().Do(context.TODO())
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. if have := len(getres); have == 0 {
  84. t.Fatalf("expected at least 1 pipeline, got %d", have)
  85. }
  86. pipeline, found := getres["my-pipeline"]
  87. if !found {
  88. t.Fatalf("expected to find pipline with id %q", "my-pipeline")
  89. }
  90. if want, have := "reset retweets", pipeline.Description; want != have {
  91. t.Fatalf("expected pipeline description of %q, have %q", want, have)
  92. }
  93. }
  94. // Get pipeline by ID
  95. {
  96. getres, err := client.IngestGetPipeline("my-pipeline").Do(context.TODO())
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if want, have := 1, len(getres); want != have {
  101. t.Fatalf("expected %d pipelines, got %d", want, have)
  102. }
  103. if _, found := getres["my-pipeline"]; !found {
  104. t.Fatalf("expected to find pipline with id %q", "my-pipeline")
  105. }
  106. }
  107. // Delete pipeline
  108. delres, err := client.IngestDeletePipeline("my-pipeline").Do(context.TODO())
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if delres == nil {
  113. t.Fatal("expected response, got nil")
  114. }
  115. if want, have := true, delres.Acknowledged; want != have {
  116. t.Fatalf("expected ack = %v, got %v", want, have)
  117. }
  118. }