From 2bea8b437e319f4c0d499a41176a82ec572e9a8b Mon Sep 17 00:00:00 2001 From: Amir Doroudian Date: Tue, 14 May 2024 10:22:54 +0330 Subject: [PATCH] Removed usage of deprecated io/ioutil package --- .github/workflows/ci.yml | 2 +- circuitbreaker/hystrix_test.go | 4 ++-- metrics/generic/generic_test.go | 4 ++-- metrics/prometheus/prometheus_test.go | 10 +++++----- sd/etcd/client.go | 4 ++-- transport/http/client.go | 5 ++--- transport/http/client_test.go | 9 ++++----- transport/http/jsonrpc/client.go | 4 ++-- transport/http/jsonrpc/client_test.go | 5 ++--- transport/http/jsonrpc/server_test.go | 13 ++++++------- transport/http/proto/client.go | 4 ++-- transport/http/proto/proto_test.go | 6 +++--- transport/http/server_test.go | 12 ++++++------ transport/httprp/server_test.go | 8 ++++---- 14 files changed, 43 insertions(+), 47 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1c270e624..ca22d40a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: # Support latest and one minor back - go: ["1.17", "1.18", "1.19"] + go: ["1.18", "1.19", "1.20", "1.21", "1.22"] env: GOFLAGS: -mod=readonly diff --git a/circuitbreaker/hystrix_test.go b/circuitbreaker/hystrix_test.go index da527576e..36bf6c2da 100644 --- a/circuitbreaker/hystrix_test.go +++ b/circuitbreaker/hystrix_test.go @@ -1,7 +1,7 @@ package circuitbreaker_test import ( - "io/ioutil" + "io" stdlog "log" "testing" "time" @@ -12,7 +12,7 @@ import ( ) func TestHystrix(t *testing.T) { - stdlog.SetOutput(ioutil.Discard) + stdlog.SetOutput(io.Discard) const ( commandName = "my-endpoint" diff --git a/metrics/generic/generic_test.go b/metrics/generic/generic_test.go index 435f0fc2d..415a063e8 100644 --- a/metrics/generic/generic_test.go +++ b/metrics/generic/generic_test.go @@ -10,9 +10,9 @@ import ( "go/parser" "go/token" "go/types" - "io/ioutil" "math" "math/rand" + "os" "sync" "testing" @@ -119,7 +119,7 @@ func TestSimpleHistogram(t *testing.T) { // But currently works for Counter and Gauge. // To have a more solid test, this test should be removed and the other tests should be run on a 32-bit arch. func TestAtomicAlignment(t *testing.T) { - content, err := ioutil.ReadFile("./generic.go") + content, err := os.ReadFile("./generic.go") if err != nil { t.Fatal(err) } diff --git a/metrics/prometheus/prometheus_test.go b/metrics/prometheus/prometheus_test.go index deb15866f..75e2957a4 100644 --- a/metrics/prometheus/prometheus_test.go +++ b/metrics/prometheus/prometheus_test.go @@ -1,7 +1,7 @@ package prometheus import ( - "io/ioutil" + "io" "math" "math/rand" "net/http" @@ -23,7 +23,7 @@ func TestCounter(t *testing.T) { scrape := func() string { resp, _ := http.Get(s.URL) - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) return string(buf) } @@ -54,7 +54,7 @@ func TestGauge(t *testing.T) { scrape := func() string { resp, _ := http.Get(s.URL) - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) return string(buf) } @@ -85,7 +85,7 @@ func TestSummary(t *testing.T) { scrape := func() string { resp, _ := http.Get(s.URL) - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) return string(buf) } @@ -130,7 +130,7 @@ func TestHistogram(t *testing.T) { scrape := func() string { resp, _ := http.Get(s.URL) - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) return string(buf) } diff --git a/sd/etcd/client.go b/sd/etcd/client.go index 287b699e1..da9743a08 100644 --- a/sd/etcd/client.go +++ b/sd/etcd/client.go @@ -5,9 +5,9 @@ import ( "crypto/tls" "crypto/x509" "errors" - "io/ioutil" "net" "net/http" + "os" "time" etcd "go.etcd.io/etcd/client/v2" @@ -81,7 +81,7 @@ func NewClient(ctx context.Context, machines []string, options ClientOptions) (C if err != nil { return nil, err } - caCertCt, err := ioutil.ReadFile(options.CACert) + caCertCt, err := os.ReadFile(options.CACert) if err != nil { return nil, err } diff --git a/transport/http/client.go b/transport/http/client.go index 7d868eee4..cfc5a72e8 100644 --- a/transport/http/client.go +++ b/transport/http/client.go @@ -6,7 +6,6 @@ import ( "encoding/json" "encoding/xml" "io" - "io/ioutil" "net/http" "net/url" @@ -180,7 +179,7 @@ func EncodeJSONRequest(c context.Context, r *http.Request, request interface{}) } } var b bytes.Buffer - r.Body = ioutil.NopCloser(&b) + r.Body = io.NopCloser(&b) return json.NewEncoder(&b).Encode(request) } @@ -195,7 +194,7 @@ func EncodeXMLRequest(c context.Context, r *http.Request, request interface{}) e } } var b bytes.Buffer - r.Body = ioutil.NopCloser(&b) + r.Body = io.NopCloser(&b) return xml.NewEncoder(&b).Encode(request) } diff --git a/transport/http/client_test.go b/transport/http/client_test.go index 3748546c1..621639356 100644 --- a/transport/http/client_test.go +++ b/transport/http/client_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "net/url" @@ -205,7 +204,7 @@ func TestEncodeJSONRequest(t *testing.T) { var body string server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil && err != io.EOF { t.Fatal(err) } @@ -266,7 +265,7 @@ func TestSetClient(t *testing.T) { var ( encode = func(context.Context, *http.Request, interface{}) error { return nil } decode = func(_ context.Context, r *http.Response) (interface{}, error) { - t, err := ioutil.ReadAll(r.Body) + t, err := io.ReadAll(r.Body) if err != nil { return nil, err } @@ -278,7 +277,7 @@ func TestSetClient(t *testing.T) { return &http.Response{ StatusCode: http.StatusOK, Request: req, - Body: ioutil.NopCloser(bytes.NewBufferString("hello, world!")), + Body: io.NopCloser(bytes.NewBufferString("hello, world!")), }, nil }) @@ -311,7 +310,7 @@ func TestNewExplicitClient(t *testing.T) { } dec := func(_ context.Context, resp *http.Response) (response interface{}, err error) { - buf, err := ioutil.ReadAll(resp.Body) + buf, err := io.ReadAll(resp.Body) resp.Body.Close() return string(buf), err } diff --git a/transport/http/jsonrpc/client.go b/transport/http/jsonrpc/client.go index 14105aa8d..cec13b667 100644 --- a/transport/http/jsonrpc/client.go +++ b/transport/http/jsonrpc/client.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "encoding/json" - "io/ioutil" + "io" "net/http" "net/url" "sync/atomic" @@ -179,7 +179,7 @@ func (c Client) Endpoint() endpoint.Endpoint { req.Header.Set("Content-Type", "application/json; charset=utf-8") var b bytes.Buffer - req.Body = ioutil.NopCloser(&b) + req.Body = io.NopCloser(&b) err = json.NewEncoder(&b).Encode(rpcReq) if err != nil { return nil, err diff --git a/transport/http/jsonrpc/client_test.go b/transport/http/jsonrpc/client_test.go index 462e0fc7d..f76019e41 100644 --- a/transport/http/jsonrpc/client_test.go +++ b/transport/http/jsonrpc/client_test.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "io" - "io/ioutil" "net/http" "net/http/httptest" "net/url" @@ -169,7 +168,7 @@ func TestClientHappyPath(t *testing.T) { t.Fatal("Header not set by before func.") } - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil && err != io.EOF { t.Fatal(err) } @@ -248,7 +247,7 @@ func TestCanUseDefaults(t *testing.T) { ) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - b, err := ioutil.ReadAll(r.Body) + b, err := io.ReadAll(r.Body) if err != nil && err != io.EOF { t.Fatal(err) } diff --git a/transport/http/jsonrpc/server_test.go b/transport/http/jsonrpc/server_test.go index 210d5b34c..44d3e68e8 100644 --- a/transport/http/jsonrpc/server_test.go +++ b/transport/http/jsonrpc/server_test.go @@ -5,7 +5,6 @@ import ( "encoding/json" "errors" "io" - "io/ioutil" "net/http" "net/http/httptest" "strings" @@ -99,7 +98,7 @@ func TestServerBadDecode(t *testing.T) { server := httptest.NewServer(handler) defer server.Close() resp, _ := http.Post(server.URL, "application/json", addBody()) - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d: %s", want, have, buf) } @@ -124,7 +123,7 @@ func TestServerBadEndpoint(t *testing.T) { if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) expectErrorCode(t, jsonrpc.InternalError, buf) expectValidRequestID(t, 1, buf) } @@ -144,7 +143,7 @@ func TestServerBadEncode(t *testing.T) { if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) expectErrorCode(t, jsonrpc.InternalError, buf) expectValidRequestID(t, 1, buf) } @@ -196,7 +195,7 @@ func TestCanRejectInvalidJSON(t *testing.T) { if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) expectErrorCode(t, jsonrpc.ParseError, buf) expectNilRequestID(t, buf) } @@ -210,7 +209,7 @@ func TestServerUnregisteredMethod(t *testing.T) { if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) expectErrorCode(t, jsonrpc.MethodNotFoundError, buf) } @@ -219,7 +218,7 @@ func TestServerHappyPath(t *testing.T) { step() resp := <-response defer resp.Body.Close() // nolint - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d (%s)", want, have, buf) } diff --git a/transport/http/proto/client.go b/transport/http/proto/client.go index 25c6dbd9a..4f5aadc23 100644 --- a/transport/http/proto/client.go +++ b/transport/http/proto/client.go @@ -4,7 +4,7 @@ import ( "bytes" "context" "errors" - "io/ioutil" + "io" "net/http" httptransport "github.com/go-kit/kit/transport/http" @@ -32,6 +32,6 @@ func EncodeProtoRequest(_ context.Context, r *http.Request, preq interface{}) er return err } r.ContentLength = int64(len(b)) - r.Body = ioutil.NopCloser(bytes.NewReader(b)) + r.Body = io.NopCloser(bytes.NewReader(b)) return nil } diff --git a/transport/http/proto/proto_test.go b/transport/http/proto/proto_test.go index 1cc27f6d7..77b6576b9 100644 --- a/transport/http/proto/proto_test.go +++ b/transport/http/proto/proto_test.go @@ -2,7 +2,7 @@ package proto import ( "context" - "io/ioutil" + "io" "net/http" "net/http/httptest" "testing" @@ -27,7 +27,7 @@ func TestEncodeProtoRequest(t *testing.T) { return } - bod, err := ioutil.ReadAll(r.Body) + bod, err := io.ReadAll(r.Body) if err != nil { t.Errorf("expected no read errors but got: %s", err) return @@ -71,7 +71,7 @@ func TestEncodeProtoResponse(t *testing.T) { return } - bod, err := ioutil.ReadAll(w.Body) + bod, err := io.ReadAll(w.Body) if err != nil { t.Errorf("expected no read errors but got: %s", err) return diff --git a/transport/http/server_test.go b/transport/http/server_test.go index 5c0fadb29..cef835050 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -3,7 +3,7 @@ package http_test import ( "context" "errors" - "io/ioutil" + "io" "net/http" "net/http/httptest" "strings" @@ -83,7 +83,7 @@ func TestServerHappyPath(t *testing.T) { step() resp := <-response defer resp.Body.Close() - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) if want, have := http.StatusOK, resp.StatusCode; want != have { t.Errorf("want %d, have %d (%s)", want, have, buf) } @@ -255,7 +255,7 @@ func TestEncodeJSONResponse(t *testing.T) { if want, have := "Snowden", resp.Header.Get("X-Edward"); want != have { t.Errorf("X-Edward: want %q, have %q", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) if want, have := `{"foo":"bar"}`, strings.TrimSpace(string(buf)); want != have { t.Errorf("Body: want %s, have %s", want, have) } @@ -327,7 +327,7 @@ func TestAddMultipleHeadersErrorEncoder(t *testing.T) { t.Errorf("Header: unexpected header %s: %v", k, expect[k]) } } - if b, _ := ioutil.ReadAll(resp.Body); errStr != string(b) { + if b, _ := io.ReadAll(resp.Body); errStr != string(b) { t.Errorf("ErrorEncoder: got: %q, expected: %q", b, errStr) } } @@ -353,7 +353,7 @@ func TestEncodeNoContent(t *testing.T) { if want, have := http.StatusNoContent, resp.StatusCode; want != have { t.Errorf("StatusCode: want %d, have %d", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) if want, have := 0, len(buf); want != have { t.Errorf("Body: want no content, have %d bytes", have) } @@ -387,7 +387,7 @@ func TestEnhancedError(t *testing.T) { if want, have := "1", resp.Header.Get("X-Enhanced"); want != have { t.Errorf("X-Enhanced: want %q, have %q", want, have) } - buf, _ := ioutil.ReadAll(resp.Body) + buf, _ := io.ReadAll(resp.Body) if want, have := `{"err":"enhanced"}`, strings.TrimSpace(string(buf)); want != have { t.Errorf("Body: want %s, have %s", want, have) } diff --git a/transport/httprp/server_test.go b/transport/httprp/server_test.go index fc5357601..4ee8f180d 100644 --- a/transport/httprp/server_test.go +++ b/transport/httprp/server_test.go @@ -2,7 +2,7 @@ package httprp_test import ( "context" - "io/ioutil" + "io" "net/http" "net/http/httptest" "net/url" @@ -30,7 +30,7 @@ func TestServerHappyPathSingleServer(t *testing.T) { t.Errorf("want %d, have %d", want, have) } - responseBody, _ := ioutil.ReadAll(resp.Body) + responseBody, _ := io.ReadAll(resp.Body) if want, have := "hey", string(responseBody); want != have { t.Errorf("want %q, have %q", want, have) } @@ -68,7 +68,7 @@ func TestServerHappyPathSingleServerWithServerOptions(t *testing.T) { t.Errorf("want %d, have %d", want, have) } - responseBody, _ := ioutil.ReadAll(resp.Body) + responseBody, _ := io.ReadAll(resp.Body) if want, have := "hey", string(responseBody); want != have { t.Errorf("want %q, have %q", want, have) } @@ -151,7 +151,7 @@ func TestMultipleServerBefore(t *testing.T) { t.Errorf("want %d, have %d", want, have) } - responseBody, _ := ioutil.ReadAll(resp.Body) + responseBody, _ := io.ReadAll(resp.Body) if want, have := "hey", string(responseBody); want != have { t.Errorf("want %q, have %q", want, have) }