Debian Codegraph @

ingest_get_pipeline_test.go

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