From f6a56f603cc30fea9b06de6b9f20b207cc143e07 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Mon, 19 Jul 2021 15:23:00 -0700 Subject: [PATCH] libct/cg/fs/*_test.go: use t.TempDir This simplifies the code as no explicit cleanup is required. Signed-off-by: Kir Kolyshkin --- libcontainer/cgroups/fs/blkio_test.go | 17 -------------- libcontainer/cgroups/fs/cpu_test.go | 7 +----- libcontainer/cgroups/fs/cpuacct_test.go | 2 -- libcontainer/cgroups/fs/cpuset_test.go | 4 ---- libcontainer/cgroups/fs/devices_test.go | 1 - libcontainer/cgroups/fs/freezer_test.go | 2 -- libcontainer/cgroups/fs/hugetlb_test.go | 6 ----- libcontainer/cgroups/fs/memory_test.go | 18 --------------- libcontainer/cgroups/fs/net_cls_test.go | 1 - libcontainer/cgroups/fs/net_prio_test.go | 1 - libcontainer/cgroups/fs/pids_test.go | 4 ---- libcontainer/cgroups/fs/util_test.go | 29 ++++++------------------ 12 files changed, 8 insertions(+), 84 deletions(-) diff --git a/libcontainer/cgroups/fs/blkio_test.go b/libcontainer/cgroups/fs/blkio_test.go index 2f5ff2aa8..860628c7e 100644 --- a/libcontainer/cgroups/fs/blkio_test.go +++ b/libcontainer/cgroups/fs/blkio_test.go @@ -179,7 +179,6 @@ func TestBlkioSetWeight(t *testing.T) { for _, legacyIOScheduler := range []bool{false, true} { // Populate cgroup helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() weightFilename := "blkio.bfq.weight" if legacyIOScheduler { weightFilename = "blkio.weight" @@ -215,7 +214,6 @@ func TestBlkioSetWeightDevice(t *testing.T) { for _, legacyIOScheduler := range []bool{false, true} { // Populate cgroup helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() weightFilename := "blkio.bfq.weight" weightDeviceFilename := "blkio.bfq.weight_device" if legacyIOScheduler { @@ -251,7 +249,6 @@ func TestBlkioSetWeightDevice(t *testing.T) { // regression #274 func TestBlkioSetMultipleWeightDevice(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() const ( weightDeviceBefore = "8:0 400" @@ -290,7 +287,6 @@ func TestBlkioSetMultipleWeightDevice(t *testing.T) { func TestBlkioBFQDebugStats(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(blkioBFQDebugStatsTestFiles) blkio := &BlkioGroup{} actualStats := *cgroups.NewStats() @@ -345,7 +341,6 @@ func TestBlkioBFQDebugStats(t *testing.T) { func TestBlkioMultipleStatsFiles(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(blkioBFQDebugStatsTestFiles) helper.writeFileContents(blkioCFQStatsTestFiles) blkio := &BlkioGroup{} @@ -401,7 +396,6 @@ func TestBlkioMultipleStatsFiles(t *testing.T) { func TestBlkioBFQStats(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(blkioBFQStatsTestFiles) blkio := &BlkioGroup{} actualStats := *cgroups.NewStats() @@ -468,7 +462,6 @@ func TestBlkioStatsNoFilesBFQDebug(t *testing.T) { for _, testCase := range testCases { helper := NewCgroupTestUtil("cpuset", t) - defer helper.cleanup() tempBlkioTestFiles := map[string]string{} for i, v := range blkioBFQDebugStatsTestFiles { @@ -488,7 +481,6 @@ func TestBlkioStatsNoFilesBFQDebug(t *testing.T) { func TestBlkioCFQStats(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(blkioCFQStatsTestFiles) blkio := &BlkioGroup{} @@ -584,7 +576,6 @@ func TestBlkioStatsNoFilesCFQ(t *testing.T) { for _, testCase := range testCases { helper := NewCgroupTestUtil("cpuset", t) - defer helper.cleanup() tempBlkioTestFiles := map[string]string{} for i, v := range blkioCFQStatsTestFiles { @@ -604,7 +595,6 @@ func TestBlkioStatsNoFilesCFQ(t *testing.T) { func TestBlkioStatsUnexpectedNumberOfFields(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "blkio.io_service_bytes_recursive": "8:0 Read 100 100", "blkio.io_serviced_recursive": servicedRecursiveContents, @@ -626,7 +616,6 @@ func TestBlkioStatsUnexpectedNumberOfFields(t *testing.T) { func TestBlkioStatsUnexpectedFieldType(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "blkio.io_service_bytes_recursive": "8:0 Read Write", "blkio.io_serviced_recursive": servicedRecursiveContents, @@ -648,7 +637,6 @@ func TestBlkioStatsUnexpectedFieldType(t *testing.T) { func TestThrottleRecursiveBlkioStats(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "blkio.io_service_bytes_recursive": "", "blkio.io_serviced_recursive": "", @@ -699,7 +687,6 @@ func TestThrottleRecursiveBlkioStats(t *testing.T) { func TestThrottleBlkioStats(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "blkio.io_service_bytes_recursive": "", "blkio.io_serviced_recursive": "", @@ -750,7 +737,6 @@ func TestThrottleBlkioStats(t *testing.T) { func TestBlkioSetThrottleReadBpsDevice(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() const ( throttleBefore = `8:0 1024` @@ -780,7 +766,6 @@ func TestBlkioSetThrottleReadBpsDevice(t *testing.T) { func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() const ( throttleBefore = `8:0 1024` @@ -810,7 +795,6 @@ func TestBlkioSetThrottleWriteBpsDevice(t *testing.T) { func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() const ( throttleBefore = `8:0 1024` @@ -840,7 +824,6 @@ func TestBlkioSetThrottleReadIOpsDevice(t *testing.T) { func TestBlkioSetThrottleWriteIOpsDevice(t *testing.T) { helper := NewCgroupTestUtil("blkio", t) - defer helper.cleanup() const ( throttleBefore = `8:0 1024` diff --git a/libcontainer/cgroups/fs/cpu_test.go b/libcontainer/cgroups/fs/cpu_test.go index 4e9c2ff9f..77d12af89 100644 --- a/libcontainer/cgroups/fs/cpu_test.go +++ b/libcontainer/cgroups/fs/cpu_test.go @@ -13,7 +13,6 @@ import ( func TestCpuSetShares(t *testing.T) { helper := NewCgroupTestUtil("cpu", t) - defer helper.cleanup() const ( sharesBefore = 1024 @@ -41,7 +40,6 @@ func TestCpuSetShares(t *testing.T) { func TestCpuSetBandWidth(t *testing.T) { helper := NewCgroupTestUtil("cpu", t) - defer helper.cleanup() const ( quotaBefore = 8000 @@ -105,7 +103,6 @@ func TestCpuSetBandWidth(t *testing.T) { func TestCpuStats(t *testing.T) { helper := NewCgroupTestUtil("cpu", t) - defer helper.cleanup() const ( nrPeriods = 2000 @@ -137,7 +134,6 @@ func TestCpuStats(t *testing.T) { func TestNoCpuStatFile(t *testing.T) { helper := NewCgroupTestUtil("cpu", t) - defer helper.cleanup() cpu := &CpuGroup{} actualStats := *cgroups.NewStats() @@ -149,7 +145,7 @@ func TestNoCpuStatFile(t *testing.T) { func TestInvalidCpuStat(t *testing.T) { helper := NewCgroupTestUtil("cpu", t) - defer helper.cleanup() + cpuStatContent := `nr_periods 2000 nr_throttled 200 throttled_time fortytwo` @@ -167,7 +163,6 @@ func TestInvalidCpuStat(t *testing.T) { func TestCpuSetRtSchedAtApply(t *testing.T) { helper := NewCgroupTestUtil("cpu", t) - defer helper.cleanup() const ( rtRuntimeBefore = 0 diff --git a/libcontainer/cgroups/fs/cpuacct_test.go b/libcontainer/cgroups/fs/cpuacct_test.go index 009380814..aaae0aa4e 100644 --- a/libcontainer/cgroups/fs/cpuacct_test.go +++ b/libcontainer/cgroups/fs/cpuacct_test.go @@ -27,7 +27,6 @@ const ( func TestCpuacctStats(t *testing.T) { helper := NewCgroupTestUtil("cpuacct.", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "cpuacct.usage": cpuAcctUsageContents, "cpuacct.usage_percpu": cpuAcctUsagePerCPUContents, @@ -68,7 +67,6 @@ func TestCpuacctStats(t *testing.T) { func TestCpuacctStatsWithoutUsageAll(t *testing.T) { helper := NewCgroupTestUtil("cpuacct.", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "cpuacct.usage": cpuAcctUsageContents, "cpuacct.usage_percpu": cpuAcctUsagePerCPUContents, diff --git a/libcontainer/cgroups/fs/cpuset_test.go b/libcontainer/cgroups/fs/cpuset_test.go index 05872ed6c..17de7bfa1 100644 --- a/libcontainer/cgroups/fs/cpuset_test.go +++ b/libcontainer/cgroups/fs/cpuset_test.go @@ -40,7 +40,6 @@ var cpusetTestFiles = map[string]string{ func TestCPUSetSetCpus(t *testing.T) { helper := NewCgroupTestUtil("cpuset", t) - defer helper.cleanup() const ( cpusBefore = "0" @@ -68,7 +67,6 @@ func TestCPUSetSetCpus(t *testing.T) { func TestCPUSetSetMems(t *testing.T) { helper := NewCgroupTestUtil("cpuset", t) - defer helper.cleanup() const ( memsBefore = "0" @@ -96,7 +94,6 @@ func TestCPUSetSetMems(t *testing.T) { func TestCPUSetStatsCorrect(t *testing.T) { helper := NewCgroupTestUtil("cpuset", t) - defer helper.cleanup() helper.writeFileContents(cpusetTestFiles) cpuset := &CpusetGroup{} @@ -211,7 +208,6 @@ func TestCPUSetStatsMissingFiles(t *testing.T) { } { t.Run(testCase.desc, func(t *testing.T) { helper := NewCgroupTestUtil("cpuset", t) - defer helper.cleanup() tempCpusetTestFiles := map[string]string{} for i, v := range cpusetTestFiles { diff --git a/libcontainer/cgroups/fs/devices_test.go b/libcontainer/cgroups/fs/devices_test.go index 1a49b4a89..a091543e4 100644 --- a/libcontainer/cgroups/fs/devices_test.go +++ b/libcontainer/cgroups/fs/devices_test.go @@ -11,7 +11,6 @@ import ( func TestDevicesSetAllow(t *testing.T) { helper := NewCgroupTestUtil("devices", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "devices.allow": "", diff --git a/libcontainer/cgroups/fs/freezer_test.go b/libcontainer/cgroups/fs/freezer_test.go index 7117795ba..4f9584979 100644 --- a/libcontainer/cgroups/fs/freezer_test.go +++ b/libcontainer/cgroups/fs/freezer_test.go @@ -11,7 +11,6 @@ import ( func TestFreezerSetState(t *testing.T) { helper := NewCgroupTestUtil("freezer", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "freezer.state": string(configs.Frozen), @@ -34,7 +33,6 @@ func TestFreezerSetState(t *testing.T) { func TestFreezerSetInvalidState(t *testing.T) { helper := NewCgroupTestUtil("freezer", t) - defer helper.cleanup() const ( invalidArg configs.FreezerState = "Invalid" diff --git a/libcontainer/cgroups/fs/hugetlb_test.go b/libcontainer/cgroups/fs/hugetlb_test.go index 181f243ec..33c8b6d20 100644 --- a/libcontainer/cgroups/fs/hugetlb_test.go +++ b/libcontainer/cgroups/fs/hugetlb_test.go @@ -27,7 +27,6 @@ const ( func TestHugetlbSetHugetlb(t *testing.T) { helper := NewCgroupTestUtil("hugetlb", t) - defer helper.cleanup() const ( hugetlbBefore = 256 @@ -67,7 +66,6 @@ func TestHugetlbSetHugetlb(t *testing.T) { func TestHugetlbStats(t *testing.T) { helper := NewCgroupTestUtil("hugetlb", t) - defer helper.cleanup() for _, pageSize := range HugePageSizes { helper.writeFileContents(map[string]string{ fmt.Sprintf(usage, pageSize): hugetlbUsageContents, @@ -90,7 +88,6 @@ func TestHugetlbStats(t *testing.T) { func TestHugetlbStatsNoUsageFile(t *testing.T) { helper := NewCgroupTestUtil("hugetlb", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ maxUsage: hugetlbMaxUsageContents, }) @@ -105,7 +102,6 @@ func TestHugetlbStatsNoUsageFile(t *testing.T) { func TestHugetlbStatsNoMaxUsageFile(t *testing.T) { helper := NewCgroupTestUtil("hugetlb", t) - defer helper.cleanup() for _, pageSize := range HugePageSizes { helper.writeFileContents(map[string]string{ fmt.Sprintf(usage, pageSize): hugetlbUsageContents, @@ -122,7 +118,6 @@ func TestHugetlbStatsNoMaxUsageFile(t *testing.T) { func TestHugetlbStatsBadUsageFile(t *testing.T) { helper := NewCgroupTestUtil("hugetlb", t) - defer helper.cleanup() for _, pageSize := range HugePageSizes { helper.writeFileContents(map[string]string{ fmt.Sprintf(usage, pageSize): "bad", @@ -140,7 +135,6 @@ func TestHugetlbStatsBadUsageFile(t *testing.T) { func TestHugetlbStatsBadMaxUsageFile(t *testing.T) { helper := NewCgroupTestUtil("hugetlb", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ usage: hugetlbUsageContents, maxUsage: "bad", diff --git a/libcontainer/cgroups/fs/memory_test.go b/libcontainer/cgroups/fs/memory_test.go index 92fba42bc..82adafc56 100644 --- a/libcontainer/cgroups/fs/memory_test.go +++ b/libcontainer/cgroups/fs/memory_test.go @@ -41,7 +41,6 @@ whatever=100 N0=0 func TestMemorySetMemory(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() const ( memoryBefore = 314572800 // 300M @@ -81,7 +80,6 @@ func TestMemorySetMemory(t *testing.T) { func TestMemorySetMemoryswap(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() const ( memoryswapBefore = 314572800 // 300M @@ -109,7 +107,6 @@ func TestMemorySetMemoryswap(t *testing.T) { func TestMemorySetMemoryLargerThanSwap(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() const ( memoryBefore = 314572800 // 300M @@ -154,7 +151,6 @@ func TestMemorySetMemoryLargerThanSwap(t *testing.T) { func TestMemorySetSwapSmallerThanMemory(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() const ( memoryBefore = 629145600 // 600M @@ -194,7 +190,6 @@ func TestMemorySetSwapSmallerThanMemory(t *testing.T) { func TestMemorySetMemorySwappinessDefault(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() swappinessBefore := 60 // default is 60 swappinessAfter := uint64(0) @@ -220,7 +215,6 @@ func TestMemorySetMemorySwappinessDefault(t *testing.T) { func TestMemoryStats(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.usage_in_bytes": memoryUsageContents, @@ -272,7 +266,6 @@ func TestMemoryStats(t *testing.T) { func TestMemoryStatsNoStatFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.usage_in_bytes": memoryUsageContents, "memory.max_usage_in_bytes": memoryMaxUsageContents, @@ -289,7 +282,6 @@ func TestMemoryStatsNoStatFile(t *testing.T) { func TestMemoryStatsNoUsageFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.max_usage_in_bytes": memoryMaxUsageContents, @@ -306,7 +298,6 @@ func TestMemoryStatsNoUsageFile(t *testing.T) { func TestMemoryStatsNoMaxUsageFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.usage_in_bytes": memoryUsageContents, @@ -323,7 +314,6 @@ func TestMemoryStatsNoMaxUsageFile(t *testing.T) { func TestMemoryStatsNoLimitInBytesFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.usage_in_bytes": memoryUsageContents, @@ -340,7 +330,6 @@ func TestMemoryStatsNoLimitInBytesFile(t *testing.T) { func TestMemoryStatsBadStatFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": "rss rss", "memory.usage_in_bytes": memoryUsageContents, @@ -358,7 +347,6 @@ func TestMemoryStatsBadStatFile(t *testing.T) { func TestMemoryStatsBadUsageFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.usage_in_bytes": "bad", @@ -376,7 +364,6 @@ func TestMemoryStatsBadUsageFile(t *testing.T) { func TestMemoryStatsBadMaxUsageFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.usage_in_bytes": memoryUsageContents, @@ -394,7 +381,6 @@ func TestMemoryStatsBadMaxUsageFile(t *testing.T) { func TestMemoryStatsBadLimitInBytesFile(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.stat": memoryStatContents, "memory.usage_in_bytes": memoryUsageContents, @@ -412,7 +398,6 @@ func TestMemoryStatsBadLimitInBytesFile(t *testing.T) { func TestMemorySetOomControl(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() const ( oomKillDisable = 1 // disable oom killer, default is 0 @@ -438,7 +423,6 @@ func TestMemorySetOomControl(t *testing.T) { func TestNoHierarchicalNumaStat(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "memory.numa_stat": memoryNUMAStatNoHierarchyContents + memoryNUMAStatExtraContents, }) @@ -489,7 +473,6 @@ anon=183 N0=12 badone }, } helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() for _, c := range memoryNUMAStatBadContents { helper.writeFileContents(map[string]string{ "memory.numa_stat": c.contents, @@ -504,7 +487,6 @@ anon=183 N0=12 badone func TestWithoutNumaStat(t *testing.T) { helper := NewCgroupTestUtil("memory", t) - defer helper.cleanup() actualStats, err := getPageUsageByNUMA(helper.CgroupPath) if err != nil { diff --git a/libcontainer/cgroups/fs/net_cls_test.go b/libcontainer/cgroups/fs/net_cls_test.go index d96814a3e..7c0ba03dc 100644 --- a/libcontainer/cgroups/fs/net_cls_test.go +++ b/libcontainer/cgroups/fs/net_cls_test.go @@ -16,7 +16,6 @@ const ( func TestNetClsSetClassid(t *testing.T) { helper := NewCgroupTestUtil("net_cls", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "net_cls.classid": strconv.FormatUint(classidBefore, 10), diff --git a/libcontainer/cgroups/fs/net_prio_test.go b/libcontainer/cgroups/fs/net_prio_test.go index a2092b69b..a1bb2ce4d 100644 --- a/libcontainer/cgroups/fs/net_prio_test.go +++ b/libcontainer/cgroups/fs/net_prio_test.go @@ -19,7 +19,6 @@ var prioMap = []*configs.IfPrioMap{ func TestNetPrioSetIfPrio(t *testing.T) { helper := NewCgroupTestUtil("net_prio", t) - defer helper.cleanup() helper.CgroupData.config.Resources.NetPrioIfpriomap = prioMap netPrio := &NetPrioGroup{} diff --git a/libcontainer/cgroups/fs/pids_test.go b/libcontainer/cgroups/fs/pids_test.go index 66edfe880..50777716b 100644 --- a/libcontainer/cgroups/fs/pids_test.go +++ b/libcontainer/cgroups/fs/pids_test.go @@ -17,7 +17,6 @@ const ( func TestPidsSetMax(t *testing.T) { helper := NewCgroupTestUtil("pids", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "pids.max": "max", @@ -40,7 +39,6 @@ func TestPidsSetMax(t *testing.T) { func TestPidsSetUnlimited(t *testing.T) { helper := NewCgroupTestUtil("pids", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "pids.max": strconv.Itoa(maxLimited), @@ -63,7 +61,6 @@ func TestPidsSetUnlimited(t *testing.T) { func TestPidsStats(t *testing.T) { helper := NewCgroupTestUtil("pids", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "pids.current": strconv.Itoa(1337), @@ -87,7 +84,6 @@ func TestPidsStats(t *testing.T) { func TestPidsStatsUnlimited(t *testing.T) { helper := NewCgroupTestUtil("pids", t) - defer helper.cleanup() helper.writeFileContents(map[string]string{ "pids.current": strconv.Itoa(4096), diff --git a/libcontainer/cgroups/fs/util_test.go b/libcontainer/cgroups/fs/util_test.go index e35509b8f..7b7858f65 100644 --- a/libcontainer/cgroups/fs/util_test.go +++ b/libcontainer/cgroups/fs/util_test.go @@ -8,7 +8,6 @@ Creates a mock of the cgroup filesystem for the duration of the test. package fs import ( - "io/ioutil" "os" "path/filepath" "testing" @@ -28,37 +27,23 @@ type cgroupTestUtil struct { // Path to the mock cgroup directory. CgroupPath string - // Temporary directory to store mock cgroup filesystem. - tempDir string - t *testing.T + t *testing.T } // Creates a new test util for the specified subsystem func NewCgroupTestUtil(subsystem string, t *testing.T) *cgroupTestUtil { d := &cgroupData{ - config: &configs.Cgroup{}, + config: &configs.Cgroup{ + Resources: &configs.Resources{}, + }, + root: t.TempDir(), } - d.config.Resources = &configs.Resources{} - tempDir, err := ioutil.TempDir("", "cgroup_test") - if err != nil { - t.Fatal(err) - } - d.root = tempDir testCgroupPath := filepath.Join(d.root, subsystem) - if err != nil { - t.Fatal(err) - } - // Ensure the full mock cgroup path exists. - err = os.MkdirAll(testCgroupPath, 0o755) - if err != nil { + if err := os.MkdirAll(testCgroupPath, 0o755); err != nil { t.Fatal(err) } - return &cgroupTestUtil{CgroupData: d, CgroupPath: testCgroupPath, tempDir: tempDir, t: t} -} - -func (c *cgroupTestUtil) cleanup() { - os.RemoveAll(c.tempDir) + return &cgroupTestUtil{CgroupData: d, CgroupPath: testCgroupPath, t: t} } // Write the specified contents on the mock of the specified cgroup files.