Skip to content

Commit

Permalink
deprecate ioutil
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed Sep 22, 2022
1 parent 3be450f commit c060331
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 38 deletions.
33 changes: 16 additions & 17 deletions ecs-init/cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"testing"
Expand Down Expand Up @@ -102,7 +101,7 @@ func TestIsAgentCachedTrue(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

file := ioutil.NopCloser(bytes.NewBufferString(fmt.Sprintf("%d", StatusCached)))
file := io.NopCloser(bytes.NewBufferString(fmt.Sprintf("%d", StatusCached)))
mockFS := NewMockfileSystem(mockCtrl)
mockFSInfo := NewMockfileSizeInfo(mockCtrl)
mockFS.EXPECT().Stat(config.CacheState()).Return(mockFSInfo, nil)
Expand Down Expand Up @@ -138,7 +137,7 @@ func TestAgentCacheStatus(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

file := ioutil.NopCloser(bytes.NewBufferString(testcase.data))
file := io.NopCloser(bytes.NewBufferString(testcase.data))
mockFS := NewMockfileSystem(mockCtrl)
mockFSInfo := NewMockfileSizeInfo(mockCtrl)

Expand Down Expand Up @@ -227,7 +226,7 @@ func TestDownloadAgentReadPublishedMd5Failure(t *testing.T) {
mockS3Downloader := NewMocks3DownloaderAPI(mockCtrl)
mockMetadata := NewMockinstanceMetadata(mockCtrl)

tempMD5File, err := ioutil.TempFile("", "md5-test")
tempMD5File, err := os.CreateTemp("", "md5-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempMD5File.Close()

Expand Down Expand Up @@ -259,11 +258,11 @@ func TestDownloadAgentDownloadTarballFailure(t *testing.T) {
mockS3Downloader := NewMocks3DownloaderAPI(mockCtrl)
mockMetadata := NewMockinstanceMetadata(mockCtrl)

tempMD5File, err := ioutil.TempFile("", "md5-test")
tempMD5File, err := os.CreateTemp("", "md5-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempMD5File.Close()

tempAgentFile, err := ioutil.TempFile("", "agent-test")
tempAgentFile, err := os.CreateTemp("", "agent-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempAgentFile.Close()

Expand Down Expand Up @@ -296,15 +295,15 @@ func TestDownloadAgentCopyFailure(t *testing.T) {
mockS3Downloader := NewMocks3DownloaderAPI(mockCtrl)
mockMetadata := NewMockinstanceMetadata(mockCtrl)

tempMD5File, err := ioutil.TempFile("", "md5-test")
tempMD5File, err := os.CreateTemp("", "md5-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempMD5File.Close()

tempAgentFile, err := ioutil.TempFile("", "agent-test")
tempAgentFile, err := os.CreateTemp("", "agent-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempAgentFile.Close()

tempReader := ioutil.NopCloser(&bytes.Buffer{})
tempReader := io.NopCloser(&bytes.Buffer{})

gomock.InOrder(
mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700),
Expand Down Expand Up @@ -339,15 +338,15 @@ func TestDownloadAgentMD5Mismatch(t *testing.T) {
mockS3Downloader := NewMocks3DownloaderAPI(mockCtrl)
mockMetadata := NewMockinstanceMetadata(mockCtrl)

tempMD5File, err := ioutil.TempFile("", "md5-test")
tempMD5File, err := os.CreateTemp("", "md5-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempMD5File.Close()

tempAgentFile, err := ioutil.TempFile("", "agent-test")
tempAgentFile, err := os.CreateTemp("", "agent-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempAgentFile.Close()

tempReader := ioutil.NopCloser(&bytes.Buffer{})
tempReader := io.NopCloser(&bytes.Buffer{})

gomock.InOrder(
mockFS.EXPECT().MkdirAll(config.CacheDirectory(), os.ModeDir|0700),
Expand Down Expand Up @@ -377,14 +376,14 @@ func TestDownloadAgentSuccess(t *testing.T) {
defer mockCtrl.Finish()

tarballContents := "tarball contents"
tarballReader := ioutil.NopCloser(bytes.NewBufferString(tarballContents))
tarballReader := io.NopCloser(bytes.NewBufferString(tarballContents))
expectedMd5Sum := fmt.Sprintf("%x\n", md5.Sum([]byte(tarballContents)))

tempMD5File, err := ioutil.TempFile("", "md5-test")
tempMD5File, err := os.CreateTemp("", "md5-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempMD5File.Close()

tempAgentFile, err := ioutil.TempFile("", "agent-test")
tempAgentFile, err := os.CreateTemp("", "agent-test")
assert.NoError(t, err, "Expect to successfully create a temporary file")
defer tempAgentFile.Close()

Expand Down Expand Up @@ -440,7 +439,7 @@ func TestLoadDesiredAgentFailReadDesired(t *testing.T) {

mockFS := NewMockfileSystem(mockCtrl)

mockFS.EXPECT().Open(config.DesiredImageLocatorFile()).Return(ioutil.NopCloser(&bytes.Buffer{}), nil)
mockFS.EXPECT().Open(config.DesiredImageLocatorFile()).Return(io.NopCloser(&bytes.Buffer{}), nil)

d := &Downloader{
fs: mockFS,
Expand Down Expand Up @@ -472,7 +471,7 @@ func TestLoadDesiredAgent(t *testing.T) {

mockFS := NewMockfileSystem(mockCtrl)

mockFS.EXPECT().Open(config.DesiredImageLocatorFile()).Return(ioutil.NopCloser(bytes.NewBufferString(desiredImage+"\n")), nil)
mockFS.EXPECT().Open(config.DesiredImageLocatorFile()).Return(io.NopCloser(bytes.NewBufferString(desiredImage+"\n")), nil)
mockFS.EXPECT().Base(gomock.Any()).Return(desiredImage + "\n")
mockFS.EXPECT().Open(config.CacheDirectory() + "/" + desiredImage)

Expand Down
9 changes: 4 additions & 5 deletions ecs-init/cache/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ package cache

import (
"io"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -118,7 +117,7 @@ func (d *s3Downloader) downloadFile(fileName string) (string, error) {
return "", errors.New("failed to download file from s3")
}

// fileSystem captures related functions from os, io, and io/ioutil packages
// fileSystem captures related functions from io and os packages
type fileSystem interface {
MkdirAll(path string, perm os.FileMode) error
TempFile(dir, prefix string) (f *os.File, err error)
Expand Down Expand Up @@ -156,7 +155,7 @@ func (s *standardFS) MkdirAll(path string, perm os.FileMode) error {
}

func (s *standardFS) TempFile(dir, prefix string) (*os.File, error) {
return ioutil.TempFile(dir, prefix)
return os.CreateTemp(dir, prefix)
}

func (s *standardFS) Remove(path string) {
Expand All @@ -176,7 +175,7 @@ func (s *standardFS) Rename(oldpath, newpath string) error {
}

func (s *standardFS) ReadAll(r io.Reader) ([]byte, error) {
return ioutil.ReadAll(r)
return io.ReadAll(r)
}

func (s *standardFS) Open(name string) (io.ReadCloser, error) {
Expand All @@ -192,5 +191,5 @@ func (s *standardFS) Base(path string) string {
}

func (s *standardFS) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
return os.WriteFile(filename, data, perm)
}
4 changes: 2 additions & 2 deletions ecs-init/docker/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ package docker
//go:generate mockgen.sh $GOPACKAGE $GOFILE

import (
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"time"

"github.com/aws/amazon-ecs-agent/ecs-init/backoff"
Expand Down Expand Up @@ -142,7 +142,7 @@ type _standardFS struct{}
var standardFS = &_standardFS{}

func (s *_standardFS) ReadFile(filename string) ([]byte, error) {
return ioutil.ReadFile(filename)
return os.ReadFile(filename)
}

func isNetworkError(err error) bool {
Expand Down
5 changes: 2 additions & 3 deletions ecs-init/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package docker

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -945,13 +944,13 @@ func TestGetCapabilityExecBinds(t *testing.T) {
}

func TestDefaultIsPathValid(t *testing.T) {
rootDir, err := ioutil.TempDir(os.TempDir(), testTempDirPrefix)
rootDir, err := os.MkdirTemp(os.TempDir(), testTempDirPrefix)
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(rootDir)

file, err := ioutil.TempFile(rootDir, "file")
file, err := os.CreateTemp(rootDir, "file")
if err != nil {
t.Fatal(err)
}
Expand Down
12 changes: 6 additions & 6 deletions ecs-init/engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"io"
"os"
"testing"

Expand Down Expand Up @@ -79,7 +79,7 @@ func TestPreStartReloadNeeded(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cachedAgentBuffer := ioutil.NopCloser(&bytes.Buffer{})
cachedAgentBuffer := io.NopCloser(&bytes.Buffer{})

mockDocker := NewMockdockerClient(mockCtrl)
defer getDockerClientMock(mockDocker)()
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestPreStartImageNotLoadedCached(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cachedAgentBuffer := ioutil.NopCloser(&bytes.Buffer{})
cachedAgentBuffer := io.NopCloser(&bytes.Buffer{})

mockDocker := NewMockdockerClient(mockCtrl)
mockDownloader := NewMockdownloader(mockCtrl)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestPreStartImageNotCached(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cachedAgentBuffer := ioutil.NopCloser(&bytes.Buffer{})
cachedAgentBuffer := io.NopCloser(&bytes.Buffer{})

mockDocker := NewMockdockerClient(mockCtrl)
defer getDockerClientMock(mockDocker)()
Expand Down Expand Up @@ -431,7 +431,7 @@ func TestReloadCacheNotCached(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cachedAgentBuffer := ioutil.NopCloser(&bytes.Buffer{})
cachedAgentBuffer := io.NopCloser(&bytes.Buffer{})

mockDocker := NewMockdockerClient(mockCtrl)
defer getDockerClientMock(mockDocker)()
Expand All @@ -456,7 +456,7 @@ func TestReloadCacheCached(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

cachedAgentBuffer := ioutil.NopCloser(&bytes.Buffer{})
cachedAgentBuffer := io.NopCloser(&bytes.Buffer{})

mockDocker := NewMockdockerClient(mockCtrl)
defer getDockerClientMock(mockDocker)()
Expand Down
3 changes: 1 addition & 2 deletions ecs-init/gpu/nvidia_gpu_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ package gpu

import (
"encoding/json"
"io/ioutil"
"os"
"path/filepath"

Expand Down Expand Up @@ -219,5 +218,5 @@ func WriteToFile(filename string, data []byte, perm os.FileMode) error {
if err != nil {
return err
}
return ioutil.WriteFile(filename, data, perm)
return os.WriteFile(filename, data, perm)
}
5 changes: 2 additions & 3 deletions ecs-init/volumes/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package volumes
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -94,7 +93,7 @@ var saveStateToDisk = saveState
func saveState(b []byte) error {
// Make our temp-file on the same volume as our data-file to ensure we can
// actually move it atomically; cross-device renaming will error out
tmpfile, err := ioutil.TempFile(PluginStatePath, "tmp_ecs_volume_plugin")
tmpfile, err := os.CreateTemp(PluginStatePath, "tmp_ecs_volume_plugin")
if err != nil {
return fmt.Errorf("failed to create temp file: %v", err)
}
Expand Down Expand Up @@ -154,5 +153,5 @@ func (s *StateManager) load(a interface{}) error {
var readStateFile = readFile

func readFile() ([]byte, error) {
return ioutil.ReadFile(PluginStateFileAbsPath)
return os.ReadFile(PluginStateFileAbsPath)
}

0 comments on commit c060331

Please sign in to comment.