ingest_get_pipeline_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. getres, err := client.IngestGetPipeline().Do(context.TODO())
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. if have := len(getres); have == 0 {
  83. t.Fatalf("expected at least 1 pipeline, got %d", have)
  84. }
  85. if _, found := getres["my-pipeline"]; !found {
  86. t.Fatalf("expected to find pipline with id %q", "my-pipeline")
  87. }
  88. // Get pipeline by ID
  89. getres, err = client.IngestGetPipeline("my-pipeline").Do(context.TODO())
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. if want, have := 1, len(getres); want != have {
  94. t.Fatalf("expected %d pipelines, got %d", want, have)
  95. }
  96. if _, found := getres["my-pipeline"]; !found {
  97. t.Fatalf("expected to find pipline with id %q", "my-pipeline")
  98. }
  99. // Delete pipeline
  100. delres, err := client.IngestDeletePipeline("my-pipeline").Do(context.TODO())
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. if delres == nil {
  105. t.Fatal("expected response, got nil")
  106. }
  107. if want, have := true, delres.Acknowledged; want != have {
  108. t.Fatalf("expected ack = %v, got %v", want, have)
  109. }
  110. }