diff --git a/internal/cgroup/cgroup_test.go b/internal/cgroup/cgroup_test.go index a8f154e..792e8f2 100644 --- a/internal/cgroup/cgroup_test.go +++ b/internal/cgroup/cgroup_test.go @@ -15,61 +15,90 @@ type MockCgroupManager struct { mock.Mock } -func (m *MockCgroupManager) Apply(_ int) error { - return nil +func (m *MockCgroupManager) Apply(pid int) error { + args := m.Called(pid) + return args.Error(0) } func (m *MockCgroupManager) GetPids() ([]int, error) { - return []int{}, nil + args := m.Called() + return args.Get(0).([]int), args.Error(1) } func (m *MockCgroupManager) GetAllPids() ([]int, error) { - return []int{}, nil + args := m.Called() + return args.Get(0).([]int), args.Error(1) } func (m *MockCgroupManager) GetStats() (*cgroups.Stats, error) { - return cgroups.NewStats(), nil + args := m.Called() + return args.Get(0).(*cgroups.Stats), args.Error(1) } -func (m *MockCgroupManager) Freeze(_ configs.FreezerState) error { - return nil +func (m *MockCgroupManager) Freeze(state configs.FreezerState) error { + args := m.Called(state) + return args.Error(0) } func (m *MockCgroupManager) Destroy() error { - return nil + args := m.Called() + return args.Error(0) } func (m *MockCgroupManager) Path(path string) string { - return path + args := m.Called(path) + return args.String(0) } -func (m *MockCgroupManager) Set(_ *configs.Resources) error { - return nil +func (m *MockCgroupManager) Set(resource *configs.Resources) error { + args := m.Called(resource) + return args.Error(0) } func (m *MockCgroupManager) GetPaths() map[string]string { - return map[string]string{} + args := m.Called() + return args.Get(0).(map[string]string) } func (m *MockCgroupManager) GetCgroups() (*configs.Cgroup, error) { - return nil, nil + args := m.Called() + return args.Get(0).(*configs.Cgroup), args.Error(1) } func (m *MockCgroupManager) GetFreezerState() (configs.FreezerState, error) { - return "", nil + args := m.Called() + return args.Get(0).(configs.FreezerState), args.Error(1) } func (m *MockCgroupManager) Exists() bool { - return true + args := m.Called() + return args.Bool(0) } func (m *MockCgroupManager) OOMKillCount() (uint64, error) { - return 0, nil + args := m.Called() + return uint64(args.Int(0)), args.Error(1) } func TestCgroup(t *testing.T) { + mockManager := new(MockCgroupManager) + // set mock behaviors + mockManager.On("Apply", mock.Anything).Return(nil) + mockManager.On("GetPids").Return([]int{}, nil) + mockManager.On("GetAllPids").Return([]int{}, nil) + mockManager.On("GetStats").Return(cgroups.NewStats(), nil) + mockManager.On("Freeze", mock.Anything).Return(nil) + mockManager.On("Destroy").Return(nil) + mockManager.On("Path", mock.Anything).Return("") + mockManager.On("Set", mock.Anything).Return(nil) + mockManager.On("GetPaths").Return(map[string]string{}) + mockManager.On("GetCgroups").Return(nil, nil) + mockManager.On("GetFreezerState").Return("", nil) + mockManager.On("Exists").Return(false) + mockManager.On("OOMKillCount").Return(0, nil) + cgroup := &cgroup.CGroup{ - Manager: &MockCgroupManager{}, + Manager: mockManager, } has, err := cgroup.HasProcess()