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 @@ package btrfs
import (
"bytes"
"context"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -46,7 +45,7 @@ func boltSnapshotter(t *testing.T) func(context.Context, string) (snapshots.Snap
t.Skipf("could not find mkfs.btrfs: %v", err)
}
procModules, err := ioutil.ReadFile("/proc/modules")
procModules, err := os.ReadFile("/proc/modules")
if err == nil && !bytes.Contains(procModules, []byte("btrfs")) {
t.Skip("check for btrfs kernel module failed, skipping test")
}
@@ -122,14 +121,14 @@ func TestBtrfsMounts(t *testing.T) {
ctx := context.Background()
// create temporary directory for mount point
mountPoint, err := ioutil.TempDir("", "containerd-btrfs-test")
mountPoint, err := os.MkdirTemp("", "containerd-btrfs-test")
if err != nil {
t.Fatal("could not create mount point for btrfs test", err)
}
defer os.RemoveAll(mountPoint)
t.Log("temporary mount point created", mountPoint)
root, err := ioutil.TempDir(mountPoint, "TestBtrfsPrepare-")
root, err := os.MkdirTemp(mountPoint, "TestBtrfsPrepare-")
if err != nil {
t.Fatal(err)
}
@@ -168,7 +167,7 @@ func TestBtrfsMounts(t *testing.T) {
defer testutil.Unmount(t, target)
// write in some data
if err := ioutil.WriteFile(filepath.Join(target, "foo"), []byte("content"), 0777); err != nil {
if err := os.WriteFile(filepath.Join(target, "foo"), []byte("content"), 0777); err != nil {
t.Fatal(err)
}
@@ -198,7 +197,7 @@ func TestBtrfsMounts(t *testing.T) {
defer testutil.Unmount(t, target)
// TODO(stevvooe): Verify contents of "foo"
if err := ioutil.WriteFile(filepath.Join(target, "bar"), []byte("content"), 0777); err != nil {
if err := os.WriteFile(filepath.Join(target, "bar"), []byte("content"), 0777); err != nil {
t.Fatal(err)
}

View File

@@ -20,7 +20,6 @@
package devmapper
import (
"io/ioutil"
"os"
"testing"
@@ -37,7 +36,7 @@ func TestLoadConfig(t *testing.T) {
BaseImageSize: "128Mb",
}
file, err := ioutil.TempFile("", "devmapper-config-")
file, err := os.CreateTemp("", "devmapper-config-")
assert.NilError(t, err)
encoder := toml.NewEncoder(file)

View File

@@ -20,7 +20,6 @@
package dmsetup
import (
"io/ioutil"
"os"
"strings"
"testing"
@@ -44,7 +43,7 @@ const (
func TestDMSetup(t *testing.T) {
testutil.RequiresRoot(t)
tempDir, err := ioutil.TempDir("", "dmsetup-tests-")
tempDir, err := os.MkdirTemp("", "dmsetup-tests-")
assert.NilError(t, err, "failed to make temp dir for tests")
defer func() {
@@ -191,7 +190,7 @@ func testVersion(t *testing.T) {
}
func createLoopbackDevice(t *testing.T, dir string) (string, string) {
file, err := ioutil.TempFile(dir, "dmsetup-tests-")
file, err := os.CreateTemp(dir, "dmsetup-tests-")
assert.NilError(t, err)
size, err := units.RAMInBytes("16Mb")

View File

@@ -21,7 +21,6 @@ package devmapper
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -233,7 +232,7 @@ func TestPoolMetadata_GetDeviceNames(t *testing.T) {
}
func createStore(t *testing.T) (tempDir string, store *PoolMetadata) {
tempDir, err := ioutil.TempDir("", "pool-metadata-")
tempDir, err := os.MkdirTemp("", "pool-metadata-")
assert.NilError(t, err, "couldn't create temp directory for metadata tests")
path := filepath.Join(tempDir, "test.db")

View File

@@ -22,7 +22,6 @@ package devmapper
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
@@ -62,7 +61,7 @@ func TestPoolDevice(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel)
ctx := context.Background()
tempDir, err := ioutil.TempDir("", "pool-device-test-")
tempDir, err := os.MkdirTemp("", "pool-device-test-")
assert.NilError(t, err, "couldn't get temp directory for testing")
_, loopDataDevice := createLoopbackDevice(t, tempDir)
@@ -112,7 +111,7 @@ func TestPoolDevice(t *testing.T) {
err = mount.WithTempMount(ctx, getMounts(thinDevice1), func(thin1MountPath string) error {
// Write v1 test file on 'thin-1' device
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
err := ioutil.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700)
err := os.WriteFile(thin1TestFilePath, []byte("test file (v1)"), 0700)
assert.NilError(t, err, "failed to write test file v1 on '%s' volume", thinDevice1)
return nil
@@ -126,7 +125,7 @@ func TestPoolDevice(t *testing.T) {
// Update TEST file on 'thin-1' to v2
err = mount.WithTempMount(ctx, getMounts(thinDevice1), func(thin1MountPath string) error {
thin1TestFilePath := filepath.Join(thin1MountPath, "TEST")
err = ioutil.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700)
err = os.WriteFile(thin1TestFilePath, []byte("test file (v2)"), 0700)
assert.NilError(t, err, "failed to write test file v2 on 'thin-1' volume after taking snapshot")
return nil
@@ -137,7 +136,7 @@ func TestPoolDevice(t *testing.T) {
// Mount 'snap-1' and make sure TEST file is v1
err = mount.WithTempMount(ctx, getMounts(snapDevice1), func(snap1MountPath string) error {
// Read test file from snapshot device and make sure it's v1
fileData, err := ioutil.ReadFile(filepath.Join(snap1MountPath, "TEST"))
fileData, err := os.ReadFile(filepath.Join(snap1MountPath, "TEST"))
assert.NilError(t, err, "couldn't read test file from '%s' device", snapDevice1)
assert.Equal(t, "test file (v1)", string(fileData), "test file content is invalid on snapshot")
@@ -293,7 +292,7 @@ func getMounts(thinDeviceName string) []mount.Mount {
}
func createLoopbackDevice(t *testing.T, dir string) (string, string) {
file, err := ioutil.TempFile(dir, testsPrefix)
file, err := os.CreateTemp(dir, testsPrefix)
assert.NilError(t, err)
size, err := units.RAMInBytes("128Mb")

View File

@@ -23,7 +23,6 @@ import (
"context"
_ "crypto/sha256"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
@@ -87,7 +86,7 @@ func TestSnapshotterSuite(t *testing.T) {
ctx = namespaces.WithNamespace(ctx, "testsuite")
t.Run("DevMapperUsage", func(t *testing.T) {
tempDir, err := ioutil.TempDir("", "snapshot-suite-usage")
tempDir, err := os.MkdirTemp("", "snapshot-suite-usage")
assert.NilError(t, err)
defer os.RemoveAll(tempDir)

View File

@@ -18,7 +18,6 @@ package native
import (
"context"
"io/ioutil"
"os"
"path/filepath"
@@ -234,7 +233,7 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
)
if kind == snapshots.KindActive || parent == "" {
td, err = ioutil.TempDir(filepath.Join(o.root, "snapshots"), "new-")
td, err = os.MkdirTemp(filepath.Join(o.root, "snapshots"), "new-")
if err != nil {
return nil, errors.Wrap(err, "failed to create temp dir")
}

View File

@@ -22,7 +22,6 @@ package overlay
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@@ -478,7 +477,7 @@ func (o *snapshotter) createSnapshot(ctx context.Context, kind snapshots.Kind, k
}
func (o *snapshotter) prepareDirectory(ctx context.Context, snapshotDir string, kind snapshots.Kind) (string, error) {
td, err := ioutil.TempDir(snapshotDir, "new-")
td, err := os.MkdirTemp(snapshotDir, "new-")
if err != nil {
return "", errors.Wrap(err, "failed to create temp dir")
}

View File

@@ -22,7 +22,6 @@ package overlay
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
@@ -80,7 +79,7 @@ func TestOverlay(t *testing.T) {
func testOverlayMounts(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
ctx := context.TODO()
root, err := ioutil.TempDir("", "overlay")
root, err := os.MkdirTemp("", "overlay")
if err != nil {
t.Fatal(err)
}
@@ -114,7 +113,7 @@ func testOverlayMounts(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
func testOverlayCommit(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
ctx := context.TODO()
root, err := ioutil.TempDir("", "overlay")
root, err := os.MkdirTemp("", "overlay")
if err != nil {
t.Fatal(err)
}
@@ -129,7 +128,7 @@ func testOverlayCommit(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
t.Fatal(err)
}
m := mounts[0]
if err := ioutil.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
if err := os.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
t.Fatal(err)
}
if err := o.Commit(ctx, "base", key); err != nil {
@@ -139,7 +138,7 @@ func testOverlayCommit(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
func testOverlayOverlayMount(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
ctx := context.TODO()
root, err := ioutil.TempDir("", "overlay")
root, err := os.MkdirTemp("", "overlay")
if err != nil {
t.Fatal(err)
}
@@ -233,7 +232,7 @@ func getParents(ctx context.Context, sn snapshots.Snapshotter, root, key string)
func testOverlayOverlayRead(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
testutil.RequiresRoot(t)
ctx := context.TODO()
root, err := ioutil.TempDir("", "overlay")
root, err := os.MkdirTemp("", "overlay")
if err != nil {
t.Fatal(err)
}
@@ -248,7 +247,7 @@ func testOverlayOverlayRead(t *testing.T, newSnapshotter testsuite.SnapshotterFu
t.Fatal(err)
}
m := mounts[0]
if err := ioutil.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
if err := os.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
t.Fatal(err)
}
if err := o.Commit(ctx, "base", key); err != nil {
@@ -265,7 +264,7 @@ func testOverlayOverlayRead(t *testing.T, newSnapshotter testsuite.SnapshotterFu
t.Fatal(err)
}
defer syscall.Unmount(dest, 0)
data, err := ioutil.ReadFile(filepath.Join(dest, "foo"))
data, err := os.ReadFile(filepath.Join(dest, "foo"))
if err != nil {
t.Fatal(err)
}
@@ -276,7 +275,7 @@ func testOverlayOverlayRead(t *testing.T, newSnapshotter testsuite.SnapshotterFu
func testOverlayView(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
ctx := context.TODO()
root, err := ioutil.TempDir("", "overlay")
root, err := os.MkdirTemp("", "overlay")
if err != nil {
t.Fatal(err)
}
@@ -291,7 +290,7 @@ func testOverlayView(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
t.Fatal(err)
}
m := mounts[0]
if err := ioutil.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
if err := os.WriteFile(filepath.Join(m.Source, "foo"), []byte("hi"), 0660); err != nil {
t.Fatal(err)
}
if err := o.Commit(ctx, "base", key); err != nil {
@@ -303,7 +302,7 @@ func testOverlayView(t *testing.T, newSnapshotter testsuite.SnapshotterFunc) {
if err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(getParents(ctx, o, root, "/tmp/top")[0], "foo"), []byte("hi, again"), 0660); err != nil {
if err := os.WriteFile(filepath.Join(getParents(ctx, o, root, "/tmp/top")[0], "foo"), []byte("hi, again"), 0660); err != nil {
t.Fatal(err)
}
if err := o.Commit(ctx, "top", key); err != nil {

View File

@@ -21,7 +21,6 @@ package overlayutils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -40,7 +39,7 @@ import (
//
// Ported from moby overlay2.
func SupportsMultipleLowerDir(d string) error {
td, err := ioutil.TempDir(d, "multiple-lowerdir-check")
td, err := os.MkdirTemp(d, "multiple-lowerdir-check")
if err != nil {
return err
}
@@ -135,7 +134,7 @@ func NeedsUserXAttr(d string) (bool, error) {
}
}()
td, err := ioutil.TempDir(tdRoot, "")
td, err := os.MkdirTemp(tdRoot, "")
if err != nil {
return false, err
}

View File

@@ -20,7 +20,6 @@
package overlayutils
import (
"io/ioutil"
"os"
"testing"
@@ -31,7 +30,7 @@ import (
func testOverlaySupported(t testing.TB, expected bool, mkfs ...string) {
testutil.RequiresRoot(t)
mnt, err := ioutil.TempDir("", "containerd-fs-test-supports-overlay")
mnt, err := os.MkdirTemp("", "containerd-fs-test-supports-overlay")
if err != nil {
t.Fatal(err)
}

View File

@@ -19,7 +19,6 @@ package storage
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
@@ -43,7 +42,7 @@ func Benchmarks(b *testing.B, name string, metaFn metaFactory) {
func makeBench(b *testing.B, name string, metaFn metaFactory, fn func(context.Context, *testing.B, *MetaStore)) func(b *testing.B) {
return func(b *testing.B) {
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "metastore-bench-"+name+"-")
tmpDir, err := os.MkdirTemp("", "metastore-bench-"+name+"-")
if err != nil {
b.Fatal(err)
}
@@ -68,7 +67,7 @@ func makeBench(b *testing.B, name string, metaFn metaFactory, fn func(context.Co
func openCloseWritable(b *testing.B, name string, metaFn metaFactory) func(b *testing.B) {
return func(b *testing.B) {
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "metastore-bench-"+name+"-")
tmpDir, err := os.MkdirTemp("", "metastore-bench-"+name+"-")
if err != nil {
b.Fatal(err)
}
@@ -96,7 +95,7 @@ func openCloseWritable(b *testing.B, name string, metaFn metaFactory) func(b *te
func openCloseReadonly(b *testing.B, name string, metaFn metaFactory) func(b *testing.B) {
return func(b *testing.B) {
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "metastore-bench-"+name+"-")
tmpDir, err := os.MkdirTemp("", "metastore-bench-"+name+"-")
if err != nil {
b.Fatal(err)
}

View File

@@ -19,7 +19,6 @@ package storage
import (
"context"
"fmt"
"io/ioutil"
"os"
"testing"
"time"
@@ -67,7 +66,7 @@ func MetaStoreSuite(t *testing.T, name string, meta func(root string) (*MetaStor
func makeTest(t *testing.T, name string, metaFn metaFactory, fn testFunc) func(t *testing.T) {
return func(t *testing.T) {
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "metastore-test-"+name+"-")
tmpDir, err := os.MkdirTemp("", "metastore-test-"+name+"-")
if err != nil {
t.Fatal(err)
}

View File

@@ -19,7 +19,6 @@ package testsuite
import (
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
@@ -30,7 +29,7 @@ import (
)
func applyToMounts(m []mount.Mount, work string, a fstest.Applier) (err error) {
td, err := ioutil.TempDir(work, "prepare")
td, err := os.MkdirTemp(work, "prepare")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
@@ -71,7 +70,7 @@ func createSnapshot(ctx context.Context, sn snapshots.Snapshotter, parent, work
}
func checkSnapshot(ctx context.Context, sn snapshots.Snapshotter, work, name, check string) (err error) {
td, err := ioutil.TempDir(work, "check")
td, err := os.MkdirTemp(work, "check")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}
@@ -112,7 +111,7 @@ func checkSnapshot(ctx context.Context, sn snapshots.Snapshotter, work, name, ch
// using the provided appliers, checking each snapshot created in a view
// against the changes applied to a single directory.
func checkSnapshots(ctx context.Context, sn snapshots.Snapshotter, work string, as ...fstest.Applier) error {
td, err := ioutil.TempDir(work, "flat")
td, err := os.MkdirTemp(work, "flat")
if err != nil {
return errors.Wrap(err, "failed to create temp dir")
}

View File

@@ -21,7 +21,6 @@ import (
//nolint:revive // go-digest needs the blank import. See https://github.com/opencontainers/go-digest#usage.
_ "crypto/sha256"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path/filepath"
@@ -87,7 +86,7 @@ func makeTest(name string, snapshotterFn func(ctx context.Context, root string)
// work/ -> passed to test functions
// root/ -> passed to snapshotter
//
tmpDir, err := ioutil.TempDir("", "snapshot-suite-"+name+"-")
tmpDir, err := os.MkdirTemp("", "snapshot-suite-"+name+"-")
if err != nil {
t.Fatal(err)
}
@@ -297,7 +296,7 @@ func checkSnapshotterStatActive(ctx context.Context, t *testing.T, snapshotter s
}
defer testutil.Unmount(t, preparing)
if err = ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
if err = os.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
t.Fatal(err)
}
@@ -331,7 +330,7 @@ func checkSnapshotterStatCommitted(ctx context.Context, t *testing.T, snapshotte
}
defer testutil.Unmount(t, preparing)
if err = ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
if err = os.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
t.Fatal(err)
}
@@ -379,7 +378,7 @@ func checkSnapshotterTransitivity(ctx context.Context, t *testing.T, snapshotter
}
defer testutil.Unmount(t, preparing)
if err = ioutil.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
if err = os.WriteFile(filepath.Join(preparing, "foo"), []byte("foo\n"), 0777); err != nil {
t.Fatal(err)
}
@@ -394,7 +393,7 @@ func checkSnapshotterTransitivity(ctx context.Context, t *testing.T, snapshotter
}
defer testutil.Unmount(t, next)
if err = ioutil.WriteFile(filepath.Join(next, "foo"), []byte("foo bar\n"), 0777); err != nil {
if err = os.WriteFile(filepath.Join(next, "foo"), []byte("foo bar\n"), 0777); err != nil {
t.Fatal(err)
}
@@ -804,7 +803,7 @@ func checkSnapshotterViewReadonly(ctx context.Context, t *testing.T, snapshotter
}
testfile := filepath.Join(viewMountPoint, "testfile")
if err := ioutil.WriteFile(testfile, []byte("testcontent"), 0777); err != nil {
if err := os.WriteFile(testfile, []byte("testcontent"), 0777); err != nil {
t.Logf("write to %q failed with %v (EROFS is expected but can be other error code)", testfile, err)
} else {
t.Fatalf("write to %q should fail (EROFS) but did not fail", testfile)

View File

@@ -23,7 +23,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
@@ -491,7 +490,7 @@ func (s *snapshotter) convertScratchToReadOnlyLayer(ctx context.Context, snapsho
return errors.Wrap(err, "failed to reimport snapshot")
}
if _, err := io.Copy(ioutil.Discard, reader); err != nil {
if _, err := io.Copy(io.Discard, reader); err != nil {
return errors.Wrap(err, "failed discarding extra data in import stream")
}