diff --git a/.golangci.yml b/.golangci.yml index a457343..9806b97 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -51,6 +51,10 @@ linters: - errcheck - thelper - usestdlibvars + - deadcode + - varcheck + - structcheck + - protogetter linters-settings: goheader: 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() diff --git a/internal/cgroup/parser/parser.go b/internal/cgroup/parser/parser.go index 9f7010e..601f8f4 100644 --- a/internal/cgroup/parser/parser.go +++ b/internal/cgroup/parser/parser.go @@ -14,7 +14,7 @@ import ( ) type Marshal interface { - Marshal(*bytes.Buffer) (*bytes.Buffer, error) + Marshal(buffer *bytes.Buffer) (*bytes.Buffer, error) } type StatManager struct { diff --git a/internal/cgroup/parser/parser_test.go b/internal/cgroup/parser/parser_test.go index b4b05cd..5588490 100644 --- a/internal/cgroup/parser/parser_test.go +++ b/internal/cgroup/parser/parser_test.go @@ -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) }