github.com/kubewharf/katalyst-core@v0.5.3/pkg/util/cgroup/manager/cgroup_test.go (about) 1 //go:build linux 2 // +build linux 3 4 /* 5 Copyright 2022 The Katalyst Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package manager 21 22 import ( 23 "context" 24 "fmt" 25 "io/ioutil" 26 "math" 27 "os" 28 "path/filepath" 29 "testing" 30 31 "bou.ke/monkey" 32 "github.com/opencontainers/runc/libcontainer/cgroups" 33 "github.com/stretchr/testify/assert" 34 35 "github.com/kubewharf/katalyst-core/pkg/util/cgroup/common" 36 v1 "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager/v1" 37 v2 "github.com/kubewharf/katalyst-core/pkg/util/cgroup/manager/v2" 38 ) 39 40 func TestManager(t *testing.T) { 41 t.Parallel() 42 43 _ = GetManager() 44 45 testV1Manager(t) 46 testV2Manager(t) 47 } 48 49 func testV1Manager(t *testing.T) { 50 _ = v1.NewManager() 51 52 testManager(t, "v1") 53 testNetCls(t, "v1") 54 } 55 56 func testV2Manager(t *testing.T) { 57 _ = v2.NewManager() 58 59 testManager(t, "v2") 60 testSwapMax(t) 61 } 62 63 func testManager(t *testing.T, version string) { 64 t.Logf("test with version %v", version) 65 66 var err error 67 68 err = ApplyMemoryWithRelativePath("/test", &common.MemoryData{}) 69 assert.NoError(t, err) 70 err = ApplyCPUWithRelativePath("/test", &common.CPUData{}) 71 assert.NoError(t, err) 72 err = ApplyCPUSetWithRelativePath("/test", &common.CPUSetData{}) 73 assert.NoError(t, err) 74 err = ApplyCPUSetWithAbsolutePath("/test", &common.CPUSetData{}) 75 assert.NoError(t, err) 76 err = ApplyCPUSetForContainer("fake-pod", "fake-container", &common.CPUSetData{}) 77 assert.NotNil(t, err) 78 err = ApplyUnifiedDataForContainer("fake-pod", "fake-container", common.CgroupSubsysMemory, "memory.high", "max") 79 assert.NotNil(t, err) 80 81 _, _ = GetMemoryWithRelativePath("/") 82 _, _ = GetMemoryWithAbsolutePath("/") 83 _, _ = GetCPUWithRelativePath("/") 84 _, _ = GetMetricsWithRelativePath("/", map[string]struct{}{"cpu": {}}) 85 _, _ = GetPidsWithRelativePath("/") 86 _, _ = GetPidsWithAbsolutePath("/") 87 _, _ = GetTasksWithRelativePath("/", "cpu") 88 _, _ = GetTasksWithAbsolutePath("/") 89 90 _ = DropCacheWithTimeoutForContainer(context.Background(), "fake-pod", "fake-container", 1, 0) 91 _ = DropCacheWithTimeoutAndAbsCGPath(1, "/test", 0) 92 } 93 94 func testNetCls(t *testing.T, version string) { 95 t.Logf("test net_cls with version %v", version) 96 var err error 97 98 err = ApplyNetClsWithRelativePath("/test", &common.NetClsData{}) 99 assert.NoError(t, err) 100 101 err = ApplyNetClsForContainer("fake-pod", "fake-container", &common.NetClsData{}) 102 assert.Error(t, err) 103 } 104 105 func testSwapMax(t *testing.T) { 106 defer monkey.UnpatchAll() 107 monkey.Patch(common.CheckCgroup2UnifiedMode, func() bool { return true }) 108 monkey.Patch(GetManager, func() Manager { return v2.NewManager() }) 109 monkey.Patch(cgroups.ReadFile, func(dir, file string) (string, error) { 110 f := filepath.Join(dir, file) 111 tmp, err := ioutil.ReadFile(f) 112 if err != nil { 113 return "", err 114 } 115 return string(tmp), nil 116 }) 117 monkey.Patch(cgroups.WriteFile, func(dir, file, data string) error { 118 f := filepath.Join(dir, file) 119 return ioutil.WriteFile(f, []byte(data), 0o700) 120 }) 121 122 rootDir := os.TempDir() 123 dir := filepath.Join(rootDir, "tmp") 124 err := os.Mkdir(dir, 0o700) 125 assert.NoError(t, err) 126 127 tmpDir, err := ioutil.TempDir(dir, "fake-cgroup") 128 assert.NoError(t, err) 129 defer os.RemoveAll(dir) 130 131 monkey.Patch(common.GetCgroupRootPath, func(s string) string { 132 t.Logf("rootDir=%v", rootDir) 133 return rootDir 134 }) 135 136 sawpFile := filepath.Join(tmpDir, "memory.swap.max") 137 err = ioutil.WriteFile(sawpFile, []byte{}, 0o700) 138 assert.NoError(t, err) 139 140 sawpFile2 := filepath.Join(dir, "memory.swap.max") 141 err = ioutil.WriteFile(sawpFile2, []byte{}, 0o700) 142 assert.NoError(t, err) 143 144 maxFile := filepath.Join(tmpDir, "memory.max") 145 err = ioutil.WriteFile(maxFile, []byte("12800"), 0o700) 146 assert.NoError(t, err) 147 148 curFile := filepath.Join(tmpDir, "memory.current") 149 err = ioutil.WriteFile(curFile, []byte("12600"), 0o700) 150 assert.NoError(t, err) 151 152 err = SetSwapMaxWithAbsolutePathRecursive(tmpDir) 153 assert.NoError(t, err) 154 155 s, err := ioutil.ReadFile(sawpFile) 156 assert.NoError(t, err) 157 assert.Equal(t, fmt.Sprintf("%v", 200), string(s)) 158 159 s, err = ioutil.ReadFile(sawpFile2) 160 assert.NoError(t, err) 161 assert.Equal(t, fmt.Sprintf("%v", math.MaxInt64), string(s)) 162 163 err = DisableSwapMaxWithAbsolutePathRecursive(tmpDir) 164 assert.NoError(t, err) 165 166 s, err = ioutil.ReadFile(sawpFile) 167 assert.NoError(t, err) 168 assert.Equal(t, fmt.Sprintf("%v", 0), string(s)) 169 }