Repository navigation
Expand file tree
/
Copy pathcgroup_test.go
More file actions
328 lines (296 loc) · 10.2 KB
/
Copy pathcgroup_test.go
File metadata and controls
328 lines (296 loc) · 10.2 KB
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
package systats
import "testing"
func TestDetectCgroupVersion(t *testing.T) {
cases := []struct {
name string
root string
want cgroupVersion
}{
{"v2", "./test_files/cgroup_v2", cgroupV2},
{"v1", "./test_files/cgroup_v1", cgroupV1},
{"none", "./test_files/does_not_exist", cgroupNone},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got := detectCgroupVersion(c.root)
if got != c.want {
t.Errorf("detectCgroupVersion(%q) = %v, want %v", c.root, got, c.want)
}
})
}
}
func TestParseSelfCgroupV2(t *testing.T) {
got, err := parseSelfCgroupV2("0::/foo\n")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != "/foo" {
t.Errorf("got %q, want %q", got, "/foo")
}
if _, err := parseSelfCgroupV2("garbage"); err == nil {
t.Errorf("expected error for malformed content, got nil")
}
}
func TestParseSelfCgroupV1(t *testing.T) {
content := "11:memory:/\n5:cpu,cpuacct:/\n"
cgPath, controllerDir, err := parseSelfCgroupV1(content, "memory")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cgPath != "/" || controllerDir != "memory" {
t.Errorf("memory: got (%q, %q), want (\"/\", \"memory\")", cgPath, controllerDir)
}
for _, controller := range []string{"cpu", "cpuacct"} {
cgPath, controllerDir, err := parseSelfCgroupV1(content, controller)
if err != nil {
t.Fatalf("unexpected error for %q: %v", controller, err)
}
if cgPath != "/" || controllerDir != "cpu,cpuacct" {
t.Errorf("%s: got (%q, %q), want (\"/\", \"cpu,cpuacct\")", controller, cgPath, controllerDir)
}
}
if _, _, err := parseSelfCgroupV1(content, "cpuset"); err == nil {
t.Errorf("expected error for missing controller, got nil")
}
}
func TestResolveCgroupControllerPath(t *testing.T) {
v2 := &SyStats{CgroupRootPath: "./test_files/cgroup_v2", SelfCgroupPath: "./test_files/cgroup_v2/self_cgroup.txt"}
dir, ok := resolveCgroupControllerPath(v2, cgroupV2, "")
if !ok || dir != "test_files/cgroup_v2" {
t.Errorf("v2: got (%q, %v), want (\"test_files/cgroup_v2\", true)", dir, ok)
}
v1 := &SyStats{CgroupRootPath: "./test_files/cgroup_v1", SelfCgroupPath: "./test_files/cgroup_v1/self_cgroup.txt"}
dir, ok = resolveCgroupControllerPath(v1, cgroupV1, "memory")
if !ok || dir != "test_files/cgroup_v1/memory" {
t.Errorf("v1 memory: got (%q, %v), want (\"test_files/cgroup_v1/memory\", true)", dir, ok)
}
dir, ok = resolveCgroupControllerPath(v1, cgroupV1, "cpuacct")
if !ok || dir != "test_files/cgroup_v1/cpu,cpuacct" {
t.Errorf("v1 cpuacct: got (%q, %v), want (\"test_files/cgroup_v1/cpu,cpuacct\", true)", dir, ok)
}
}
func TestReadCgroupMemoryLimit(t *testing.T) {
cases := []struct {
name string
version cgroupVersion
dir string
wantLimit uint64
wantLimited bool
}{
{"v2 limited", cgroupV2, "./test_files/cgroup_v2", 536870912, true},
{"v2 unlimited", cgroupV2, "./test_files/cgroup_v2_unlimited", 0, false},
{"v1 limited", cgroupV1, "./test_files/cgroup_v1/memory", 536870912, true},
{"v1 unlimited", cgroupV1, "./test_files/cgroup_v1_unlimited/memory", 0, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
limit, limited, err := readCgroupMemoryLimit(c.version, c.dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if limited != c.wantLimited {
t.Errorf("limited = %v, want %v", limited, c.wantLimited)
}
if c.wantLimited && limit != c.wantLimit {
t.Errorf("limit = %d, want %d", limit, c.wantLimit)
}
})
}
}
func TestReadCgroupMemoryUsage(t *testing.T) {
usage, err := readCgroupMemoryUsage(cgroupV2, "./test_files/cgroup_v2")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if usage != 104857600 {
t.Errorf("v2 usage = %d, want %d", usage, 104857600)
}
usage, err = readCgroupMemoryUsage(cgroupV1, "./test_files/cgroup_v1/memory")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if usage != 104857600 {
t.Errorf("v1 usage = %d, want %d", usage, 104857600)
}
}
func TestReadCgroupMemoryInactiveFileBytes(t *testing.T) {
// v2 has only "inactive_file".
got := readCgroupMemoryInactiveFileBytes(cgroupV2, "./test_files/cgroup_v2")
if got != 10485760 {
t.Errorf("v2 = %d, want %d", got, 10485760)
}
// v1 fixture has both inactive_file (9999999) and total_inactive_file
// (10485760) with different values specifically to prove
// total_inactive_file wins.
got = readCgroupMemoryInactiveFileBytes(cgroupV1, "./test_files/cgroup_v1/memory")
if got != 10485760 {
t.Errorf("v1 = %d, want %d (total_inactive_file should be preferred)", got, 10485760)
}
}
func TestApplyCgroupMemory(t *testing.T) {
cases := []struct {
name string
systats *SyStats
wantLimited bool
wantTotal uint64 // KiB
wantUsed uint64 // KiB
}{
{
name: "v2 limited",
systats: &SyStats{
ContainerAware: true,
CgroupRootPath: "./test_files/cgroup_v2",
SelfCgroupPath: "./test_files/cgroup_v2/self_cgroup.txt",
},
wantLimited: true,
wantTotal: 536870912 / 1024,
wantUsed: (104857600 - 10485760) / 1024,
},
{
name: "v1 limited",
systats: &SyStats{
ContainerAware: true,
CgroupRootPath: "./test_files/cgroup_v1",
SelfCgroupPath: "./test_files/cgroup_v1/self_cgroup.txt",
},
wantLimited: true,
wantTotal: 536870912 / 1024,
wantUsed: (104857600 - 10485760) / 1024,
},
{
name: "v2 unlimited falls back",
systats: &SyStats{
ContainerAware: true,
CgroupRootPath: "./test_files/cgroup_v2_unlimited",
SelfCgroupPath: "./test_files/cgroup_v2_unlimited/self_cgroup.txt",
},
wantLimited: false,
},
{
name: "ContainerAware false ignores a fully valid limited fixture",
systats: &SyStats{
ContainerAware: false,
CgroupRootPath: "./test_files/cgroup_v2",
SelfCgroupPath: "./test_files/cgroup_v2/self_cgroup.txt",
},
wantLimited: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
m := &memoryKiB{total: 999, used: 111} // sentinel host-wide values
applyCgroupMemory(m, c.systats)
if m.limited != c.wantLimited {
t.Errorf("limited = %v, want %v", m.limited, c.wantLimited)
}
if !c.wantLimited {
if m.total != 999 || m.used != 111 {
t.Errorf("expected host-wide sentinel values untouched, got total=%d used=%d", m.total, m.used)
}
return
}
if m.total != c.wantTotal {
t.Errorf("total = %d KiB, want %d KiB", m.total, c.wantTotal)
}
if m.used != c.wantUsed {
t.Errorf("used = %d KiB, want %d KiB", m.used, c.wantUsed)
}
if m.available != m.free {
t.Errorf("available (%d) and free (%d) should be equal when limited", m.available, m.free)
}
if m.available != m.total-m.used {
t.Errorf("available = %d, want total-used = %d", m.available, m.total-m.used)
}
})
}
}
func TestReadCgroupCPUQuota(t *testing.T) {
cases := []struct {
name string
version cgroupVersion
dir string
wantCores float64
wantOK bool
}{
{"v2 limited (0.5 cores)", cgroupV2, "./test_files/cgroup_v2", 0.5, true},
{"v2 unlimited", cgroupV2, "./test_files/cgroup_v2_unlimited", 0, false},
{"v1 limited (0.5 cores)", cgroupV1, "./test_files/cgroup_v1/cpu,cpuacct", 0.5, true},
{"v1 unlimited", cgroupV1, "./test_files/cgroup_v1_unlimited/cpu,cpuacct", 0, false},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
cores, limited, err := readCgroupCPUQuota(c.version, c.dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if limited != c.wantOK {
t.Errorf("limited = %v, want %v", limited, c.wantOK)
}
if c.wantOK && cores != c.wantCores {
t.Errorf("cores = %v, want %v", cores, c.wantCores)
}
})
}
}
func TestParseCPUStatUsageUsec(t *testing.T) {
value, ok := parseCPUStatUsageUsec("usage_usec 1234567\nuser_usec 1000000\n")
if !ok || value != 1234567 {
t.Errorf("got (%d, %v), want (1234567, true)", value, ok)
}
_, ok = parseCPUStatUsageUsec("user_usec 1000000\n")
if ok {
t.Errorf("expected ok=false when usage_usec is missing")
}
}
func TestApplyCgroupCPU(t *testing.T) {
info := cgroupCPUInfo{ok: true, limited: true, cores: 0.5}
output := &CPU{LoadAvg: 999, CoreAvg: []int{1, 2, 3}}
// 0.3 cores used out of 0.5 allocated over 1 second => 60%
applyCgroupCPU(output, info, 10.0, 10.3, true, true, 1.0)
if !output.Limited {
t.Errorf("Limited = false, want true")
}
if output.AllocatedCores != 0.5 {
t.Errorf("AllocatedCores = %v, want 0.5", output.AllocatedCores)
}
if output.LoadAvg != 60 {
t.Errorf("LoadAvg = %d, want 60", output.LoadAvg)
}
if output.CoreAvg != nil {
t.Errorf("CoreAvg = %v, want nil", output.CoreAvg)
}
// Unlimited: Limited/AllocatedCores set, but host-wide LoadAvg/CoreAvg
// left untouched.
unlimitedInfo := cgroupCPUInfo{ok: true, limited: false, cores: 0}
output2 := &CPU{LoadAvg: 42, CoreAvg: []int{1, 2}}
applyCgroupCPU(output2, unlimitedInfo, 0, 0, true, true, 1.0)
if output2.Limited {
t.Errorf("Limited = true, want false")
}
if output2.LoadAvg != 42 || output2.CoreAvg == nil {
t.Errorf("host-wide LoadAvg/CoreAvg should be untouched when not limited, got LoadAvg=%d CoreAvg=%v", output2.LoadAvg, output2.CoreAvg)
}
// !info.ok: nothing touched at all.
output3 := &CPU{LoadAvg: 7}
applyCgroupCPU(output3, cgroupCPUInfo{ok: false}, 0, 0, true, true, 1.0)
if output3.Limited || output3.LoadAvg != 7 {
t.Errorf("expected no changes when info.ok is false, got %+v", output3)
}
}
func TestResolveCgroupCPU(t *testing.T) {
v2 := &SyStats{ContainerAware: true, CgroupRootPath: "./test_files/cgroup_v2", SelfCgroupPath: "./test_files/cgroup_v2/self_cgroup.txt"}
info := resolveCgroupCPU(v2)
if !info.ok || !info.limited || info.cores != 0.5 {
t.Errorf("v2: got %+v, want ok=true limited=true cores=0.5", info)
}
v1 := &SyStats{ContainerAware: true, CgroupRootPath: "./test_files/cgroup_v1", SelfCgroupPath: "./test_files/cgroup_v1/self_cgroup.txt"}
info = resolveCgroupCPU(v1)
if !info.ok || !info.limited || info.cores != 0.5 {
t.Errorf("v1: got %+v, want ok=true limited=true cores=0.5", info)
}
none := &SyStats{ContainerAware: true, CgroupRootPath: "./test_files/does_not_exist", SelfCgroupPath: "./test_files/does_not_exist/self_cgroup.txt"}
info = resolveCgroupCPU(none)
if info.ok {
t.Errorf("expected ok=false for a nonexistent cgroup root, got %+v", info)
}
}