-
Notifications
You must be signed in to change notification settings - Fork 237
/
manager_test.go
361 lines (305 loc) · 9.45 KB
/
manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cgroup2
import (
"fmt"
"os"
"os/exec"
"syscall"
"testing"
"time"
"github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/goleak"
)
func setupForNewSystemd(t *testing.T) (cmd *exec.Cmd, group string) {
cmd = exec.Command("cat")
err := cmd.Start()
require.NoError(t, err, "failed to start cat process")
proc := cmd.Process
require.NotNil(t, proc, "process was nil")
group = fmt.Sprintf("testing-watcher-%d.scope", proc.Pid)
return
}
func TestErrorsWhenUnitAlreadyExists(t *testing.T) {
checkCgroupMode(t)
cmd, group := setupForNewSystemd(t)
proc := cmd.Process
_, err := NewSystemd("", group, proc.Pid, &Resources{})
require.NoError(t, err, "Failed to init new cgroup manager")
_, err = NewSystemd("", group, proc.Pid, &Resources{})
if err == nil {
t.Fatal("Expected recreating cgroup manager should fail")
} else if !isUnitExists(err) {
t.Fatalf("Failed to init cgroup manager with unexpected error: %s", err)
}
}
// kubelet relies on this behavior to make sure a slice exists
func TestIgnoreUnitExistsWhenPidNegativeOne(t *testing.T) {
checkCgroupMode(t)
cmd, group := setupForNewSystemd(t)
proc := cmd.Process
_, err := NewSystemd("", group, proc.Pid, &Resources{})
require.NoError(t, err, "Failed to init new cgroup manager")
_, err = NewSystemd("", group, -1, &Resources{})
require.NoError(t, err, "Expected to be able to recreate cgroup manager")
}
//nolint:staticcheck // Staticcheck false positives for nil pointer deference after t.Fatal
func TestEventChanCleanupOnCgroupRemoval(t *testing.T) {
checkCgroupMode(t)
cmd := exec.Command("cat")
stdin, err := cmd.StdinPipe()
require.NoError(t, err, "failed to create cat process")
err = cmd.Start()
require.NoError(t, err, "failed to start cat process")
proc := cmd.Process
require.NotNil(t, proc, "process was nil")
group := fmt.Sprintf("testing-watcher-%d.scope", proc.Pid)
c, err := NewSystemd("", group, proc.Pid, &Resources{})
require.NoError(t, err, "failed to init new cgroup manager")
evCh, errCh := c.EventChan()
// give event goroutine a chance to start
time.Sleep(500 * time.Millisecond)
err = stdin.Close()
require.NoError(t, err, "failed closing stdin")
err = cmd.Wait()
require.NoError(t, err, "failed waiting for cmd")
done := false
for !done {
select {
case <-evCh:
case err := <-errCh:
require.NoError(t, err, "unexpected error on error channel")
done = true
case <-time.After(5 * time.Second):
t.Fatal("Timed out")
}
}
goleak.VerifyNone(t)
}
func TestSystemdFullPath(t *testing.T) {
tests := []struct {
inputSlice string
inputGroup string
expectedOut string
}{
{
inputSlice: "user.slice",
inputGroup: "myGroup.slice",
expectedOut: "/sys/fs/cgroup/user.slice/myGroup.slice",
},
{
inputSlice: "/",
inputGroup: "myGroup.slice",
expectedOut: "/sys/fs/cgroup/myGroup.slice",
},
{
inputSlice: "system.slice",
inputGroup: "myGroup.slice",
expectedOut: "/sys/fs/cgroup/system.slice/myGroup.slice",
},
{
inputSlice: "user.slice",
inputGroup: "my-group.slice",
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice",
},
{
inputSlice: "user.slice",
inputGroup: "my-group-more-dashes.slice",
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice/my-group-more.slice/my-group-more-dashes.slice",
},
{
inputSlice: "user.slice",
inputGroup: "my-group-dashes.slice",
expectedOut: "/sys/fs/cgroup/user.slice/my.slice/my-group.slice/my-group-dashes.slice",
},
{
inputSlice: "user.slice",
inputGroup: "myGroup.scope",
expectedOut: "/sys/fs/cgroup/user.slice/myGroup.scope",
},
{
inputSlice: "user.slice",
inputGroup: "my-group-dashes.scope",
expectedOut: "/sys/fs/cgroup/user.slice/my-group-dashes.scope",
},
{
inputSlice: "test-waldo.slice",
inputGroup: "my-group.slice",
expectedOut: "/sys/fs/cgroup/test.slice/test-waldo.slice/my.slice/my-group.slice",
},
{
inputSlice: "test-waldo.slice",
inputGroup: "my.service",
expectedOut: "/sys/fs/cgroup/test.slice/test-waldo.slice/my.service",
},
}
for _, test := range tests {
actual := getSystemdFullPath(test.inputSlice, test.inputGroup)
assert.Equal(t, test.expectedOut, actual)
}
}
func TestKill(t *testing.T) {
checkCgroupMode(t)
manager, err := NewManager(defaultCgroup2Path, "/test1", ToResources(&specs.LinuxResources{}))
require.NoError(t, err)
t.Cleanup(func() {
_ = manager.Delete()
})
var (
procs []*exec.Cmd
numProcs = 5
)
for i := 0; i < numProcs; i++ {
cmd := exec.Command("sleep", "infinity")
// Don't leak the process if we fail to join the cg,
// send sigkill after tests over.
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
err = cmd.Start()
require.NoError(t, err)
err = manager.AddProc(uint64(cmd.Process.Pid))
require.NoError(t, err)
procs = append(procs, cmd)
}
// Verify we have 5 pids before beginning Kill below.
pids, err := manager.Procs(true)
require.NoError(t, err)
require.Len(t, pids, numProcs, "pid count unexpected")
threads, err := manager.Threads(true)
require.NoError(t, err)
require.Len(t, threads, numProcs, "pid count unexpected")
// Now run kill, and check that nothing is running after.
err = manager.Kill()
require.NoError(t, err)
done := make(chan struct{})
go func() {
for _, proc := range procs {
_ = proc.Wait()
}
done <- struct{}{}
}()
select {
case <-time.After(time.Second * 3):
t.Fatal("timed out waiting for processes to exit")
case <-done:
}
}
func TestMoveTo(t *testing.T) {
checkCgroupMode(t)
src, err := NewManager(defaultCgroup2Path, "/test-moveto-src", ToResources(&specs.LinuxResources{}))
require.NoError(t, err)
t.Cleanup(func() {
_ = src.Kill()
_ = src.Delete()
})
cmd := exec.Command("sleep", "infinity")
// Don't leak the process if we fail to join the cg,
// send sigkill after tests over.
cmd.SysProcAttr = &syscall.SysProcAttr{
Pdeathsig: syscall.SIGKILL,
}
err = cmd.Start()
require.NoError(t, err)
proc := cmd.Process.Pid
err = src.AddProc(uint64(proc))
require.NoError(t, err)
destination, err := NewManager(defaultCgroup2Path, "/test-moveto-dest", ToResources(&specs.LinuxResources{}))
require.NoError(t, err)
t.Cleanup(func() {
_ = destination.Kill()
_ = destination.Delete()
})
err = src.MoveTo(destination)
require.NoError(t, err)
desProcs, err := destination.Procs(true)
require.NoError(t, err)
desMap := make(map[int]bool)
for _, p := range desProcs {
desMap[int(p)] = true
}
if !desMap[proc] {
t.Fatalf("process %v not in destination cgroup", proc)
}
}
func TestCgroupType(t *testing.T) {
checkCgroupMode(t)
manager, err := NewManager(defaultCgroup2Path, "/test-type", ToResources(&specs.LinuxResources{}))
require.NoError(t, err)
submanager, err := NewManager(defaultCgroup2Path, "/test-type/sub", ToResources(&specs.LinuxResources{}))
require.NoError(t, err)
t.Cleanup(func() {
_ = submanager.Delete()
_ = manager.Delete()
})
// Check initial type is domain
cgType, err := manager.GetType()
require.NoError(t, err)
require.Equal(t, cgType, Domain)
// Swap sub cgroup to threaded
require.NoError(t, submanager.SetType(Threaded))
// Check sub cgroup type is threaded
cgType, err = submanager.GetType()
require.NoError(t, err)
require.Equal(t, cgType, Threaded)
// Check parent cgroup type is domain threaded
cgType, err = manager.GetType()
require.NoError(t, err)
require.Equal(t, cgType, DomainThreaded)
}
func TestCgroupv2PSIStats(t *testing.T) {
checkCgroupMode(t)
group := "/psi-test-cg"
groupPath := fmt.Sprintf("%s-%d", group, os.Getpid())
res := Resources{}
c, err := NewManager(defaultCgroup2Path, groupPath, &res)
require.NoError(t, err, "failed to init new cgroup manager")
t.Cleanup(func() {
os.Remove(c.path)
})
stats, err := c.Stat()
require.NoError(t, err, "failed to get cgroup stats")
if stats.CPU.PSI == nil || stats.Memory.PSI == nil || stats.Io.PSI == nil {
t.Error("expected psi not nil but got nil")
}
}
func TestSystemdCgroupPSIController(t *testing.T) {
checkCgroupMode(t)
group := fmt.Sprintf("testing-psi-%d.scope", os.Getpid())
pid := os.Getpid()
res := Resources{}
c, err := NewSystemd("", group, pid, &res)
require.NoError(t, err, "failed to init new cgroup systemd manager")
stats, err := c.Stat()
require.NoError(t, err, "failed to get cgroup stats")
if stats.CPU.PSI == nil || stats.Memory.PSI == nil || stats.Io.PSI == nil {
t.Error("expected psi not nil but got nil")
}
}
func BenchmarkStat(b *testing.B) {
checkCgroupMode(b)
group := "/stat-test-cg"
groupPath := fmt.Sprintf("%s-%d", group, os.Getpid())
c, err := NewManager(defaultCgroup2Path, groupPath, &Resources{})
require.NoErrorf(b, err, "failed to init new cgroup manager")
b.Cleanup(func() {
_ = c.Delete()
})
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := c.Stat()
require.NoError(b, err)
}
}