Skip to content

Commit

Permalink
refactor: move from io/ioutil to io and os package (influxdata#9811)
Browse files Browse the repository at this point in the history
  • Loading branch information
Juneezee authored Sep 28, 2021
1 parent c4d2ad8 commit 6a3b271
Show file tree
Hide file tree
Showing 165 changed files with 456 additions and 517 deletions.
6 changes: 3 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package config
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -933,7 +933,7 @@ func loadConfig(config string) ([]byte, error) {
}

// If it isn't a https scheme, try it as a file
return ioutil.ReadFile(config)
return os.ReadFile(config)
}

func fetchConfig(u *url.URL) ([]byte, error) {
Expand Down Expand Up @@ -964,7 +964,7 @@ func fetchConfig(u *url.URL) ([]byte, error) {
return nil, fmt.Errorf("Retry %d of %d failed to retrieve remote config: %s", i, retries, resp.Status)
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

return nil, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/content_coding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package internal

import (
"bytes"
"io/ioutil"
"io"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -68,7 +68,7 @@ func TestStreamIdentityDecode(t *testing.T) {
dec, err := NewStreamContentDecoder("identity", &r)
require.NoError(t, err)

data, err := ioutil.ReadAll(dec)
data, err := io.ReadAll(dec)
require.NoError(t, err)

require.Equal(t, []byte("howdy"), data)
Expand Down
7 changes: 3 additions & 4 deletions internal/internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"compress/gzip"
"crypto/rand"
"io"
"io/ioutil"
"log"
"os/exec"
"regexp"
Expand Down Expand Up @@ -182,7 +181,7 @@ func TestCompressWithGzip(t *testing.T) {
assert.NoError(t, err)
defer gzipReader.Close()

output, err := ioutil.ReadAll(gzipReader)
output, err := io.ReadAll(gzipReader)
assert.NoError(t, err)

assert.Equal(t, testData, string(output))
Expand All @@ -203,15 +202,15 @@ func TestCompressWithGzipEarlyClose(t *testing.T) {
rc, err := CompressWithGzip(mr)
assert.NoError(t, err)

n, err := io.CopyN(ioutil.Discard, rc, 10000)
n, err := io.CopyN(io.Discard, rc, 10000)
assert.NoError(t, err)
assert.Equal(t, int64(10000), n)

r1 := mr.readN
err = rc.Close()
assert.NoError(t, err)

n, err = io.CopyN(ioutil.Discard, rc, 10000)
n, err = io.CopyN(io.Discard, rc, 10000)
assert.Error(t, io.EOF, err)
assert.Equal(t, int64(0), n)

Expand Down
3 changes: 1 addition & 2 deletions internal/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"os/exec"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -187,5 +186,5 @@ func isQuitting(ctx context.Context) bool {
}

func defaultReadPipe(r io.Reader) {
io.Copy(ioutil.Discard, r)
_, _ = io.Copy(io.Discard, r)
}
35 changes: 17 additions & 18 deletions internal/rotate/file_writer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rotate

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand All @@ -12,7 +11,7 @@ import (
)

func TestFileWriter_NoRotation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationNo")
tempDir, err := os.MkdirTemp("", "RotationNo")
require.NoError(t, err)
writer, err := NewFileWriter(filepath.Join(tempDir, "test"), 0, 0, 0)
require.NoError(t, err)
Expand All @@ -22,12 +21,12 @@ func TestFileWriter_NoRotation(t *testing.T) {
require.NoError(t, err)
_, err = writer.Write([]byte("Hello World 2"))
require.NoError(t, err)
files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 1, len(files))
}

func TestFileWriter_TimeRotation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationTime")
tempDir, err := os.MkdirTemp("", "RotationTime")
require.NoError(t, err)
interval, _ := time.ParseDuration("1s")
writer, err := NewFileWriter(filepath.Join(tempDir, "test"), interval, 0, -1)
Expand All @@ -39,28 +38,28 @@ func TestFileWriter_TimeRotation(t *testing.T) {
time.Sleep(1 * time.Second)
_, err = writer.Write([]byte("Hello World 2"))
require.NoError(t, err)
files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 2, len(files))
}

func TestFileWriter_ReopenTimeRotation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationTime")
tempDir, err := os.MkdirTemp("", "RotationTime")
require.NoError(t, err)
interval, _ := time.ParseDuration("1s")
filePath := filepath.Join(tempDir, "test.log")
err = ioutil.WriteFile(filePath, []byte("Hello World"), 0644)
err = os.WriteFile(filePath, []byte("Hello World"), 0644)
time.Sleep(1 * time.Second)
assert.NoError(t, err)
writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), interval, 0, -1)
require.NoError(t, err)
defer func() { writer.Close(); os.RemoveAll(tempDir) }()

files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 2, len(files))
}

