Debian Codegraph @

internal/cgroups/cgroups2_test.go

raw symbols SCIP
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 cgroupsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/
25
26 import (
27 "osscip-go gomod github.com/golang/go/src go1.18 os/"
28 "os/userscip-go gomod github.com/golang/go/src go1.18 `os/user`/"
29 "path/filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/"
30 "testingscip-go gomod github.com/golang/go/src go1.18 testing/"
31
32 "github.com/stretchr/testify/assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/"
33 "github.com/stretchr/testify/requirescip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/"
34 )
35
36 func TestCGroupsIsCGroupV2scip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupsIsCGroupV2().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 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.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
66 mountInfoPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", tt.name)
67 procCgroupPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", "cgroup-root")
68 _, err := newCGroups2Fromscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/newCGroups2From().(mountInfoPath, procCgroupPath)
69 switch {
70 case tt.wantErr:
71 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Error().(t, err)
72 case !tt.isV2:
73 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.ErrorIsscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/ErrorIs().(t, err, ErrNotV2scip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/ErrNotV2.)
74 default:
75 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/NoError().(t, err)
76 }
77 })
78 }
79 }
80
81 func TestCGroupsCPUQuotaV2scip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupsCPUQuotaV2().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 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 := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataCGroupsPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataCGroupsPath., "v2")
135 for _, tt := range tests {
136 t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(tt.name, func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
137 quota, defined, err := (&CGroups2scip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#{
138 mountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#mountPoint.: mountPoint,
139 groupPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#groupPath.: "/",
140 cpuMaxFilescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#cpuMaxFile.: tt.name,
141 }).CPUQuotascip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#CPUQuota().()
142
143 if len(tt.wantErr) > 0 {
144 requirescip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/Error().(t, err, tt.name)
145 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Containsscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Contains().(t, err.Error(), tt.wantErr)
146 } else {
147 requirescip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/NoError().(t, err, tt.name)
148 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.want, quota, tt.name)
149 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.wantOK, defined, tt.name)
150 }
151 })
152 }
153 }
154
155 func TestCGroup2GroupPathDiscoveryscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroup2GroupPathDiscovery().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 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.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().(tt.procCgroup, func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
172 mountInfoPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", "mountinfo-v2")
173 procCgroupPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", tt.procCgroup)
174 cgroups, err := newCGroups2Fromscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/newCGroups2From().(mountInfoPath, procCgroupPath)
175 requirescip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/NoError().(t, err)
176 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Equalscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Equal().(t, tt.wantPath, cgroups.groupPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#groupPath.)
177 })
178 }
179 }
180
181 func TestCGroup2GroupPathDiscovery_Errorsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroup2GroupPathDiscovery_Errors().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
182 t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("no matching subsystem", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
183 mountInfoPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", "mountinfo-v2")
184 procCgroupPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", "cgroup-no-match")
185 _, err := newCGroups2Fromscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/newCGroups2From().(mountInfoPath, procCgroupPath)
186 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.ErrorIsscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/ErrorIs().(t, err, ErrNotV2scip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/ErrNotV2.)
187 })
188
189 t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("invalid subsystems", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
190 mountInfoPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", "mountinfo-v2")
191 procCgroupPath := filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(testDataProcPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/testDataProcPath., "v2", "cgroup-invalid")
192 _, err := newCGroups2Fromscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/newCGroups2From().(mountInfoPath, procCgroupPath)
193 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Containsscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Contains().(t, err.Error(), "invalid format for CGroupSubsys")
194 })
195 }
196
197 func TestCGroupsCPUQuotaV2_OtherErrorsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/TestCGroupsCPUQuotaV2_OtherErrors().(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
198 t.Runscip-go gomod github.com/golang/go/src go1.18 testing/T#Run().("no permissions to open", func(t *testingscip-go gomod github.com/golang/go/src go1.18 testing/.Tscip-go gomod github.com/golang/go/src go1.18 testing/T#) {
199 if u, err := userscip-go gomod github.com/golang/go/src go1.18 `os/user`/.Currentscip-go gomod github.com/golang/go/src go1.18 `os/user`/Current().(); err == nil && u.Uidscip-go gomod github.com/golang/go/src go1.18 `os/user`/User#Uid. == "0" {
200 t.Skipscip-go gomod github.com/golang/go/src go1.18 testing/common#Skip().("running as root, test skipped")
201 }
202
203 t.Parallelscip-go gomod github.com/golang/go/src go1.18 testing/T#Parallel().()
204
205 const name = "foo"
206
207 mountPoint := t.TempDirscip-go gomod github.com/golang/go/src go1.18 testing/common#TempDir().()
208 requirescip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/.NoErrorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/NoError().(t, osscip-go gomod github.com/golang/go/src go1.18 os/.WriteFilescip-go gomod github.com/golang/go/src go1.18 os/WriteFile().(filepathscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.18 `path/filepath`/Join().(mountPoint, name), nil /* write only*/, 0222))
209
210 _, _, err := (&CGroups2scip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#{mountPointscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#mountPoint.: mountPoint, groupPathscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#groupPath.: "/", cpuMaxFilescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#cpuMaxFile.: name}).CPUQuotascip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/cgroups`/CGroups2#CPUQuota().()
211 requirescip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/.Errorscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/require`/Error().(t, err)
212 assertscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/.Containsscip-go gomod github.com/stretchr/testify v1.7.1 `github.com/stretchr/testify/assert`/Contains().(t, err.Error(), "permission denied")
213 })
214 }