Skip to content

Commit 7dfff93

Browse files
committed
test(sysmonitor): refactor memory tests to use a helper function to fix dupl error
1 parent 604b5b5 commit 7dfff93

File tree

1 file changed

+64
-80
lines changed

1 file changed

+64
-80
lines changed

internal/sysmonitor/memory_linux_cgroup_test.go

Lines changed: 64 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,60 @@ func TestReadCgroupStatWithFS(t *testing.T) {
259259
}
260260
}
261261

262+
// cgroupMemoryTestCase represents a test case for readCgroupMemoryWithFS
263+
type cgroupMemoryTestCase struct {
264+
name string
265+
usage string
266+
limit string
267+
stat string
268+
wantTotal uint64
269+
wantAvailable uint64
270+
wantErr bool
271+
errContains string
272+
}
273+
274+
// testCgroupMemoryCase is a helper function to test readCgroupMemoryWithFS with different file paths and configs
275+
func testCgroupMemoryCase(
276+
t *testing.T,
277+
tt cgroupMemoryTestCase,
278+
usagePath, limitPath, statPath string,
279+
config cgroupMemoryConfig,
280+
) {
281+
t.Helper()
282+
t.Run(tt.name, func(t *testing.T) {
283+
fs := newMockFileSystem()
284+
if tt.usage != "" {
285+
fs.files[usagePath] = []byte(tt.usage)
286+
}
287+
if tt.limit != "" {
288+
fs.files[limitPath] = []byte(tt.limit)
289+
}
290+
if tt.stat != "" {
291+
fs.files[statPath] = []byte(tt.stat)
292+
}
293+
294+
got, err := readCgroupMemoryWithFS(fs, config)
295+
if (err != nil) != tt.wantErr {
296+
t.Errorf("readCgroupMemoryWithFS() error = %v, wantErr %v", err, tt.wantErr)
297+
return
298+
}
299+
if tt.wantErr {
300+
if err != nil && tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
301+
t.Errorf("readCgroupMemoryWithFS() error = %v, want error containing %q", err, tt.errContains)
302+
}
303+
} else {
304+
if got.Total != tt.wantTotal {
305+
t.Errorf("readCgroupMemoryWithFS() Total = %v, want %v", got.Total, tt.wantTotal)
306+
}
307+
if got.Available != tt.wantAvailable {
308+
t.Errorf("readCgroupMemoryWithFS() Available = %v, want %v", got.Available, tt.wantAvailable)
309+
}
310+
}
311+
})
312+
}
313+
262314
func TestReadCgroupMemoryWithFS_V2(t *testing.T) {
263-
tests := []struct {
264-
name string
265-
usage string
266-
limit string
267-
stat string
268-
wantTotal uint64
269-
wantAvailable uint64
270-
wantErr bool
271-
errContains string
272-
}{
315+
tests := []cgroupMemoryTestCase{
273316
{
274317
name: "normal case",
275318
usage: "1073741824", // 1GB
@@ -348,50 +391,16 @@ func TestReadCgroupMemoryWithFS_V2(t *testing.T) {
348391
}
349392

350393
for _, tt := range tests {
351-
t.Run(tt.name, func(t *testing.T) {
352-
fs := newMockFileSystem()
353-
if tt.usage != "" {
354-
fs.files["/sys/fs/cgroup/memory.current"] = []byte(tt.usage)
355-
}
356-
if tt.limit != "" {
357-
fs.files["/sys/fs/cgroup/memory.max"] = []byte(tt.limit)
358-
}
359-
if tt.stat != "" {
360-
fs.files["/sys/fs/cgroup/memory.stat"] = []byte(tt.stat)
361-
}
362-
363-
got, err := readCgroupMemoryWithFS(fs, cgroupV2Config)
364-
if (err != nil) != tt.wantErr {
365-
t.Errorf("readCgroupMemoryWithFS() error = %v, wantErr %v", err, tt.wantErr)
366-
return
367-
}
368-
if tt.wantErr {
369-
if err != nil && tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
370-
t.Errorf("readCgroupMemoryWithFS() error = %v, want error containing %q", err, tt.errContains)
371-
}
372-
} else {
373-
if got.Total != tt.wantTotal {
374-
t.Errorf("readCgroupMemoryWithFS() Total = %v, want %v", got.Total, tt.wantTotal)
375-
}
376-
if got.Available != tt.wantAvailable {
377-
t.Errorf("readCgroupMemoryWithFS() Available = %v, want %v", got.Available, tt.wantAvailable)
378-
}
379-
}
380-
})
394+
testCgroupMemoryCase(t, tt,
395+
"/sys/fs/cgroup/memory.current",
396+
"/sys/fs/cgroup/memory.max",
397+
"/sys/fs/cgroup/memory.stat",
398+
cgroupV2Config)
381399
}
382400
}
383401

384402
func TestReadCgroupMemoryWithFS_V1(t *testing.T) {
385-
tests := []struct {
386-
name string
387-
usage string
388-
limit string
389-
stat string
390-
wantTotal uint64
391-
wantAvailable uint64
392-
wantErr bool
393-
errContains string
394-
}{
403+
tests := []cgroupMemoryTestCase{
395404
{
396405
name: "normal case",
397406
usage: "1073741824", // 1GB
@@ -432,36 +441,11 @@ func TestReadCgroupMemoryWithFS_V1(t *testing.T) {
432441
}
433442

434443
for _, tt := range tests {
435-
t.Run(tt.name, func(t *testing.T) {
436-
fs := newMockFileSystem()
437-
if tt.usage != "" {
438-
fs.files["/sys/fs/cgroup/memory/memory.usage_in_bytes"] = []byte(tt.usage)
439-
}
440-
if tt.limit != "" {
441-
fs.files["/sys/fs/cgroup/memory/memory.limit_in_bytes"] = []byte(tt.limit)
442-
}
443-
if tt.stat != "" {
444-
fs.files["/sys/fs/cgroup/memory/memory.stat"] = []byte(tt.stat)
445-
}
446-
447-
got, err := readCgroupMemoryWithFS(fs, cgroupV1Config)
448-
if (err != nil) != tt.wantErr {
449-
t.Errorf("readCgroupMemoryWithFS() error = %v, wantErr %v", err, tt.wantErr)
450-
return
451-
}
452-
if tt.wantErr {
453-
if err != nil && tt.errContains != "" && !strings.Contains(err.Error(), tt.errContains) {
454-
t.Errorf("readCgroupMemoryWithFS() error = %v, want error containing %q", err, tt.errContains)
455-
}
456-
} else {
457-
if got.Total != tt.wantTotal {
458-
t.Errorf("readCgroupMemoryWithFS() Total = %v, want %v", got.Total, tt.wantTotal)
459-
}
460-
if got.Available != tt.wantAvailable {
461-
t.Errorf("readCgroupMemoryWithFS() Available = %v, want %v", got.Available, tt.wantAvailable)
462-
}
463-
}
464-
})
444+
testCgroupMemoryCase(t, tt,
445+
"/sys/fs/cgroup/memory/memory.usage_in_bytes",
446+
"/sys/fs/cgroup/memory/memory.limit_in_bytes",
447+
"/sys/fs/cgroup/memory/memory.stat",
448+
cgroupV1Config)
465449
}
466450
}
467451

0 commit comments

Comments
 (0)