func TestFileWriter_SizeRotation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationSize")
tempDir, err := os.MkdirTemp("", "RotationSize")
require.NoError(t, err)
maxSize := int64(9)
writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), 0, maxSize, -1)
Expand All @@ -71,29 +70,29 @@ func TestFileWriter_SizeRotation(t *testing.T) {
require.NoError(t, err)
_, err = writer.Write([]byte("World 2"))
require.NoError(t, err)
files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 2, len(files))
}

func TestFileWriter_ReopenSizeRotation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationSize")
tempDir, err := os.MkdirTemp("", "RotationSize")
require.NoError(t, err)
maxSize := int64(12)
filePath := filepath.Join(tempDir, "test.log")
err = ioutil.WriteFile(filePath, []byte("Hello World"), 0644)
err = os.WriteFile(filePath, []byte("Hello World"), 0644)
assert.NoError(t, err)
writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), 0, maxSize, -1)
require.NoError(t, err)
defer func() { writer.Close(); os.RemoveAll(tempDir) }()

_, err = writer.Write([]byte("Hello World Again"))
require.NoError(t, err)
files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 2, len(files))
}

func TestFileWriter_DeleteArchives(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationDeleteArchives")
tempDir, err := os.MkdirTemp("", "RotationDeleteArchives")
require.NoError(t, err)
maxSize := int64(5)
writer, err := NewFileWriter(filepath.Join(tempDir, "test.log"), 0, maxSize, 2)
Expand All @@ -112,14 +111,14 @@ func TestFileWriter_DeleteArchives(t *testing.T) {
_, err = writer.Write([]byte("Third file"))
require.NoError(t, err)

files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 3, len(files))

for _, tempFile := range files {
var bytes []byte
var err error
path := filepath.Join(tempDir, tempFile.Name())
if bytes, err = ioutil.ReadFile(path); err != nil {
if bytes, err = os.ReadFile(path); err != nil {
t.Error(err.Error())
return
}
Expand All @@ -133,7 +132,7 @@ func TestFileWriter_DeleteArchives(t *testing.T) {
}

func TestFileWriter_CloseRotates(t *testing.T) {
tempDir, err := ioutil.TempDir("", "RotationClose")
tempDir, err := os.MkdirTemp("", "RotationClose")
require.NoError(t, err)
defer os.RemoveAll(tempDir)
maxSize := int64(9)
Expand All @@ -142,7 +141,7 @@ func TestFileWriter_CloseRotates(t *testing.T) {

writer.Close()

files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 1, len(files))
assert.Regexp(t, "^test\\.[^\\.]+\\.log$", files[0].Name())
}
27 changes: 13 additions & 14 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package logger
import (
"bytes"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
Expand All @@ -15,7 +14,7 @@ import (
)

func TestWriteLogToFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "")
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()

Expand All @@ -24,27 +23,27 @@ func TestWriteLogToFile(t *testing.T) {
log.Printf("I! TEST")
log.Printf("D! TEST") // <- should be ignored

f, err := ioutil.ReadFile(tmpfile.Name())
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
}

func TestDebugWriteLogToFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "")
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Debug = true
SetupLogging(config)
log.Printf("D! TEST")

f, err := ioutil.ReadFile(tmpfile.Name())
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z D! TEST\n"))
}

func TestErrorWriteLogToFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "")
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
Expand All @@ -53,35 +52,35 @@ func TestErrorWriteLogToFile(t *testing.T) {
log.Printf("E! TEST")
log.Printf("I! TEST") // <- should be ignored

f, err := ioutil.ReadFile(tmpfile.Name())
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z E! TEST\n"))
}

