chore: use io/os over ioutil (#22656)

pull/22669/head
Dane Strandboge 2021-10-12 16:55:07 -05:00 committed by GitHub
parent cac4b42898
commit ca992e9fff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 53 additions and 58 deletions

View File

@ -5,7 +5,7 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"path" "path"
@ -309,7 +309,7 @@ func TestService_handleGetChecks(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handleGetChecks() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) t.Errorf("%q. handleGetChecks() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
@ -594,7 +594,7 @@ func TestService_handleGetCheck(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
t.Logf(res.Header.Get("X-Influx-Error")) t.Logf(res.Header.Get("X-Influx-Error"))
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
@ -756,7 +756,7 @@ func TestService_handlePostCheck(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handlePostCheck() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) t.Errorf("%q. handlePostCheck() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
@ -865,7 +865,7 @@ func TestService_handleDeleteCheck(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handleDeleteCheck() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) t.Errorf("%q. handleDeleteCheck() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
@ -1042,7 +1042,7 @@ func TestService_handlePatchCheck(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handlePatchCheck() = %v, want %v %v", tt.name, res.StatusCode, tt.wants.statusCode, w.Header()) t.Errorf("%q. handlePatchCheck() = %v, want %v %v", tt.name, res.StatusCode, tt.wants.statusCode, w.Header())
@ -1232,7 +1232,7 @@ func TestService_handleUpdateCheck(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handlePutCheck() = %v, want %v %v %v", tt.name, res.StatusCode, tt.wants.statusCode, w.Header(), string(body)) t.Errorf("%q. handlePutCheck() = %v, want %v %v %v", tt.name, res.StatusCode, tt.wants.statusCode, w.Header(), string(body))
@ -1333,7 +1333,7 @@ func TestService_handlePostCheckMember(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handlePostCheckMember() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) t.Errorf("%q. handlePostCheckMember() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)
@ -1427,7 +1427,7 @@ func TestService_handlePostCheckOwner(t *testing.T) {
res := w.Result() res := w.Result()
content := res.Header.Get("Content-Type") content := res.Header.Get("Content-Type")
body, _ := ioutil.ReadAll(res.Body) body, _ := io.ReadAll(res.Body)
if res.StatusCode != tt.wants.statusCode { if res.StatusCode != tt.wants.statusCode {
t.Errorf("%q. handlePostCheckOwner() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode) t.Errorf("%q. handlePostCheckOwner() = %v, want %v", tt.name, res.StatusCode, tt.wants.statusCode)

View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"time" "time"
io2 "github.com/influxdata/influxdb/v2/kit/io" io2 "github.com/influxdata/influxdb/v2/kit/io"
@ -113,7 +112,7 @@ func readAll(ctx context.Context, rc io.ReadCloser) (data []byte, err error) {
span.Finish() span.Finish()
}() }()
data, err = ioutil.ReadAll(rc) data, err = io.ReadAll(rc)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -3,7 +3,6 @@ package tsm1
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
@ -21,7 +20,7 @@ type keyValues struct {
} }
func MustTempDir() string { func MustTempDir() string {
dir, err := ioutil.TempDir("", "tsm1-test") dir, err := os.MkdirTemp("", "tsm1-test")
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to create temp dir: %v", err)) panic(fmt.Sprintf("failed to create temp dir: %v", err))
} }
@ -29,7 +28,7 @@ func MustTempDir() string {
} }
func MustTempFile(dir string) *os.File { func MustTempFile(dir string) *os.File {
f, err := ioutil.TempFile(dir, "tsm1test") f, err := os.CreateTemp(dir, "tsm1test")
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to create temp file: %v", err)) panic(fmt.Sprintf("failed to create temp file: %v", err))
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"math/rand" "math/rand"
"os" "os"
@ -802,7 +801,7 @@ func TestCache_Split(t *testing.T) {
} }
func mustTempDir() string { func mustTempDir() string {
dir, err := ioutil.TempDir("", "tsm1-test") dir, err := os.MkdirTemp("", "tsm1-test")
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to create temp dir: %v", err)) panic(fmt.Sprintf("failed to create temp dir: %v", err))
} }
@ -810,7 +809,7 @@ func mustTempDir() string {
} }
func mustTempFile(dir string) *os.File { func mustTempFile(dir string) *os.File {
f, err := ioutil.TempFile(dir, "tsm1test") f, err := os.CreateTemp(dir, "tsm1test")
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to create temp file: %v", err)) panic(fmt.Sprintf("failed to create temp file: %v", err))
} }

View File

@ -8,7 +8,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@ -2299,7 +2298,7 @@ func (e *Engine) reloadCache() error {
// cleanup removes all temp files and dirs that exist on disk. This is should only be run at startup to avoid // cleanup removes all temp files and dirs that exist on disk. This is should only be run at startup to avoid
// removing tmp files that are still in use. // removing tmp files that are still in use.
func (e *Engine) cleanup() error { func (e *Engine) cleanup() error {
allfiles, err := ioutil.ReadDir(e.path) allfiles, err := os.ReadDir(e.path)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
} else if err != nil { } else if err != nil {

View File

@ -2,7 +2,6 @@ package tsm1
import ( import (
"context" "context"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -15,7 +14,7 @@ import (
) )
func TestEngine_ConcurrentShardSnapshots(t *testing.T) { func TestEngine_ConcurrentShardSnapshots(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "shard_test") tmpDir, err := os.MkdirTemp("", "shard_test")
require.NoError(t, err, "error creating temporary directory") require.NoError(t, err, "error creating temporary directory")
defer os.RemoveAll(tmpDir) defer os.RemoveAll(tmpDir)
@ -83,7 +82,7 @@ func TestEngine_ConcurrentShardSnapshots(t *testing.T) {
func NewSeriesFile(tb testing.TB, tmpDir string) *tsdb.SeriesFile { func NewSeriesFile(tb testing.TB, tmpDir string) *tsdb.SeriesFile {
tb.Helper() tb.Helper()
dir, err := ioutil.TempDir(tmpDir, "tsdb-series-file-") dir, err := os.MkdirTemp(tmpDir, "tsdb-series-file-")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -6,7 +6,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"math/rand" "math/rand"
"os" "os"
@ -379,7 +378,7 @@ func TestEngine_Backup(t *testing.T) {
defer sfile.Close() defer sfile.Close()
// Generate temporary file. // Generate temporary file.
f, _ := ioutil.TempFile("", "tsm") f, _ := os.CreateTemp("", "tsm")
f.Close() f.Close()
os.Remove(f.Name()) os.Remove(f.Name())
walPath := filepath.Join(f.Name(), "wal") walPath := filepath.Join(f.Name(), "wal")
@ -498,7 +497,7 @@ func TestEngine_Backup(t *testing.T) {
func TestEngine_Export(t *testing.T) { func TestEngine_Export(t *testing.T) {
// Generate temporary file. // Generate temporary file.
f, _ := ioutil.TempFile("", "tsm") f, _ := os.CreateTemp("", "tsm")
f.Close() f.Close()
os.Remove(f.Name()) os.Remove(f.Name())
walPath := filepath.Join(f.Name(), "wal") walPath := filepath.Join(f.Name(), "wal")
@ -1831,7 +1830,7 @@ func TestEngine_SnapshotsDisabled(t *testing.T) {
defer sfile.Close() defer sfile.Close()
// Generate temporary file. // Generate temporary file.
dir, _ := ioutil.TempDir("", "tsm") dir, _ := os.MkdirTemp("", "tsm")
walPath := filepath.Join(dir, "wal") walPath := filepath.Join(dir, "wal")
os.MkdirAll(walPath, 0777) os.MkdirAll(walPath, 0777)
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
@ -2594,7 +2593,7 @@ type Engine struct {
func NewEngine(tb testing.TB, index string) (*Engine, error) { func NewEngine(tb testing.TB, index string) (*Engine, error) {
tb.Helper() tb.Helper()
root, err := ioutil.TempDir("", "tsm1-") root, err := os.MkdirTemp("", "tsm1-")
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -2768,7 +2767,7 @@ type SeriesFile struct {
// NewSeriesFile returns a new instance of SeriesFile with a temporary file path. // NewSeriesFile returns a new instance of SeriesFile with a temporary file path.
func NewSeriesFile() *SeriesFile { func NewSeriesFile() *SeriesFile {
dir, err := ioutil.TempDir("", "tsdb-series-file-") dir, err := os.MkdirTemp("", "tsdb-series-file-")
if err != nil { if err != nil {
panic(err) panic(err)
} }

View File

@ -5,7 +5,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@ -489,7 +488,7 @@ func (f *FileStore) Open(ctx context.Context) error {
} }
// find the current max ID for temp directories // find the current max ID for temp directories
tmpfiles, err := ioutil.ReadDir(f.dir) tmpfiles, err := os.ReadDir(f.dir)
if err != nil { if err != nil {
return err return err
} }

View File

@ -3,7 +3,6 @@ package tsm1_test
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -2725,7 +2724,7 @@ func TestFileStore_CreateSnapshot(t *testing.T) {
} }
t.Logf("temp file for hard links: %q", s) t.Logf("temp file for hard links: %q", s)
tfs, e := ioutil.ReadDir(s) tfs, e := os.ReadDir(s)
if e != nil { if e != nil {
t.Fatal(e) t.Fatal(e)
} }
@ -2927,7 +2926,7 @@ type keyValues struct {
} }
func MustTempDir() string { func MustTempDir() string {
dir, err := ioutil.TempDir("", "tsm1-test") dir, err := os.MkdirTemp("", "tsm1-test")
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to create temp dir: %v", err)) panic(fmt.Sprintf("failed to create temp dir: %v", err))
} }
@ -2935,7 +2934,7 @@ func MustTempDir() string {
} }
func MustTempFile(dir string) *os.File { func MustTempFile(dir string) *os.File {
f, err := ioutil.TempFile(dir, "tsm1test") f, err := os.CreateTemp(dir, "tsm1test")
if err != nil { if err != nil {
panic(fmt.Sprintf("failed to create temp file: %v", err)) panic(fmt.Sprintf("failed to create temp file: %v", err))
} }

View File

@ -2,7 +2,6 @@ package tsm1
import ( import (
"fmt" "fmt"
"io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@ -1658,7 +1657,7 @@ func TestTSMReader_FuzzCrashes(t *testing.T) {
defer os.RemoveAll(dir) defer os.RemoveAll(dir)
filename := filepath.Join(dir, "x.tsm") filename := filepath.Join(dir, "x.tsm")
if err := ioutil.WriteFile(filename, []byte(c), 0600); err != nil { if err := os.WriteFile(filename, []byte(c), 0600); err != nil {
t.Fatalf("exp no error, got %s", err) t.Fatalf("exp no error, got %s", err)
} }
defer os.RemoveAll(dir) defer os.RemoveAll(dir)

View File

@ -7,7 +7,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"os" "os"
"path/filepath" "path/filepath"
@ -270,7 +269,7 @@ func (t *Tombstoner) Walk(fn func(t Tombstone) error) error {
} }
func (t *Tombstoner) writeTombstoneV3(tombstones []Tombstone) error { func (t *Tombstoner) writeTombstoneV3(tombstones []Tombstone) error {
tmp, err := ioutil.TempFile(filepath.Dir(t.Path), TombstoneFileExtension) tmp, err := os.CreateTemp(filepath.Dir(t.Path), TombstoneFileExtension)
if err != nil { if err != nil {
return err return err
} }

View File

@ -2,7 +2,6 @@ package tsm1_test
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -242,7 +241,7 @@ func TestTombstoner_ReadV1(t *testing.T) {
defer func() { os.RemoveAll(dir) }() defer func() { os.RemoveAll(dir) }()
f := MustTempFile(dir) f := MustTempFile(dir)
if err := ioutil.WriteFile(f.Name(), []byte("foo\n"), 0x0600); err != nil { if err := os.WriteFile(f.Name(), []byte("foo\n"), 0x0600); err != nil {
t.Fatalf("write v1 file: %v", err) t.Fatalf("write v1 file: %v", err)
} }
f.Close() f.Close()

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -449,20 +448,24 @@ func TestWALRollSegment(t *testing.T) {
_, err := w.WriteMulti(context.Background(), values) _, err := w.WriteMulti(context.Background(), values)
require.NoError(t, err) require.NoError(t, err)
files, err := ioutil.ReadDir(w.Path()) files, err := os.ReadDir(w.Path())
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(files)) require.Equal(t, 1, len(files))
encodeSize := files[0].Size() file, err := files[0].Info()
require.NoError(t, err)
encodeSize := file.Size()
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
_, err := w.WriteMulti(context.Background(), values) _, err := w.WriteMulti(context.Background(), values)
require.NoError(t, err) require.NoError(t, err)
} }
files, err = ioutil.ReadDir(w.Path()) files, err = os.ReadDir(w.Path())
require.NoError(t, err) require.NoError(t, err)
for _, f := range files { for _, f := range files {
require.True(t, f.Size() <= int64(segSize)+encodeSize) file, err := f.Info()
require.NoError(t, err)
require.True(t, file.Size() <= int64(segSize)+encodeSize)
} }
require.NoError(t, w.Close()) require.NoError(t, w.Close())
} }
@ -470,7 +473,7 @@ func TestWALRollSegment(t *testing.T) {
func TestWAL_DiskSize(t *testing.T) { func TestWAL_DiskSize(t *testing.T) {
test := func(w *tsm1.WAL, oldZero, curZero bool) { test := func(w *tsm1.WAL, oldZero, curZero bool) {
// get disk size by reading file // get disk size by reading file
files, err := ioutil.ReadDir(w.Path()) files, err := os.ReadDir(w.Path())
require.NoError(t, err) require.NoError(t, err)
sort.Slice(files, func(i, j int) bool { sort.Slice(files, func(i, j int) bool {
@ -479,9 +482,13 @@ func TestWAL_DiskSize(t *testing.T) {
var old, cur int64 var old, cur int64
if len(files) > 0 { if len(files) > 0 {
cur = files[len(files)-1].Size() file, err := files[len(files)-1].Info()
require.NoError(t, err)
cur = file.Size()
for i := 0; i < len(files)-1; i++ { for i := 0; i < len(files)-1; i++ {
old += files[i].Size() file, err := files[i].Info()
require.NoError(t, err)
old += file.Size()
} }
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"io" "io"
"io/ioutil"
"os" "os"
"testing" "testing"
@ -75,7 +74,7 @@ func TestTSMWriter_Write_Single(t *testing.T) {
t.Fatalf("unexpected error open file: %v", err) t.Fatalf("unexpected error open file: %v", err)
} }
b, err := ioutil.ReadAll(fd) b, err := io.ReadAll(fd)
if err != nil { if err != nil {
t.Fatalf("unexpected error reading: %v", err) t.Fatalf("unexpected error reading: %v", err)
} }
@ -419,7 +418,7 @@ func TestTSMWriter_WriteBlock_Empty(t *testing.T) {
} }
defer fd.Close() defer fd.Close()
b, err := ioutil.ReadAll(fd) b, err := io.ReadAll(fd)
if err != nil { if err != nil {
t.Fatalf("unexpected error read all: %v", err) t.Fatalf("unexpected error read all: %v", err)
} }
@ -467,7 +466,7 @@ func TestTSMWriter_WriteBlock_Multiple(t *testing.T) {
} }
defer fd.Close() defer fd.Close()
b, err := ioutil.ReadAll(fd) b, err := io.ReadAll(fd)
if err != nil { if err != nil {
t.Fatalf("unexpected error read all: %v", err) t.Fatalf("unexpected error read all: %v", err)
} }
@ -596,7 +595,7 @@ func TestTSMWriter_Sync(t *testing.T) {
io.Writer io.Writer
fakeSyncer fakeSyncer
}{ }{
Writer: ioutil.Discard, Writer: io.Discard,
} }
w, err := tsm1.NewTSMWriter(f) w, err := tsm1.NewTSMWriter(f)

View File

@ -3,7 +3,7 @@ package tsdb_test
import ( import (
"compress/gzip" "compress/gzip"
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
@ -369,12 +369,12 @@ func MustNewIndex(tb testing.TB, index string, eopts ...EngineOption) *Index {
opt(&opts) opt(&opts)
} }
rootPath, err := ioutil.TempDir("", "influxdb-tsdb") rootPath, err := os.MkdirTemp("", "influxdb-tsdb")
if err != nil { if err != nil {
panic(err) panic(err)
} }
seriesPath, err := ioutil.TempDir(rootPath, tsdb.SeriesFileDirectory) seriesPath, err := os.MkdirTemp(rootPath, tsdb.SeriesFileDirectory)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -492,7 +492,7 @@ func BenchmarkIndexSet_TagSets(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
data, err := ioutil.ReadAll(gzr) data, err := io.ReadAll(gzr)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }
@ -590,7 +590,7 @@ func BenchmarkIndex_ConcurrentWriteQuery(b *testing.B) {
b.Fatal(err) b.Fatal(err)
} }
data, err := ioutil.ReadAll(gzr) data, err := io.ReadAll(gzr)
if err != nil { if err != nil {
b.Fatal(err) b.Fatal(err)
} }