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

The io/ioutil package has been deprecated in Go 1.16. 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-08-24 18:10:50 +08:00
parent 2b70006e3b
commit c55a4ac779
397 changed files with 1371 additions and 1606 deletions

View File

@@ -3,7 +3,7 @@ package main
import (
"bytes"
"fmt"
"io/ioutil"
"io"
"os"
"os/exec"
"path"
@@ -50,7 +50,7 @@ func (s *DockerSuite) TestCpGarbagePath(c *testing.T) {
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
tmpname := filepath.Join(tmpdir, cpTestName)
@@ -63,7 +63,7 @@ func (s *DockerSuite) TestCpGarbagePath(c *testing.T) {
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
test, err := io.ReadAll(file)
assert.NilError(c, err)
assert.Assert(c, string(test) != cpHostContents, "output matched host file -- garbage path can escape container rootfs")
assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for garbage path")
@@ -86,7 +86,7 @@ func (s *DockerSuite) TestCpRelativePath(c *testing.T) {
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
tmpname := filepath.Join(tmpdir, cpTestName)
@@ -105,7 +105,7 @@ func (s *DockerSuite) TestCpRelativePath(c *testing.T) {
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
test, err := io.ReadAll(file)
assert.NilError(c, err)
assert.Assert(c, string(test) != cpHostContents, "output matched host file -- relative path can escape container rootfs")
assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for relative path")
@@ -128,7 +128,7 @@ func (s *DockerSuite) TestCpAbsolutePath(c *testing.T) {
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
tmpname := filepath.Join(tmpdir, cpTestName)
@@ -141,7 +141,7 @@ func (s *DockerSuite) TestCpAbsolutePath(c *testing.T) {
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
test, err := io.ReadAll(file)
assert.NilError(c, err)
assert.Assert(c, string(test) != cpHostContents, "output matched host file -- absolute path can escape container rootfs")
assert.Assert(c, string(test) == cpContainerContents, "output doesn't match the input for absolute path")
@@ -167,7 +167,7 @@ func (s *DockerSuite) TestCpAbsoluteSymlink(c *testing.T) {
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
tmpname := filepath.Join(tmpdir, "container_path")
@@ -194,7 +194,7 @@ func (s *DockerSuite) TestCpFromSymlinkToDirectory(c *testing.T) {
out, _ = dockerCmd(c, "wait", containerID)
assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
testDir, err := ioutil.TempDir("", "test-cp-from-symlink-to-dir-")
testDir, err := os.MkdirTemp("", "test-cp-from-symlink-to-dir-")
assert.NilError(c, err)
defer os.RemoveAll(testDir)
@@ -235,7 +235,7 @@ func (s *DockerSuite) TestCpToSymlinkToDirectory(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, testEnv.IsLocalDaemon) // Requires local volume mount bind.
testVol, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
testVol, err := os.MkdirTemp("", "test-cp-to-symlink-to-dir-")
assert.NilError(c, err)
defer os.RemoveAll(testVol)
@@ -246,7 +246,7 @@ func (s *DockerSuite) TestCpToSymlinkToDirectory(c *testing.T) {
containerID := strings.TrimSpace(out)
// Create a temp directory to hold a test file nested in a directory.
testDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
testDir, err := os.MkdirTemp("", "test-cp-to-symlink-to-dir-")
assert.NilError(c, err)
defer os.RemoveAll(testDir)
@@ -254,11 +254,11 @@ func (s *DockerSuite) TestCpToSymlinkToDirectory(c *testing.T) {
// the test volume later.
hostTestFilename := filepath.Join(testDir, cpFullPath)
assert.NilError(c, os.MkdirAll(filepath.Dir(hostTestFilename), os.FileMode(0700)))
assert.NilError(c, ioutil.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)))
assert.NilError(c, os.WriteFile(hostTestFilename, []byte(cpHostContents), os.FileMode(0600)))
// Now create another temp directory to hold a symlink to the
// "/testDir/some" directory.
linkDir, err := ioutil.TempDir("", "test-cp-to-symlink-to-dir-")
linkDir, err := os.MkdirTemp("", "test-cp-to-symlink-to-dir-")
assert.NilError(c, err)
defer os.RemoveAll(linkDir)
@@ -301,7 +301,7 @@ func (s *DockerSuite) TestCpToSymlinkToDirectory(c *testing.T) {
// And this directory should contain the file copied from the host at the
// expected location: "/testVol/dir_link/path/test"
expectedFilepath := filepath.Join(testVol, "dir_link/path/test")
fileContents, err := ioutil.ReadFile(expectedFilepath)
fileContents, err := os.ReadFile(expectedFilepath)
assert.NilError(c, err)
assert.Equal(c, string(fileContents), cpHostContents)
}
@@ -326,7 +326,7 @@ func (s *DockerSuite) TestCpSymlinkComponent(c *testing.T) {
fmt.Fprintf(hostFile, "%s", cpHostContents)
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
@@ -340,7 +340,7 @@ func (s *DockerSuite) TestCpSymlinkComponent(c *testing.T) {
file, _ := os.Open(tmpname)
defer file.Close()
test, err := ioutil.ReadAll(file)
test, err := io.ReadAll(file)
assert.NilError(c, err)
assert.Assert(c, string(test) != cpHostContents, "output matched host file -- symlink path component can escape container rootfs")
assert.Equal(c, string(test), cpContainerContents, "output doesn't match the input for symlink path component")
@@ -358,7 +358,7 @@ func (s *DockerSuite) TestCpUnprivilegedUser(c *testing.T) {
out, _ = dockerCmd(c, "wait", containerID)
assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
defer os.RemoveAll(tmpdir)
@@ -375,7 +375,7 @@ func (s *DockerSuite) TestCpSpecialFiles(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, testEnv.IsLocalDaemon)
outDir, err := ioutil.TempDir("", "cp-test-special-files")
outDir, err := os.MkdirTemp("", "cp-test-special-files")
assert.NilError(c, err)
defer os.RemoveAll(outDir)
@@ -390,7 +390,7 @@ func (s *DockerSuite) TestCpSpecialFiles(c *testing.T) {
dockerCmd(c, "cp", containerID+":/etc/resolv.conf", outDir)
expected := readContainerFile(c, containerID, "resolv.conf")
actual, err := ioutil.ReadFile(outDir + "/resolv.conf")
actual, err := os.ReadFile(outDir + "/resolv.conf")
assert.NilError(c, err)
assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container resolvconf")
@@ -398,7 +398,7 @@ func (s *DockerSuite) TestCpSpecialFiles(c *testing.T) {
dockerCmd(c, "cp", containerID+":/etc/hosts", outDir)
expected = readContainerFile(c, containerID, "hosts")
actual, err = ioutil.ReadFile(outDir + "/hosts")
actual, err = os.ReadFile(outDir + "/hosts")
assert.NilError(c, err)
assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container hosts")
@@ -406,7 +406,7 @@ func (s *DockerSuite) TestCpSpecialFiles(c *testing.T) {
dockerCmd(c, "cp", containerID+":/etc/hostname", outDir)
expected = readContainerFile(c, containerID, "hostname")
actual, err = ioutil.ReadFile(outDir + "/hostname")
actual, err = os.ReadFile(outDir + "/hostname")
assert.NilError(c, err)
assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container hostname")
}
@@ -417,10 +417,10 @@ func (s *DockerSuite) TestCpVolumePath(c *testing.T) {
testRequires(c, DaemonIsLinux)
testRequires(c, testEnv.IsLocalDaemon)
tmpDir, err := ioutil.TempDir("", "cp-test-volumepath")
tmpDir, err := os.MkdirTemp("", "cp-test-volumepath")
assert.NilError(c, err)
defer os.RemoveAll(tmpDir)
outDir, err := ioutil.TempDir("", "cp-test-volumepath-out")
outDir, err := os.MkdirTemp("", "cp-test-volumepath-out")
assert.NilError(c, err)
defer os.RemoveAll(outDir)
_, err = os.Create(tmpDir + "/test")
@@ -459,17 +459,17 @@ func (s *DockerSuite) TestCpVolumePath(c *testing.T) {
// Copy file nested in bind-mounted dir
dockerCmd(c, "cp", containerID+":/baz/test", outDir)
fb, err := ioutil.ReadFile(outDir + "/baz/test")
fb, err := os.ReadFile(outDir + "/baz/test")
assert.NilError(c, err)
fb2, err := ioutil.ReadFile(tmpDir + "/test")
fb2, err := os.ReadFile(tmpDir + "/test")
assert.NilError(c, err)
assert.Assert(c, bytes.Equal(fb, fb2), "Expected copied file to be duplicate of bind-mounted file")
// Copy bind-mounted file
dockerCmd(c, "cp", containerID+":/test", outDir)
fb, err = ioutil.ReadFile(outDir + "/test")
fb, err = os.ReadFile(outDir + "/test")
assert.NilError(c, err)
fb2, err = ioutil.ReadFile(tmpDir + "/test")
fb2, err = os.ReadFile(tmpDir + "/test")
assert.NilError(c, err)
assert.Assert(c, bytes.Equal(fb, fb2), "Expected copied file to be duplicate of bind-mounted file")
}
@@ -482,7 +482,7 @@ func (s *DockerSuite) TestCpToDot(c *testing.T) {
out, _ = dockerCmd(c, "wait", containerID)
assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
defer os.RemoveAll(tmpdir)
cwd, err := os.Getwd()
@@ -492,7 +492,7 @@ func (s *DockerSuite) TestCpToDot(c *testing.T) {
assert.NilError(c, err)
dockerCmd(c, "cp", containerID+":/test", ".")
content, err := ioutil.ReadFile("./test")
content, err := os.ReadFile("./test")
assert.NilError(c, err)
assert.Equal(c, string(content), "lololol\n")
}
@@ -524,11 +524,11 @@ func (s *DockerSuite) TestCpNameHasColon(c *testing.T) {
out, _ = dockerCmd(c, "wait", containerID)
assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
tmpdir, err := ioutil.TempDir("", "docker-integration")
tmpdir, err := os.MkdirTemp("", "docker-integration")
assert.NilError(c, err)
defer os.RemoveAll(tmpdir)
dockerCmd(c, "cp", containerID+":/te:s:t", tmpdir)
content, err := ioutil.ReadFile(tmpdir + "/te:s:t")
content, err := os.ReadFile(tmpdir + "/te:s:t")
assert.NilError(c, err)
assert.Equal(c, string(content), "lololol\n")
}
@@ -542,7 +542,7 @@ func (s *DockerSuite) TestCopyAndRestart(c *testing.T) {
out, _ = dockerCmd(c, "wait", containerID)
assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
tmpDir, err := ioutil.TempDir("", "test-docker-restart-after-copy-")
tmpDir, err := os.MkdirTemp("", "test-docker-restart-after-copy-")
assert.NilError(c, err)
defer os.RemoveAll(tmpDir)
@@ -556,7 +556,7 @@ func (s *DockerSuite) TestCopyCreatedContainer(c *testing.T) {
testRequires(c, DaemonIsLinux)
dockerCmd(c, "create", "--name", "test_cp", "-v", "/test", "busybox")
tmpDir, err := ioutil.TempDir("", "test")
tmpDir, err := os.MkdirTemp("", "test")
assert.NilError(c, err)
defer os.RemoveAll(tmpDir)
dockerCmd(c, "cp", "test_cp:/bin/sh", tmpDir)
@@ -575,7 +575,7 @@ func (s *DockerSuite) TestCpSymlinkFromConToHostFollowSymlink(c *testing.T) {
out, _ = dockerCmd(c, "wait", cleanedContainerID)
assert.Equal(c, strings.TrimSpace(out), "0", "failed to set up container")
testDir, err := ioutil.TempDir("", "test-cp-symlink-container-to-host-follow-symlink")
testDir, err := os.MkdirTemp("", "test-cp-symlink-container-to-host-follow-symlink")
assert.NilError(c, err)
defer os.RemoveAll(testDir)
@@ -586,7 +586,7 @@ func (s *DockerSuite) TestCpSymlinkFromConToHostFollowSymlink(c *testing.T) {
expectedPath := filepath.Join(testDir, "dir_link")
expected := []byte(cpContainerContents)
actual, err := ioutil.ReadFile(expectedPath)
actual, err := os.ReadFile(expectedPath)
assert.NilError(c, err)
os.Remove(expectedPath)
assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container symbol link target")
@@ -600,7 +600,7 @@ func (s *DockerSuite) TestCpSymlinkFromConToHostFollowSymlink(c *testing.T) {
dockerCmd(c, "cp", "-L", cleanedContainerID+":"+"/dir_link", expectedPath)
actual, err = ioutil.ReadFile(expectedPath)
actual, err = os.ReadFile(expectedPath)
assert.NilError(c, err)
defer os.Remove(expectedPath)
assert.Assert(c, bytes.Equal(actual, expected), "Expected copied file to be duplicate of the container symbol link target")