func TestAddDefaultLogLevel(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "")
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Debug = true
SetupLogging(config)
log.Printf("TEST")

f, err := ioutil.ReadFile(tmpfile.Name())
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))
}

func TestWriteToTruncatedFile(t *testing.T) {
tmpfile, err := ioutil.TempFile("", "")
tmpfile, err := os.CreateTemp("", "")
assert.NoError(t, err)
defer func() { os.Remove(tmpfile.Name()) }()
config := createBasicLogConfig(tmpfile.Name())
config.Debug = true
SetupLogging(config)
log.Printf("TEST")

f, err := ioutil.ReadFile(tmpfile.Name())
f, err := os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! TEST\n"))

Expand All @@ -91,13 +90,13 @@ func TestWriteToTruncatedFile(t *testing.T) {

log.Printf("SHOULD BE FIRST")

f, err = ioutil.ReadFile(tmpfile.Name())
f, err = os.ReadFile(tmpfile.Name())
assert.NoError(t, err)
assert.Equal(t, f[19:], []byte("Z I! SHOULD BE FIRST\n"))
}

func TestWriteToFileInRotation(t *testing.T) {
tempDir, err := ioutil.TempDir("", "LogRotation")
tempDir, err := os.MkdirTemp("", "LogRotation")
require.NoError(t, err)
cfg := createBasicLogConfig(filepath.Join(tempDir, "test.log"))
cfg.LogTarget = LogTargetFile
Expand All @@ -110,7 +109,7 @@ func TestWriteToFileInRotation(t *testing.T) {

log.Printf("I! TEST 1") // Writes 31 bytes, will rotate
log.Printf("I! TEST") // Writes 29 byes, no rotation expected
files, _ := ioutil.ReadDir(tempDir)
files, _ := os.ReadDir(tempDir)
assert.Equal(t, 2, len(files))
}

Expand Down
5 changes: 2 additions & 3 deletions plugins/common/cookie/cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/cookiejar"
"strings"
Expand Down Expand Up @@ -78,7 +77,7 @@ func (c *CookieAuthConfig) authRenewal(ctx context.Context, ticker *clockutil.Ti
func (c *CookieAuthConfig) auth() error {
var body io.ReadCloser
if c.Body != "" {
body = ioutil.NopCloser(strings.NewReader(c.Body))
body = io.NopCloser(strings.NewReader(c.Body))
defer body.Close()
}

Expand All @@ -97,7 +96,7 @@ func (c *CookieAuthConfig) auth() error {
}
defer resp.Body.Close()

if _, err = io.Copy(ioutil.Discard, resp.Body); err != nil {
if _, err = io.Copy(io.Discard, resp.Body); err != nil {
return err
}

Expand Down
4 changes: 2 additions & 2 deletions plugins/common/cookie/cookie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cookie
import (
"context"
"fmt"
"io/ioutil"
"io"
"net/http"
"net/http/httptest"
"sync/atomic"
Expand Down Expand Up @@ -50,7 +50,7 @@ func newFakeServer(t *testing.T) fakeServer {
case authEndpointNoCreds:
authed()
case authEndpointWithBody:
body, err := ioutil.ReadAll(r.Body)
body, err := io.ReadAll(r.Body)
require.NoError(t, err)
if !cmp.Equal([]byte(reqBody), body) {
w.WriteHeader(http.StatusUnauthorized)
Expand Down
Loading

0 comments on commit 6a3b271

Please sign in to comment.