Skip to content

Commit

Permalink
libct/*_test.go: use t.TempDir
Browse files Browse the repository at this point in the history
Replace ioutil.TempDir (mostly) with t.TempDir, which require no
explicit cleanup.

While at it, fix incorrect usage of os.ModePerm in libcontainer/intelrdt
test. This is supposed to be a mask, not mode bits.

Signed-off-by: Kir Kolyshkin <[email protected]>
  • Loading branch information
kolyshkin committed Jul 20, 2021
1 parent 78c2b73 commit 23ed6b8
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 115 deletions.
7 changes: 1 addition & 6 deletions libcontainer/cgroups/fs2/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fs2

import (
"io/ioutil"
"os"
"path/filepath"
"reflect"
"sort"
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 2 additions & 7 deletions libcontainer/cgroups/fscommon/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions libcontainer/configs/validate/validator_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package validate_test

import (
"io/ioutil"
"os"
"path/filepath"
"testing"
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 1 addition & 8 deletions libcontainer/container_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package libcontainer

import (
"fmt"
"io/ioutil"
"os"
"testing"

Expand Down Expand Up @@ -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{
Expand Down
40 changes: 5 additions & 35 deletions libcontainer/factory_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package libcontainer

import (
"errors"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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"
Expand Down
14 changes: 1 addition & 13 deletions libcontainer/intelrdt/cmt_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package intelrdt

import (
"os"
"path/filepath"
"testing"
)
Expand All @@ -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
Expand Down
14 changes: 1 addition & 13 deletions libcontainer/intelrdt/mbm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package intelrdt

import (
"os"
"path/filepath"
"testing"
)
Expand All @@ -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
Expand Down
29 changes: 8 additions & 21 deletions libcontainer/intelrdt/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down
5 changes: 1 addition & 4 deletions libcontainer/notify_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 23ed6b8

Please sign in to comment.