Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: use T.TempDir to create temporary test directory #151

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 4 additions & 21 deletions pkg/koordlet/audit/auditor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"io/ioutil"
"net"
"net/http"
"os"
"testing"
"time"
)
Expand Down Expand Up @@ -64,11 +63,7 @@ func mustCreateHttpServer(t *testing.T, handler http.Handler) *TestServer {
}

func TestAuditorLogger(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

c := NewDefaultConfig()
c.LogDir = tempDir
Expand Down Expand Up @@ -155,11 +150,7 @@ func TestAuditorLogger(t *testing.T) {
}

func TestAuditorLoggerTxtOutput(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

c := NewDefaultConfig()
c.LogDir = tempDir
Expand Down Expand Up @@ -198,11 +189,7 @@ func TestAuditorLoggerTxtOutput(t *testing.T) {
}

func TestAuditorLoggerReaderInvalidPageToken(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

c := NewDefaultConfig()
c.LogDir = tempDir
Expand Down Expand Up @@ -269,11 +256,7 @@ func TestAuditorLoggerReaderInvalidPageToken(t *testing.T) {
}

func TestAuditorLoggerMaxActiveReaders(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

c := NewDefaultConfig()
c.LogDir = tempDir
Expand Down
25 changes: 4 additions & 21 deletions pkg/koordlet/audit/event_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

func TestEventLogger(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

logger := NewEventLogger(tempDir, 10, 0)
logger.Log(0, &Event{Reason: "hello"})
Expand All @@ -48,11 +43,7 @@ func TestEventLogger(t *testing.T) {
}

func TestFluentEventLogger(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

logger := NewFluentEventLogger(tempDir, 10, 0)
defer logger.Close()
Expand Down Expand Up @@ -81,11 +72,7 @@ func makeBlock(n int, b byte) []byte {
}

func TestReverseEventReaderSingleFile(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

logger := NewFluentEventLogger(tempDir, 10, 0)

Expand All @@ -111,11 +98,7 @@ func TestReverseEventReaderSingleFile(t *testing.T) {
}

func TestReverseEventReaderMultiFile(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

logger := NewFluentEventLogger(tempDir, 10, 0)

Expand Down
13 changes: 2 additions & 11 deletions pkg/koordlet/audit/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,12 @@ import (
"bytes"
"io"
"io/ioutil"
"os"
"strings"
"testing"
)

func TestLogWriter(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

name := tempDir + "/audit.log"
writer, err := OpenLogWriter(name)
Expand All @@ -56,11 +51,7 @@ func TestLogWriter(t *testing.T) {
}

func TestLogReader(t *testing.T) {
tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

name := tempDir + "/audit.log"
writer, err := OpenLogWriter(name)
Expand Down
7 changes: 1 addition & 6 deletions pkg/koordlet/pleg/watcher_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package pleg

import (
"io/ioutil"
"os"
"path"
"testing"
Expand All @@ -34,11 +33,7 @@ func TestWatcher(t *testing.T) {
assert.NoError(t, err, "create watcher failed")
defer watcher.Close()

tempDir, err := ioutil.TempDir(".", "_test")
if err != nil {
t.Fatal("failed to create dir", err)
}
defer os.RemoveAll(tempDir)
tempDir := t.TempDir()

err = watcher.AddWatch(tempDir)
assert.NoError(t, err, "watch path: %v failed", tempDir)
Expand Down
7 changes: 3 additions & 4 deletions pkg/koordlet/resmanager/be_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package resmanager

import (
"bufio"
"io/ioutil"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -231,7 +230,7 @@ func Test_reconcileBECPULimit(t *testing.T) {
}
for _, tt := range tests {
system.Conf = system.NewDsModeConfig()
system.Conf.CgroupRootDir, _ = ioutil.TempDir("/tmp", "koordlet-test")
system.Conf.CgroupRootDir = t.TempDir()
err := initTestPodCFS(tt.args.podMeta, tt.args.podCurCFS, tt.args.containerCurCFS)
if err != nil {
t.Errorf("init cfs quota failed, error: %v", err)
Expand Down Expand Up @@ -341,7 +340,7 @@ func Test_reconcileBECPUShare(t *testing.T) {
}
for _, tt := range tests {
system.Conf = system.NewDsModeConfig()
system.Conf.CgroupRootDir, _ = ioutil.TempDir("/tmp", "koordlet-test")
system.Conf.CgroupRootDir = t.TempDir()
err := initTestPodCPUShare(tt.args.podMeta, tt.args.podCurCPUShare, tt.args.containerCurCPUShare)
if err != nil {
t.Errorf("init cpu share failed, error: %v", err)
Expand Down Expand Up @@ -451,7 +450,7 @@ func Test_reconcileBEMemLimit(t *testing.T) {
}
for _, tt := range tests {
system.Conf = system.NewDsModeConfig()
system.Conf.CgroupRootDir, _ = ioutil.TempDir("/tmp", "koordlet-test")
system.Conf.CgroupRootDir = t.TempDir()
err := initTestPodMemLimit(tt.args.podMeta, tt.args.podCurMemLimit, tt.args.containerCurMemLimit)
if err != nil {
t.Errorf("init cpu share failed, error: %v", err)
Expand Down
4 changes: 1 addition & 3 deletions pkg/koordlet/resmanager/cgroup_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ func Test_calculateAndUpdateResources(t *testing.T) {
defer func() { stop <- struct{}{} }()

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

initQoSStrategy := defaultQoSStrategy()
initQoSCgroupFile(initQoSStrategy, helper)
Expand Down Expand Up @@ -805,8 +804,7 @@ func TestCgroupResourceReconcile_calculateResources(t *testing.T) {
assert.NoError(t, err)
defer func() { stop <- struct{}{} }()

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
_ = system.NewFileTestUtil(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove the equation?


got, got1, got2 := m.calculateResources(tt.args.nodeCfg, tt.args.node, tt.args.podMetas)
assertCgroupResourceEqual(t, tt.want, got)
Expand Down
3 changes: 0 additions & 3 deletions pkg/koordlet/resmanager/cpu_burst_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,6 @@ func TestCPUBurst_applyCPUBurst(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testHelper := system.NewFileTestUtil(t)
defer testHelper.Cleanup()

b := &CPUBurst{
executor: NewResourceUpdateExecutor("CPUBurstTestExecutor", 60),
Expand Down Expand Up @@ -1187,7 +1186,6 @@ func TestCPUBurst_applyCFSQuotaBurst(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testHelper := system.NewFileTestUtil(t)
defer testHelper.Cleanup()

stop := make(chan struct{})
defer func() { stop <- struct{}{} }()
Expand Down Expand Up @@ -1566,7 +1564,6 @@ func TestCPUBurst_start(t *testing.T) {
}

testHelper := system.NewFileTestUtil(t)
defer testHelper.Cleanup()

b := NewCPUBurst(resmanager)
stop := make(chan struct{})
Expand Down
8 changes: 0 additions & 8 deletions pkg/koordlet/resmanager/cpu_suppress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@ func Test_cpuSuppress_suppressBECPU(t *testing.T) {

// prepare testing files
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
helper.WriteCgroupFileContents(util.GetKubeQosRelativePath(corev1.PodQOSGuaranteed), system.CPUSet, tt.args.nodeCPUSet)
helper.WriteCgroupFileContents(util.GetKubeQosRelativePath(corev1.PodQOSBestEffort), system.CPUSet, tt.args.preBECPUSet)
helper.WriteCgroupFileContents(util.GetKubeQosRelativePath(corev1.PodQOSBestEffort), system.CPUCFSQuota, strconv.FormatInt(tt.args.preBECFSQuota, 10))
Expand Down Expand Up @@ -917,7 +916,6 @@ func Test_cpuSuppress_recoverCPUSetIfNeed(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// prepare testing files
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
podDirs := []string{"pod1", "pod2", "pod3"}
testingPrepareBECgroupData(helper, podDirs, tt.args.oldCPUSets)
helper.WriteCgroupFileContents(util.GetKubeQosRelativePath(corev1.PodQOSGuaranteed), system.CPUSet, tt.args.rootCPUSets)
Expand Down Expand Up @@ -973,7 +971,6 @@ func Test_cpuSuppress_recoverCFSQuotaIfNeed(t *testing.T) {
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
beQosDir := util.GetKubeQosRelativePath(corev1.PodQOSBestEffort)
helper.CreateCgroupFile(beQosDir, system.CPUCFSQuota)

Expand Down Expand Up @@ -1195,7 +1192,6 @@ func Test_calculateBESuppressCPUSetPolicy(t *testing.T) {
func Test_applyBESuppressCPUSetPolicy(t *testing.T) {
// prepare testing files
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
podDirs := []string{"pod1", "pod2", "pod3"}
testingPrepareBECgroupData(helper, podDirs, "1,2")

Expand All @@ -1218,7 +1214,6 @@ func Test_applyBESuppressCPUSetPolicy(t *testing.T) {
func Test_getBECgroupCPUSetPathsRecursive(t *testing.T) {
// prepare testing files
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
podDirs := []string{"pod1", "pod2", "pod3"}
testingPrepareBECgroupData(helper, podDirs, "1,2")
var wantPaths []string
Expand Down Expand Up @@ -1288,7 +1283,6 @@ func Test_adjustByCPUSet(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
// prepare testing files
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
podDirs := []string{"pod1", "pod2", "pod3"}
testingPrepareBECgroupData(helper, podDirs, tt.args.oldCPUSets)

Expand All @@ -1306,7 +1300,6 @@ func Test_adjustByCPUSet(t *testing.T) {

func Test_adjustByCfsQuota(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
beQosDir := util.GetKubeQosRelativePath(corev1.PodQOSBestEffort)
helper.CreateCgroupFile(beQosDir, system.CPUCFSQuota)
node := &corev1.Node{
Expand Down Expand Up @@ -1375,7 +1368,6 @@ func Test_adjustByCfsQuota(t *testing.T) {
func Test_writeBECgroupsCPUSet(t *testing.T) {
// prepare testing files
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
podDirs := []string{"pod1", "pod2", "pod3"}
testingPrepareBECgroupData(helper, podDirs, "1,2")

Expand Down
13 changes: 1 addition & 12 deletions pkg/koordlet/resmanager/resctrl_reconcile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ func Test_calculateCatL3Schemata(t *testing.T) {
func Test_initCatResctrl(t *testing.T) {
t.Run("test", func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "initCatResctrl"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -400,9 +399,7 @@ func Test_getPodCgroupNewTaskIds(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()
_ = system.NewFileTestUtil(t)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we remove the equation


testingPrepareContainerCgroupCPUTasks(t,
tt.fields.containerParentDir, tt.fields.containerTasksStr)
Expand Down Expand Up @@ -611,7 +608,6 @@ func TestResctrlReconcile_calculateAndApplyCatL3PolicyForGroup(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "calculateAndApplyCatL3PolicyForGroup"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -762,7 +758,6 @@ func TestResctrlReconcile_calculateAndApplyCatMbPolicyForGroup(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "calculateAndApplyCatMbPolicyForGroup"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -857,7 +852,6 @@ func TestResctrlReconcile_calculateAndApplyCatL3GroupTasks(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "writeCatL3GroupTasks"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -891,7 +885,6 @@ func TestResctrlReconcile_calculateAndApplyCatL3GroupTasks(t *testing.T) {
func TestResctrlReconcile_reconcileCatResctrlPolicy(t *testing.T) {
t.Run("test", func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "reconcileCatResctrlPolicy"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -1062,7 +1055,6 @@ func TestResctrlReconcile_reconcileResctrlGroups(t *testing.T) {
statesInformer.EXPECT().GetAllPods().Return([]*statesinformer.PodMeta{testingPodMeta}).MaxTimes(2)

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "reconcileResctrlGroups"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -1181,7 +1173,6 @@ func TestResctrlReconcile_reconcile(t *testing.T) {
}

helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "ResctrlReconcile"
helper.MkDirAll(sysFSRootDirName)
Expand Down Expand Up @@ -1283,7 +1274,6 @@ func Test_calculateMbaPercentForGroup(t *testing.T) {
func Test_calculateL3SchemataResource(t *testing.T) {
t.Run("test", func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "reconcileCatResctrlPolicy"
helper.MkDirAll(sysFSRootDirName)
Expand All @@ -1299,7 +1289,6 @@ func Test_calculateL3SchemataResource(t *testing.T) {
func Test_calculateMbSchemataResource(t *testing.T) {
t.Run("test", func(t *testing.T) {
helper := system.NewFileTestUtil(t)
defer helper.Cleanup()

sysFSRootDirName := "reconcileCatResctrlPolicy"
helper.MkDirAll(sysFSRootDirName)
Expand Down
Loading