diff --git a/libcontainer/cgroups/fs2/io_test.go b/libcontainer/cgroups/fs2/io_test.go index 9b2706173..d7ad1c56c 100644 --- a/libcontainer/cgroups/fs2/io_test.go +++ b/libcontainer/cgroups/fs2/io_test.go @@ -2,7 +2,6 @@ package fs2 import ( "io/ioutil" - "os" "path/filepath" "reflect" "sort" @@ -60,11 +59,7 @@ func TestStatIo(t *testing.T) { // We're using a fake cgroupfs. cgroups.TestMode = true - fakeCgroupDir, err := ioutil.TempDir("", "runc-stat-io-test.*") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(fakeCgroupDir) + fakeCgroupDir := t.TempDir() statPath := filepath.Join(fakeCgroupDir, "io.stat") if err := ioutil.WriteFile(statPath, []byte(exampleIoStatData), 0o644); err != nil { diff --git a/libcontainer/cgroups/fscommon/utils_test.go b/libcontainer/cgroups/fscommon/utils_test.go index 103b9bed2..d429db93b 100644 --- a/libcontainer/cgroups/fscommon/utils_test.go +++ b/libcontainer/cgroups/fscommon/utils_test.go @@ -25,16 +25,11 @@ func init() { func TestGetCgroupParamsInt(t *testing.T) { // Setup tempdir. - tempDir, err := ioutil.TempDir("", "cgroup_utils_test") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(tempDir) + tempDir := t.TempDir() tempFile := filepath.Join(tempDir, cgroupFile) // Success. - err = ioutil.WriteFile(tempFile, []byte(floatString), 0o755) - if err != nil { + if err := ioutil.WriteFile(tempFile, []byte(floatString), 0o755); err != nil { t.Fatal(err) } value, err := GetCgroupParamUint(tempDir, cgroupFile) diff --git a/libcontainer/configs/validate/validator_test.go b/libcontainer/configs/validate/validator_test.go index 687ce1b09..4f72252d1 100644 --- a/libcontainer/configs/validate/validator_test.go +++ b/libcontainer/configs/validate/validator_test.go @@ -1,7 +1,6 @@ package validate_test import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -264,13 +263,7 @@ func TestValidateSysctlWithBindHostNetNS(t *testing.T) { const selfnet = "/proc/self/ns/net" - dir, err := ioutil.TempDir("", t.Name()+"-*") - if err != nil { - t.Fatal(err) - } - defer os.Remove(dir) - - file := filepath.Join(dir, "default") + file := filepath.Join(t.TempDir(), "default") fd, err := os.Create(file) if err != nil { t.Fatal(err) diff --git a/libcontainer/container_linux_test.go b/libcontainer/container_linux_test.go index 4072ec1a7..01b167c56 100644 --- a/libcontainer/container_linux_test.go +++ b/libcontainer/container_linux_test.go @@ -4,7 +4,6 @@ package libcontainer import ( "fmt" - "io/ioutil" "os" "testing" @@ -339,14 +338,8 @@ func TestGetContainerStateAfterUpdate(t *testing.T) { t.Fatal(err) } - rootDir, err := ioutil.TempDir("", "TestGetContainerStateAfterUpdate") - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(rootDir) - container := &linuxContainer{ - root: rootDir, + root: t.TempDir(), id: "myid", config: &configs.Config{ Namespaces: []configs.Namespace{ diff --git a/libcontainer/factory_linux_test.go b/libcontainer/factory_linux_test.go index 928e4f21b..5d389909d 100644 --- a/libcontainer/factory_linux_test.go +++ b/libcontainer/factory_linux_test.go @@ -4,7 +4,6 @@ package libcontainer import ( "errors" - "io/ioutil" "os" "path/filepath" "reflect" @@ -18,20 +17,8 @@ import ( "golang.org/x/sys/unix" ) -func newTestRoot() (string, error) { - dir, err := ioutil.TempDir("", "libcontainer") - if err != nil { - return "", err - } - return dir, nil -} - func TestFactoryNew(t *testing.T) { - root, rerr := newTestRoot() - if rerr != nil { - t.Fatal(rerr) - } - defer os.RemoveAll(root) + root := t.TempDir() factory, err := New(root, Cgroupfs) if err != nil { t.Fatal(err) @@ -53,11 +40,7 @@ func TestFactoryNew(t *testing.T) { } func TestFactoryNewIntelRdt(t *testing.T) { - root, rerr := newTestRoot() - if rerr != nil { - t.Fatal(rerr) - } - defer os.RemoveAll(root) + root := t.TempDir() factory, err := New(root, Cgroupfs, IntelRdtFs) if err != nil { t.Fatal(err) @@ -79,11 +62,7 @@ func TestFactoryNewIntelRdt(t *testing.T) { } func TestFactoryNewTmpfs(t *testing.T) { - root, rerr := newTestRoot() - if rerr != nil { - t.Fatal(rerr) - } - defer os.RemoveAll(root) + root := t.TempDir() factory, err := New(root, Cgroupfs, TmpfsRoot) if err != nil { t.Fatal(err) @@ -130,12 +109,7 @@ func TestFactoryNewTmpfs(t *testing.T) { } func TestFactoryLoadNotExists(t *testing.T) { - root, rerr := newTestRoot() - if rerr != nil { - t.Fatal(rerr) - } - defer os.RemoveAll(root) //nolint: errcheck - factory, err := New(root, Cgroupfs) + factory, err := New(t.TempDir(), Cgroupfs) if err != nil { t.Fatal(err) } @@ -149,11 +123,7 @@ func TestFactoryLoadNotExists(t *testing.T) { } func TestFactoryLoadContainer(t *testing.T) { - root, err := newTestRoot() - if err != nil { - t.Fatal(err) - } - defer os.RemoveAll(root) //nolint: errcheck + root := t.TempDir() // setup default container config and state for mocking var ( id = "1" diff --git a/libcontainer/intelrdt/cmt_test.go b/libcontainer/intelrdt/cmt_test.go index e061695e8..3bd43adae 100644 --- a/libcontainer/intelrdt/cmt_test.go +++ b/libcontainer/intelrdt/cmt_test.go @@ -1,7 +1,6 @@ package intelrdt import ( - "os" "path/filepath" "testing" ) @@ -13,18 +12,7 @@ func TestGetCMTNumaNodeStats(t *testing.T) { "llc_occupancy": 9123911, } - mockedL3_MON, err := mockResctrlL3_MON(mocksNUMANodesToCreate, mocksFilesToCreate) - - defer func() { - err := os.RemoveAll(mockedL3_MON) - if err != nil { - t.Fatal(err) - } - }() - - if err != nil { - t.Fatal(err) - } + mockedL3_MON := mockResctrlL3_MON(t, mocksNUMANodesToCreate, mocksFilesToCreate) t.Run("Gather mbm", func(t *testing.T) { enabledMonFeatures.llcOccupancy = true diff --git a/libcontainer/intelrdt/mbm_test.go b/libcontainer/intelrdt/mbm_test.go index 9949f1238..319f8de2b 100644 --- a/libcontainer/intelrdt/mbm_test.go +++ b/libcontainer/intelrdt/mbm_test.go @@ -3,7 +3,6 @@ package intelrdt import ( - "os" "path/filepath" "testing" ) @@ -16,18 +15,7 @@ func TestGetMBMNumaNodeStats(t *testing.T) { "mbm_local_bytes": 2361361, } - mockedL3_MON, err := mockResctrlL3_MON(mocksNUMANodesToCreate, mocksFilesToCreate) - - defer func() { - err := os.RemoveAll(mockedL3_MON) - if err != nil { - t.Fatal(err) - } - }() - - if err != nil { - t.Fatal(err) - } + mockedL3_MON := mockResctrlL3_MON(t, mocksNUMANodesToCreate, mocksFilesToCreate) t.Run("Gather mbm", func(t *testing.T) { enabledMonFeatures.mbmTotalBytes = true diff --git a/libcontainer/intelrdt/monitoring_test.go b/libcontainer/intelrdt/monitoring_test.go index 66ce830eb..7685fa31b 100644 --- a/libcontainer/intelrdt/monitoring_test.go +++ b/libcontainer/intelrdt/monitoring_test.go @@ -38,30 +38,28 @@ func TestParseMonFeatures(t *testing.T) { }) } -func mockResctrlL3_MON(NUMANodes []string, mocks map[string]uint64) (string, error) { - testDir, err := ioutil.TempDir("", "rdt_mbm_test") - if err != nil { - return "", err - } +func mockResctrlL3_MON(t *testing.T, NUMANodes []string, mocks map[string]uint64) string { + t.Helper() + testDir := t.TempDir() monDataPath := filepath.Join(testDir, "mon_data") for _, numa := range NUMANodes { numaPath := filepath.Join(monDataPath, numa) - err = os.MkdirAll(numaPath, os.ModePerm) + err := os.MkdirAll(numaPath, 0o700) if err != nil { - return "", err + t.Fatal(err) } for fileName, value := range mocks { err := ioutil.WriteFile(filepath.Join(numaPath, fileName), []byte(strconv.FormatUint(value, 10)), 0o644) if err != nil { - return "", err + t.Fatal(err) } } } - return testDir, nil + return testDir } func TestGetMonitoringStats(t *testing.T) { @@ -79,18 +77,7 @@ func TestGetMonitoringStats(t *testing.T) { "llc_occupancy": 123331, } - mockedL3_MON, err := mockResctrlL3_MON(mocksNUMANodesToCreate, mocksFilesToCreate) - - defer func() { - err := os.RemoveAll(mockedL3_MON) - if err != nil { - t.Fatal(err) - } - }() - - if err != nil { - t.Fatal(err) - } + mockedL3_MON := mockResctrlL3_MON(t, mocksNUMANodesToCreate, mocksFilesToCreate) t.Run("Gather monitoring stats", func(t *testing.T) { var stats Stats diff --git a/libcontainer/notify_linux_test.go b/libcontainer/notify_linux_test.go index b14cfc91e..80859c61e 100644 --- a/libcontainer/notify_linux_test.go +++ b/libcontainer/notify_linux_test.go @@ -17,10 +17,7 @@ import ( type notifyFunc func(path string) (<-chan struct{}, error) func testMemoryNotification(t *testing.T, evName string, notify notifyFunc, targ string) { - memoryPath, err := ioutil.TempDir("", "testmemnotification-"+evName) - if err != nil { - t.Fatal(err) - } + memoryPath := t.TempDir() evFile := filepath.Join(memoryPath, evName) eventPath := filepath.Join(memoryPath, "cgroup.event_control") if err := ioutil.WriteFile(evFile, []byte{}, 0o700); err != nil {