github.com/google/cadvisor@v0.49.1/container/libcontainer/helpers_test.go (about) 1 // Copyright 2014 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package libcontainer 16 17 import ( 18 "os" 19 "path/filepath" 20 "reflect" 21 "strings" 22 "testing" 23 24 "github.com/opencontainers/runc/libcontainer/cgroups" 25 "github.com/stretchr/testify/assert" 26 ) 27 28 var defaultCgroupSubsystems = []string{ 29 "systemd", "freezer", "memory", "blkio", "hugetlb", "net_cls,net_prio", "pids", "cpu,cpuacct", "devices", "cpuset", "perf_events", 30 } 31 32 func cgroupMountsAt(path string, subsystems []string) []cgroups.Mount { 33 res := []cgroups.Mount{} 34 for _, subsystem := range subsystems { 35 res = append(res, cgroups.Mount{ 36 Root: "/", 37 Subsystems: strings.Split(subsystem, ","), 38 Mountpoint: filepath.Join(path, subsystem), 39 }) 40 } 41 return res 42 } 43 44 func TestGetCgroupSubsystems(t *testing.T) { 45 testCases := []struct { 46 mounts []cgroups.Mount 47 expected map[string]string 48 err bool 49 }{ 50 { 51 mounts: []cgroups.Mount{}, 52 err: true, 53 }, 54 { 55 // normal case 56 mounts: cgroupMountsAt("/sys/fs/cgroup", defaultCgroupSubsystems), 57 expected: map[string]string{ 58 "blkio": "/sys/fs/cgroup/blkio", 59 "cpu": "/sys/fs/cgroup/cpu,cpuacct", 60 "cpuacct": "/sys/fs/cgroup/cpu,cpuacct", 61 "cpuset": "/sys/fs/cgroup/cpuset", 62 "devices": "/sys/fs/cgroup/devices", 63 "memory": "/sys/fs/cgroup/memory", 64 "hugetlb": "/sys/fs/cgroup/hugetlb", 65 "pids": "/sys/fs/cgroup/pids", 66 }, 67 }, 68 { 69 // multiple croup subsystems, should ignore second one 70 mounts: append(cgroupMountsAt("/sys/fs/cgroup", defaultCgroupSubsystems), 71 cgroupMountsAt("/var/lib/rkt/pods/run/ccdd4e36-2d4c-49fd-8b94-4fb06133913d/stage1/rootfs/opt/stage2/flannel/rootfs/sys/fs/cgroup", defaultCgroupSubsystems)...), 72 expected: map[string]string{ 73 "blkio": "/sys/fs/cgroup/blkio", 74 "cpu": "/sys/fs/cgroup/cpu,cpuacct", 75 "cpuacct": "/sys/fs/cgroup/cpu,cpuacct", 76 "cpuset": "/sys/fs/cgroup/cpuset", 77 "devices": "/sys/fs/cgroup/devices", 78 "memory": "/sys/fs/cgroup/memory", 79 "hugetlb": "/sys/fs/cgroup/hugetlb", 80 "pids": "/sys/fs/cgroup/pids", 81 }, 82 }, 83 { 84 // most subsystems not mounted 85 mounts: cgroupMountsAt("/sys/fs/cgroup", []string{"cpu"}), 86 expected: map[string]string{ 87 "cpu": "/sys/fs/cgroup/cpu", 88 }, 89 }, 90 } 91 92 for i, testCase := range testCases { 93 subSystems, err := getCgroupSubsystemsHelper(testCase.mounts, nil) 94 if testCase.err { 95 if err == nil { 96 t.Fatalf("[case %d] Expected error but didn't get one", i) 97 } 98 continue 99 } 100 if err != nil { 101 t.Fatalf("[case %d] Expected no error, but got %v", i, err) 102 } 103 if !reflect.DeepEqual(testCase.expected, subSystems) { 104 t.Fatalf("[case %d] Expected %v == %v", i, testCase.expected, subSystems) 105 } 106 } 107 } 108 109 func getFileContent(t *testing.T, filePath string) string { 110 fileContent, err := os.ReadFile(filePath) 111 assert.Nil(t, err) 112 return string(fileContent) 113 } 114 115 func clearTestData(t *testing.T, clearRefsPaths []string) { 116 for _, clearRefsPath := range clearRefsPaths { 117 err := os.WriteFile(clearRefsPath, []byte("0\n"), 0o644) 118 assert.Nil(t, err) 119 } 120 }