mirror of
https://github.com/moby/moby.git
synced 2025-08-01 05:47:11 +03:00
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:
@ -7,7 +7,6 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
@ -384,7 +383,7 @@ func (s *DockerSuite) TestRunCreateVolumesInSymlinkDir(c *testing.T) {
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
|
||||
name := "test-volume-symlink"
|
||||
|
||||
dir, err := ioutil.TempDir("", name)
|
||||
dir, err := os.MkdirTemp("", name)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -620,7 +619,7 @@ func (s *DockerSuite) TestRunCreateVolume(c *testing.T) {
|
||||
func (s *DockerSuite) TestRunCreateVolumeWithSymlink(c *testing.T) {
|
||||
// Cannot run on Windows as relies on Linux-specific functionality (sh -c mount...)
|
||||
testRequires(c, DaemonIsLinux)
|
||||
workingDirectory, err := ioutil.TempDir("", "TestRunCreateVolumeWithSymlink")
|
||||
workingDirectory, err := os.MkdirTemp("", "TestRunCreateVolumeWithSymlink")
|
||||
assert.NilError(c, err)
|
||||
image := "docker-test-createvolumewithsymlink"
|
||||
|
||||
@ -658,7 +657,7 @@ func (s *DockerSuite) TestRunVolumesFromSymlinkPath(c *testing.T) {
|
||||
// Windows does not support symlinks inside a volume path
|
||||
testRequires(c, DaemonIsLinux)
|
||||
|
||||
workingDirectory, err := ioutil.TempDir("", "TestRunVolumesFromSymlinkPath")
|
||||
workingDirectory, err := os.MkdirTemp("", "TestRunVolumesFromSymlinkPath")
|
||||
assert.NilError(c, err)
|
||||
name := "docker-test-volumesfromsymlinkpath"
|
||||
prefix := ""
|
||||
@ -1254,13 +1253,13 @@ func (s *DockerSuite) TestRunDNSDefaultOptions(c *testing.T) {
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
|
||||
|
||||
// preserve original resolv.conf for restoring after test
|
||||
origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
origResolvConf, err := os.ReadFile("/etc/resolv.conf")
|
||||
if os.IsNotExist(err) {
|
||||
c.Fatalf("/etc/resolv.conf does not exist")
|
||||
}
|
||||
// defer restored original conf
|
||||
defer func() {
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@ -1269,7 +1268,7 @@ func (s *DockerSuite) TestRunDNSDefaultOptions(c *testing.T) {
|
||||
// 2 are removed from the file at container start, and the 3rd (commented out) one is ignored by
|
||||
// GetNameservers(), leading to a replacement of nameservers with the default set
|
||||
tmpResolvConf := []byte("nameserver 127.0.0.1\n#nameserver 127.0.2.1\nnameserver ::1")
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1321,7 +1320,7 @@ func (s *DockerSuite) TestRunDNSOptionsBasedOnHostResolvConf(c *testing.T) {
|
||||
// Not applicable on Windows as testing Unix specific functionality
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux)
|
||||
|
||||
origResolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
origResolvConf, err := os.ReadFile("/etc/resolv.conf")
|
||||
if os.IsNotExist(err) {
|
||||
c.Fatalf("/etc/resolv.conf does not exist")
|
||||
}
|
||||
@ -1364,17 +1363,17 @@ func (s *DockerSuite) TestRunDNSOptionsBasedOnHostResolvConf(c *testing.T) {
|
||||
|
||||
// test with file
|
||||
tmpResolvConf := []byte("search example.com\nnameserver 12.34.56.78\nnameserver 127.0.0.1")
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
// put the old resolvconf back
|
||||
defer func() {
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", origResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
resolvConf, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
resolvConf, err := os.ReadFile("/etc/resolv.conf")
|
||||
if os.IsNotExist(err) {
|
||||
c.Fatalf("/etc/resolv.conf does not exist")
|
||||
}
|
||||
@ -1430,7 +1429,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
tmpLocalhostResolvConf := []byte("nameserver 127.0.0.1")
|
||||
|
||||
// take a copy of resolv.conf for restoring after test completes
|
||||
resolvConfSystem, err := ioutil.ReadFile("/etc/resolv.conf")
|
||||
resolvConfSystem, err := os.ReadFile("/etc/resolv.conf")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -1448,7 +1447,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
|
||||
// cleanup
|
||||
defer func() {
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
}()
|
||||
@ -1458,7 +1457,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
containerID1 := getIDByName(c, "first")
|
||||
|
||||
// replace resolv.conf with our temporary copy
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1472,7 +1471,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
}
|
||||
|
||||
/* // make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
} */
|
||||
// 2. test that a restarting container does not receive resolv.conf updates
|
||||
@ -1481,7 +1480,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
containerID2 := getIDByName(c, "second")
|
||||
|
||||
// make a change to resolv.conf (in this case replacing our tmp copy with orig copy)
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1499,7 +1498,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
runningContainerID := strings.TrimSpace(out)
|
||||
|
||||
// replace resolv.conf
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", tmpResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1523,7 +1522,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
// host resolv.conf before updating container's resolv.conf copies
|
||||
|
||||
// replace resolv.conf with a localhost-only nameserver copy
|
||||
if err = ioutil.WriteFile("/etc/resolv.conf", tmpLocalhostResolvConf, 0644); err != nil {
|
||||
if err = os.WriteFile("/etc/resolv.conf", tmpLocalhostResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1542,7 +1541,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
// of containers' resolv.conf.
|
||||
|
||||
// Restore the original resolv.conf
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf", resolvConfSystem, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1551,7 +1550,7 @@ func (s *DockerSuite) TestRunResolvconfUpdate(c *testing.T) {
|
||||
containerID3 := getIDByName(c, "third")
|
||||
|
||||
// Create a modified resolv.conf.aside and override resolv.conf with it
|
||||
if err := ioutil.WriteFile("/etc/resolv.conf.aside", tmpResolvConf, 0644); err != nil {
|
||||
if err := os.WriteFile("/etc/resolv.conf.aside", tmpResolvConf, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -1885,7 +1884,7 @@ func (s *DockerSuite) TestRunBindMounts(c *testing.T) {
|
||||
|
||||
prefix, _ := getPrefixAndSlashFromDaemonPlatform()
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "docker-test-container")
|
||||
tmpDir, err := os.MkdirTemp("", "docker-test-container")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -1932,7 +1931,7 @@ func (s *DockerSuite) TestRunCidFileCleanupIfEmpty(c *testing.T) {
|
||||
// Skip on Windows. Base image on Windows has a CMD set in the image.
|
||||
testRequires(c, DaemonIsLinux)
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
|
||||
tmpDir, err := os.MkdirTemp("", "TestRunCidFile")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -1960,7 +1959,7 @@ func (s *DockerSuite) TestRunCidFileCleanupIfEmpty(c *testing.T) {
|
||||
// sudo docker run --cidfile /tmp/docker_tesc.cid ubuntu echo "test"
|
||||
// TestRunCidFile tests that run --cidfile returns the longid
|
||||
func (s *DockerSuite) TestRunCidFileCheckIDLength(c *testing.T) {
|
||||
tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
|
||||
tmpDir, err := os.MkdirTemp("", "TestRunCidFile")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -1970,7 +1969,7 @@ func (s *DockerSuite) TestRunCidFileCheckIDLength(c *testing.T) {
|
||||
out, _ := dockerCmd(c, "run", "-d", "--cidfile", tmpCidFile, "busybox", "true")
|
||||
|
||||
id := strings.TrimSpace(out)
|
||||
buffer, err := ioutil.ReadFile(tmpCidFile)
|
||||
buffer, err := os.ReadFile(tmpCidFile)
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -2082,13 +2081,13 @@ func (s *DockerSuite) TestRunMountOrdering(c *testing.T) {
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, NotUserNamespace)
|
||||
prefix, _ := getPrefixAndSlashFromDaemonPlatform()
|
||||
|
||||
tmpDir, err := ioutil.TempDir("", "docker_nested_mount_test")
|
||||
tmpDir, err := os.MkdirTemp("", "docker_nested_mount_test")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmpDir)
|
||||
|
||||
tmpDir2, err := ioutil.TempDir("", "docker_nested_mount_test2")
|
||||
tmpDir2, err := os.MkdirTemp("", "docker_nested_mount_test2")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -2100,15 +2099,15 @@ func (s *DockerSuite) TestRunMountOrdering(c *testing.T) {
|
||||
c.Fatalf("failed to mkdir at %s - %s", fooDir, err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil {
|
||||
if err := os.WriteFile(fmt.Sprintf("%s/touch-me", fooDir), []byte{}, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil {
|
||||
if err := os.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir), []byte{}, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
if err := ioutil.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil {
|
||||
if err := os.WriteFile(fmt.Sprintf("%s/touch-me", tmpDir2), []byte{}, 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
|
||||
@ -2127,7 +2126,7 @@ func (s *DockerSuite) TestRunReuseBindVolumeThatIsSymlink(c *testing.T) {
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, NotUserNamespace)
|
||||
prefix, _ := getPrefixAndSlashFromDaemonPlatform()
|
||||
|
||||
tmpDir, err := ioutil.TempDir(os.TempDir(), "testlink")
|
||||
tmpDir, err := os.MkdirTemp(os.TempDir(), "testlink")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -4058,7 +4057,7 @@ func (s *DockerSuite) TestRunCredentialSpecWellFormed(c *testing.T) {
|
||||
func (s *DockerSuite) TestRunDuplicateMount(c *testing.T) {
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, NotUserNamespace)
|
||||
|
||||
tmpFile, err := ioutil.TempFile("", "touch-me")
|
||||
tmpFile, err := os.CreateTemp("", "touch-me")
|
||||
assert.NilError(c, err)
|
||||
defer tmpFile.Close()
|
||||
|
||||
@ -4190,7 +4189,7 @@ func (s *delayedReader) Read([]byte) (int, error) {
|
||||
// #28823 (originally #28639)
|
||||
func (s *DockerSuite) TestRunMountReadOnlyDevShm(c *testing.T) {
|
||||
testRequires(c, testEnv.IsLocalDaemon, DaemonIsLinux, NotUserNamespace)
|
||||
emptyDir, err := ioutil.TempDir("", "test-read-only-dev-shm")
|
||||
emptyDir, err := os.MkdirTemp("", "test-read-only-dev-shm")
|
||||
assert.NilError(c, err)
|
||||
defer os.RemoveAll(emptyDir)
|
||||
out, _, err := dockerCmdWithError("run", "--rm", "--read-only",
|
||||
@ -4204,7 +4203,7 @@ func (s *DockerSuite) TestRunMount(c *testing.T) {
|
||||
testRequires(c, DaemonIsLinux, testEnv.IsLocalDaemon, NotUserNamespace)
|
||||
|
||||
// mnt1, mnt2, and testCatFooBar are commonly used in multiple test cases
|
||||
tmpDir, err := ioutil.TempDir("", "mount")
|
||||
tmpDir, err := os.MkdirTemp("", "mount")
|
||||
if err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
@ -4216,10 +4215,10 @@ func (s *DockerSuite) TestRunMount(c *testing.T) {
|
||||
if err := os.Mkdir(mnt2, 0755); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(mnt1, "test1"), []byte("test1"), 0644); err != nil {
|
||||
if err := os.WriteFile(path.Join(mnt1, "test1"), []byte("test1"), 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(mnt2, "test2"), []byte("test2"), 0644); err != nil {
|
||||
if err := os.WriteFile(path.Join(mnt2, "test2"), []byte("test2"), 0644); err != nil {
|
||||
c.Fatal(err)
|
||||
}
|
||||
testCatFooBar := func(cName string) error {
|
||||
|
Reference in New Issue
Block a user