Skip to content

Commit

Permalink
remove the usage of deprecated io/ioutil package
Browse files Browse the repository at this point in the history
Signed-off-by: myan <myan@redhat.com>
  • Loading branch information
yanmxa committed Jul 18, 2023
1 parent e15d03d commit 766b88e
Show file tree
Hide file tree
Showing 15 changed files with 56 additions and 62 deletions.
7 changes: 2 additions & 5 deletions observability/opencensus/v2/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -113,7 +113,6 @@ func TestTracedClientReceiveError(t *testing.T) {
defer trace.UnregisterExporter(&te)

t.Run("RecordCallingInvoker error", func(t *testing.T) {

evt := func() event.Event {
e := event.Event{
Context: event.EventContextV03{
Expand Down Expand Up @@ -272,7 +271,6 @@ func TestTracingClientSend(t *testing.T) {
}

func TestTracingClientSendError(t *testing.T) {

// simple exporter that holds the spans in an array
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
var te testExporter
Expand All @@ -291,7 +289,6 @@ func TestTracingClientSendError(t *testing.T) {
defer ts.Close()

t.Run("RecordSendingEvent error", func(t *testing.T) {

sender := simpleTracingBinaryClient(t, ts.URL, fakeObservabilityServiceWithError{})
event := func() event.Event {
e := event.Event{
Expand Down Expand Up @@ -391,7 +388,7 @@ func (f *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

// Make a copy of the request.
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
f.t.Error("failed to read the request body")
}
Expand Down
11 changes: 6 additions & 5 deletions protocol/amqp/v2/write_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package amqp
import (
"context"
"io"
"io/ioutil"

"github.com/Azure/go-amqp"

Expand Down Expand Up @@ -37,7 +36,7 @@ func WriteMessage(ctx context.Context, m binding.Message, amqpMessage *amqp.Mess
type amqpMessageWriter amqp.Message

func (b *amqpMessageWriter) SetStructuredEvent(ctx context.Context, format format.Format, event io.Reader) error {
val, err := ioutil.ReadAll(event)
val, err := io.ReadAll(event)
if err != nil {
return err
}
Expand All @@ -58,7 +57,7 @@ func (b *amqpMessageWriter) End(ctx context.Context) error {
}

func (b *amqpMessageWriter) SetData(reader io.Reader) error {
data, err := ioutil.ReadAll(reader)
data, err := io.ReadAll(reader)
if err != nil {
return err
}
Expand Down Expand Up @@ -100,5 +99,7 @@ func (b *amqpMessageWriter) SetExtension(name string, value interface{}) error {
return nil
}

var _ binding.BinaryWriter = (*amqpMessageWriter)(nil) // Test it conforms to the interface
var _ binding.StructuredWriter = (*amqpMessageWriter)(nil) // Test it conforms to the interface
var (
_ binding.BinaryWriter = (*amqpMessageWriter)(nil) // Test it conforms to the interface
_ binding.StructuredWriter = (*amqpMessageWriter)(nil) // Test it conforms to the interface
)
13 changes: 7 additions & 6 deletions protocol/ws/v2/client_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"sync"

"nhooyr.io/websocket"
Expand Down Expand Up @@ -110,8 +109,8 @@ func (c *ClientProtocol) UnsafeReceive(ctx context.Context) (binding.Message, er
}

func consumeStream(reader io.Reader) {
//TODO is there a less expensive way to consume the stream?
ioutil.ReadAll(reader)
// TODO is there a less expensive way to consume the stream?
io.ReadAll(reader)
}

func (c *ClientProtocol) Close(ctx context.Context) error {
Expand All @@ -131,6 +130,8 @@ func (c *ClientProtocol) Close(ctx context.Context) error {
return nil
}

var _ protocol.Receiver = (*ClientProtocol)(nil)
var _ protocol.Sender = (*ClientProtocol)(nil)
var _ protocol.Closer = (*ClientProtocol)(nil)
var (
_ protocol.Receiver = (*ClientProtocol)(nil)
_ protocol.Sender = (*ClientProtocol)(nil)
_ protocol.Closer = (*ClientProtocol)(nil)
)
4 changes: 2 additions & 2 deletions samples/http/requester-with-custom-client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -52,7 +52,7 @@ func main() {
if err != nil {
log.Fatalln("unable to load certs", err)
}
clientCACert, err := ioutil.ReadFile(env.ClientCert)
clientCACert, err := os.ReadFile(env.ClientCert)
if err != nil {
log.Fatal("unable to open cert", err)
}
Expand Down
4 changes: 2 additions & 2 deletions sql/v2/test/tck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package test

import (
"io/ioutil"
"io"
"os"
"path"
"runtime"
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestTCK(t *testing.T) {
file, err := os.Open(testFilePath)
require.NoError(t, err)

fileBytes, err := ioutil.ReadAll(file)
fileBytes, err := io.ReadAll(file)
require.NoError(t, err)

tckFileModel := TckFile{}
Expand Down
3 changes: 1 addition & 2 deletions test/benchmark/e2e/http/http_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package main
import (
"bytes"
"io"
"io/ioutil"
nethttp "net/http"
"net/http/httptest"
"net/url"
Expand Down Expand Up @@ -52,7 +51,7 @@ func MockedClient() (cloudevents.Client, *http.Protocol) {
return &nethttp.Response{
StatusCode: 202,
Header: make(nethttp.Header),
Body: ioutil.NopCloser(bytes.NewReader([]byte{})),
Body: io.NopCloser(bytes.NewReader([]byte{})),
}
})

Expand Down
8 changes: 4 additions & 4 deletions test/integration/http/tap_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package http
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -95,12 +95,12 @@ func (t *tapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
r.Header.Del("ce-" + unitTestIDKey)

// Make a copy of the request.
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
fmt.Printf("failed to read the request body")
}
// Set the body back
r.Body = ioutil.NopCloser(bytes.NewReader(body))
r.Body = io.NopCloser(bytes.NewReader(body))

t.req[id] = TapValidation{
Method: r.Method,
Expand Down Expand Up @@ -129,7 +129,7 @@ func (t *tapHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
w.WriteHeader(resp.StatusCode)
body, err = ioutil.ReadAll(resp.Body)
body, err = io.ReadAll(resp.Body)
if err != nil {
fmt.Printf("failed to read the resp body")
}
Expand Down
11 changes: 6 additions & 5 deletions v2/binding/test/mock_binary_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"

"github.com/cloudevents/sdk-go/v2/binding"
"github.com/cloudevents/sdk-go/v2/binding/spec"
Expand Down Expand Up @@ -121,14 +120,16 @@ func (bm *MockBinaryMessage) SetExtension(name string, value interface{}) error
}

func (bm *MockBinaryMessage) SetData(data io.Reader) (err error) {
bm.Body, err = ioutil.ReadAll(data)
bm.Body, err = io.ReadAll(data)
return err
}

func (bm *MockBinaryMessage) End(ctx context.Context) error {
return nil
}

var _ binding.Message = (*MockBinaryMessage)(nil)
var _ binding.MessageMetadataReader = (*MockBinaryMessage)(nil)
var _ binding.BinaryWriter = (*MockBinaryMessage)(nil)
var (
_ binding.Message = (*MockBinaryMessage)(nil)
_ binding.MessageMetadataReader = (*MockBinaryMessage)(nil)
_ binding.BinaryWriter = (*MockBinaryMessage)(nil)
)
9 changes: 5 additions & 4 deletions v2/binding/test/mock_structured_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"testing"

"github.com/cloudevents/sdk-go/v2/binding"
Expand Down Expand Up @@ -49,13 +48,15 @@ func (s *MockStructuredMessage) Finish(error) error { return nil }

func (s *MockStructuredMessage) SetStructuredEvent(ctx context.Context, format format.Format, event io.Reader) (err error) {
s.Format = format
s.Bytes, err = ioutil.ReadAll(event)
s.Bytes, err = io.ReadAll(event)
if err != nil {
return
}

return nil
}

var _ binding.Message = (*MockStructuredMessage)(nil)
var _ binding.StructuredWriter = (*MockStructuredMessage)(nil)
var (
_ binding.Message = (*MockStructuredMessage)(nil)
_ binding.StructuredWriter = (*MockStructuredMessage)(nil)
)
4 changes: 2 additions & 2 deletions v2/binding/utils/structured_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package utils_test
import (
"bytes"
"context"
"io/ioutil"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -23,7 +23,7 @@ func TestNewStructuredMessage(t *testing.T) {
testEvent := test.ConvertEventExtensionsToString(t, test.FullEvent())
jsonBytes := test.MustJSON(t, testEvent)

message := utils.NewStructuredMessage(format.JSON, ioutil.NopCloser(bytes.NewReader(jsonBytes)))
message := utils.NewStructuredMessage(format.JSON, io.NopCloser(bytes.NewReader(jsonBytes)))

require.Equal(t, binding.EncodingStructured, message.ReadEncoding())

Expand Down
12 changes: 5 additions & 7 deletions v2/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net"
"net/http"
Expand Down Expand Up @@ -181,7 +181,7 @@ func TestClientSend(t *testing.T) {
func simpleBinaryOptions(port int, path string) []cehttp.Option {
opts := []cehttp.Option{
cehttp.WithPort(port),
//cehttp.WithBinaryEncoding(),
// cehttp.WithBinaryEncoding(),
}
if len(path) > 0 {
opts = append(opts, cehttp.WithPath(path))
Expand All @@ -192,7 +192,7 @@ func simpleBinaryOptions(port int, path string) []cehttp.Option {
func simpleStructuredOptions(port int, path string) []cehttp.Option {
opts := []cehttp.Option{
cehttp.WithPort(port),
//cehttp.WithStructuredEncoding(),
// cehttp.WithStructuredEncoding(),
}
if len(path) > 0 {
opts = append(opts, cehttp.WithPath(path))
Expand Down Expand Up @@ -268,7 +268,6 @@ func TestClientReceive(t *testing.T) {
for n, tc := range testCases {
for _, path := range []string{"", "/", "/unittest/"} {
t.Run(n+" at path "+path, func(t *testing.T) {

events := make(chan event.Event)

p, err := cehttp.New(tc.optsFn(0, "")...)
Expand Down Expand Up @@ -318,7 +317,7 @@ func TestClientReceive(t *testing.T) {
Method: "POST",
URL: target,
Header: tc.req.Headers,
Body: ioutil.NopCloser(bytes.NewReader(tc.req.Body)),
Body: io.NopCloser(bytes.NewReader(tc.req.Body)),
ContentLength: int64(len(tc.req.Body)),
}

Expand Down Expand Up @@ -373,7 +372,6 @@ func TestClientContext(t *testing.T) {
next.ServeHTTP(w, r.WithContext(ctx))
})
}))

if err != nil {
t.Fatalf("error creating client: %v", err)
}
Expand Down Expand Up @@ -417,7 +415,7 @@ func (f *fakeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()

// Make a copy of the request.
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
f.t.Error("failed to read the request body")
}
Expand Down
5 changes: 2 additions & 3 deletions v2/protocol/http/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"bytes"
"context"
"errors"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -38,7 +38,6 @@ func TestNewMessageFromHttpRequest(t *testing.T) {
for _, tt := range tests {
test.EachEvent(t, test.Events(), func(t *testing.T, eventIn event.Event) {
t.Run(tt.name, func(t *testing.T) {

ctx := context.TODO()
if tt.encoding == binding.EncodingStructured {
ctx = binding.WithForceStructured(ctx)
Expand Down Expand Up @@ -167,7 +166,7 @@ func TestNewMessageFromHttpResponse(t *testing.T) {
Header: http.Header{
"Content-Type": {event.ApplicationCloudEventsJSON},
},
Body: ioutil.NopCloser(bytes.NewReader([]byte(`{"data":"foo","datacontenttype":"application/json","id":"id","source":"source","specversion":"1.0","type":"type"}`))),
Body: io.NopCloser(bytes.NewReader([]byte(`{"data":"foo","datacontenttype":"application/json","id":"id","source":"source","specversion":"1.0","type":"type"}`))),
ContentLength: 113,
},
}, {
Expand Down
7 changes: 3 additions & 4 deletions v2/protocol/http/protocol_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
Expand Down Expand Up @@ -67,7 +66,7 @@ func (p *Protocol) doWithRetry(ctx context.Context, params *cecontext.RetryParam
cecontext.LoggerFrom(ctx).Warnw("could not close request body", zap.Error(err))
}
}()
body, err = ioutil.ReadAll(req.Body)
body, err = io.ReadAll(req.Body)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -134,12 +133,12 @@ func resetBody(req *http.Request, body []byte) {
return
}

req.Body = ioutil.NopCloser(bytes.NewReader(body))
req.Body = io.NopCloser(bytes.NewReader(body))

// do not modify existing GetBody function
if req.GetBody == nil {
req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(body)), nil
return io.NopCloser(bytes.NewReader(body)), nil
}
}
}
Loading

0 comments on commit 766b88e

Please sign in to comment.