mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-11 06:03:57 +08:00
libct/*_test.go: use t.TempDir
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 <kolyshkin@gmail.com>
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user