refactor: move from io/ioutil to io and os package

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-09-10 20:28:11 +08:00
parent c16be1a5e2
commit 50da673592
126 changed files with 291 additions and 399 deletions

View File

@@ -22,7 +22,6 @@ import (
"flag"
"fmt"
"io"
"io/ioutil"
"os"
"testing"
"time"
@@ -90,7 +89,7 @@ func TestMain(m *testing.M) {
if !noDaemon {
sys.ForceRemoveAll(defaultRoot)
stdioFile, err := ioutil.TempFile("", "")
stdioFile, err := os.CreateTemp("", "")
if err != nil {
fmt.Fprintf(os.Stderr, "could not create a new stdio temp file: %s\n", err)
os.Exit(1)
@@ -497,7 +496,7 @@ func TestClientReconnect(t *testing.T) {
}
func createShimDebugConfig() string {
f, err := ioutil.TempFile("", "containerd-config-")
f, err := os.CreateTemp("", "containerd-config-")
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create config file: %s\n", err)
os.Exit(1)

View File

@@ -22,7 +22,6 @@ import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -455,7 +454,7 @@ func TestCheckpointRestoreWithImagePath(t *testing.T) {
}
// create image path store criu image files
crDir, err := ioutil.TempDir("", "test-cr")
crDir, err := os.MkdirTemp("", "test-cr")
if err != nil {
t.Fatal(err)
}
@@ -473,7 +472,7 @@ func TestCheckpointRestoreWithImagePath(t *testing.T) {
task.Delete(ctx)
// check image files have been dumped into image path
if files, err := ioutil.ReadDir(imagePath); err != nil || len(files) == 0 {
if files, err := os.ReadDir(imagePath); err != nil || len(files) == 0 {
t.Fatal("failed to checkpoint with image path set")
}

View File

@@ -21,7 +21,6 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"runtime"
@@ -573,7 +572,7 @@ func TestDaemonReconnectsToShimIOPipesOnRestart(t *testing.T) {
<-statusC
stdioContents, err := ioutil.ReadFile(ctrdStdioFilePath)
stdioContents, err := os.ReadFile(ctrdStdioFilePath)
if err != nil {
t.Fatal(err)
}
@@ -1963,7 +1962,7 @@ func TestContainerNoSTDIN(t *testing.T) {
}
defer container.Delete(ctx, WithSnapshotCleanup)
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, ioutil.Discard, ioutil.Discard)))
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(nil, io.Discard, io.Discard)))
if err != nil {
t.Fatal(err)
}

View File

@@ -20,7 +20,6 @@ import (
"bytes"
"context"
"io"
"io/ioutil"
"os"
"path"
"runtime"
@@ -231,7 +230,7 @@ func TestContainerOutput(t *testing.T) {
}
func withByteBuffers(stdout io.Writer) cio.Opt {
// TODO: could this use ioutil.Discard?
// TODO: could this use io.Discard?
return func(streams *cio.Streams) {
streams.Stdin = new(bytes.Buffer)
streams.Stdout = stdout
@@ -515,7 +514,7 @@ func TestContainerCloseIO(t *testing.T) {
t.Fatal(err)
}
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(r, stdout, ioutil.Discard)))
task, err := container.NewTask(ctx, cio.NewCreator(cio.WithStreams(r, stdout, io.Discard)))
if err != nil {
t.Fatal(err)
}

View File

@@ -21,7 +21,6 @@ import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -52,7 +51,7 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
buf = bytes.NewBuffer(nil)
)
tempDir, err := ioutil.TempDir("", "containerd-test-new-daemon-with-config")
tempDir, err := os.MkdirTemp("", "containerd-test-new-daemon-with-config")
if err != nil {
t.Fatal(err)
}
@@ -63,7 +62,7 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
}()
configTOMLFile := filepath.Join(tempDir, "config.toml")
if err = ioutil.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil {
if err = os.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil {
t.Fatal(err)
}
@@ -119,7 +118,7 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
// TestDaemonRuntimeRoot ensures plugin.linux.runtime_root is not ignored
func TestDaemonRuntimeRoot(t *testing.T) {
runtimeRoot, err := ioutil.TempDir("", "containerd-test-runtime-root")
runtimeRoot, err := os.MkdirTemp("", "containerd-test-runtime-root")
if err != nil {
t.Fatal(err)
}

View File

@@ -22,7 +22,6 @@ import (
"encoding/json"
"io"
"io/ioutil"
"math/rand"
"reflect"
"testing"
@@ -285,7 +284,7 @@ func checkImages(t *testing.T, target digest.Digest, actual []images.Image, name
}
func createContent(size int64, seed int64) ([]byte, digest.Digest) {
b, err := ioutil.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), size))
b, err := io.ReadAll(io.LimitReader(rand.New(rand.NewSource(seed)), size))
if err != nil {
panic(err)
}