test: use T.TempDir to create temporary test directory

The directory created by `T.TempDir` is automatically removed when the
test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.TempDir
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
This commit is contained in:
Eng Zer Jun
2022-03-15 11:41:08 +08:00
parent ed4cc4b482
commit 18ec2761c0
40 changed files with 137 additions and 428 deletions

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)