forked from compose/transporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelasticsearch_test.go
151 lines (137 loc) · 4.03 KB
/
elasticsearch_test.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package elasticsearch
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/compose/transporter/adaptor"
"github.com/compose/transporter/client"
)
var (
mockElasticsearch = &Elasticsearch{}
)
func TestDescription(t *testing.T) {
if mockElasticsearch.Description() != description {
t.Errorf("wrong description returned, expected %s, got %s", description, mockElasticsearch.Description())
}
}
func TestSampleConfig(t *testing.T) {
if mockElasticsearch.SampleConfig() != sampleConfig {
t.Errorf("wrong config returned, expected %s, got %s", sampleConfig, mockElasticsearch.SampleConfig())
}
}
var goodVersionServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{\"version\":{\"number\":\"5.0.0\"}}")
}))
var (
testUser = "user"
testPwd = "pwd"
authURI = func() string {
uri, _ := url.Parse(authedServer.URL)
uri.User = url.UserPassword(testUser, testPwd)
return uri.String()
}
)
var authedServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, ok := r.BasicAuth()
if !ok || u != testUser || p != testPwd {
w.WriteHeader(http.StatusForbidden)
return
}
fmt.Fprint(w, "{\"version\":{\"number\":\"5.0.0\"}}")
}))
var emptyBodyServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{}")
}))
var badJSONServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello, client")
}))
var badVersionServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{\"version\":{\"number\":\"not a version\"}}")
}))
var unsupportedVersionServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "{\"version\":{\"number\":\"0.9.2\"}}")
}))
var clientTests = []struct {
name string
cfg adaptor.Config
err error
}{
{
"base config",
adaptor.Config{"uri": goodVersionServer.URL},
nil,
},
{
"timeout config",
adaptor.Config{"uri": goodVersionServer.URL, "timeout": "60s"},
nil,
},
{
"authed URI",
adaptor.Config{"uri": authURI()},
nil,
},
{
"parent_id config",
adaptor.Config{"uri": authURI(), "parent_id": "parent_id"},
nil,
},
{
"bad URI",
adaptor.Config{"uri": "%gh&%ij"},
client.InvalidURIError{URI: "%gh&%ij", Err: `parse %gh&%ij: invalid URL escape "%gh"`},
},
{
"no connection",
adaptor.Config{"uri": "http://localhost:7200"},
client.ConnectError{Reason: "http://localhost:7200"},
},
{
"empty body",
adaptor.Config{"uri": fmt.Sprintf("%s/test", emptyBodyServer.URL)},
client.VersionError{URI: emptyBodyServer.URL, V: "", Err: "missing version: {}"},
},
{
"malformed JSON",
adaptor.Config{"uri": fmt.Sprintf("%s/test", badJSONServer.URL)},
client.VersionError{URI: badJSONServer.URL, V: "", Err: "malformed JSON: Hello, client"},
},
{
"bad version",
adaptor.Config{"uri": badVersionServer.URL},
client.VersionError{URI: badVersionServer.URL, V: "not a version", Err: "Malformed version: not a version"},
},
{
"unsupported version",
adaptor.Config{"uri": unsupportedVersionServer.URL},
client.VersionError{URI: unsupportedVersionServer.URL, V: "0.9.2", Err: "unsupported client"},
},
}
func TestInit(t *testing.T) {
defer func() {
goodVersionServer.Close()
authedServer.Close()
emptyBodyServer.Close()
badJSONServer.Close()
badVersionServer.Close()
unsupportedVersionServer.Close()
}()
for _, ct := range clientTests {
c, err := adaptor.GetAdaptor("elasticsearch", ct.cfg)
if err != nil {
t.Errorf("[%s] unexpected error: %q", ct.name, err)
}
if _, err := c.Client(); err != nil {
t.Errorf("unexpected Client() error, %s", err)
}
rerr := adaptor.ErrFuncNotSupported{Name: "Reader()", Func: "elasticsearch"}
if _, err := c.Reader(); err != rerr {
t.Errorf("wrong Reader() error, expected %s, got %s", rerr, err)
}
if _, err := c.Writer(nil, nil); err != ct.err {
t.Errorf("[%s] wrong error\nexpected: %q\ngot: %q", ct.name, ct.err, err)
}
}
}