Skip to content

Commit a440b0c

Browse files
test: check errors in test suite (#1034)
1 parent 9d9d191 commit a440b0c

8 files changed

+271
-74
lines changed

elasticsearch_benchmark_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ package elasticsearch_test
2222

2323
import (
2424
"context"
25-
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
26-
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
2725
"io/ioutil"
2826
"net/http"
2927
"strconv"
@@ -32,6 +30,8 @@ import (
3230

3331
"github.com/elastic/go-elasticsearch/v9"
3432
"github.com/elastic/go-elasticsearch/v9/esapi"
33+
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
34+
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
3535
)
3636

3737
var defaultResponse = http.Response{
@@ -252,7 +252,7 @@ func (m mockTransp) RoundTrip(request *http.Request) (*http.Response, error) {
252252

253253
func BenchmarkAllocsSearch(t *testing.B) {
254254
t.Run("struct search", func(b *testing.B) {
255-
c, _ := elasticsearch.NewTypedClient(elasticsearch.Config{
255+
c, err := elasticsearch.NewTypedClient(elasticsearch.Config{
256256
Transport: &mockTransp{
257257
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
258258
return &http.Response{
@@ -265,6 +265,10 @@ func BenchmarkAllocsSearch(t *testing.B) {
265265
},
266266
})
267267

268+
if err != nil {
269+
b.Fatalf("Unexpected error when creating a client: %s", err)
270+
}
271+
268272
for i := 0; i < b.N; i++ {
269273
s := c.Search()
270274
s.Index("foo")
@@ -276,7 +280,7 @@ func BenchmarkAllocsSearch(t *testing.B) {
276280
})
277281

278282
t.Run("esdsl search", func(b *testing.B) {
279-
c, _ := elasticsearch.NewTypedClient(elasticsearch.Config{
283+
c, err := elasticsearch.NewTypedClient(elasticsearch.Config{
280284
Transport: &mockTransp{
281285
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
282286
return &http.Response{
@@ -289,6 +293,10 @@ func BenchmarkAllocsSearch(t *testing.B) {
289293
},
290294
})
291295

296+
if err != nil {
297+
b.Fatalf("Unexpected error when creating a client: %s", err)
298+
}
299+
292300
for i := 0; i < b.N; i++ {
293301
s := c.Search()
294302
s.Index("foo")

elasticsearch_internal_test.go

Lines changed: 103 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ import (
4040
"testing"
4141
"time"
4242

43+
"github.com/elastic/elastic-transport-go/v8/elastictransport"
4344
"github.com/elastic/go-elasticsearch/v9/esapi"
4445
"github.com/elastic/go-elasticsearch/v9/typedapi/types"
45-
46-
"github.com/elastic/elastic-transport-go/v8/elastictransport"
4746
)
4847

4948
var metaHeaderReValidation = regexp.MustCompile(`^[a-z]{1,}=[a-z0-9\.\-]{1,}(?:,[a-z]{1,}=[a-z0-9\.\-]+)*$`)
@@ -162,7 +161,10 @@ func TestClientConfiguration(t *testing.T) {
162161
if err == nil {
163162
t.Fatalf("Expected error, got: %v", err)
164163
}
165-
match, _ := regexp.MatchString("both .* are set", err.Error())
164+
match, err := regexp.MatchString("both .* are set", err.Error())
165+
if err != nil {
166+
t.Fatalf("Unexpected error: %s", err)
167+
}
166168
if !match {
167169
t.Errorf("Expected error when addresses from environment and configuration are used together, got: %v", err)
168170
}
@@ -292,7 +294,10 @@ func TestAddrsToURLs(t *testing.T) {
292294
if err == nil {
293295
t.Errorf("Expected error, got: %v", err)
294296
}
295-
match, _ := regexp.MatchString(tc.err.Error(), err.Error())
297+
match, err := regexp.MatchString(tc.err.Error(), err.Error())
298+
if err != nil {
299+
t.Fatalf("Unexpected error: %s", err)
300+
}
296301
if !match {
297302
t.Errorf("Expected err [%s] to match: %s", err.Error(), tc.err.Error())
298303
}
@@ -359,7 +364,10 @@ func TestCloudID(t *testing.T) {
359364
if err == nil {
360365
t.Errorf("Expected error for input %q, got %v", input, err)
361366
}
362-
match, _ := regexp.MatchString("unexpected format", err.Error())
367+
match, err := regexp.MatchString("unexpected format", err.Error())
368+
if err != nil {
369+
t.Fatalf("Unexpected error: %s", err)
370+
}
363371
if !match {
364372
t.Errorf("Unexpected error string: %s", err)
365373
}
@@ -371,7 +379,10 @@ func TestCloudID(t *testing.T) {
371379
if err == nil {
372380
t.Errorf("Expected error for input %q, got %v", input, err)
373381
}
374-
match, _ := regexp.MatchString("illegal base64 data", err.Error())
382+
match, err := regexp.MatchString("illegal base64 data", err.Error())
383+
if err != nil {
384+
t.Fatalf("Unexpected error: %s", err)
385+
}
375386
if !match {
376387
t.Errorf("Unexpected error string: %s", err)
377388
}
@@ -385,7 +396,10 @@ func TestVersion(t *testing.T) {
385396
}
386397

387398
func TestClientMetrics(t *testing.T) {
388-
c, _ := NewClient(Config{EnableMetrics: true})
399+
c, err := NewClient(Config{EnableMetrics: true})
400+
if err != nil {
401+
t.Fatalf("Unexpected error: %s", err)
402+
}
389403

390404
m, err := c.Metrics()
391405
if err != nil {
@@ -482,12 +496,15 @@ func TestResponseCheckOnly(t *testing.T) {
482496

483497
for _, tt := range tests {
484498
t.Run(tt.name, func(t *testing.T) {
485-
c, _ := NewClient(Config{
499+
c, err := NewClient(Config{
486500
Transport: &mockTransp{RoundTripFunc: func(request *http.Request) (*http.Response, error) {
487501
return tt.response, tt.requestErr
488502
}},
489503
})
490-
_, err := c.Cat.Indices()
504+
if err != nil {
505+
t.Fatalf("Unexpected error: %s", err)
506+
}
507+
_, err = c.Cat.Indices()
491508
if (err != nil) != tt.wantErr {
492509
t.Errorf("Unexpected error, got %v, wantErr %v", err, tt.wantErr)
493510
}
@@ -511,7 +528,10 @@ func TestProductCheckError(t *testing.T) {
511528
}))
512529
defer server.Close()
513530

514-
c, _ := NewClient(Config{Addresses: []string{server.URL}, DisableRetry: true})
531+
c, err := NewClient(Config{Addresses: []string{server.URL}, DisableRetry: true})
532+
if err != nil {
533+
t.Fatalf("Unexpected error: %s", err)
534+
}
515535
if _, err := c.Cat.Indices(); err != nil {
516536
t.Fatalf("unexpected error: %s", err)
517537
}
@@ -619,7 +639,10 @@ oftUHvkHS0Vv/LicMEOufFGslb4T9aPJ7oyhoSlz9CfAutDWk/q/
619639
}
620640
defer res.Body.Close()
621641

622-
data, _ := ioutil.ReadAll(res.Body)
642+
data, err := ioutil.ReadAll(res.Body)
643+
if err != nil {
644+
t.Fatalf("Unexpected error: %s", err)
645+
}
623646
if !bytes.Equal(data, body) {
624647
t.Fatalf("unexpected payload returned: expected: %s, got: %s", body, data)
625648
}
@@ -680,7 +703,7 @@ func TestCompatibilityHeader(t *testing.T) {
680703
t.Run(test.name, func(t *testing.T) {
681704
t.Setenv(esCompatHeader, strconv.FormatBool(test.compatibilityHeader))
682705

683-
c, _ := NewClient(Config{
706+
c, err := NewClient(Config{
684707
EnableCompatibilityMode: test.configVar,
685708
Addresses: []string{},
686709
Transport: &mockTransp{
@@ -710,6 +733,10 @@ func TestCompatibilityHeader(t *testing.T) {
710733
},
711734
})
712735

736+
if err != nil {
737+
t.Fatalf("Unexpected error: %s", err)
738+
}
739+
713740
req := &http.Request{URL: &url.URL{}, Header: make(http.Header)}
714741
if test.bodyPresent {
715742
req.Body = ioutil.NopCloser(strings.NewReader("{}"))
@@ -781,7 +808,7 @@ func TestBuildStrippedVersion(t *testing.T) {
781808

782809
func TestMetaHeader(t *testing.T) {
783810
t.Run("MetaHeader with elastictransport", func(t *testing.T) {
784-
tp, _ := elastictransport.New(elastictransport.Config{
811+
tp, err := elastictransport.New(elastictransport.Config{
785812
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
786813
Transport: &mockTransp{
787814
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -799,15 +826,24 @@ func TestMetaHeader(t *testing.T) {
799826
},
800827
},
801828
})
829+
if err != nil {
830+
t.Fatalf("Unexpected error: %s", err)
831+
}
802832

803-
c, _ := NewDefaultClient()
833+
c, err := NewDefaultClient()
834+
if err != nil {
835+
t.Fatalf("Unexpected error: %s", err)
836+
}
804837
c.Transport = tp
805838

806-
_, _ = c.Info()
839+
_, err = c.Info()
840+
if err != nil {
841+
t.Fatalf("Unexpected error: %s", err)
842+
}
807843
})
808844

809845
t.Run("Metaheader with typedclient", func(t *testing.T) {
810-
tp, _ := elastictransport.New(elastictransport.Config{
846+
tp, err := elastictransport.New(elastictransport.Config{
811847
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
812848
Transport: &mockTransp{
813849
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -828,16 +864,22 @@ func TestMetaHeader(t *testing.T) {
828864
},
829865
},
830866
})
867+
if err != nil {
868+
t.Fatalf("Unexpected error: %s", err)
869+
}
831870

832-
c, _ := NewTypedClient(Config{})
871+
c, err := NewTypedClient(Config{})
872+
if err != nil {
873+
t.Fatalf("Unexpected error: %s", err)
874+
}
833875
c.Transport = tp
834876

835877
_, _ = c.Info().Do(nil)
836878
})
837879
}
838880

839881
func TestNewTypedClient(t *testing.T) {
840-
tp, _ := elastictransport.New(elastictransport.Config{
882+
tp, err := elastictransport.New(elastictransport.Config{
841883
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
842884
Transport: &mockTransp{
843885
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -861,8 +903,14 @@ func TestNewTypedClient(t *testing.T) {
861903
},
862904
},
863905
})
906+
if err != nil {
907+
t.Fatalf("Unexpected error: %s", err)
908+
}
864909

865-
c, _ := NewTypedClient(Config{})
910+
c, err := NewTypedClient(Config{})
911+
if err != nil {
912+
t.Fatalf("Unexpected error: %s", err)
913+
}
866914
c.Transport = tp
867915

868916
res, err := c.Info().Do(context.Background())
@@ -884,7 +932,7 @@ func TestContentTypeOverride(t *testing.T) {
884932
t.Run("default JSON Content-Type", func(t *testing.T) {
885933
contentType := "application/json"
886934

887-
tp, _ := elastictransport.New(elastictransport.Config{
935+
tp, err := elastictransport.New(elastictransport.Config{
888936
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
889937
Transport: &mockTransp{
890938
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -902,16 +950,25 @@ func TestContentTypeOverride(t *testing.T) {
902950
},
903951
},
904952
})
953+
if err != nil {
954+
t.Fatalf("Unexpected error: %s", err)
955+
}
905956

906-
c, _ := NewDefaultClient()
957+
c, err := NewDefaultClient()
958+
if err != nil {
959+
t.Fatalf("Unexpected error: %s", err)
960+
}
907961
c.Transport = tp
908962

909-
_, _ = c.Search(c.Search.WithBody(strings.NewReader("")))
963+
_, err = c.Search(c.Search.WithBody(strings.NewReader("")))
964+
if err != nil {
965+
t.Fatalf("Unexpected error: %s", err)
966+
}
910967
})
911968
t.Run("overriden CBOR Content-Type functional options style", func(t *testing.T) {
912969
contentType := "application/cbor"
913970

914-
tp, _ := elastictransport.New(elastictransport.Config{
971+
tp, err := elastictransport.New(elastictransport.Config{
915972
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
916973
Transport: &mockTransp{
917974
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -929,8 +986,14 @@ func TestContentTypeOverride(t *testing.T) {
929986
},
930987
},
931988
})
989+
if err != nil {
990+
t.Fatalf("Unexpected error: %s", err)
991+
}
932992

933-
c, _ := NewDefaultClient()
993+
c, err := NewDefaultClient()
994+
if err != nil {
995+
t.Fatalf("Unexpected error: %s", err)
996+
}
934997
c.Transport = tp
935998

936999
_, _ = c.Search(
@@ -943,7 +1006,7 @@ func TestContentTypeOverride(t *testing.T) {
9431006
t.Run("overriden CBOR Content-Type direct call style", func(t *testing.T) {
9441007
contentType := "application/cbor"
9451008

946-
tp, _ := elastictransport.New(elastictransport.Config{
1009+
tp, err := elastictransport.New(elastictransport.Config{
9471010
URLs: []*url.URL{{Scheme: "http", Host: "foo"}},
9481011
Transport: &mockTransp{
9491012
RoundTripFunc: func(request *http.Request) (*http.Response, error) {
@@ -961,8 +1024,14 @@ func TestContentTypeOverride(t *testing.T) {
9611024
},
9621025
},
9631026
})
1027+
if err != nil {
1028+
t.Fatalf("Unexpected error: %s", err)
1029+
}
9641030

965-
c, _ := NewDefaultClient()
1031+
c, err := NewDefaultClient()
1032+
if err != nil {
1033+
t.Fatalf("Unexpected error: %s", err)
1034+
}
9661035
c.Transport = tp
9671036

9681037
search := esapi.SearchRequest{}
@@ -1197,10 +1266,13 @@ func TestInstrumentation(t *testing.T) {
11971266
instrument.BeforeRequestFunc = test.want.BeforeRequestFunc
11981267
instrument.AfterRequestFunc = test.want.AfterRequestFunc
11991268

1200-
es, _ := NewTypedClient(Config{
1269+
es, err := NewTypedClient(Config{
12011270
Transport: &mockTransp{RoundTripFunc: test.args.roundTripFunc},
12021271
Instrumentation: instrument,
12031272
})
1273+
if err != nil {
1274+
t.Fatalf("Unexpected error: %s", err)
1275+
}
12041276
es.Search().
12051277
Index("foo").
12061278
Query(&types.Query{
@@ -1234,10 +1306,13 @@ func TestInstrumentation(t *testing.T) {
12341306
instrument.BeforeRequestFunc = test.want.BeforeRequestFunc
12351307
instrument.AfterRequestFunc = test.want.AfterRequestFunc
12361308

1237-
es, _ := NewClient(Config{
1309+
es, err := NewClient(Config{
12381310
Transport: &mockTransp{RoundTripFunc: test.args.roundTripFunc},
12391311
Instrumentation: instrument,
12401312
})
1313+
if err != nil {
1314+
t.Fatalf("Unexpected error: %s", err)
1315+
}
12411316
es.Search(
12421317
es.Search.WithIndex("foo"),
12431318
es.Search.WithBody(strings.NewReader("{\"query\":{\"match_all\":{}}}")),

0 commit comments

Comments
 (0)