Merge pull request #5973 from Juneezee/deprecate-ioutil
refactor: move from io/ioutil to io and os package
This commit is contained in:
@@ -20,7 +20,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -32,7 +31,7 @@ import (
|
||||
)
|
||||
|
||||
func TestAdditionalGids(t *testing.T) {
|
||||
testPodLogDir, err := ioutil.TempDir("/tmp", "additional-gids")
|
||||
testPodLogDir, err := os.MkdirTemp("/tmp", "additional-gids")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testPodLogDir)
|
||||
|
||||
@@ -74,7 +73,7 @@ func TestAdditionalGids(t *testing.T) {
|
||||
}, time.Second, 30*time.Second))
|
||||
|
||||
t.Log("Search additional groups in container log")
|
||||
content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
content, err := os.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, string(content), "groups=1(daemon),10(wheel),1234")
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
|
||||
"io/ioutil"
|
||||
"math/rand"
|
||||
"reflect"
|
||||
"runtime"
|
||||
@@ -298,7 +297,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)
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
cri "github.com/containerd/containerd/integration/cri-api/pkg/apis"
|
||||
@@ -56,7 +56,7 @@ func initImages(imageListFile string) {
|
||||
}
|
||||
|
||||
if imageListFile != "" {
|
||||
fileContent, err := ioutil.ReadFile(imageListFile)
|
||||
fileContent, err := os.ReadFile(imageListFile)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("error reading '%v' file contents: %v", imageList, err))
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ package integration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -31,7 +30,7 @@ import (
|
||||
)
|
||||
|
||||
func TestContainerLogWithoutTailingNewLine(t *testing.T) {
|
||||
testPodLogDir, err := ioutil.TempDir("/tmp", "container-log-without-tailing-newline")
|
||||
testPodLogDir, err := os.MkdirTemp("/tmp", "container-log-without-tailing-newline")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testPodLogDir)
|
||||
|
||||
@@ -73,7 +72,7 @@ func TestContainerLogWithoutTailingNewLine(t *testing.T) {
|
||||
}, time.Second, 30*time.Second))
|
||||
|
||||
t.Log("Check container log")
|
||||
content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
content, err := os.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
assert.NoError(t, err)
|
||||
checkContainerLog(t, string(content), []string{
|
||||
fmt.Sprintf("%s %s %s", runtime.Stdout, runtime.LogTagPartial, "abcd"),
|
||||
@@ -81,7 +80,7 @@ func TestContainerLogWithoutTailingNewLine(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestLongContainerLog(t *testing.T) {
|
||||
testPodLogDir, err := ioutil.TempDir("/tmp", "long-container-log")
|
||||
testPodLogDir, err := os.MkdirTemp("/tmp", "long-container-log")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testPodLogDir)
|
||||
|
||||
@@ -130,7 +129,7 @@ func TestLongContainerLog(t *testing.T) {
|
||||
}, time.Second, 30*time.Second))
|
||||
|
||||
t.Log("Check container log")
|
||||
content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
content, err := os.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
assert.NoError(t, err)
|
||||
checkContainerLog(t, string(content), []string{
|
||||
fmt.Sprintf("%s %s %s", runtime.Stdout, runtime.LogTagFull, strings.Repeat("a", maxSize-1)),
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -36,7 +35,7 @@ func createRegularFile(basePath, content string) (string, error) {
|
||||
}
|
||||
|
||||
newFile := filepath.Join(newFolder, "foo.txt")
|
||||
err = ioutil.WriteFile(newFile, []byte(content), 0644)
|
||||
err = os.WriteFile(newFile, []byte(content), 0644)
|
||||
return filepath.Join("regular", "foo.txt"), err
|
||||
}
|
||||
|
||||
@@ -83,11 +82,11 @@ func TestContainerSymlinkVolumes(t *testing.T) {
|
||||
} {
|
||||
testCase := testCase // capture range variable
|
||||
t.Run(name, func(t *testing.T) {
|
||||
testPodLogDir, err := ioutil.TempDir("", "symlink-test")
|
||||
testPodLogDir, err := os.MkdirTemp("", "symlink-test")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testPodLogDir)
|
||||
|
||||
testVolDir, err := ioutil.TempDir("", "symlink-test-vol")
|
||||
testVolDir, err := os.MkdirTemp("", "symlink-test-vol")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testVolDir)
|
||||
|
||||
@@ -137,7 +136,7 @@ func TestContainerSymlinkVolumes(t *testing.T) {
|
||||
return false, nil
|
||||
}, time.Second, 30*time.Second))
|
||||
|
||||
output, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
output, err := os.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Contains(t, string(output), content)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
@@ -40,8 +39,8 @@ func TestImageLoad(t *testing.T) {
|
||||
t.Logf("docker save image into tarball")
|
||||
output, err := exec.Command("docker", "pull", testImage).CombinedOutput()
|
||||
require.NoError(t, err, "output: %q", output)
|
||||
// ioutil.TempFile also opens a file, which might prevent us from overwriting that file with docker save.
|
||||
tarDir, err := ioutil.TempDir("", "image-load")
|
||||
// os.CreateTemp also opens a file, which might prevent us from overwriting that file with docker save.
|
||||
tarDir, err := os.MkdirTemp("", "image-load")
|
||||
tar := filepath.Join(tarDir, "image.tar")
|
||||
require.NoError(t, err)
|
||||
defer func() {
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -32,7 +31,7 @@ import (
|
||||
)
|
||||
|
||||
func TestPodDualStack(t *testing.T) {
|
||||
testPodLogDir, err := ioutil.TempDir("/tmp", "dualstack")
|
||||
testPodLogDir, err := os.MkdirTemp("/tmp", "dualstack")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testPodLogDir)
|
||||
|
||||
@@ -77,7 +76,7 @@ func TestPodDualStack(t *testing.T) {
|
||||
return false, nil
|
||||
}, time.Second, 30*time.Second))
|
||||
|
||||
content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
content, err := os.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
assert.NoError(t, err)
|
||||
status, err := runtimeService.PodSandboxStatus(sb)
|
||||
require.NoError(t, err)
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
goruntime "runtime"
|
||||
@@ -65,7 +64,7 @@ func TestPodHostname(t *testing.T) {
|
||||
if test.needsHostNetwork && goruntime.GOOS == "windows" {
|
||||
t.Skip("Skipped on Windows.")
|
||||
}
|
||||
testPodLogDir, err := ioutil.TempDir("/tmp", "hostname")
|
||||
testPodLogDir, err := os.MkdirTemp("/tmp", "hostname")
|
||||
require.NoError(t, err)
|
||||
defer os.RemoveAll(testPodLogDir)
|
||||
|
||||
@@ -121,7 +120,7 @@ func TestPodHostname(t *testing.T) {
|
||||
return false, nil
|
||||
}, time.Second, 30*time.Second))
|
||||
|
||||
content, err := ioutil.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
content, err := os.ReadFile(filepath.Join(testPodLogDir, containerName))
|
||||
assert.NoError(t, err)
|
||||
|
||||
t.Log("Search hostname env in container log")
|
||||
|
||||
@@ -38,7 +38,6 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -73,7 +72,7 @@ func CreateListener(endpoint string) (net.Listener, error) {
|
||||
}
|
||||
|
||||
// Create the socket on a tempfile and move it to the destination socket to handle improprer cleanup
|
||||
file, err := ioutil.TempFile(filepath.Dir(addr), "")
|
||||
file, err := os.CreateTemp(filepath.Dir(addr), "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temporary file: %v", err)
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@
|
||||
package integration
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -40,11 +39,11 @@ func TestSandboxRemoveWithoutIPLeakage(t *testing.T) {
|
||||
t.Logf("Make sure host-local ipam is in use")
|
||||
config, err := CRIConfig()
|
||||
require.NoError(t, err)
|
||||
fs, err := ioutil.ReadDir(config.NetworkPluginConfDir)
|
||||
fs, err := os.ReadDir(config.NetworkPluginConfDir)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, fs)
|
||||
f := filepath.Join(config.NetworkPluginConfDir, fs[0].Name())
|
||||
cniConfig, err := ioutil.ReadFile(f)
|
||||
cniConfig, err := os.ReadFile(f)
|
||||
require.NoError(t, err)
|
||||
if !strings.Contains(string(cniConfig), "host-local") {
|
||||
t.Skip("host-local ipam is not in use")
|
||||
|
||||
@@ -38,7 +38,6 @@ package util
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -73,7 +72,7 @@ func CreateListener(endpoint string) (net.Listener, error) {
|
||||
}
|
||||
|
||||
// Create the socket on a tempfile and move it to the destination socket to handle improprer cleanup
|
||||
file, err := ioutil.TempFile(filepath.Dir(addr), "")
|
||||
file, err := os.CreateTemp(filepath.Dir(addr), "")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create temporary file: %v", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user