Skip to content

Commit

Permalink
Merge pull request #7 from apptainer/update_mock_test
Browse files Browse the repository at this point in the history
update the mock test
  • Loading branch information
JasonYangShadow authored Oct 23, 2023
2 parents 41b55d9 + d770f3c commit 8f6f5df
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 24 deletions.
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ linters:
- errcheck
- thelper
- usestdlibvars
- deadcode
- varcheck
- structcheck
- protogetter

linters-settings:
goheader:
Expand Down
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
2 changes: 1 addition & 1 deletion internal/cgroup/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

type Marshal interface {
Marshal(*bytes.Buffer) (*bytes.Buffer, error)
Marshal(buffer *bytes.Buffer) (*bytes.Buffer, error)
}

type StatManager struct {
Expand Down
12 changes: 6 additions & 6 deletions internal/cgroup/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ func TestParser(t *testing.T) {
require.Len(t, allFuncs, 5)

usage := allFuncs[0]()
require.Equal(t, 0.0, usage["cpu_usage_per"])
require.InEpsilon(t, 0.01, usage["cpu_usage_per"], 1)

usage = allFuncs[1]()
require.Equal(t, 0.0, usage["memory_usage_per"])
require.InEpsilon(t, 0.01, usage["memory_usage_per"], 1)

usage = allFuncs[2]()
require.Equal(t, 0.0, usage["memory_swap_usage_per"])
require.InEpsilon(t, 0.01, usage["memory_swap_usage_per"], 1)

usage = allFuncs[3]()
require.Equal(t, 0.0, usage["pid_usage_per"])
require.InEpsilon(t, 0.01, usage["pid_usage_per"], 1)

usage = allFuncs[4]()
require.Equal(t, 0.0, usage["blkio_read"])
require.Equal(t, 0.0, usage["blkio_write"])
require.InEpsilon(t, 0.01, usage["blkio_read"], 1)
require.InEpsilon(t, 0.01, usage["blkio_write"], 1)
}

0 comments on commit 8f6f5df

Please sign in to comment.