refactor: move from io/ioutil to io and os packages (#906)

* chore: run `go fmt ./...`

This commit synchronizes `//go:build` lines with `// +build` lines.

Reference: https://go.googlesource.com/proposal/+/master/design/draft-gobuild.md
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>

* refactor: move from io/ioutil to io and os packages

The io/ioutil package has been deprecated as of Go 1.16, see
https://golang.org/doc/go1.16#ioutil. This commit replaces the existing
io/ioutil functions with their new definitions in io and os packages.

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun 2021-12-10 11:14:45 +08:00 committed by Gaius
parent 10ec7d2bbc
commit f579d3c9ce
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
46 changed files with 135 additions and 178 deletions

View File

@ -19,7 +19,6 @@ package local
import ( import (
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
@ -156,7 +155,7 @@ func (ds *driver) GetBytes(raw *storedriver.Raw) (data []byte, err error) {
return nil, err return nil, err
} }
if raw.Length == 0 { if raw.Length == 0 {
data, err = ioutil.ReadAll(f) data, err = io.ReadAll(f)
} else { } else {
data = make([]byte, raw.Length) data = make([]byte, raw.Length)
_, err = f.Read(data) _, err = f.Read(data)

View File

@ -18,7 +18,6 @@ package local
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -46,7 +45,7 @@ type LocalDriverTestSuite struct {
} }
func (s *LocalDriverTestSuite) SetupSuite() { func (s *LocalDriverTestSuite) SetupSuite() {
s.workHome, _ = ioutil.TempDir("/tmp", "cdn-local-driver-repo") s.workHome, _ = os.MkdirTemp("/tmp", "cdn-local-driver-repo")
pluginProps := map[plugins.PluginType][]*plugins.PluginProperties{ pluginProps := map[plugins.PluginType][]*plugins.PluginProperties{
plugins.StorageDriverPlugin: { plugins.StorageDriverPlugin: {
&plugins.PluginProperties{ &plugins.PluginProperties{
@ -296,7 +295,7 @@ func (s *LocalDriverTestSuite) TestGetPut() {
r, err := s.Get(v.getRaw) r, err := s.Get(v.getRaw)
s.True(v.getErrCheck(err)) s.True(v.getErrCheck(err))
if err == nil { if err == nil {
result, err := ioutil.ReadAll(r) result, err := io.ReadAll(r)
s.Nil(err) s.Nil(err)
s.Equal(v.expected, string(result)) s.Equal(v.expected, string(result))
} }
@ -386,7 +385,7 @@ func (s *LocalDriverTestSuite) TestAppendBytes() {
r, err := s.Get(v.getRaw) r, err := s.Get(v.getRaw)
s.True(v.getErrCheck(err)) s.True(v.getErrCheck(err))
if err == nil { if err == nil {
result, err := ioutil.ReadAll(r) result, err := io.ReadAll(r)
s.Nil(err) s.Nil(err)
s.Equal(v.expected, string(result)) s.Equal(v.expected, string(result))
} }
@ -466,7 +465,7 @@ func (s *LocalDriverTestSuite) TestPutTrunc() {
s.Nil(err) s.Nil(err)
if err == nil { if err == nil {
result, err := ioutil.ReadAll(r) result, err := io.ReadAll(r)
s.Nil(err) s.Nil(err)
s.Equal(string(result[:]), v.expectedData) s.Equal(string(result[:]), v.expectedData)
} }

View File

@ -22,7 +22,6 @@ import (
"fmt" "fmt"
"hash" "hash"
"io" "io"
"io/ioutil"
"sort" "sort"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -270,7 +269,7 @@ func checkPieceContent(reader io.Reader, pieceRecord *storage.PieceMetaRecord, f
// TODO Analyze the original data for the slice format to calculate fileMd5 // TODO Analyze the original data for the slice format to calculate fileMd5
pieceMd5 := md5.New() pieceMd5 := md5.New()
tee := io.TeeReader(io.TeeReader(io.LimitReader(reader, int64(pieceRecord.PieceLen)), pieceMd5), fileDigest) tee := io.TeeReader(io.TeeReader(io.LimitReader(reader, int64(pieceRecord.PieceLen)), pieceMd5), fileDigest)
if n, err := io.Copy(ioutil.Discard, tee); n != int64(pieceRecord.PieceLen) || err != nil { if n, err := io.Copy(io.Discard, tee); n != int64(pieceRecord.PieceLen) || err != nil {
return errors.Wrap(err, "read piece content") return errors.Wrap(err, "read piece content")
} }
realPieceMd5 := digestutils.ToHashString(pieceMd5) realPieceMd5 := digestutils.ToHashString(pieceMd5)

View File

@ -21,7 +21,6 @@ import (
"crypto/md5" "crypto/md5"
"hash" "hash"
"io" "io"
"io/ioutil"
"os" "os"
"sort" "sort"
"strings" "strings"
@ -66,21 +65,21 @@ func (suite *CacheDetectorTestSuite) SetupSuite() {
storageManager.EXPECT().ReadFileMetadata(noCache.taskID).Return(noCache.fileMeta, os.ErrNotExist).AnyTimes() storageManager.EXPECT().ReadFileMetadata(noCache.taskID).Return(noCache.fileMeta, os.ErrNotExist).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(fullNoExpiredCache.taskID).DoAndReturn( storageManager.EXPECT().ReadDownloadFile(fullNoExpiredCache.taskID).DoAndReturn(
func(taskID string) (io.ReadCloser, error) { func(taskID string) (io.ReadCloser, error) {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html") content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err) suite.Nil(err)
return ioutil.NopCloser(strings.NewReader(string(content))), nil return io.NopCloser(strings.NewReader(string(content))), nil
}).AnyTimes() }).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(partialNotSupportRangeCache.taskID).DoAndReturn( storageManager.EXPECT().ReadDownloadFile(partialNotSupportRangeCache.taskID).DoAndReturn(
func(taskID string) (io.ReadCloser, error) { func(taskID string) (io.ReadCloser, error) {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html") content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err) suite.Nil(err)
return ioutil.NopCloser(strings.NewReader(string(content))), nil return io.NopCloser(strings.NewReader(string(content))), nil
}).AnyTimes() }).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(partialSupportRangeCache.taskID).DoAndReturn( storageManager.EXPECT().ReadDownloadFile(partialSupportRangeCache.taskID).DoAndReturn(
func(taskID string) (io.ReadCloser, error) { func(taskID string) (io.ReadCloser, error) {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html") content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err) suite.Nil(err)
return ioutil.NopCloser(strings.NewReader(string(content))), nil return io.NopCloser(strings.NewReader(string(content))), nil
}).AnyTimes() }).AnyTimes()
storageManager.EXPECT().ReadDownloadFile(noCache.taskID).Return(nil, os.ErrNotExist).AnyTimes() storageManager.EXPECT().ReadDownloadFile(noCache.taskID).Return(nil, os.ErrNotExist).AnyTimes()
storageManager.EXPECT().ReadPieceMetaRecords(fullNoExpiredCache.taskID).Return(fullNoExpiredCache.pieces, nil).AnyTimes() storageManager.EXPECT().ReadPieceMetaRecords(fullNoExpiredCache.taskID).Return(fullNoExpiredCache.pieces, nil).AnyTimes()
@ -421,7 +420,7 @@ func (suite *CacheDetectorTestSuite) TestParseByReadMetaFile() {
} }
func (suite *CacheDetectorTestSuite) TestCheckPieceContent() { func (suite *CacheDetectorTestSuite) TestCheckPieceContent() {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html") content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err) suite.Nil(err)
type args struct { type args struct {
reader io.Reader reader io.Reader

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -83,7 +82,7 @@ func NewPlugins(workHome string) map[plugins.PluginType][]*plugins.PluginPropert
} }
func (suite *CacheWriterTestSuite) SetupSuite() { func (suite *CacheWriterTestSuite) SetupSuite() {
suite.workHome, _ = ioutil.TempDir("/tmp", "cdn-CacheWriterDetectorTestSuite-") suite.workHome, _ = os.MkdirTemp("/tmp", "cdn-CacheWriterDetectorTestSuite-")
suite.T().Log("workHome:", suite.workHome) suite.T().Log("workHome:", suite.workHome)
suite.Nil(plugins.Initialize(NewPlugins(suite.workHome))) suite.Nil(plugins.Initialize(NewPlugins(suite.workHome)))
storeMgr, ok := storage.Get(config.DefaultStorageMode) storeMgr, ok := storage.Get(config.DefaultStorageMode)
@ -105,7 +104,7 @@ func (suite *CacheWriterTestSuite) TearDownSuite() {
} }
func (suite *CacheWriterTestSuite) TestStartWriter() { func (suite *CacheWriterTestSuite) TestStartWriter() {
content, err := ioutil.ReadFile("../../testdata/cdn/go.html") content, err := os.ReadFile("../../testdata/cdn/go.html")
suite.Nil(err) suite.Nil(err)
contentLen := int64(len(content)) contentLen := int64(len(content))
type args struct { type args struct {

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -54,7 +53,7 @@ type CDNManagerTestSuite struct {
} }
func (suite *CDNManagerTestSuite) SetupSuite() { func (suite *CDNManagerTestSuite) SetupSuite() {
suite.workHome, _ = ioutil.TempDir("/tmp", "cdn-ManagerTestSuite-") suite.workHome, _ = os.MkdirTemp("/tmp", "cdn-ManagerTestSuite-")
fmt.Printf("workHome: %s", suite.workHome) fmt.Printf("workHome: %s", suite.workHome)
suite.Nil(plugins.Initialize(NewPlugins(suite.workHome))) suite.Nil(plugins.Initialize(NewPlugins(suite.workHome)))
storeMgr, ok := storage.Get(config.DefaultStorageMode) storeMgr, ok := storage.Get(config.DefaultStorageMode)
@ -93,26 +92,26 @@ func (suite *CDNManagerTestSuite) TestTriggerCDN() {
sourceClient.EXPECT().IsExpired(gomock.Any(), gomock.Any()).Return(false, nil).AnyTimes() sourceClient.EXPECT().IsExpired(gomock.Any(), gomock.Any()).Return(false, nil).AnyTimes()
sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn( sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) { func(request *source.Request) (io.ReadCloser, error) {
content, _ := ioutil.ReadFile("../../testdata/cdn/go.html") content, _ := os.ReadFile("../../testdata/cdn/go.html")
if request.Header.Get(source.Range) != "" { if request.Header.Get(source.Range) != "" {
parsed, _ := rangeutils.GetRange(request.Header.Get(source.Range)) parsed, _ := rangeutils.GetRange(request.Header.Get(source.Range))
return ioutil.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))), nil return io.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))), nil
} }
return ioutil.NopCloser(strings.NewReader(string(content))), nil return io.NopCloser(strings.NewReader(string(content))), nil
}, },
).AnyTimes() ).AnyTimes()
sourceClient.EXPECT().DownloadWithExpireInfo(gomock.Any()).DoAndReturn( sourceClient.EXPECT().DownloadWithExpireInfo(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, *source.ExpireInfo, error) { func(request *source.Request) (io.ReadCloser, *source.ExpireInfo, error) {
content, _ := ioutil.ReadFile("../../testdata/cdn/go.html") content, _ := os.ReadFile("../../testdata/cdn/go.html")
if request.Header.Get(source.Range) != "" { if request.Header.Get(source.Range) != "" {
parsed, _ := rangeutils.GetRange(request.Header.Get(source.Range)) parsed, _ := rangeutils.GetRange(request.Header.Get(source.Range))
return ioutil.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))), return io.NopCloser(io.NewSectionReader(strings.NewReader(string(content)), int64(parsed.StartIndex), int64(parsed.EndIndex))),
&source.ExpireInfo{ &source.ExpireInfo{
LastModified: "Sun, 06 Jun 2021 12:52:30 GMT", LastModified: "Sun, 06 Jun 2021 12:52:30 GMT",
ETag: "etag", ETag: "etag",
}, nil }, nil
} }
return ioutil.NopCloser(strings.NewReader(string(content))), return io.NopCloser(strings.NewReader(string(content))),
&source.ExpireInfo{ &source.ExpireInfo{
LastModified: "Sun, 06 Jun 2021 12:52:30 GMT", LastModified: "Sun, 06 Jun 2021 12:52:30 GMT",
ETag: "etag", ETag: "etag",

View File

@ -1,3 +1,4 @@
//go:build darwin
// +build darwin // +build darwin
/* /*

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux // +build linux
/* /*

View File

@ -17,7 +17,6 @@
package config package config
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -32,10 +31,7 @@ import (
) )
func TestDynconfigNewDynconfig(t *testing.T) { func TestDynconfigNewDynconfig(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test") mockCacheDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
mockCachePath := filepath.Join(mockCacheDir, cacheFileName) mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct { tests := []struct {
@ -125,10 +121,7 @@ func TestDynconfigNewDynconfig(t *testing.T) {
} }
func TestDynconfigGet(t *testing.T) { func TestDynconfigGet(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test") mockCacheDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
mockCachePath := filepath.Join(mockCacheDir, cacheFileName) mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct { tests := []struct {
@ -310,10 +303,7 @@ func TestDynconfigGet(t *testing.T) {
} }
func TestDynconfigGetSchedulers(t *testing.T) { func TestDynconfigGetSchedulers(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test") mockCacheDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
mockCachePath := filepath.Join(mockCacheDir, cacheFileName) mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct { tests := []struct {

View File

@ -22,9 +22,9 @@ import (
"crypto/x509" "crypto/x509"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"net/url" "net/url"
"os"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strings" "strings"
@ -70,7 +70,7 @@ func NewDaemonConfig() *DaemonOption {
} }
func (p *DaemonOption) Load(path string) error { func (p *DaemonOption) Load(path string) error {
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return fmt.Errorf("unable to load peer host configuration from %q [%v]", path, err) return fmt.Errorf("unable to load peer host configuration from %q [%v]", path, err)
} }
@ -206,7 +206,7 @@ func (p *ProxyOption) UnmarshalJSON(b []byte) error {
switch value := v.(type) { switch value := v.(type) {
case string: case string:
file, err := ioutil.ReadFile(value) file, err := os.ReadFile(value)
if err != nil { if err != nil {
return err return err
} }
@ -232,7 +232,7 @@ func (p *ProxyOption) UnmarshalYAML(node *yaml.Node) error {
return err return err
} }
file, err := ioutil.ReadFile(path) file, err := os.ReadFile(path)
if err != nil { if err != nil {
return err return err
} }
@ -445,7 +445,7 @@ func (f *FileString) UnmarshalJSON(b []byte) error {
return err return err
} }
file, err := ioutil.ReadFile(s) file, err := os.ReadFile(s)
if err != nil { if err != nil {
return err return err
} }
@ -465,7 +465,7 @@ func (f *FileString) UnmarshalYAML(node *yaml.Node) error {
return errors.New("invalid filestring") return errors.New("invalid filestring")
} }
file, err := ioutil.ReadFile(s) file, err := os.ReadFile(s)
if err != nil { if err != nil {
return err return err
} }
@ -628,7 +628,7 @@ func certPoolFromFiles(files ...string) (*x509.CertPool, error) {
roots := x509.NewCertPool() roots := x509.NewCertPool()
for _, f := range files { for _, f := range files {
cert, err := ioutil.ReadFile(f) cert, err := os.ReadFile(f)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "read cert file %s", f) return nil, errors.Wrapf(err, "read cert file %s", f)
} }

View File

@ -21,7 +21,6 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -226,7 +225,7 @@ func New(opt *config.DaemonOption, d dfpath.Dfpath) (Daemon, error) {
func loadGPRCTLSCredentials(opt config.SecurityOption) (credentials.TransportCredentials, error) { func loadGPRCTLSCredentials(opt config.SecurityOption) (credentials.TransportCredentials, error) {
// Load certificate of the CA who signed client's certificate // Load certificate of the CA who signed client's certificate
pemClientCA, err := ioutil.ReadFile(opt.CACert) pemClientCA, err := os.ReadFile(opt.CACert)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -301,7 +300,7 @@ func (*clientDaemon) prepareTCPListener(opt config.ListenOption, withTLS bool) (
} }
tlsConfig := opt.Security.TLSConfig tlsConfig := opt.Security.TLSConfig
if opt.Security.CACert != "" { if opt.Security.CACert != "" {
caCert, err := ioutil.ReadFile(opt.Security.CACert) caCert, err := os.ReadFile(opt.Security.CACert)
if err != nil { if err != nil {
return nil, -1, err return nil, -1, err
} }

View File

@ -1,3 +1,4 @@
//go:build linux
// +build linux // +build linux
/* /*

View File

@ -1,3 +1,4 @@
//go:build !linux
// +build !linux // +build !linux
/* /*

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -47,7 +46,7 @@ func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
require := testifyrequire.New(t) require := testifyrequire.New(t)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -77,7 +76,7 @@ func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
downloader := NewMockPieceDownloader(ctrl) downloader := NewMockPieceDownloader(ctrl)
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(func(task *DownloadPieceRequest) (io.Reader, io.Closer, error) { downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(func(task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -98,7 +97,7 @@ func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn( sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) { func(request *source.Request) (io.ReadCloser, error) {
if request.URL.String() == url { if request.URL.String() == url {
return ioutil.NopCloser(bytes.NewBuffer(testBytes)), nil return io.NopCloser(bytes.NewBuffer(testBytes)), nil
} }
return nil, fmt.Errorf("unexpect url: %s", request.URL.String()) return nil, fmt.Errorf("unexpect url: %s", request.URL.String())
}) })
@ -162,7 +161,7 @@ func TestFilePeerTask_BackSource_WithContentLength(t *testing.T) {
assert.NotNil(p) assert.NotNil(p)
assert.True(p.PeerTaskDone) assert.True(p.PeerTaskDone)
outputBytes, err := ioutil.ReadFile(output) outputBytes, err := os.ReadFile(output)
assert.Nil(err, "load output file") assert.Nil(err, "load output file")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }
@ -172,7 +171,7 @@ func TestFilePeerTask_BackSource_WithoutContentLength(t *testing.T) {
require := testifyrequire.New(t) require := testifyrequire.New(t)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -203,7 +202,7 @@ func TestFilePeerTask_BackSource_WithoutContentLength(t *testing.T) {
downloader := NewMockPieceDownloader(ctrl) downloader := NewMockPieceDownloader(ctrl)
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn( downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) { func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -223,7 +222,7 @@ func TestFilePeerTask_BackSource_WithoutContentLength(t *testing.T) {
sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn( sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) { func(request *source.Request) (io.ReadCloser, error) {
if request.URL.String() == url { if request.URL.String() == url {
return ioutil.NopCloser(bytes.NewBuffer(testBytes)), nil return io.NopCloser(bytes.NewBuffer(testBytes)), nil
} }
return nil, fmt.Errorf("unexpect url: %s", request.URL.String()) return nil, fmt.Errorf("unexpect url: %s", request.URL.String())
}) })
@ -287,7 +286,7 @@ func TestFilePeerTask_BackSource_WithoutContentLength(t *testing.T) {
assert.NotNil(p) assert.NotNil(p)
assert.True(p.PeerTaskDone) assert.True(p.PeerTaskDone)
outputBytes, err := ioutil.ReadFile(output) outputBytes, err := os.ReadFile(output)
assert.Nil(err, "load output file") assert.Nil(err, "load output file")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"sync" "sync"
"time" "time"
@ -228,7 +227,7 @@ func (ptm *peerTaskManager) StartStreamPeerTask(ctx context.Context, req *schedu
ptm.storeTinyPeerTask(ctx, tiny) ptm.storeTinyPeerTask(ctx, tiny)
logger.Infof("copied tasks data %d bytes to buffer", len(tiny.Content)) logger.Infof("copied tasks data %d bytes to buffer", len(tiny.Content))
tiny.span.SetAttributes(config.AttributePeerTaskSuccess.Bool(true)) tiny.span.SetAttributes(config.AttributePeerTaskSuccess.Bool(true))
return ioutil.NopCloser(bytes.NewBuffer(tiny.Content)), map[string]string{ return io.NopCloser(bytes.NewBuffer(tiny.Content)), map[string]string{
headers.ContentLength: fmt.Sprintf("%d", len(tiny.Content)), headers.ContentLength: fmt.Sprintf("%d", len(tiny.Content)),
}, nil }, nil
} }

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"math" "math"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -152,7 +151,7 @@ func setupPeerTaskManagerComponents(ctrl *gomock.Controller, opt componentsOptio
func(ctx context.Context, pr *scheduler.PeerResult, opts ...grpc.CallOption) error { func(ctx context.Context, pr *scheduler.PeerResult, opts ...grpc.CallOption) error {
return nil return nil
}) })
tempDir, _ := ioutil.TempDir("", "d7y-test-*") tempDir, _ := os.MkdirTemp("", "d7y-test-*")
storageManager, _ := storage.NewStorageManager( storageManager, _ := storage.NewStorageManager(
config.SimpleLocalTaskStoreStrategy, config.SimpleLocalTaskStoreStrategy,
&config.StorageOption{ &config.StorageOption{
@ -169,7 +168,7 @@ func TestPeerTaskManager_StartFilePeerTask(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -200,7 +199,7 @@ func TestPeerTaskManager_StartFilePeerTask(t *testing.T) {
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).Times( downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).Times(
int(math.Ceil(float64(len(testBytes)) / float64(pieceSize)))).DoAndReturn( int(math.Ceil(float64(len(testBytes)) / float64(pieceSize)))).DoAndReturn(
func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) { func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -246,7 +245,7 @@ func TestPeerTaskManager_StartFilePeerTask(t *testing.T) {
assert.NotNil(p) assert.NotNil(p)
assert.True(p.PeerTaskDone) assert.True(p.PeerTaskDone)
outputBytes, err := ioutil.ReadFile(output) outputBytes, err := os.ReadFile(output)
assert.Nil(err, "load output file") assert.Nil(err, "load output file")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }
@ -256,7 +255,7 @@ func TestPeerTaskManager_StartStreamPeerTask(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -282,7 +281,7 @@ func TestPeerTaskManager_StartStreamPeerTask(t *testing.T) {
downloader := NewMockPieceDownloader(ctrl) downloader := NewMockPieceDownloader(ctrl)
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn( downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) { func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -315,7 +314,7 @@ func TestPeerTaskManager_StartStreamPeerTask(t *testing.T) {
}) })
assert.Nil(err, "start stream peer task") assert.Nil(err, "start stream peer task")
outputBytes, err := ioutil.ReadAll(r) outputBytes, err := io.ReadAll(r)
assert.Nil(err, "load read data") assert.Nil(err, "load read data")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }
@ -325,7 +324,7 @@ func TestPeerTaskManager_StartStreamPeerTask_BackSource(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
defer ctrl.Finish() defer ctrl.Finish()
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -388,7 +387,7 @@ func TestPeerTaskManager_StartStreamPeerTask_BackSource(t *testing.T) {
}) })
assert.Nil(err, "start stream peer task") assert.Nil(err, "start stream peer task")
outputBytes, err := ioutil.ReadAll(r) outputBytes, err := io.ReadAll(r)
assert.Nil(err, "load read data") assert.Nil(err, "load read data")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }

View File

@ -21,10 +21,10 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"math" "math"
"net" "net"
"os"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -162,7 +162,7 @@ func setupBackSourcePartialComponents(ctrl *gomock.Controller, testBytes []byte,
func(ctx context.Context, pr *scheduler.PeerResult, opts ...grpc.CallOption) error { func(ctx context.Context, pr *scheduler.PeerResult, opts ...grpc.CallOption) error {
return nil return nil
}) })
tempDir, _ := ioutil.TempDir("", "d7y-test-*") tempDir, _ := os.MkdirTemp("", "d7y-test-*")
storageManager, _ := storage.NewStorageManager( storageManager, _ := storage.NewStorageManager(
config.SimpleLocalTaskStoreStrategy, config.SimpleLocalTaskStoreStrategy,
&config.StorageOption{ &config.StorageOption{
@ -178,7 +178,7 @@ func TestStreamPeerTask_BackSource_Partial_WithContentLength(t *testing.T) {
assert := testifyassert.New(t) assert := testifyassert.New(t)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -206,7 +206,7 @@ func TestStreamPeerTask_BackSource_Partial_WithContentLength(t *testing.T) {
downloader := NewMockPieceDownloader(ctrl) downloader := NewMockPieceDownloader(ctrl)
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn( downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) { func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -223,7 +223,7 @@ func TestStreamPeerTask_BackSource_Partial_WithContentLength(t *testing.T) {
}) })
sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn( sourceClient.EXPECT().Download(gomock.Any()).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) { func(request *source.Request) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBuffer(testBytes)), nil return io.NopCloser(bytes.NewBuffer(testBytes)), nil
}) })
pm := &pieceManager{ pm := &pieceManager{
@ -269,7 +269,7 @@ func TestStreamPeerTask_BackSource_Partial_WithContentLength(t *testing.T) {
rc, _, err := pt.Start(ctx) rc, _, err := pt.Start(ctx)
assert.Nil(err, "start stream peer task") assert.Nil(err, "start stream peer task")
outputBytes, err := ioutil.ReadAll(rc) outputBytes, err := io.ReadAll(rc)
assert.Nil(err, "load read data") assert.Nil(err, "load read data")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }

View File

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"io/ioutil" "os"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -45,7 +45,7 @@ func TestStreamPeerTask_BackSource_WithContentLength(t *testing.T) {
require := testifyrequire.New(t) require := testifyrequire.New(t)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -73,7 +73,7 @@ func TestStreamPeerTask_BackSource_WithContentLength(t *testing.T) {
downloader := NewMockPieceDownloader(ctrl) downloader := NewMockPieceDownloader(ctrl)
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn( downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) { func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -92,7 +92,7 @@ func TestStreamPeerTask_BackSource_WithContentLength(t *testing.T) {
}) })
sourceClient.EXPECT().Download(source.RequestEq(request.URL.String())).DoAndReturn( sourceClient.EXPECT().Download(source.RequestEq(request.URL.String())).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) { func(request *source.Request) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBuffer(testBytes)), nil return io.NopCloser(bytes.NewBuffer(testBytes)), nil
}) })
ptm := &peerTaskManager{ ptm := &peerTaskManager{
@ -145,7 +145,7 @@ func TestStreamPeerTask_BackSource_WithContentLength(t *testing.T) {
rc, _, err := pt.Start(ctx) rc, _, err := pt.Start(ctx)
assert.Nil(err, "start stream peer task") assert.Nil(err, "start stream peer task")
outputBytes, err := ioutil.ReadAll(rc) outputBytes, err := io.ReadAll(rc)
assert.Nil(err, "load read data") assert.Nil(err, "load read data")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }
@ -155,7 +155,7 @@ func TestStreamPeerTask_BackSource_WithoutContentLength(t *testing.T) {
require := testifyrequire.New(t) require := testifyrequire.New(t)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -183,7 +183,7 @@ func TestStreamPeerTask_BackSource_WithoutContentLength(t *testing.T) {
downloader := NewMockPieceDownloader(ctrl) downloader := NewMockPieceDownloader(ctrl)
downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn( downloader.EXPECT().DownloadPiece(gomock.Any(), gomock.Any()).AnyTimes().DoAndReturn(
func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) { func(ctx context.Context, task *DownloadPieceRequest) (io.Reader, io.Closer, error) {
rc := ioutil.NopCloser( rc := io.NopCloser(
bytes.NewBuffer( bytes.NewBuffer(
testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)], testBytes[task.piece.RangeStart : task.piece.RangeStart+uint64(task.piece.RangeSize)],
)) ))
@ -202,7 +202,7 @@ func TestStreamPeerTask_BackSource_WithoutContentLength(t *testing.T) {
}) })
sourceClient.EXPECT().Download(source.RequestEq(request.URL.String())).DoAndReturn( sourceClient.EXPECT().Download(source.RequestEq(request.URL.String())).DoAndReturn(
func(request *source.Request) (io.ReadCloser, error) { func(request *source.Request) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBuffer(testBytes)), nil return io.NopCloser(bytes.NewBuffer(testBytes)), nil
}) })
ptm := &peerTaskManager{ ptm := &peerTaskManager{
@ -255,7 +255,7 @@ func TestStreamPeerTask_BackSource_WithoutContentLength(t *testing.T) {
rc, _, err := pt.Start(ctx) rc, _, err := pt.Start(ctx)
assert.Nil(err, "start stream peer task") assert.Nil(err, "start stream peer task")
outputBytes, err := ioutil.ReadAll(rc) outputBytes, err := io.ReadAll(rc)
assert.Nil(err, "load read data") assert.Nil(err, "load read data")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
} }

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@ -101,7 +100,7 @@ func (p *pieceDownloader) DownloadPiece(ctx context.Context, d *DownloadPieceReq
return nil, nil, err return nil, nil, err
} }
if resp.StatusCode > 299 { if resp.StatusCode > 299 {
_, _ = io.Copy(ioutil.Discard, resp.Body) _, _ = io.Copy(io.Discard, resp.Body)
_ = resp.Body.Close() _ = resp.Body.Close()
return nil, nil, fmt.Errorf("download piece failed with http code: %s", resp.Status) return nil, nil, fmt.Errorf("download piece failed with http code: %s", resp.Status)
} }

View File

@ -21,11 +21,12 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io/ioutil" "io"
"math" "math"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os"
"testing" "testing"
"time" "time"
@ -47,7 +48,7 @@ func TestPieceDownloader_DownloadPiece(t *testing.T) {
source.UnRegister("http") source.UnRegister("http")
require.Nil(t, source.Register("http", httpprotocol.NewHTTPSourceClient(), httpprotocol.Adapter)) require.Nil(t, source.Register("http", httpprotocol.NewHTTPSourceClient(), httpprotocol.Adapter))
defer source.UnRegister("http") defer source.UnRegister("http")
testData, err := ioutil.ReadFile(test.File) testData, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
pieceDownloadTimeout := 30 * time.Second pieceDownloadTimeout := 30 * time.Second
@ -152,7 +153,7 @@ func TestPieceDownloader_DownloadPiece(t *testing.T) {
}) })
assert.Nil(err, "downloaded piece should success") assert.Nil(err, "downloaded piece should success")
data, err := ioutil.ReadAll(r) data, err := io.ReadAll(r)
assert.Nil(err, "read piece data should success") assert.Nil(err, "read piece data should success")
c.Close() c.Close()

View File

@ -23,7 +23,6 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
@ -53,7 +52,7 @@ func TestPieceManager_DownloadSource(t *testing.T) {
source.UnRegister("http") source.UnRegister("http")
require.Nil(t, source.Register("http", httpprotocol.NewHTTPSourceClient(), httpprotocol.Adapter)) require.Nil(t, source.Register("http", httpprotocol.NewHTTPSourceClient(), httpprotocol.Adapter))
defer source.UnRegister("http") defer source.UnRegister("http")
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var ( var (
@ -63,11 +62,10 @@ func TestPieceManager_DownloadSource(t *testing.T) {
) )
pieceDownloadTimeout := 30 * time.Second pieceDownloadTimeout := 30 * time.Second
tempDir, _ := ioutil.TempDir("", "d7y-piece-manager-test-*")
storageManager, _ := storage.NewStorageManager( storageManager, _ := storage.NewStorageManager(
config.SimpleLocalTaskStoreStrategy, config.SimpleLocalTaskStoreStrategy,
&config.StorageOption{ &config.StorageOption{
DataPath: tempDir, DataPath: t.TempDir(),
TaskExpireTime: clientutil.Duration{ TaskExpireTime: clientutil.Duration{
Duration: -1 * time.Second, Duration: -1 * time.Second,
}, },
@ -219,7 +217,7 @@ func TestPieceManager_DownloadSource(t *testing.T) {
}) })
assert.Nil(err) assert.Nil(err)
outputBytes, err := ioutil.ReadFile(output) outputBytes, err := os.ReadFile(output)
assert.Nil(err, "load output file") assert.Nil(err, "load output file")
assert.Equal(testBytes, outputBytes, "output and desired output must match") assert.Equal(testBytes, outputBytes, "output and desired output must match")
}) })

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"encoding/json" "encoding/json"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"sync" "sync"
@ -71,7 +70,7 @@ func (t *localTaskStore) WritePiece(ctx context.Context, req *WritePieceRequest)
if piece, ok := t.Pieces[req.Num]; ok { if piece, ok := t.Pieces[req.Num]; ok {
t.RUnlock() t.RUnlock()
// discard data for back source // discard data for back source
n, err := io.Copy(ioutil.Discard, io.LimitReader(req.Reader, req.Range.Length)) n, err := io.Copy(io.Discard, io.LimitReader(req.Reader, req.Range.Length))
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return n, err return n, err
} }
@ -394,7 +393,7 @@ func (t *localTaskStore) Reclaim() error {
t.Infof("purged task work directory: %s", t.dataDir) t.Infof("purged task work directory: %s", t.dataDir)
taskDir := path.Dir(t.dataDir) taskDir := path.Dir(t.dataDir)
if dirs, err := ioutil.ReadDir(taskDir); err != nil { if dirs, err := os.ReadDir(taskDir); err != nil {
t.Warnf("stat task directory %q error: %s", taskDir, err) t.Warnf("stat task directory %q error: %s", taskDir, err)
} else { } else {
if len(dirs) == 0 { if len(dirs) == 0 {

View File

@ -22,7 +22,6 @@ import (
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"io" "io"
"io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path" "path"
@ -46,7 +45,7 @@ func TestMain(m *testing.M) {
func TestLocalTaskStore_PutAndGetPiece_Simple(t *testing.T) { func TestLocalTaskStore_PutAndGetPiece_Simple(t *testing.T) {
assert := testifyassert.New(t) assert := testifyassert.New(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
dst := path.Join(test.DataDir, taskData+".copy") dst := path.Join(test.DataDir, taskData+".copy")
@ -155,7 +154,7 @@ func TestLocalTaskStore_PutAndGetPiece_Simple(t *testing.T) {
}, },
}) })
assert.Nil(err, "get piece should be ok") assert.Nil(err, "get piece should be ok")
data, err := ioutil.ReadAll(rd) data, err := io.ReadAll(rd)
cl.Close() cl.Close()
assert.Nil(err, "read piece should be ok") assert.Nil(err, "read piece should be ok")
assert.Equal(p.end-p.start, len(data), "piece length should match") assert.Equal(p.end-p.start, len(data), "piece length should match")
@ -177,7 +176,7 @@ func TestLocalTaskStore_StoreTaskData_Simple(t *testing.T) {
meta := path.Join(test.DataDir, taskData+".meta") meta := path.Join(test.DataDir, taskData+".meta")
// prepare test data // prepare test data
testData := []byte("test data") testData := []byte("test data")
err := ioutil.WriteFile(src, testData, defaultFileMode) err := os.WriteFile(src, testData, defaultFileMode)
assert.Nil(err, "prepare test data") assert.Nil(err, "prepare test data")
defer os.Remove(src) defer os.Remove(src)
defer os.Remove(dst) defer os.Remove(dst)
@ -207,7 +206,7 @@ func TestLocalTaskStore_StoreTaskData_Simple(t *testing.T) {
}, },
}) })
assert.Nil(err, "store test data") assert.Nil(err, "store test data")
bs, err := ioutil.ReadFile(dst) bs, err := os.ReadFile(dst)
assert.Nil(err, "read output test data") assert.Nil(err, "read output test data")
assert.Equal(testData, bs, "data must match") assert.Equal(testData, bs, "data must match")
} }
@ -218,7 +217,7 @@ func TestLocalTaskStore_ReloadPersistentTask_Simple(t *testing.T) {
func TestLocalTaskStore_PutAndGetPiece_Advance(t *testing.T) { func TestLocalTaskStore_PutAndGetPiece_Advance(t *testing.T) {
assert := testifyassert.New(t) assert := testifyassert.New(t)
testBytes, err := ioutil.ReadFile(test.File) testBytes, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
dst := path.Join(test.DataDir, taskData+".copy") dst := path.Join(test.DataDir, taskData+".copy")
@ -328,7 +327,7 @@ func TestLocalTaskStore_PutAndGetPiece_Advance(t *testing.T) {
}, },
}) })
assert.Nil(err, "get piece should be ok") assert.Nil(err, "get piece should be ok")
data, err := ioutil.ReadAll(rd) data, err := io.ReadAll(rd)
cl.Close() cl.Close()
assert.Nil(err, "read piece should be ok") assert.Nil(err, "read piece should be ok")
assert.Equal(p.end-p.start, len(data), "piece length should match") assert.Equal(p.end-p.start, len(data), "piece length should match")

View File

@ -21,7 +21,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -466,7 +465,7 @@ func (s *storageManager) IsInvalid(req *PeerTaskMetadata) (bool, error) {
} }
func (s *storageManager) ReloadPersistentTask(gcCallback GCCallback) error { func (s *storageManager) ReloadPersistentTask(gcCallback GCCallback) error {
dirs, err := ioutil.ReadDir(s.storeOption.DataPath) dirs, err := os.ReadDir(s.storeOption.DataPath)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil return nil
} }
@ -479,7 +478,7 @@ func (s *storageManager) ReloadPersistentTask(gcCallback GCCallback) error {
) )
for _, dir := range dirs { for _, dir := range dirs {
taskID := dir.Name() taskID := dir.Name()
peerDirs, err := ioutil.ReadDir(path.Join(s.storeOption.DataPath, taskID)) peerDirs, err := os.ReadDir(path.Join(s.storeOption.DataPath, taskID))
if err != nil { if err != nil {
continue continue
} }
@ -502,7 +501,7 @@ func (s *storageManager) ReloadPersistentTask(gcCallback GCCallback) error {
Warnf("open task metadata error: %s", err) Warnf("open task metadata error: %s", err)
continue continue
} }
bytes, err0 := ioutil.ReadAll(t.metadataFile) bytes, err0 := io.ReadAll(t.metadataFile)
if err0 != nil { if err0 != nil {
loadErrs = append(loadErrs, err0) loadErrs = append(loadErrs, err0)
loadErrDirs = append(loadErrDirs, dataDir) loadErrDirs = append(loadErrDirs, dataDir)

View File

@ -20,7 +20,6 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"os" "os"
"testing" "testing"
@ -40,7 +39,7 @@ func TestMain(m *testing.M) {
func TestTransport_RoundTrip(t *testing.T) { func TestTransport_RoundTrip(t *testing.T) {
assert := testifyassert.New(t) assert := testifyassert.New(t)
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
testData, err := ioutil.ReadFile(test.File) testData, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
var url = "http://x/y" var url = "http://x/y"
@ -48,7 +47,7 @@ func TestTransport_RoundTrip(t *testing.T) {
peerTaskManager.EXPECT().StartStreamPeerTask(gomock.Any(), gomock.Any()).DoAndReturn( peerTaskManager.EXPECT().StartStreamPeerTask(gomock.Any(), gomock.Any()).DoAndReturn(
func(ctx context.Context, req *scheduler.PeerTaskRequest) (io.ReadCloser, map[string]string, error) { func(ctx context.Context, req *scheduler.PeerTaskRequest) (io.ReadCloser, map[string]string, error) {
assert.Equal(req.Url, url) assert.Equal(req.Url, url)
return ioutil.NopCloser(bytes.NewBuffer(testData)), nil, nil return io.NopCloser(bytes.NewBuffer(testData)), nil, nil
}, },
) )
rt, _ := New( rt, _ := New(
@ -65,7 +64,7 @@ func TestTransport_RoundTrip(t *testing.T) {
return return
} }
defer resp.Body.Close() defer resp.Body.Close()
output, err := ioutil.ReadAll(resp.Body) output, err := io.ReadAll(resp.Body)
assert.Nil(err) assert.Nil(err)
if err != nil { if err != nil {
return return

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"os" "os"
@ -46,14 +45,14 @@ func TestUploadManager_Serve(t *testing.T) {
defer ctrl.Finish() defer ctrl.Finish()
assert := testifyassert.New(t) assert := testifyassert.New(t)
testData, err := ioutil.ReadFile(test.File) testData, err := os.ReadFile(test.File)
assert.Nil(err, "load test file") assert.Nil(err, "load test file")
mockStorageManager := mock_storage.NewMockManager(ctrl) mockStorageManager := mock_storage.NewMockManager(ctrl)
mockStorageManager.EXPECT().ReadPiece(gomock.Any(), gomock.Any()).AnyTimes(). mockStorageManager.EXPECT().ReadPiece(gomock.Any(), gomock.Any()).AnyTimes().
DoAndReturn(func(ctx context.Context, req *storage.ReadPieceRequest) (io.Reader, io.Closer, error) { DoAndReturn(func(ctx context.Context, req *storage.ReadPieceRequest) (io.Reader, io.Closer, error) {
return bytes.NewBuffer(testData[req.Range.Start : req.Range.Start+req.Range.Length]), return bytes.NewBuffer(testData[req.Range.Start : req.Range.Start+req.Range.Length]),
ioutil.NopCloser(nil), nil io.NopCloser(nil), nil
}) })
um, err := NewUploadManager(mockStorageManager, WithLimiter(rate.NewLimiter(16*1024, 16*1024))) um, err := NewUploadManager(mockStorageManager, WithLimiter(rate.NewLimiter(16*1024, 16*1024)))
@ -103,7 +102,7 @@ func TestUploadManager_Serve(t *testing.T) {
resp, err := http.DefaultClient.Do(req) resp, err := http.DefaultClient.Do(req)
assert.Nil(err, "get piece data") assert.Nil(err, "get piece data")
data, _ := ioutil.ReadAll(resp.Body) data, _ := io.ReadAll(resp.Body)
resp.Body.Close() resp.Body.Close()
assert.Equal(tt.targetPieceData, data) assert.Equal(tt.targetPieceData, data)
} }

View File

@ -20,7 +20,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -142,7 +141,7 @@ func downloadFromSource(ctx context.Context, cfg *config.DfgetConfig, hdr map[st
wLog.Info("try to download from source and ignore rate limit") wLog.Info("try to download from source and ignore rate limit")
fmt.Println("try to download from source and ignore rate limit") fmt.Println("try to download from source and ignore rate limit")
if target, err = ioutil.TempFile(filepath.Dir(cfg.Output), ".df_"); err != nil { if target, err = os.CreateTemp(filepath.Dir(cfg.Output), ".df_"); err != nil {
return err return err
} }
defer os.Remove(target.Name()) defer os.Remove(target.Name())

View File

@ -18,7 +18,7 @@ package dfget
import ( import (
"context" "context"
"io/ioutil" "io"
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -56,7 +56,7 @@ func Test_downloadFromSource(t *testing.T) {
} }
request, err := source.NewRequest(cfg.URL) request, err := source.NewRequest(cfg.URL)
assert.Nil(t, err) assert.Nil(t, err)
sourceClient.EXPECT().Download(request).Return(ioutil.NopCloser(strings.NewReader(content)), nil) sourceClient.EXPECT().Download(request).Return(io.NopCloser(strings.NewReader(content)), nil)
err = downloadFromSource(context.Background(), cfg, nil) err = downloadFromSource(context.Background(), cfg, nil)
assert.Nil(t, err) assert.Nil(t, err)

View File

@ -19,7 +19,6 @@ package dependency
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -48,7 +47,7 @@ func ListAvailablePlugins() {
} }
fmt.Fprintf(os.Stderr, "search plugin in %s\n", d.PluginDir()) fmt.Fprintf(os.Stderr, "search plugin in %s\n", d.PluginDir())
files, err := ioutil.ReadDir(d.PluginDir()) files, err := os.ReadDir(d.PluginDir())
if os.IsNotExist(err) { if os.IsNotExist(err) {
fmt.Fprintf(os.Stderr, "no plugin found\n") fmt.Fprintf(os.Stderr, "no plugin found\n")
return return

View File

@ -55,7 +55,6 @@ import (
"bytes" "bytes"
"context" "context"
"io" "io"
"io/ioutil"
"time" "time"
"d7y.io/dragonfly/v2/pkg/source" "d7y.io/dragonfly/v2/pkg/source"
@ -88,11 +87,11 @@ func (c *client) IsExpired(ctx context.Context, url string, header source.Reques
} }
func (c *client) Download(ctx context.Context, url string, header source.RequestHeader, rang *rangeutils.Range) (io.ReadCloser, error) { func (c *client) Download(ctx context.Context, url string, header source.RequestHeader, rang *rangeutils.Range) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBufferString(data)), nil return io.NopCloser(bytes.NewBufferString(data)), nil
} }
func (c *client) DownloadWithResponseHeader(ctx context.Context, url string, header source.RequestHeader, rang *rangeutils.Range) (io.ReadCloser, source.ResponseHeader, error) { func (c *client) DownloadWithResponseHeader(ctx context.Context, url string, header source.RequestHeader, rang *rangeutils.Range) (io.ReadCloser, source.ResponseHeader, error) {
return ioutil.NopCloser(bytes.NewBufferString(data)), map[string]string{}, nil return io.NopCloser(bytes.NewBufferString(data)), map[string]string{}, nil
} }
func (c *client) GetLastModifiedMillis(ctx context.Context, url string, header source.RequestHeader) (int64, error) { func (c *client) GetLastModifiedMillis(ctx context.Context, url string, header source.RequestHeader) (int64, error) {

View File

@ -17,7 +17,7 @@
package dynconfig package dynconfig
import ( import (
"io/ioutil" "os"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
) )
@ -38,7 +38,7 @@ func newDynconfigLocal(path string) (*dynconfigLocal, error) {
// Unmarshal unmarshals the config into a Struct. Make sure that the tags // Unmarshal unmarshals the config into a Struct. Make sure that the tags
// on the fields of the structure are properly set. // on the fields of the structure are properly set.
func (d *dynconfigLocal) Unmarshal(rawVal interface{}) error { func (d *dynconfigLocal) Unmarshal(rawVal interface{}) error {
b, err := ioutil.ReadFile(d.filepath) b, err := os.ReadFile(d.filepath)
if err != nil { if err != nil {
return err return err
} }

View File

@ -17,7 +17,7 @@
package config package config
import ( import (
"io/ioutil" "os"
"testing" "testing"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -76,7 +76,7 @@ func TestManagerConfig_Load(t *testing.T) {
} }
managerConfigYAML := &Config{} managerConfigYAML := &Config{}
contentYAML, _ := ioutil.ReadFile("./testdata/manager.yaml") contentYAML, _ := os.ReadFile("./testdata/manager.yaml")
var dataYAML map[string]interface{} var dataYAML map[string]interface{}
if err := yaml.Unmarshal(contentYAML, &dataYAML); err != nil { if err := yaml.Unmarshal(contentYAML, &dataYAML); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -22,7 +22,7 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -230,7 +230,7 @@ func (p *preheat) getManifests(ctx context.Context, url string, header http.Head
} }
func (p *preheat) parseLayers(resp *http.Response, url, filter string, header http.Header, image *preheatImage) ([]*internaljob.PreheatRequest, error) { func (p *preheat) parseLayers(resp *http.Response, url, filter string, header http.Header, image *preheatImage) ([]*internaljob.PreheatRequest, error) {
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -284,7 +284,7 @@ func getAuthToken(ctx context.Context, header http.Header) (string, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body) body, _ := io.ReadAll(resp.Body)
var result map[string]interface{} var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil { if err := json.Unmarshal(body, &result); err != nil {
return "", err return "", err

View File

@ -18,7 +18,7 @@ package cache
import ( import (
"bytes" "bytes"
"io/ioutil" "os"
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
@ -331,7 +331,7 @@ func TestFileSerialization(t *testing.T) {
t.Error(err) t.Error(err)
} }
f, err := ioutil.TempFile("", "go-cache-cache.dat") f, err := os.CreateTemp("", "go-cache-cache.dat")
if err != nil { if err != nil {
t.Fatal("Couldn't create cache file:", err) t.Fatal("Couldn't create cache file:", err)
} }

View File

@ -21,7 +21,6 @@ package hdfsprotocol
import ( import (
"io" "io"
"io/ioutil"
"os" "os"
"reflect" "reflect"
"testing" "testing"
@ -231,7 +230,7 @@ func Test_Download_FileExist_ByRange(t *testing.T) {
download, err := sourceClient.Download(request) download, err := sourceClient.Download(request)
assert.Nil(t, err) assert.Nil(t, err)
data, _ := ioutil.ReadAll(download) data, _ := io.ReadAll(download)
assert.Equal(t, hdfsExistFileContent, string(data)) assert.Equal(t, hdfsExistFileContent, string(data))
} }
@ -290,7 +289,7 @@ func Test_DownloadWithResponseHeader_FileExist_ByRange(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.Equal(t, hdfsExistFileLastModified, expireInfo.LastModified) assert.Equal(t, hdfsExistFileLastModified, expireInfo.LastModified)
data, _ := ioutil.ReadAll(body) data, _ := io.ReadAll(body)
assert.Equal(t, string(data), string([]byte(hdfsExistFileContent)[hdfsExistFileRangeStart:hdfsExistFileRangeEnd])) assert.Equal(t, string(data), string([]byte(hdfsExistFileContent)[hdfsExistFileRangeStart:hdfsExistFileRangeEnd]))
} }

View File

@ -19,7 +19,7 @@ package httpprotocol
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"testing" "testing"
"time" "time"
@ -208,7 +208,7 @@ func (suite *HTTPSourceClientTestSuite) TestHttpSourceClientDownloadWithResponse
suite.True(tt.wantErr.Error() == err.Error()) suite.True(tt.wantErr.Error() == err.Error())
return return
} }
bytes, err := ioutil.ReadAll(reader) bytes, err := io.ReadAll(reader)
suite.Nil(err) suite.Nil(err)
suite.Equal(tt.content, string(bytes)) suite.Equal(tt.content, string(bytes))
suite.Equal(tt.expireInfo, expireInfo) suite.Equal(tt.expireInfo, expireInfo)
@ -325,7 +325,7 @@ func (suite *HTTPSourceClientTestSuite) TestHttpSourceClientDoRequest() {
suite.Nil(err) suite.Nil(err)
res, err := suite.httpClient.doRequest(http.MethodGet, request) res, err := suite.httpClient.doRequest(http.MethodGet, request)
suite.Nil(err) suite.Nil(err)
bytes, err := ioutil.ReadAll(res.Body) bytes, err := io.ReadAll(res.Body)
suite.Nil(err) suite.Nil(err)
suite.EqualValues("ok", string(bytes)) suite.EqualValues("ok", string(bytes))
} }

View File

@ -18,7 +18,7 @@ package main
import ( import (
"fmt" "fmt"
"io/ioutil" "io"
"os" "os"
"d7y.io/dragonfly/v2/pkg/source" "d7y.io/dragonfly/v2/pkg/source"
@ -45,7 +45,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
data, err := ioutil.ReadAll(rc) data, err := io.ReadAll(rc)
if err != nil { if err != nil {
fmt.Printf("read error: %s\n", err) fmt.Printf("read error: %s\n", err)
os.Exit(1) os.Exit(1)

View File

@ -19,7 +19,6 @@ package main
import ( import (
"bytes" "bytes"
"io" "io"
"io/ioutil"
"d7y.io/dragonfly/v2/pkg/source" "d7y.io/dragonfly/v2/pkg/source"
) )
@ -44,11 +43,11 @@ func (c *client) IsExpired(request *source.Request, info *source.ExpireInfo) (bo
} }
func (c *client) Download(request *source.Request) (io.ReadCloser, error) { func (c *client) Download(request *source.Request) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBufferString(data)), nil return io.NopCloser(bytes.NewBufferString(data)), nil
} }
func (c *client) DownloadWithExpireInfo(request *source.Request) (io.ReadCloser, *source.ExpireInfo, error) { func (c *client) DownloadWithExpireInfo(request *source.Request) (io.ReadCloser, *source.ExpireInfo, error) {
return ioutil.NopCloser(bytes.NewBufferString(data)), nil, nil return io.NopCloser(bytes.NewBufferString(data)), nil, nil
} }
func (c *client) GetLastModified(request *source.Request) (int64, error) { func (c *client) GetLastModified(request *source.Request) (int64, error) {

View File

@ -20,7 +20,7 @@ import (
"bytes" "bytes"
"crypto/md5" "crypto/md5"
"encoding/hex" "encoding/hex"
"io/ioutil" "io"
"os" "os"
"testing" "testing"
@ -43,7 +43,7 @@ func TestNewDigestReader(t *testing.T) {
buf := bytes.NewBuffer(testBytes) buf := bytes.NewBuffer(testBytes)
reader := NewDigestReader(logger.With("test", "test"), buf, digest) reader := NewDigestReader(logger.With("test", "test"), buf, digest)
data, err := ioutil.ReadAll(reader) data, err := io.ReadAll(reader)
assert.Nil(err) assert.Nil(err)
assert.Equal(testBytes, data) assert.Equal(testBytes, data)

View File

@ -18,7 +18,6 @@ package test
import ( import (
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"syscall" "syscall"
@ -49,7 +48,7 @@ func Test(t *testing.T) {
} }
func (s *FileUtilsTestSuite) SetupSuite() { func (s *FileUtilsTestSuite) SetupSuite() {
s.workDir, _ = ioutil.TempDir(basic.TmpDir, "DF-FileUtilsTestSuite-") s.workDir, _ = os.MkdirTemp(basic.TmpDir, "DF-FileUtilsTestSuite-")
s.username = basic.Username s.username = basic.Username
} }
@ -154,7 +153,7 @@ func (s *FileUtilsTestSuite) TestCopyFile() {
_, err = filerw.CopyFile(s.testFile, s.testFile+".new") _, err = filerw.CopyFile(s.testFile, s.testFile+".new")
s.Require().Nil(err) s.Require().Nil(err)
content, err := ioutil.ReadFile(s.testFile + ".new") content, err := os.ReadFile(s.testFile + ".new")
s.Require().Nil(err) s.Require().Nil(err)
s.Require().Equal("hello,world", string(content)) s.Require().Equal("hello,world", string(content))
} }

View File

@ -17,7 +17,7 @@
package config package config
import ( import (
"io/ioutil" "os"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@ -79,7 +79,7 @@ func TestSchedulerConfig_Load(t *testing.T) {
} }
schedulerConfigYAML := &Config{} schedulerConfigYAML := &Config{}
contentYAML, _ := ioutil.ReadFile("./testdata/scheduler.yaml") contentYAML, _ := os.ReadFile("./testdata/scheduler.yaml")
var dataYAML map[string]interface{} var dataYAML map[string]interface{}
if err := yaml.Unmarshal(contentYAML, &dataYAML); err != nil { if err := yaml.Unmarshal(contentYAML, &dataYAML); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@ -18,7 +18,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -223,7 +222,7 @@ func (d *dynconfig) Get() (*DynconfigData, error) {
} }
func (d *dynconfig) getCDNFromDirPath() ([]*CDN, error) { func (d *dynconfig) getCDNFromDirPath() ([]*CDN, error) {
files, err := ioutil.ReadDir(d.cdnDirPath) files, err := os.ReadDir(d.cdnDirPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -236,7 +235,7 @@ func (d *dynconfig) getCDNFromDirPath() ([]*CDN, error) {
} }
p := filepath.Join(d.cdnDirPath, file.Name()) p := filepath.Join(d.cdnDirPath, file.Name())
if file.Mode()&os.ModeSymlink != 0 { if file.Type()&os.ModeSymlink != 0 {
stat, err := os.Stat(p) stat, err := os.Stat(p)
if err != nil { if err != nil {
logger.Errorf("stat %s error: %s", file.Name(), err) logger.Errorf("stat %s error: %s", file.Name(), err)
@ -247,7 +246,7 @@ func (d *dynconfig) getCDNFromDirPath() ([]*CDN, error) {
continue continue
} }
} }
b, err := ioutil.ReadFile(p) b, err := os.ReadFile(p)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -17,7 +17,6 @@
package config package config
import ( import (
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@ -33,10 +32,7 @@ import (
) )
func TestDynconfigGet_ManagerSourceType(t *testing.T) { func TestDynconfigGet_ManagerSourceType(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test") mockCacheDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
mockCachePath := filepath.Join(mockCacheDir, cacheFileName) mockCachePath := filepath.Join(mockCacheDir, cacheFileName)
tests := []struct { tests := []struct {
@ -139,10 +135,7 @@ func TestDynconfigGet_ManagerSourceType(t *testing.T) {
} }
func TestDynconfigGet_LocalSourceType(t *testing.T) { func TestDynconfigGet_LocalSourceType(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test") mockCacheDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string
@ -197,10 +190,7 @@ func TestDynconfigGet_LocalSourceType(t *testing.T) {
} }
func TestDynconfigGetCDNFromDirPath(t *testing.T) { func TestDynconfigGetCDNFromDirPath(t *testing.T) {
mockCacheDir, err := ioutil.TempDir("", "dragonfly-test") mockCacheDir := t.TempDir()
if err != nil {
t.Fatal(err)
}
tests := []struct { tests := []struct {
name string name string

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"reflect" "reflect"
"sync" "sync"
@ -235,7 +234,7 @@ func downloadTinyFile(ctx context.Context, task *Task, cdnHost *Host) ([]byte, e
} }
defer resp.Body.Close() defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -21,7 +21,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"reflect" "reflect"
"testing" "testing"
@ -471,7 +470,7 @@ func TestCDN_Initial(t *testing.T) {
const testwords string = "dragonfly-scheduler-test" const testwords string = "dragonfly-scheduler-test"
res := &http.Response{ res := &http.Response{
Body: ioutil.NopCloser( Body: io.NopCloser(
bytes.NewBuffer([]byte(testwords))), bytes.NewBuffer([]byte(testwords))),
} }
httpRet := []gomonkey.OutputCell{ httpRet := []gomonkey.OutputCell{

View File

@ -21,7 +21,6 @@ import (
"flag" "flag"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
"net/http" "net/http"
@ -166,7 +165,7 @@ func process(ctx context.Context, wg *sync.WaitGroup, result chan *Result) {
continue continue
} }
var msg string var msg string
n, err := io.Copy(ioutil.Discard, resp.Body) n, err := io.Copy(io.Discard, resp.Body)
if err != nil { if err != nil {
msg = err.Error() msg = err.Error()
log.Printf("discard data error: %s", err) log.Printf("discard data error: %s", err)