Merge pull request #6681 from Juneezee/test/t.TempDir

This commit is contained in:
Fu Wei
2022-03-16 14:54:16 +08:00
committed by GitHub
40 changed files with 136 additions and 431 deletions

View File

@@ -31,9 +31,7 @@ import (
)
func TestAdditionalGids(t *testing.T) {
testPodLogDir, err := os.MkdirTemp("", "additional-gids")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testPodLogDir := t.TempDir()
t.Log("Create a sandbox with log directory")
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "additional-gids",

View File

@@ -455,11 +455,7 @@ func TestCheckpointRestoreWithImagePath(t *testing.T) {
}
// create image path store criu image files
crDir, err := os.MkdirTemp("", "test-cr")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(crDir)
crDir := t.TempDir()
imagePath := filepath.Join(crDir, "cr")
// checkpoint task
if _, err := task.Checkpoint(ctx, WithCheckpointImagePath(imagePath)); err != nil {

View File

@@ -35,15 +35,7 @@ import (
// TestDaemonRuntimeRoot ensures plugin.linux.runtime_root is not ignored
func TestDaemonRuntimeRoot(t *testing.T) {
runtimeRoot, err := os.MkdirTemp("", "containerd-test-runtime-root")
if err != nil {
t.Fatal(err)
}
defer func() {
if err != nil {
os.RemoveAll(runtimeRoot)
}
}()
runtimeRoot := t.TempDir()
configTOML := `
version = 2
[plugins]

View File

@@ -49,22 +49,14 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
buf = bytes.NewBuffer(nil)
)
tempDir, err := os.MkdirTemp("", "containerd-test-new-daemon-with-config")
if err != nil {
t.Fatal(err)
}
defer func() {
if err != nil {
os.RemoveAll(tempDir)
}
}()
tempDir := t.TempDir()
configTOMLFile := filepath.Join(tempDir, "config.toml")
if err = os.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil {
if err := os.WriteFile(configTOMLFile, []byte(configTOML), 0600); err != nil {
t.Fatal(err)
}
if err = srvconfig.LoadConfig(configTOMLFile, &configTOMLDecoded); err != nil {
if err := srvconfig.LoadConfig(configTOMLFile, &configTOMLDecoded); err != nil {
t.Fatal(err)
}
@@ -83,7 +75,7 @@ func newDaemonWithConfig(t *testing.T, configTOML string) (*Client, *daemon, fun
if configTOMLDecoded.State == "" {
args = append(args, "--state", filepath.Join(tempDir, "state"))
}
if err = ctrd.start("containerd", address, args, buf, buf); err != nil {
if err := ctrd.start("containerd", address, args, buf, buf); err != nil {
t.Fatalf("%v: %s", err, buf.String())
}

View File

@@ -30,9 +30,7 @@ import (
)
func TestContainerLogWithoutTailingNewLine(t *testing.T) {
testPodLogDir, err := os.MkdirTemp("", "container-log-without-tailing-newline")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testPodLogDir := t.TempDir()
t.Log("Create a sandbox with log directory")
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "container-log-without-tailing-newline",
@@ -80,9 +78,7 @@ func TestContainerLogWithoutTailingNewLine(t *testing.T) {
}
func TestLongContainerLog(t *testing.T) {
testPodLogDir, err := os.MkdirTemp("", "long-container-log")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testPodLogDir := t.TempDir()
t.Log("Create a sandbox with log directory")
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "long-container-log",

View File

@@ -83,13 +83,8 @@ func TestContainerSymlinkVolumes(t *testing.T) {
} {
testCase := testCase // capture range variable
t.Run(name, func(t *testing.T) {
testPodLogDir, err := os.MkdirTemp("", "symlink-test")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testVolDir, err := os.MkdirTemp("", "symlink-test-vol")
require.NoError(t, err)
defer os.RemoveAll(testVolDir)
testPodLogDir := t.TempDir()
testVolDir := t.TempDir()
content := "hello there\n"
regularFile, err := createRegularFile(testVolDir, content)

View File

@@ -17,12 +17,10 @@
package integration
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
exec "golang.org/x/sys/execabs"
runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
@@ -40,12 +38,9 @@ func TestImageLoad(t *testing.T) {
output, err := exec.Command("docker", "pull", testImage).CombinedOutput()
require.NoError(t, err, "output: %q", output)
// os.CreateTemp also opens a file, which might prevent us from overwriting that file with docker save.
tarDir, err := os.MkdirTemp("", "image-load")
tarDir := t.TempDir()
tar := filepath.Join(tarDir, "image.tar")
require.NoError(t, err)
defer func() {
assert.NoError(t, os.RemoveAll(tarDir))
}()
output, err = exec.Command("docker", "save", testImage, "-o", tar).CombinedOutput()
require.NoError(t, err, "output: %q", output)

View File

@@ -31,9 +31,7 @@ import (
)
func TestPodDualStack(t *testing.T) {
testPodLogDir, err := os.MkdirTemp("", "dualstack")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testPodLogDir := t.TempDir()
t.Log("Create a sandbox")
sb, sbConfig := PodSandboxConfigWithCleanup(t, "sandbox", "dualstack", WithPodLogDirectory(testPodLogDir))

View File

@@ -64,9 +64,7 @@ func TestPodHostname(t *testing.T) {
if test.needsHostNetwork && goruntime.GOOS == "windows" {
t.Skip("Skipped on Windows.")
}
testPodLogDir, err := os.MkdirTemp("", "hostname")
require.NoError(t, err)
defer os.RemoveAll(testPodLogDir)
testPodLogDir := t.TempDir()
opts := append(test.opts, WithPodLogDirectory(testPodLogDir))
t.Log("Create a sandbox with hostname")

View File

@@ -150,10 +150,7 @@ func testFailFastWhenConnectShim(abstract bool, dialFn dialFunc) func(*testing.T
}
func newTestListener(t testing.TB, abstract bool) (string, net.Listener, func()) {
tmpDir, err := os.MkdirTemp("", "shim-ut-XX")
if err != nil {
t.Fatalf("failed to create tmp directory: %v", err)
}
tmpDir := t.TempDir()
// NOTE(fuweid):
//