summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/olivere/elastic.v5/ingest_get_pipeline_test.go
blob: 009b717ca17e1a618285459ce2a762efc26a250a (plain)
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
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.

package elastic

import (
	"context"
	"testing"
)

func TestIngestGetPipelineURL(t *testing.T) {
	client := setupTestClientAndCreateIndex(t)

	tests := []struct {
		Id       []string
		Expected string
	}{
		{
			nil,
			"/_ingest/pipeline",
		},
		{
			[]string{"my-pipeline-id"},
			"/_ingest/pipeline/my-pipeline-id",
		},
		{
			[]string{"*"},
			"/_ingest/pipeline/%2A",
		},
		{
			[]string{"pipeline-1", "pipeline-2"},
			"/_ingest/pipeline/pipeline-1%2Cpipeline-2",
		},
	}

	for _, test := range tests {
		path, _, err := client.IngestGetPipeline(test.Id...).buildURL()
		if err != nil {
			t.Fatal(err)
		}
		if path != test.Expected {
			t.Errorf("expected %q; got: %q", test.Expected, path)
		}
	}
}

func TestIngestLifecycle(t *testing.T) {
	client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))

	// With the new ES Docker images, XPack is already installed and returns a pipeline. So we cannot test for "no pipelines". Skipping for now.
	/*
		// Get all pipelines (returns 404 that indicates an error)
		getres, err := client.IngestGetPipeline().Do(context.TODO())
		if err == nil {
			t.Fatal(err)
		}
		if getres != nil {
			t.Fatalf("expected no response, got %v", getres)
		}
	//*/

	// Add a pipeline
	pipelineDef := `{
  "description" : "reset retweets",
  "processors" : [
    {
      "set" : {
        "field": "retweets",
        "value": 0
      }
    }
  ]
}`
	putres, err := client.IngestPutPipeline("my-pipeline").BodyString(pipelineDef).Do(context.TODO())
	if err != nil {
		t.Fatal(err)
	}
	if putres == nil {
		t.Fatal("expected response, got nil")
	}
	if want, have := true, putres.Acknowledged; want != have {
		t.Fatalf("expected ack = %v, got %v", want, have)
	}

	// Get all pipelines again
	getres, err := client.IngestGetPipeline().Do(context.TODO())
	if err != nil {
		t.Fatal(err)
	}
	if have := len(getres); have == 0 {
		t.Fatalf("expected at least 1 pipeline, got %d", have)
	}
	if _, found := getres["my-pipeline"]; !found {
		t.Fatalf("expected to find pipline with id %q", "my-pipeline")
	}

	// Get pipeline by ID
	getres, err = client.IngestGetPipeline("my-pipeline").Do(context.TODO())
	if err != nil {
		t.Fatal(err)
	}
	if want, have := 1, len(getres); want != have {
		t.Fatalf("expected %d pipelines, got %d", want, have)
	}
	if _, found := getres["my-pipeline"]; !found {
		t.Fatalf("expected to find pipline with id %q", "my-pipeline")
	}

	// Delete pipeline
	delres, err := client.IngestDeletePipeline("my-pipeline").Do(context.TODO())
	if err != nil {
		t.Fatal(err)
	}
	if delres == nil {
		t.Fatal("expected response, got nil")
	}
	if want, have := true, delres.Acknowledged; want != have {
		t.Fatalf("expected ack = %v, got %v", want, have)
	}
}