Resolve gopsutil & unit test issues with net proto stats
This commit is contained in:
@@ -1,13 +1,15 @@
|
||||
package system
|
||||
|
||||
import "github.com/stretchr/testify/mock"
|
||||
import (
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
import "github.com/shirou/gopsutil/cpu"
|
||||
import "github.com/shirou/gopsutil/disk"
|
||||
"github.com/shirou/gopsutil/cpu"
|
||||
"github.com/shirou/gopsutil/disk"
|
||||
|
||||
import "github.com/shirou/gopsutil/load"
|
||||
import "github.com/shirou/gopsutil/mem"
|
||||
import "github.com/shirou/gopsutil/net"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
"github.com/shirou/gopsutil/mem"
|
||||
"github.com/shirou/gopsutil/net"
|
||||
)
|
||||
|
||||
type MockPS struct {
|
||||
mock.Mock
|
||||
@@ -21,6 +23,7 @@ func (m *MockPS) LoadAvg() (*load.LoadAvgStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -29,6 +32,7 @@ func (m *MockPS) CPUTimes(perCPU, totalCPU bool) ([]cpu.CPUTimesStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -37,6 +41,7 @@ func (m *MockPS) DiskUsage() ([]*disk.DiskUsageStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -45,6 +50,16 @@ func (m *MockPS) NetIO() ([]net.NetIOCountersStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) NetProto() ([]net.NetProtoCountersStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
r0 := ret.Get(0).([]net.NetProtoCountersStat)
|
||||
r1 := ret.Error(1)
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -53,6 +68,7 @@ func (m *MockPS) DiskIO() (map[string]disk.DiskIOCountersStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -61,6 +77,7 @@ func (m *MockPS) VMStat() (*mem.VirtualMemoryStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -69,6 +86,7 @@ func (m *MockPS) SwapStat() (*mem.SwapMemoryStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
@@ -77,6 +95,7 @@ func (m *MockPS) DockerStat() ([]*DockerContainerStat, error) {
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m *MockPS) NetConnections() ([]net.NetConnectionStat, error) {
|
||||
ret := m.Called()
|
||||
|
||||
|
||||
@@ -81,10 +81,12 @@ func (s *NetIOStats) Gather(acc plugins.Accumulator) error {
|
||||
}
|
||||
|
||||
// Get system wide stats for different network protocols
|
||||
netprotos, err := s.ps.NetProto()
|
||||
// (ignore these stats if the call fails)
|
||||
netprotos, _ := s.ps.NetProto()
|
||||
for _, proto := range netprotos {
|
||||
for stat, value := range proto.Stats {
|
||||
name := fmt.Sprintf("%s_%s", proto.Protocol, strings.ToLower(stat))
|
||||
name := fmt.Sprintf("%s_%s", strings.ToLower(proto.Protocol),
|
||||
strings.ToLower(stat))
|
||||
acc.Add(name, value, nil)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,6 +113,17 @@ func TestSystemStats_GenerateStats(t *testing.T) {
|
||||
|
||||
mps.On("NetIO").Return([]net.NetIOCountersStat{netio}, nil)
|
||||
|
||||
netprotos := []net.NetProtoCountersStat{
|
||||
net.NetProtoCountersStat{
|
||||
Protocol: "Udp",
|
||||
Stats: map[string]int64{
|
||||
"InDatagrams": 4655,
|
||||
"NoPorts": 892592,
|
||||
},
|
||||
},
|
||||
}
|
||||
mps.On("NetProto").Return(netprotos, nil)
|
||||
|
||||
vms := &mem.VirtualMemoryStat{
|
||||
Total: 12400,
|
||||
Available: 7600,
|
||||
@@ -273,6 +284,8 @@ func TestSystemStats_GenerateStats(t *testing.T) {
|
||||
assert.NoError(t, acc.ValidateTaggedValue("err_out", uint64(8), ntags))
|
||||
assert.NoError(t, acc.ValidateTaggedValue("drop_in", uint64(7), ntags))
|
||||
assert.NoError(t, acc.ValidateTaggedValue("drop_out", uint64(1), ntags))
|
||||
assert.NoError(t, acc.ValidateValue("udp_noports", int64(892592)))
|
||||
assert.NoError(t, acc.ValidateValue("udp_indatagrams", int64(4655)))
|
||||
|
||||
preDiskIOPoints := len(acc.Points)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user