forked from opensearch-project/opensearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopensearch_internal_test.go
More file actions
867 lines (759 loc) · 27.2 KB
/
Copy pathopensearch_internal_test.go
File metadata and controls
867 lines (759 loc) · 27.2 KB
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
// 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.
//go:build !integration
package opensearch
import (
"context"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"testing"
"testing/iotest"
"time"
"github.com/stretchr/testify/require"
"github.com/opensearch-project/opensearch-go/v5/internal/build"
"github.com/opensearch-project/opensearch-go/v5/internal/ttlcache"
"github.com/opensearch-project/opensearch-go/v5/opensearchtransport"
"github.com/opensearch-project/opensearch-go/v5/opensearchtransport/testutil/mockhttp"
)
var called int
func boolPtr(v bool) *bool { return &v }
var defaultRoundTripFunc = func(req *http.Request) (*http.Response, error) {
response := &http.Response{Header: http.Header{}}
switch req.URL.Path {
case "/":
response.Body = io.NopCloser(strings.NewReader(`{
"version" : {
"number" : "1.0.0",
"distribution" : "opensearch"
}
}`))
response.Header.Add("Content-Type", "application/json")
case "/test":
called++
}
return response, nil
}
type testReq struct {
Method string
Path string
Body io.Reader
Params map[string]string
Error bool
Headers http.Header
}
func (r testReq) GetRequest(method string) (*http.Request, error) {
if r.Error {
return nil, fmt.Errorf("test error")
}
return build.Request(
method,
r.Path,
r.Body,
r.Params,
r.Headers,
)
}
func TestClientConfiguration(t *testing.T) {
t.Run("With empty", func(t *testing.T) {
c, err := NewDefaultClient()
require.NoError(t, err)
u := c.Transport.(*opensearchtransport.Transport).URLs()[0].String()
require.Equal(t, defaultURL, u)
})
t.Run("With URL from Addresses", func(t *testing.T) {
c, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}, Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
u := c.Transport.(*opensearchtransport.Transport).URLs()[0].String()
require.Equal(t, "http://localhost:8080", 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: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
u := c.Transport.(*opensearchtransport.Transport).URLs()[0].String()
require.Equal(t, "http://opensearch.com", u)
})
t.Run("With URL from environment and cfg.Addresses", func(t *testing.T) {
os.Setenv(envOpenSearchURL, "http://example.com")
defer func() { os.Setenv(envOpenSearchURL, "") }()
c, err := NewClient(Config{Addresses: []string{"http://localhost:8080//"}, Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
u := c.Transport.(*opensearchtransport.Transport).URLs()[0].String()
require.Equal(t, "http://localhost:8080", u)
})
t.Run("With invalid URL", func(t *testing.T) {
u := ":foo"
_, err := NewClient(Config{Addresses: []string{u}})
require.Error(t, err)
})
t.Run("With invalid URL from environment", func(t *testing.T) {
os.Setenv(envOpenSearchURL, ":foobar")
defer func() { os.Setenv(envOpenSearchURL, "") }()
_, err := NewDefaultClient()
require.Error(t, err)
})
t.Run("With skip check", func(t *testing.T) {
_, err := NewClient(
Config{
Transport: mockhttp.NewRoundTripFunc(t, func(request *http.Request) (*http.Response, error) {
return &http.Response{
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("")),
}, nil
}),
})
require.NoError(t, err)
})
t.Run("With User:Password", func(t *testing.T) {
c, err := NewClient(
Config{
Addresses: []string{"http://admin:admin@localhost:8080//"},
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
},
)
require.NoError(t, err)
u := c.Transport.(*opensearchtransport.Transport).URLs()[0].String()
require.Equal(t, "http://admin:admin@localhost:8080", u)
})
t.Run("With DiscoverNodes on start", func(t *testing.T) {
c, err := NewClient(
Config{
Addresses: []string{"http://localhost:8080//"},
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
DiscoverNodesOnStart: boolPtr(true),
},
)
require.NoError(t, err)
u := c.Transport.(*opensearchtransport.Transport).URLs()[0].String()
require.Equal(t, "http://localhost:8080", u)
})
t.Run("With failing creation", func(t *testing.T) {
_, err := NewClient(
Config{
Addresses: []string{"http://admin:admin@localhost:8080//"},
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
CACert: []byte{1},
},
)
require.ErrorIs(t, err, ErrCreateTransport)
})
}
func TestClientInterfe(t *testing.T) {
t.Run("Stream()", func(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
call := called
res, err := c.Stream(&http.Request{URL: &url.URL{Path: "/test"}, Header: make(http.Header)})
if err == nil && res != nil && res.Body != nil {
res.Body.Close()
}
require.Equal(t, called-1, call, "Expected client to call transport")
})
t.Run("Do()", func(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
req := testReq{}
resp, err := Execute[NoBody](t.Context(), c, http.MethodGet, req, nil)
require.NoError(t, err)
require.NotNil(t, resp)
})
t.Run("Generic Execute()", func(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
type versionInfo struct {
Number string `json:"number"`
Distribution string `json:"distribution"`
}
type rootResp struct {
Version versionInfo `json:"version"`
}
var got rootResp
resp, err := Execute(t.Context(), c, http.MethodGet, testReq{Path: "/"}, &got)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, "1.0.0", got.Version.Number)
require.Equal(t, "opensearch", got.Version.Distribution)
})
t.Run("Generic Execute() nil NoBody pointer", func(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
resp, err := Execute[NoBody](t.Context(), c, http.MethodGet, testReq{Path: "/"}, nil)
require.NoError(t, err)
require.NotNil(t, resp)
})
t.Run("Do() GetRequest error", func(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
req := testReq{Error: true}
resp, err := Execute[NoBody](t.Context(), c, http.MethodGet, req, nil)
require.Error(t, err)
require.Nil(t, resp)
})
t.Run("Do() Unmarshal error", func(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
type failStr struct {
Version int `json:"version"`
}
req := testReq{Path: "/"}
resp, err := Execute(t.Context(), c, http.MethodGet, req, &failStr{})
require.Error(t, err)
require.ErrorIs(t, err, ErrJSONUnmarshalBody)
require.NotNil(t, resp)
})
t.Run("Do() io read error", func(t *testing.T) {
c, err := NewClient(
Config{
Transport: mockhttp.NewRoundTripFunc(t, func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(iotest.ErrReader(errors.New("io reader test"))),
},
nil
}),
},
)
require.NoError(t, err)
type failStr struct {
Version int `json:"version"`
}
req := testReq{}
resp, err := Execute(t.Context(), c, http.MethodGet, req, &failStr{})
require.Error(t, err)
require.ErrorIs(t, err, ErrReadBody)
require.NotNil(t, resp)
})
}
// TestResponseString_RawBody verifies that String renders from the buffered
// rawBody set by Do and never consumes Body, so a value copy (as fmt makes)
// renders the body and Body remains readable afterwards.
func TestResponseString_RawBody(t *testing.T) {
c, err := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
require.NoError(t, err)
type versionInfo struct {
Number string `json:"number"`
}
type rootResp struct {
Version versionInfo `json:"version"`
}
var got rootResp
resp, err := Execute(t.Context(), c, http.MethodGet, testReq{Path: "/"}, &got)
require.NoError(t, err)
require.NotNil(t, resp)
require.NotNil(t, resp.rawBody, "Do should buffer the decoded body into rawBody")
// A value copy renders the buffered body without draining Body.
valueCopy := *resp
rendered := valueCopy.String()
require.Contains(t, rendered, `"number" : "1.0.0"`)
// Body is still fully readable after rendering.
body, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.Contains(t, string(body), `"number" : "1.0.0"`)
}
// TestResponseString_ErrorBodyNotDrained reproduces the canonical error-handling
// flow: logging an error response with String() must not drain Body, so a
// subsequent ParseError still sees the real API error. Do buffers error-response
// bodies into rawBody, so String takes the rawBody fast-path and never touches
// Body. Without that buffering, the value-receiver String would consume the
// single-use error stream and ParseError would read an empty body.
func TestResponseString_ErrorBodyNotDrained(t *testing.T) {
const errBody = `{"error":{"type":"index_not_found_exception","reason":"no such index"},"status":404}`
rt := mockhttp.NewRoundTripFunc(t, func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusNotFound,
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: io.NopCloser(strings.NewReader(errBody)),
}, nil
})
c, err := NewClient(Config{Transport: rt})
require.NoError(t, err)
type rootResp struct {
Version struct{ Number string } `json:"version"`
}
var got rootResp
resp, err := Execute(t.Context(), c, http.MethodGet, testReq{Path: "/"}, &got)
require.NoError(t, err)
require.NotNil(t, resp)
require.True(t, resp.IsError())
require.NotNil(t, resp.rawBody, "Do should buffer error-response bodies into rawBody")
// Logging the error via String (value copy, as fmt makes) must not drain Body.
valueCopy := *resp
_ = valueCopy.String()
// ParseError still reads the intact body and surfaces the real API error,
// not ErrJSONUnmarshalBody from an empty payload.
perr := ParseError(resp)
require.Error(t, perr)
require.NotErrorIs(t, perr, ErrJSONUnmarshalBody)
require.Contains(t, perr.Error(), "index_not_found_exception")
}
// fakeTransport is an opensearchtransport.Interface that returns a fixed
// (response, error) pair, used to exercise Client.Do's handling of the
// (resp != nil, err != nil) contract that Stream may return.
type fakeTransport struct {
resp *http.Response
err error
}
func (f fakeTransport) Stream(*http.Request) (*http.Response, error) {
return f.resp, f.err
}
func (f fakeTransport) Request(req *http.Request) (*http.Response, error) {
return f.Stream(req)
}
// TestDoStreamErrorClassification verifies that Client.Do only labels a
// returned error as ErrReadBody when it is a genuine body-read failure
// (signaled by opensearchtransport.ErrResponseBodyRead). An unrelated transport
// error returned alongside a response -- e.g. context cancellation during retry
// backoff -- must surface with its identity intact and without the misleading
// "failed to read body" prefix.
func TestDoStreamErrorClassification(t *testing.T) {
t.Parallel()
tests := []struct {
name string
streamErr error
wantReadErr bool // expect errors.Is(err, ErrReadBody)
wantIs error // an error the result must still wrap (nil to skip)
wantNotMsg string // substring that must NOT appear in the message (empty to skip)
}{
{
name: "genuine body-read failure is labeled ErrReadBody",
streamErr: fmt.Errorf("%w: %w", opensearchtransport.ErrResponseBodyRead, io.ErrUnexpectedEOF),
wantReadErr: true,
wantIs: io.ErrUnexpectedEOF,
},
{
name: "context cancellation during backoff is not ErrReadBody",
streamErr: context.Canceled,
wantReadErr: false,
wantIs: context.Canceled,
wantNotMsg: "failed to read body",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
// Disable on-start discovery: this test swaps c.Transport after
// construction, which would race with the discovery goroutine that
// the default (router-on) config otherwise spawns.
noDiscovery := false
c, err := NewClient(Config{
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
DiscoverNodesOnStart: &noDiscovery,
})
require.NoError(t, err)
// Replace the transport with one that returns a fixed
// (response, error) pair so Do exercises the (resp != nil,
// err != nil) classification path.
c.Transport = fakeTransport{
resp: &http.Response{
StatusCode: http.StatusServiceUnavailable,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("")),
},
err: tt.streamErr,
}
resp, err := Execute[NoBody](t.Context(), c, http.MethodGet, testReq{Path: "/test"}, nil)
require.Error(t, err)
require.NotNil(t, resp, "response must be returned alongside the error")
if tt.wantReadErr {
require.ErrorIs(t, err, ErrReadBody)
} else {
require.NotErrorIs(t, err, ErrReadBody)
}
if tt.wantIs != nil {
require.ErrorIs(t, err, tt.wantIs)
}
if tt.wantNotMsg != "" {
require.NotContains(t, err.Error(), tt.wantNotMsg)
}
})
}
}
// headerCapturingTransport records the request header it is handed so a test
// can assert Client.Do never dispatches a request with a nil Header map.
type headerCapturingTransport struct {
gotHeader http.Header
called bool
}
func (h *headerCapturingTransport) Stream(req *http.Request) (*http.Response, error) {
h.called = true
h.gotHeader = req.Header
return &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{},
Body: io.NopCloser(strings.NewReader("{}")),
}, nil
}
func (h *headerCapturingTransport) Request(req *http.Request) (*http.Response, error) {
return h.Stream(req)
}
// TestDoInitializesNilRequestHeader guards the contract that Client.Do routes
// through Client.Stream, which allocates req.Header when a Request builds one
// with a nil Header. A custom transport that touches req.Header (e.g. calls
// req.SetBasicAuth) would otherwise panic with "assignment to entry in nil
// map". Regression test for the Perform -> Stream transition.
func TestDoInitializesNilRequestHeader(t *testing.T) {
t.Parallel()
noDiscovery := false
c, err := NewClient(Config{
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
DiscoverNodesOnStart: &noDiscovery,
})
require.NoError(t, err)
tr := &headerCapturingTransport{}
c.Transport = tr
// testReq with no Headers builds an *http.Request whose Header is nil.
resp, err := Execute[NoBody](t.Context(), c, http.MethodGet, testReq{Path: "/test"}, nil)
require.NoError(t, err)
require.NotNil(t, resp)
require.True(t, tr.called, "transport must be invoked")
require.NotNil(t, tr.gotHeader, "Do must hand the transport a non-nil request Header")
}
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 {
require.Error(t, err)
require.Contains(t, err.Error(), tc.err.Error())
}
for i := range tc.urls {
require.Equal(t, tc.urls[i].Scheme, res[i].Scheme, tc.name)
}
for i := range tc.urls {
require.Equal(t, tc.urls[i].Host, res[i].Host, tc.name)
}
for i := range tc.urls {
require.Equal(t, tc.urls[i].Path, res[i].Path, tc.name)
}
})
}
}
func TestVersion(t *testing.T) {
require.NotEmpty(t, Version)
}
func TestClientMetrics(t *testing.T) {
c, _ := NewClient(Config{Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc)})
m, err := c.Metrics()
require.NoError(t, err)
require.LessOrEqual(t, m.Requests, 1, 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) {
major, minor, patch, err := ParseVersion(tt.version)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}
require.Equal(t, major, tt.major)
require.Equal(t, minor, tt.minor)
require.Equal(t, patch, tt.patch)
})
}
}
func TestToPointer(t *testing.T) {
testPointer := ToPointer(true)
require.NotNil(t, testPointer)
require.True(t, *testPointer)
}
func TestClientGetConfig(t *testing.T) {
t.Run("returns config", func(t *testing.T) {
expectedAddresses := []string{"http://localhost:9200"}
expectedUsername := "admin"
expectedMaxRetries := 5
osClient, err := NewClient(Config{
Addresses: expectedAddresses,
Username: expectedUsername,
MaxRetries: expectedMaxRetries,
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
})
require.NoError(t, err)
config := osClient.GetConfig()
require.Equal(t, expectedAddresses, config.Addresses)
require.Equal(t, expectedUsername, config.Username)
require.Equal(t, expectedMaxRetries, config.MaxRetries)
})
t.Run("preserves all config fields", func(t *testing.T) {
expectedConfig := Config{
Addresses: []string{"http://localhost:9200", "http://localhost:9201"},
Username: "testuser",
Password: "testpass",
DisableRetry: true,
MaxRetries: 10,
CompressRequestBody: true,
EnableRetryOnTimeout: true,
Transport: mockhttp.NewRoundTripFunc(t, defaultRoundTripFunc),
}
osClient, err := NewClient(expectedConfig)
require.NoError(t, err)
config := osClient.GetConfig()
require.Equal(t, expectedConfig.Addresses, config.Addresses)
require.Equal(t, expectedConfig.Username, config.Username)
require.Equal(t, expectedConfig.Password, config.Password)
require.Equal(t, expectedConfig.DisableRetry, config.DisableRetry)
require.Equal(t, expectedConfig.MaxRetries, config.MaxRetries)
require.Equal(t, expectedConfig.CompressRequestBody, config.CompressRequestBody)
require.Equal(t, expectedConfig.EnableRetryOnTimeout, config.EnableRetryOnTimeout)
})
}
func TestClientClose(t *testing.T) {
t.Run("release hook takes precedence", func(t *testing.T) {
count := 0
c := &Client{release: func() error { count++; return nil }}
require.NoError(t, c.Close())
require.Equal(t, 1, count)
})
t.Run("falls back to transport io.Closer", func(t *testing.T) {
tc := &stubTransportCloser{}
c := &Client{Transport: tc}
require.NoError(t, c.Close())
require.Equal(t, 1, tc.closed)
})
t.Run("no-op when transport lacks Close", func(t *testing.T) {
c := &Client{Transport: stubStreamOnly{}}
require.NoError(t, c.Close())
})
}
// stubTransportCloser implements opensearchtransport.Interface + io.Closer.
type stubTransportCloser struct{ closed int }
//nolint:nilnil // stub: Stream is never called, only Close is exercised
func (s *stubTransportCloser) Stream(*http.Request) (*http.Response, error) { return nil, nil }
//nolint:nilnil // stub: Request is never called, only Close is exercised
func (s *stubTransportCloser) Request(*http.Request) (*http.Response, error) { return nil, nil }
//nolint:unparam // Close must return error to satisfy io.Closer; stub never fails
func (s *stubTransportCloser) Close() error { s.closed++; return nil }
// stubStreamOnly implements only opensearchtransport.Interface.
type stubStreamOnly struct{}
//nolint:nilnil // stub: Stream is never called, exists only to satisfy Interface
func (stubStreamOnly) Stream(*http.Request) (*http.Response, error) { return nil, nil }
//nolint:nilnil // stub: Request is never called, exists only to satisfy Interface
func (stubStreamOnly) Request(*http.Request) (*http.Response, error) { return nil, nil }
func TestConfigKey(t *testing.T) {
t.Run("hashable configs", func(t *testing.T) {
tests := []struct {
name string
a, b Config
wantEq bool
}{
{"identical empty", Config{}, Config{}, true},
{"same addresses", Config{Addresses: []string{"http://x:9200"}}, Config{Addresses: []string{"http://x:9200"}}, true},
{"diff addresses", Config{Addresses: []string{"http://x:9200"}}, Config{Addresses: []string{"http://y:9200"}}, false},
{"diff username", Config{Username: "a"}, Config{Username: "b"}, false},
{"diff password", Config{Password: "a"}, Config{Password: "b"}, false},
{"diff insecure", Config{InsecureSkipVerify: true}, Config{}, false},
{"diff cacert", Config{CACert: []byte("a")}, Config{CACert: []byte("b")}, false},
{"diff maxretries", Config{MaxRetries: 3}, Config{MaxRetries: 5}, false},
{"diff request timeout", Config{RequestTimeout: time.Second}, Config{}, false},
{"same header", Config{Header: http.Header{"X": {"1"}}}, Config{Header: http.Header{"X": {"1"}}}, true},
{"diff header", Config{Header: http.Header{"X": {"1"}}}, Config{Header: http.Header{"X": {"2"}}}, false},
{
"header value-count boundary",
Config{Header: http.Header{"a": {"b"}, "c": {"d"}}},
Config{Header: http.Header{"a": {"b", "c", "d"}}},
false,
},
{"diff retry-on-status", Config{RetryOnStatus: []int{502}}, Config{RetryOnStatus: []int{503}}, false},
{"same retry-on-status", Config{RetryOnStatus: []int{502, 503}}, Config{RetryOnStatus: []int{502, 503}}, true},
{
"discover-on-start true vs false",
Config{DiscoverNodesOnStart: boolPtr(true)},
Config{DiscoverNodesOnStart: boolPtr(false)},
false,
},
{
"discover-on-start explicit false vs nil auto",
Config{DiscoverNodesOnStart: boolPtr(false)},
Config{},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ha, oka := configKey(tt.a)
hb, okb := configKey(tt.b)
require.True(t, oka)
require.True(t, okb)
if tt.wantEq {
require.Equal(t, ha, hb)
} else {
require.NotEqual(t, ha, hb)
}
})
}
})
t.Run("un-hashable configs bypass", func(t *testing.T) {
tests := []struct {
name string
cfg Config
}{
{"transport", Config{Transport: http.DefaultTransport}},
{"context", Config{Context: context.Background()}},
{"retry backoff", Config{RetryBackoff: func(int) time.Duration { return 0 }}},
{"health modifier", Config{HealthCheckRequestModifier: func(*http.Request) {}}},
{"operation classifier", Config{OperationClassifier: opensearchtransport.NewOperationClassifier()}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, ok := configKey(tt.cfg)
require.False(t, ok, "un-hashable field must force bypass")
})
}
})
}
// TestCachedDefaultKeyNotCacheable verifies cachedDefault.Key surfaces
// ttlcache.ErrNotCacheable for an un-hashable config, so GetOrCreate falls
// back to a direct build instead of caching a client that cannot be keyed.
func TestCachedDefaultKeyNotCacheable(t *testing.T) {
d := cachedDefault{cfg: Config{Transport: http.DefaultTransport}}
_, err := d.Key()
require.ErrorIs(t, err, ttlcache.ErrNotCacheable)
}
// TestConfigKey_FieldGuard fails loudly when Config grows a field without a
// corresponding update to configKey, preventing a silent cache-key collision.
func TestConfigKey_FieldGuard(t *testing.T) {
const knownFieldCount = 48
got := reflect.TypeFor[Config]().NumField()
require.Equal(t, knownFieldCount, got,
"Config field count changed: audit configKey for the new field, then update knownFieldCount")
}
func TestNewDefaultClientCaches(t *testing.T) {
c1, err := NewDefaultClient()
require.NoError(t, err)
t.Cleanup(func() { _ = c1.Close() })
c2, err := NewDefaultClient()
require.NoError(t, err)
t.Cleanup(func() { _ = c2.Close() })
require.NotSame(t, c1, c2, "each call returns a distinct *Client wrapper")
require.Same(t, c1.Transport, c2.Transport, "identical default config must share one transport")
require.NotNil(t, c1.release, "cached client must carry a release hook")
require.NotNil(t, c1.GetConfig(), "cached client must preserve config threaded through the shared handle")
}
// TestNewClientNeverCaches locks in the acceptance criterion that user-built
// NewClient(cfg) clients never enter the shared cache -- only the implicit
// default path (NewDefaultClient) is cached. Two explicit calls with identical
// config must build independent transports and carry no cache release hook.
func TestNewClientNeverCaches(t *testing.T) {
cfg := Config{Addresses: []string{"http://never-cache-core:9200"}}
c1, err := NewClient(cfg)
require.NoError(t, err)
t.Cleanup(func() { _ = c1.Close() })
c2, err := NewClient(cfg)
require.NoError(t, err)
t.Cleanup(func() { _ = c2.Close() })
require.NotSame(t, c1.Transport, c2.Transport,
"explicit NewClient must not share a cached transport")
require.Nil(t, c1.release, "explicit NewClient must not carry a cache release hook")
}