Skip to content

Commit

Permalink
Remove usage of deprecated ioutil package (#5869)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan <[email protected]>

Co-authored-by: Alex Boten <[email protected]>
  • Loading branch information
bogdandrutu and Alex Boten authored Aug 9, 2022
1 parent cbde6c5 commit b3539e8
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 33 deletions.
3 changes: 1 addition & 2 deletions cmd/builder/internal/builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package builder // import "go.opentelemetry.io/collector/cmd/builder/internal/bu
import (
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -70,7 +69,7 @@ func NewDefaultConfig() Config {
panic(fmt.Sprintf("failed to obtain a logger instance: %v", err))
}

outputDir, err := ioutil.TempDir("", "otelcol-distribution")
outputDir, err := os.MkdirTemp("", "otelcol-distribution")
if err != nil {
log.Error("failed to obtain a temporary directory", zap.Error(err))
}
Expand Down
3 changes: 1 addition & 2 deletions config/configgrpc/configgrpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package configgrpc
import (
"context"
"errors"
"io/ioutil"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -1032,7 +1031,7 @@ func (gts *grpcTraceServer) Export(ctx context.Context, _ ptraceotlp.Request) (p

// tempSocketName provides a temporary Unix socket name for testing.
func tempSocketName(t *testing.T) string {
tmpfile, err := ioutil.TempFile("", "sock")
tmpfile, err := os.CreateTemp("", "sock")
require.NoError(t, err)
require.NoError(t, tmpfile.Close())
socket := tmpfile.Name()
Expand Down
14 changes: 7 additions & 7 deletions config/confighttp/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"compress/gzip"
"compress/zlib"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestHTTPClientCompression(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "failed to read request body: %v", err)
assert.EqualValues(t, tt.reqBody, body)
w.WriteHeader(200)
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestHTTPClientCompression(t *testing.T) {
}
require.NoError(t, err)

_, err = ioutil.ReadAll(res.Body)
_, err = io.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
require.NoError(t, srv.Close())
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestHTTPContentDecompressionHandler(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "failed to read request body: %v", err)
assert.EqualValues(t, testBody, string(body))
w.WriteHeader(200)
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestHTTPContentDecompressionHandler(t *testing.T) {

assert.Equal(t, tt.respCode, res.StatusCode, "test handler returned unexpected status code ")
if tt.respBody != "" {
body, err := ioutil.ReadAll(res.Body)
body, err := io.ReadAll(res.Body)
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
assert.Equal(t, tt.respBody, string(body))
}
Expand All @@ -239,7 +239,7 @@ func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
compressedGzipBody, _ := compressGzip([]byte{})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err, "failed to read request body: %v", err)
assert.EqualValues(t, compressedGzipBody.Bytes(), body)
}))
Expand All @@ -253,7 +253,7 @@ func TestHTTPContentCompressionRequestWithNilBody(t *testing.T) {
res, err := client.Do(req)
require.NoError(t, err)

_, err = ioutil.ReadAll(res.Body)
_, err = io.ReadAll(res.Body)
require.NoError(t, err)
require.NoError(t, res.Body.Close(), "failed to close request body: %v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions config/confighttp/confighttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -566,7 +566,7 @@ func TestHttpReception(t *testing.T) {
assert.Error(t, errResp)
} else {
assert.NoError(t, errResp)
body, errRead := ioutil.ReadAll(resp.Body)
body, errRead := io.ReadAll(resp.Body)
assert.NoError(t, errRead)
assert.Equal(t, "test", string(body))
assert.Equal(t, expectedProto, resp.Proto)
Expand Down Expand Up @@ -1059,7 +1059,7 @@ func BenchmarkHttpRequest(b *testing.B) {
for pb.Next() {
resp, errResp := c.Get(hcs.Endpoint)
require.NoError(b, errResp)
body, errRead := ioutil.ReadAll(resp.Body)
body, errRead := io.ReadAll(resp.Body)
_ = resp.Body.Close()
require.NoError(b, errRead)
require.Equal(b, "test", string(body))
Expand Down
4 changes: 2 additions & 2 deletions config/configtls/configtls.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"crypto/x509"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
"time"
Expand Down Expand Up @@ -195,7 +195,7 @@ func (c TLSSetting) loadTLSConfig() (*tls.Config, error) {
}

func (c TLSSetting) loadCert(caPath string) (*x509.CertPool, error) {
caPEM, err := ioutil.ReadFile(filepath.Clean(caPath))
caPEM, err := os.ReadFile(filepath.Clean(caPath))
if err != nil {
return nil, fmt.Errorf("failed to load CA %s: %w", caPath, err)
}
Expand Down
4 changes: 2 additions & 2 deletions confmap/confmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package confmap

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
Expand Down Expand Up @@ -220,7 +220,7 @@ func TestMapKeyStringToMapKeyTextUnmarshalerHookFuncErrorUnmarshal(t *testing.T)

// newConfFromFile creates a new Conf by reading the given file.
func newConfFromFile(t testing.TB, fileName string) map[string]interface{} {
content, err := ioutil.ReadFile(filepath.Clean(fileName))
content, err := os.ReadFile(filepath.Clean(fileName))
require.NoErrorf(t, err, "unable to read the file %v", fileName)

var data map[string]interface{}
Expand Down
4 changes: 2 additions & 2 deletions confmap/provider/fileprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package fileprovider // import "go.opentelemetry.io/collector/confmap/provider/f
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"

Expand Down Expand Up @@ -54,7 +54,7 @@ func (fmp *provider) Retrieve(_ context.Context, uri string, _ confmap.WatcherFu
}

// Clean the path before using it.
content, err := ioutil.ReadFile(filepath.Clean(uri[len(schemeName)+1:]))
content, err := os.ReadFile(filepath.Clean(uri[len(schemeName)+1:]))
if err != nil {
return nil, fmt.Errorf("unable to read the file %v: %w", uri, err)
}
Expand Down
3 changes: 1 addition & 2 deletions exporter/otlphttpexporter/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"runtime"
Expand Down Expand Up @@ -140,7 +139,7 @@ func (e *exporter) export(ctx context.Context, url string, request []byte) error

defer func() {
// Discard any remaining response body when we are done reading.
io.CopyN(ioutil.Discard, resp.Body, maxHTTPResponseReadBytes) // nolint:errcheck
io.CopyN(io.Discard, resp.Body, maxHTTPResponseReadBytes) // nolint:errcheck
resp.Body.Close()
}()

Expand Down
6 changes: 3 additions & 3 deletions exporter/otlphttpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -248,11 +248,11 @@ func TestLogsRoundTrip(t *testing.T) {
func TestIssue_4221(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() { assert.NoError(t, r.Body.Close()) }()
compressedData, err := ioutil.ReadAll(r.Body)
compressedData, err := io.ReadAll(r.Body)
require.NoError(t, err)
gzipReader, err := gzip.NewReader(bytes.NewReader(compressedData))
require.NoError(t, err)
data, err := ioutil.ReadAll(gzipReader)
data, err := io.ReadAll(gzipReader)
require.NoError(t, err)
base64Data := base64.StdEncoding.EncodeToString(data)
// Verify same base64 encoded string is received.
Expand Down
12 changes: 6 additions & 6 deletions receiver/otlpreceiver/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"sync"
Expand Down Expand Up @@ -322,7 +322,7 @@ func TestHandleInvalidRequests(t *testing.T) {
resp, err2 := client.Do(req)
require.NoError(t, err2)

body, err2 := ioutil.ReadAll(resp.Body)
body, err2 := io.ReadAll(resp.Body)
require.NoError(t, err2)

require.Equal(t, resp.Header.Get("Content-Type"), "text/plain")
Expand Down Expand Up @@ -355,7 +355,7 @@ func testHTTPJSONRequest(t *testing.T, url string, sink *errOrSinkConsumer, enco
resp, err := client.Do(req)
require.NoError(t, err, "Error posting trace to http server: %v", err)

respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
t.Errorf("Error reading response from trace http server, %v", err)
}
Expand Down Expand Up @@ -478,7 +478,7 @@ func testHTTPProtobufRequest(
resp, err := client.Do(req)
require.NoError(t, err, "Error posting trace to grpc-gateway server: %v", err)

respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err, "Error reading response from trace grpc-gateway")
require.NoError(t, resp.Body.Close(), "Error closing response body")

Expand Down Expand Up @@ -571,7 +571,7 @@ func TestOTLPReceiverInvalidContentEncoding(t *testing.T) {
resp, err := client.Do(req)
require.NoError(t, err, "Error posting trace to grpc-gateway server: %v", err)

respBytes, err := ioutil.ReadAll(resp.Body)
respBytes, err := io.ReadAll(resp.Body)
require.NoError(t, err, "Error reading response from trace grpc-gateway")
exRespBytes, err := test.resBodyFunc()
require.NoError(t, err, "Error creating expecting response body")
Expand Down Expand Up @@ -832,7 +832,7 @@ func testHTTPMaxRequestBodySizeJSON(t *testing.T, payload []byte, size int, expe
client := &http.Client{}
resp, err := client.Do(req)
require.NoError(t, err)
_, err = ioutil.ReadAll(resp.Body)
_, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, expectedStatusCode, resp.StatusCode)

Expand Down
4 changes: 2 additions & 2 deletions receiver/otlpreceiver/otlphttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package otlpreceiver // import "go.opentelemetry.io/collector/receiver/otlpreceiver"

import (
"io/ioutil"
"io"
"net/http"

spb "google.golang.org/genproto/googleapis/rpc/status"
Expand Down Expand Up @@ -111,7 +111,7 @@ func handleLogs(resp http.ResponseWriter, req *http.Request, logsReceiver *logs.
}

func readAndCloseBody(resp http.ResponseWriter, req *http.Request, encoder encoder) ([]byte, bool) {
body, err := ioutil.ReadAll(req.Body)
body, err := io.ReadAll(req.Body)
if err != nil {
writeError(resp, encoder, err, http.StatusBadRequest)
return nil, false
Expand Down

0 comments on commit b3539e8

Please sign in to comment.