github.com/minio/madmin-go@v1.7.5/cgroup/linux_test.go (about) 1 //go:build linux 2 // +build linux 3 4 // 5 // MinIO Object Storage (c) 2022 MinIO, Inc. 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 cgroup 21 22 import ( 23 "io/ioutil" 24 "os" 25 "testing" 26 ) 27 28 // Testing parsing correctness for various process cgroup files. 29 func TestProcCGroup(t *testing.T) { 30 tmpPath, err := ioutil.TempFile("", "cgroup") 31 if err != nil { 32 t.Fatal(err) 33 } 34 defer os.Remove(tmpPath.Name()) 35 36 cgroup := ` 37 11:memory:/user.slice 38 10:blkio:/user.slice 39 9:hugetlb:/ 40 8:net_cls,net_prio:/ 41 7:perf_event:/ 42 6:pids:/user.slice/user-1000.slice 43 5:devices:/user.slice 44 4:cpuset:/ 45 3:cpu,cpuacct:/user.slice 46 2:freezer:/ 47 1:name=systemd:/user.slice/user-1000.slice/session-1.scope 48 ` 49 _, err = tmpPath.WriteString(cgroup) 50 if err != nil { 51 t.Fatal(err) 52 } 53 54 // Seek back to read from the beginning. 55 tmpPath.Seek(0, 0) 56 57 cg, err := parseProcCGroup(tmpPath) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 path := cg["memory"] 63 if len(path) == 0 { 64 t.Fatal("Path component cannot be empty") 65 } 66 67 if path != "/user.slice" { 68 t.Fatal("Path component cannot be empty") 69 } 70 71 path = cg["systemd"] 72 if path != "/user.slice/user-1000.slice/session-1.scope" { 73 t.Fatal("Path component cannot be empty") 74 } 75 76 // Mixed cgroups with different group names. 77 cgroup = ` 78 11:memory:/newtest/newtest 79 10:blkio:/user.slice 80 9:hugetlb:/ 81 8:net_cls,net_prio:/ 82 7:perf_event:/ 83 6:pids:/user.slice/user-1000.slice 84 5:devices:/user.slice 85 4:cpuset:/ 86 3:cpu,cpuacct:/newtest/newtest 87 2:freezer:/ 88 1:name=systemd:/user.slice/user-1000.slice/session-1.scope 89 ` 90 91 // Seek back to read from the beginning. 92 tmpPath.Seek(0, 0) 93 94 _, err = tmpPath.WriteString(cgroup) 95 if err != nil { 96 t.Fatal(err) 97 } 98 99 // Seek back to read from the beginning. 100 tmpPath.Seek(0, 0) 101 102 cg, err = parseProcCGroup(tmpPath) 103 if err != nil { 104 t.Fatal(err) 105 } 106 107 path = cg["memory"] 108 if path != "/newtest/newtest" { 109 t.Fatal("Path component cannot be empty") 110 } 111 112 path = cg["systemd"] 113 if path != "/user.slice/user-1000.slice/session-1.scope" { 114 t.Fatal("Path component cannot be empty") 115 } 116 } 117 118 // Tests cgroup memory limit path construction. 119 func TestMemoryLimitPath(t *testing.T) { 120 testCases := []struct { 121 cgroupPath string 122 expectedPath string 123 }{ 124 { 125 cgroupPath: "/user.slice", 126 expectedPath: "/sys/fs/cgroup/memory/user.slice/memory.limit_in_bytes", 127 }, 128 { 129 cgroupPath: "/docker/testing", 130 expectedPath: "/sys/fs/cgroup/memory/memory.limit_in_bytes", 131 }, 132 } 133 134 for i, testCase := range testCases { 135 actualPath := getMemoryLimitFilePath(testCase.cgroupPath) 136 if actualPath != testCase.expectedPath { 137 t.Fatalf("Test: %d: Expected: %s, got %s", i+1, testCase.expectedPath, actualPath) 138 } 139 } 140 }