Skip to content

Commit

Permalink
update the mock test
Browse files Browse the repository at this point in the history
Signed-off-by: jason yang <[email protected]>
  • Loading branch information
JasonYangShadow committed Oct 23, 2023
1 parent 41b55d9 commit 9fd26dc
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions internal/cgroup/cgroup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 9fd26dc

Please sign in to comment.