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

@@ -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)