| 1 | // Copyright (c) 2022 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package cgroups |
| 25 | |
| 26 | import ( |
| 27 | "os" |
| 28 | "os/user" |
| 29 | "path/filepath" |
| 30 | "testing" |
| 31 | |
| 32 | "github.com/stretchr/testify/assert" |
| 33 | "github.com/stretchr/testify/require" |
| 34 | ) |
| 35 | |
| 36 | func TestCGroupsIsCGroupV2(t *testing.T) { |
| 37 | tests := []struct { |
| 38 | name string |
| 39 | isV2 bool |
| 40 | wantErr bool // should be false if isV2 is true |
| 41 | }{ |
| 42 | { |
| 43 | name: "mountinfo", |
| 44 | isV2: false, |
| 45 | wantErr: false, |
| 46 | }, |
| 47 | { |
| 48 | name: "mountinfo-v1-v2", |
| 49 | isV2: false, |
| 50 | wantErr: false, |
| 51 | }, |
| 52 | { |
| 53 | name: "mountinfo-v2", |
| 54 | isV2: true, |
| 55 | wantErr: false, |
| 56 | }, |
| 57 | { |
| 58 | name: "mountinfo-nonexistent", |
| 59 | isV2: false, |
| 60 | wantErr: true, |
| 61 | }, |
| 62 | } |
| 63 | |
| 64 | for _, tt := range tests { |
| 65 | t.Run(tt.name, func(t *testing.T) { |
| 66 | mountInfoPath := filepath.Join(testDataProcPath, "v2", tt.name) |
| 67 | procCgroupPath := filepath.Join(testDataProcPath, "v2", "cgroup-root") |
| 68 | _, err := newCGroups2From(mountInfoPath, procCgroupPath) |
| 69 | switch { |
| 70 | case tt.wantErr: |
| 71 | assert.Error(t, err) |
| 72 | case !tt.isV2: |
| 73 | assert.ErrorIs(t, err, ErrNotV2) |
| 74 | default: |
| 75 | assert.NoError(t, err) |
| 76 | } |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func TestCGroupsCPUQuotaV2(t *testing.T) { |
| 82 | tests := []struct { |
| 83 | name string |
| 84 | want float64 |
| 85 | wantOK bool |
| 86 | wantErr string |
| 87 | }{ |
| 88 | { |
| 89 | name: "set", |
| 90 | want: 2.5, |
| 91 | wantOK: true, |
| 92 | }, |
| 93 | { |
| 94 | name: "unset", |
| 95 | want: -1.0, |
| 96 | wantOK: false, |
| 97 | }, |
| 98 | { |
| 99 | name: "only-max", |
| 100 | want: 5.0, |
| 101 | wantOK: true, |
| 102 | }, |
| 103 | { |
| 104 | name: "invalid-max", |
| 105 | wantErr: `parsing "asdf": invalid syntax`, |
| 106 | }, |
| 107 | { |
| 108 | name: "invalid-period", |
| 109 | wantErr: `parsing "njn": invalid syntax`, |
| 110 | }, |
| 111 | { |
| 112 | name: "nonexistent", |
| 113 | want: -1.0, |
| 114 | wantOK: false, |
| 115 | }, |
| 116 | { |
| 117 | name: "empty", |
| 118 | wantErr: "unexpected EOF", |
| 119 | }, |
| 120 | { |
| 121 | name: "too-few-fields", |
| 122 | wantErr: "invalid format", |
| 123 | }, |
| 124 | { |
| 125 | name: "too-many-fields", |
| 126 | wantErr: "invalid format", |
| 127 | }, |
| 128 | { |
| 129 | name: "zero-period", |
| 130 | wantErr: "zero value for period is not allowed", |
| 131 | }, |
| 132 | } |
| 133 | |
| 134 | mountPoint := filepath.Join(testDataCGroupsPath, "v2") |
| 135 | for _, tt := range tests { |
| 136 | t.Run(tt.name, func(t *testing.T) { |
| 137 | quota, defined, err := (&CGroups2{ |
| 138 | mountPoint: mountPoint, |
| 139 | groupPath: "/", |
| 140 | cpuMaxFile: tt.name, |
| 141 | }).CPUQuota() |
| 142 | |
| 143 | if len(tt.wantErr) > 0 { |
| 144 | require.Error(t, err, tt.name) |
| 145 | assert.Contains(t, err.Error(), tt.wantErr) |
| 146 | } else { |
| 147 | require.NoError(t, err, tt.name) |
| 148 | assert.Equal(t, tt.want, quota, tt.name) |
| 149 | assert.Equal(t, tt.wantOK, defined, tt.name) |
| 150 | } |
| 151 | }) |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | func TestCGroup2GroupPathDiscovery(t *testing.T) { |
| 156 | tests := []struct { |
| 157 | procCgroup string |
| 158 | wantPath string |
| 159 | }{ |
| 160 | { |
| 161 | procCgroup: "cgroup-root", |
| 162 | wantPath: "/", |
| 163 | }, |
| 164 | { |
| 165 | procCgroup: "cgroup-subdir", |
| 166 | wantPath: "/Example", |
| 167 | }, |
| 168 | } |
| 169 | |
| 170 | for _, tt := range tests { |
| 171 | t.Run(tt.procCgroup, func(t *testing.T) { |
| 172 | mountInfoPath := filepath.Join(testDataProcPath, "v2", "mountinfo-v2") |
| 173 | procCgroupPath := filepath.Join(testDataProcPath, "v2", tt.procCgroup) |
| 174 | cgroups, err := newCGroups2From(mountInfoPath, procCgroupPath) |
| 175 | require.NoError(t, err) |
| 176 | assert.Equal(t, tt.wantPath, cgroups.groupPath) |
| 177 | }) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | func TestCGroup2GroupPathDiscovery_Errors(t *testing.T) { |
| 182 | t.Run("no matching subsystem", func(t *testing.T) { |
| 183 | mountInfoPath := filepath.Join(testDataProcPath, "v2", "mountinfo-v2") |
| 184 | procCgroupPath := filepath.Join(testDataProcPath, "v2", "cgroup-no-match") |
| 185 | _, err := newCGroups2From(mountInfoPath, procCgroupPath) |
| 186 | assert.ErrorIs(t, err, ErrNotV2) |
| 187 | }) |
| 188 | |
| 189 | t.Run("invalid subsystems", func(t *testing.T) { |
| 190 | mountInfoPath := filepath.Join(testDataProcPath, "v2", "mountinfo-v2") |
| 191 | procCgroupPath := filepath.Join(testDataProcPath, "v2", "cgroup-invalid") |
| 192 | _, err := newCGroups2From(mountInfoPath, procCgroupPath) |
| 193 | assert.Contains(t, err.Error(), "invalid format for CGroupSubsys") |
| 194 | }) |
| 195 | } |
| 196 | |
| 197 | func TestCGroupsCPUQuotaV2_OtherErrors(t *testing.T) { |
| 198 | t.Run("no permissions to open", func(t *testing.T) { |
| 199 | if u, err := user.Current(); err == nil && u.Uid == "0" { |
| 200 | t.Skip("running as root, test skipped") |
| 201 | } |
| 202 | |
| 203 | t.Parallel() |
| 204 | |
| 205 | const name = "foo" |
| 206 | |
| 207 | mountPoint := t.TempDir() |
| 208 | require.NoError(t, os.WriteFile(filepath.Join(mountPoint, name), nil /* write only*/, 0222)) |
| 209 | |
| 210 | _, _, err := (&CGroups2{mountPoint: mountPoint, groupPath: "/", cpuMaxFile: name}).CPUQuota() |
| 211 | require.Error(t, err) |
| 212 | assert.Contains(t, err.Error(), "permission denied") |
| 213 | }) |
| 214 | } |