-
Notifications
You must be signed in to change notification settings - Fork 0
/
opensearch_internal_test.go
446 lines (391 loc) · 10.8 KB
/
opensearch_internal_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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// SPDX-License-Identifier: Apache-2.0
//
// The OpenSearch Contributors require contributions made to
// this file be licensed under the Apache-2.0 license or a
// compatible open source license.
//
// Modifications Copyright OpenSearch Contributors. See
// GitHub history for details.
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// +build !integration
package opensearch
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/opensearch-project/opensearch-go/v2/opensearchtransport"
)
var called bool
type mockTransp struct {
RoundTripFunc func(*http.Request) (*http.Response, error)
}
var defaultRoundTripFunc = func(req *http.Request) (*http.Response, error) {
response := &http.Response{Header: http.Header{}}
if req.URL.Path == "/" {
response.Body = ioutil.NopCloser(strings.NewReader(`{
"version" : {
"number" : "1.0.0",
"distribution" : "opensearch"
}
}`))
response.Header.Add("Content-Type", "application/json")
} else {
called = true
}
return response, nil
}
func (t *mockTransp) RoundTrip(req *http.Request) (*http.Response, error) {
if t.RoundTripFunc == nil {
return defaultRoundTripFunc(req)
}
return t.RoundTripFunc(req)
}
func TestClientConfiguration(t *testing.T) {
t.Parallel()
t.Run("With empty", func(t *testing.T) {
c, err := NewDefaultClient()
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
u := c.Transport.(*opensearchtransport.Client).URLs()[0].String()
if u != defaultURL {
t.Errorf("Unexpected URL, want=%s, got=%s", defaultURL, u)
}
})
t.Run("With URL from Addresses", func(t *testing.T) {
c, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}, Transport: &mockTransp{}})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
u := c.Transport.(*opensearchtransport.Client).URLs()[0].String()
if u != "http://localhost:8080" {
t.Errorf("Unexpected URL, want=http://localhost:8080, got=%s", u)
}
})
t.Run("With URL from ELASTICSEARCH_URL", func(t *testing.T) {
os.Setenv(envElasticsearchURL, "http://elasticsearch.com")
defer func() { os.Setenv(envElasticsearchURL, "") }()
c, err := NewClient(Config{Transport: &mockTransp{}})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
u := c.Transport.(*opensearchtransport.Client).URLs()[0].String()
if u != "http://elasticsearch.com" {
t.Errorf("Unexpected URL, want=http://elasticsearch.com, got=%s", u)
}
})
t.Run("With URL from OPENSEARCH_URL", func(t *testing.T) {
os.Setenv(envOpenSearchURL, "http://opensearch.com")
defer func() { os.Setenv(envOpenSearchURL, "") }()
c, err := NewClient(Config{Transport: &mockTransp{}})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
u := c.Transport.(*opensearchtransport.Client).URLs()[0].String()
if u != "http://opensearch.com" {
t.Errorf("Unexpected URL, want=http://opensearch.com, got=%s", u)
}
})
t.Run("With URL from OPENSEARCH_URL and ELASTICSEARCH_URL", func(t *testing.T) {
os.Setenv(envOpenSearchURL, "http://opensearch.com")
defer func() { os.Setenv(envOpenSearchURL, "") }()
os.Setenv(envElasticsearchURL, "http://elasticsearch.com")
defer func() { os.Setenv(envElasticsearchURL, "") }()
_, err := NewClient(Config{Transport: &mockTransp{}})
assert.Error(t, err, "Expected error")
match, _ := regexp.MatchString("both .* are set", err.Error())
if !match {
t.Errorf("Expected error when addresses from OPENSEARCH_URL and ELASTICSEARCH_URL are used together, got: %v", err)
}
})
t.Run("With URL from environment and cfg.Addresses", func(t *testing.T) {
os.Setenv(envElasticsearchURL, "http://example.com")
defer func() { os.Setenv(envElasticsearchURL, "") }()
c, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}, Transport: &mockTransp{}})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
u := c.Transport.(*opensearchtransport.Client).URLs()[0].String()
if u != "http://localhost:8080" {
t.Errorf("Unexpected URL, want=http://localhost:8080, got=%s", u)
}
})
t.Run("With invalid URL", func(t *testing.T) {
u := ":foo"
_, err := NewClient(Config{Addresses: []string{u}})
if err == nil {
t.Errorf("Expected error for URL %q, got %v", u, err)
}
})
t.Run("With invalid URL from environment", func(t *testing.T) {
os.Setenv(envElasticsearchURL, ":foobar")
defer func() { os.Setenv(envElasticsearchURL, "") }()
c, err := NewDefaultClient()
if err == nil {
t.Errorf("Expected error, got: %+v", c)
}
})
t.Run("With skip check", func(t *testing.T) {
_, err := NewClient(
Config{
Transport: &mockTransp{
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
return &http.Response{
Header: http.Header{},
Body: ioutil.NopCloser(strings.NewReader("")),
}, nil
},
},
})
if err != nil {
t.Errorf("Unexpected error, got: %+v", err)
}
})
}
func TestClientInterface(t *testing.T) {
t.Run("Transport", func(t *testing.T) {
c, err := NewClient(Config{Transport: &mockTransp{}})
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
if called != false { // megacheck ignore
t.Errorf("Unexpected call to transport by client")
}
c.Perform(&http.Request{URL: &url.URL{}, Header: make(http.Header)}) // errcheck ignore
if called != true { // megacheck ignore
t.Errorf("Expected client to call transport")
}
})
}
func TestAddrsToURLs(t *testing.T) {
tt := []struct {
name string
addrs []string
urls []*url.URL
err error
}{
{
name: "valid",
addrs: []string{
"http://example.com",
"https://example.com",
"http://192.168.255.255",
"http://example.com:8080",
},
urls: []*url.URL{
{Scheme: "http", Host: "example.com"},
{Scheme: "https", Host: "example.com"},
{Scheme: "http", Host: "192.168.255.255"},
{Scheme: "http", Host: "example.com:8080"},
},
err: nil,
},
{
name: "trim trailing slash",
addrs: []string{"http://example.com/", "http://example.com//"},
urls: []*url.URL{
{Scheme: "http", Host: "example.com", Path: ""},
{Scheme: "http", Host: "example.com", Path: ""},
},
},
{
name: "keep suffix",
addrs: []string{"http://example.com/foo"},
urls: []*url.URL{{Scheme: "http", Host: "example.com", Path: "/foo"}},
},
{
name: "invalid url",
addrs: []string{"://invalid.com"},
urls: nil,
err: errors.New("missing protocol scheme"),
},
}
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
res, err := addrsToURLs(tc.addrs)
if tc.err != nil {
if err == nil {
t.Errorf("Expected error, got: %v", err)
}
match, _ := regexp.MatchString(tc.err.Error(), err.Error())
if !match {
t.Errorf("Expected err [%s] to match: %s", err.Error(), tc.err.Error())
}
}
for i := range tc.urls {
if res[i].Scheme != tc.urls[i].Scheme {
t.Errorf("%s: Unexpected scheme, want=%s, got=%s", tc.name, tc.urls[i].Scheme, res[i].Scheme)
}
}
for i := range tc.urls {
if res[i].Host != tc.urls[i].Host {
t.Errorf("%s: Unexpected host, want=%s, got=%s", tc.name, tc.urls[i].Host, res[i].Host)
}
}
for i := range tc.urls {
if res[i].Path != tc.urls[i].Path {
t.Errorf("%s: Unexpected path, want=%s, got=%s", tc.name, tc.urls[i].Path, res[i].Path)
}
}
})
}
}
func TestVersion(t *testing.T) {
if Version == "" {
t.Error("Version is empty")
}
}
func TestClientMetrics(t *testing.T) {
c, _ := NewClient(Config{EnableMetrics: true, Transport: &mockTransp{}})
m, err := c.Metrics()
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if m.Requests > 1 {
t.Errorf("Unexpected output: %s", m)
}
}
func TestParseElasticsearchVersion(t *testing.T) {
tests := []struct {
name string
version string
major int64
minor int64
patch int64
wantErr bool
}{
{
name: "Nominal version parsing",
version: "7.14.0",
major: 7,
minor: 14,
patch: 0,
wantErr: false,
},
{
name: "Snapshot version parsing",
version: "1.0.0-SNAPSHOT",
major: 1,
minor: 0,
patch: 0,
wantErr: false,
},
{
name: "Previous major parsing",
version: "6.15.1",
major: 6,
minor: 15,
patch: 1,
wantErr: false,
},
{
name: "Error parsing version",
version: "6.15",
major: 0,
minor: 0,
patch: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1, got2, err := ParseVersion(tt.version)
if (err != nil) != tt.wantErr {
t.Errorf("ParseVersion() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.major {
t.Errorf("ParseVersion() got = %v, want %v", got, tt.major)
}
if got1 != tt.minor {
t.Errorf("ParseVersion() got1 = %v, want %v", got1, tt.minor)
}
if got2 != tt.patch {
t.Errorf("ParseVersion() got2 = %v, want %v", got2, tt.patch)
}
})
}
}
func TestGenuineCheckInfo(t *testing.T) {
tests := []struct {
name string
info info
wantErr bool
err error
}{
{
name: "Supported OpenSearch 1.0.0",
info: info{
Version: esVersion{
Number: "1.0.0",
Distribution: openSearch,
},
},
wantErr: false,
err: nil,
},
{
name: "Supported Elasticsearch 7.10.0",
info: info{
Version: esVersion{
Number: "7.10.0",
BuildFlavor: "anything",
},
},
wantErr: false,
err: nil,
},
{
name: "Unsupported Elasticsearch Version 6.15.1",
info: info{
Version: esVersion{
Number: "6.15.1",
BuildFlavor: "default",
},
Tagline: "You Know, for Search",
},
wantErr: true,
err: errors.New(unsupportedProduct),
},
{
name: "Elasticsearch oss",
info: info{
Version: esVersion{
Number: "7.10.0",
BuildFlavor: "oss",
},
Tagline: "You Know, for Search",
},
wantErr: false,
err: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := checkCompatibleInfo(tt.info); (err != nil) != tt.wantErr && err != tt.err {
t.Errorf("checkCompatibleInfo() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}