Skip to content

Commit

Permalink
Drop most ioutil usage across k6
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 10, 2023
1 parent 13f845a commit 0563bc4
Show file tree
Hide file tree
Showing 36 changed files with 98 additions and 109 deletions.
5 changes: 2 additions & 3 deletions api/v1/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"

Expand Down Expand Up @@ -84,7 +83,7 @@ func (c *Client) CallAPI(ctx context.Context, method string, rel *url.URL, body,
return err
}
}
bodyReader = ioutil.NopCloser(bytes.NewBuffer(bodyData))
bodyReader = io.NopCloser(bytes.NewBuffer(bodyData))
}

req := &http.Request{
Expand All @@ -100,7 +99,7 @@ func (c *Client) CallAPI(ctx context.Context, method string, rel *url.URL, body,
}
defer func() { _ = res.Body.Close() }()

data, err := ioutil.ReadAll(res.Body)
data, err := io.ReadAll(res.Body)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions api/v1/setup_teardown_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package v1

import (
"encoding/json"
"io/ioutil"
"io"
"net/http"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func handleGetSetupData(cs *ControlSurface, rw http.ResponseWriter, _ *http.Requ

// handleSetSetupData just parses the JSON request body and sets the result as setup data for the runner
func handleSetSetupData(cs *ControlSurface, rw http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
apiError(rw, "Error reading request body", err.Error(), http.StatusBadRequest)
return
Expand Down
4 changes: 2 additions & 2 deletions api/v1/status_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"io"
"net/http"

"go.k6.io/k6/errext"
Expand Down Expand Up @@ -37,7 +37,7 @@ func getFirstExternallyControlledExecutor(execScheduler *execution.Scheduler) (*
func handlePatchStatus(cs *ControlSurface, rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json; charset=utf-8")

body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
if err != nil {
apiError(rw, "Couldn't read request", err.Error(), http.StatusBadRequest)
return
Expand Down
7 changes: 3 additions & 4 deletions cloudapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"math/rand"
"net/http"
"time"
Expand Down Expand Up @@ -76,7 +75,7 @@ func (c *Client) NewRequest(method, url string, data interface{}) (*http.Request

func (c *Client) Do(req *http.Request, v interface{}) error {
if req.Body != nil && req.GetBody == nil {
originalBody, err := ioutil.ReadAll(req.Body)
originalBody, err := io.ReadAll(req.Body)
if err != nil {
return err
}
Expand All @@ -85,7 +84,7 @@ func (c *Client) Do(req *http.Request, v interface{}) error {
}

req.GetBody = func() (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewReader(originalBody)), nil
return io.NopCloser(bytes.NewReader(originalBody)), nil
}
req.Body, _ = req.GetBody()
}
Expand Down Expand Up @@ -167,7 +166,7 @@ func checkResponse(r *http.Response) error {
return nil
}

data, err := ioutil.ReadAll(r.Body)
data, err := io.ReadAll(r.Body)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cloudapi/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestMSGLog(t *testing.T) {
}

logger := logrus.New()
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook()
logger.AddHook(hook)
expectMsg.Log(logger)
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestStreamLogsToLogger(t *testing.T) {
})

logger := logrus.New()
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook()
logger.AddHook(hook)

Expand Down Expand Up @@ -321,7 +321,7 @@ func TestStreamLogsToLogger(t *testing.T) {
})

logger := logrus.New()
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook()
logger.AddHook(hook)

Expand Down Expand Up @@ -389,7 +389,7 @@ func TestStreamLogsToLogger(t *testing.T) {
})

logger := logrus.New()
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook()
logger.AddHook(hook)

Expand Down
2 changes: 1 addition & 1 deletion cmd/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func getFiles(t *testing.T, fileSystem afero.Fs) map[string]*bytes.Buffer {
}

func assertEqual(t *testing.T, exp string, actual io.Reader) {
act, err := ioutil.ReadAll(actual)
act, err := io.ReadAll(actual)
require.NoError(t, err)
assert.Equal(t, []byte(exp), act)
}
Expand Down
10 changes: 5 additions & 5 deletions execution/scheduler_ext_exec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package execution_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net/url"
"testing"
"time"
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestExecutionInfoVUSharing(t *testing.T) {
`)

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

Expand Down Expand Up @@ -176,7 +176,7 @@ func TestExecutionInfoScenarioIter(t *testing.T) {
`)

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

Expand Down Expand Up @@ -258,7 +258,7 @@ func TestSharedIterationsStable(t *testing.T) {
`)

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

Expand Down Expand Up @@ -393,7 +393,7 @@ func TestExecutionInfoAll(t *testing.T) {
t.Parallel()

logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
logHook := testutils.NewLogHook(logrus.InfoLevel)
logger.AddHook(logHook)

Expand Down
4 changes: 2 additions & 2 deletions execution/scheduler_ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"errors"
"fmt"
"io/ioutil"
"io"
"net"
"net/url"
"reflect"
Expand Down Expand Up @@ -1082,7 +1082,7 @@ func TestDNSResolver(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()
logger := logrus.New()
logger.SetOutput(ioutil.Discard)
logger.SetOutput(io.Discard)
logHook := testutils.NewLogHook(logrus.WarnLevel)
logger.AddHook(logHook)

Expand Down
3 changes: 2 additions & 1 deletion js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -435,7 +436,7 @@ func TestNewBundle(t *testing.T) {
t.Parallel()
logger := logrus.New()
logger.SetLevel(logrus.InfoLevel)
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook(
logrus.WarnLevel, logrus.InfoLevel, logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel,
)
Expand Down
8 changes: 4 additions & 4 deletions js/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package compiler

import (
"errors"
"io/ioutil"
"io"
"strings"
"testing"

Expand Down Expand Up @@ -167,7 +167,7 @@ func TestCorruptSourceMap(t *testing.T) {

logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

Expand Down Expand Up @@ -196,7 +196,7 @@ func TestCorruptSourceMapOnlyForBabel(t *testing.T) {

logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

Expand Down Expand Up @@ -226,7 +226,7 @@ func TestMinimalSourceMap(t *testing.T) {

logger := logrus.New()
logger.SetLevel(logrus.DebugLevel)
logger.Out = ioutil.Discard
logger.Out = io.Discard
hook := testutils.NewLogHook(logrus.InfoLevel, logrus.WarnLevel)
logger.AddHook(hook)

Expand Down
7 changes: 4 additions & 3 deletions js/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package js
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/url"
"os"
Expand Down Expand Up @@ -219,7 +220,7 @@ func TestConsoleLog(t *testing.T) {

logger := extractLogger(vu.(*ActiveVU).Console.logger)

logger.Out = ioutil.Discard
logger.Out = io.Discard
logger.Level = logrus.DebugLevel
hook := logtest.NewLocal(logger)

Expand Down Expand Up @@ -277,7 +278,7 @@ func TestConsoleLevels(t *testing.T) {

logger := extractLogger(vu.(*ActiveVU).Console.logger)

logger.Out = ioutil.Discard
logger.Out = io.Discard
logger.Level = logrus.DebugLevel
hook := logtest.NewLocal(logger)

Expand Down Expand Up @@ -406,7 +407,7 @@ func TestFileConsole(t *testing.T) {
f, err = os.Open(logFilename) //nolint:gosec
require.NoError(t, err)

fileContent, err := ioutil.ReadAll(f)
fileContent, err := io.ReadAll(f)
require.NoError(t, err)

expectedStr := entryStr
Expand Down
6 changes: 3 additions & 3 deletions js/initcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package js
import (
"context"
"fmt"
"io/ioutil"
"io"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestRequestWithBinaryFile(t *testing.T) {

logger := logrus.New()
logger.Level = logrus.DebugLevel
logger.Out = ioutil.Discard
logger.Out = io.Discard

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down Expand Up @@ -494,7 +494,7 @@ func TestRequestWithMultipleBinaryFiles(t *testing.T) {

logger := logrus.New()
logger.Level = logrus.DebugLevel
logger.Out = ioutil.Discard
logger.Out = io.Discard

registry := metrics.NewRegistry()
builtinMetrics := metrics.RegisterBuiltinMetrics(registry)
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/execution/execution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"net"
"testing"
"time"
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestVUTagsErrorOutOnInvalidValues(t *testing.T) {
logHook := testutils.NewLogHook(logrus.WarnLevel)
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(ioutil.Discard)
testLog.SetOutput(io.Discard)

cases := []string{
"null",
Expand Down
4 changes: 2 additions & 2 deletions js/modules/k6/grpc/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"context"
"crypto/tls"
"errors"
"io/ioutil"
"io"
"net/url"
"os"
"runtime"
Expand Down Expand Up @@ -952,7 +952,7 @@ func TestClientInvokeHeadersDeprecated(t *testing.T) {
logHook := testutils.NewLogHook(logrus.WarnLevel)
testLog := logrus.New()
testLog.AddHook(logHook)
testLog.SetOutput(ioutil.Discard)
testLog.SetOutput(io.Discard)

rt := goja.New()
c := Client{
Expand Down
Loading

0 comments on commit 0563bc4

Please sign in to comment.