forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_test.go
124 lines (106 loc) · 2.77 KB
/
net_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package net
import (
"syscall"
"testing"
"github.com/influxdata/telegraf/plugins/inputs/system"
"github.com/influxdata/telegraf/testutil"
"github.com/shirou/gopsutil/net"
"github.com/stretchr/testify/require"
)
func TestNetStats(t *testing.T) {
var mps system.MockPS
var err error
defer mps.AssertExpectations(t)
var acc testutil.Accumulator
netio := net.IOCountersStat{
Name: "eth0",
BytesSent: 1123,
BytesRecv: 8734422,
PacketsSent: 781,
PacketsRecv: 23456,
Errin: 832,
Errout: 8,
Dropin: 7,
Dropout: 1,
}
mps.On("NetIO").Return([]net.IOCountersStat{netio}, nil)
netprotos := []net.ProtoCountersStat{
{
Protocol: "Udp",
Stats: map[string]int64{
"InDatagrams": 4655,
"NoPorts": 892592,
},
},
}
mps.On("NetProto").Return(netprotos, nil)
netstats := []net.ConnectionStat{
{
Type: syscall.SOCK_DGRAM,
Laddr: net.Addr{IP: "0.0.0.0", Port: 2003},
},
{
Status: "ESTABLISHED",
Laddr: net.Addr{IP: "10.0.0.2", Port: 2003},
},
{
Status: "ESTABLISHED",
Laddr: net.Addr{IP: "10.0.0.2", Port: 2003},
},
{
Status: "CLOSE",
Laddr: net.Addr{IP: "10.0.0.3", Port: 2003},
},
}
mps.On("NetConnections").Return(netstats, nil)
err = (&NetIOStats{ps: &mps, skipChecks: true}).Gather(&acc)
require.NoError(t, err)
ntags := map[string]string{
"interface": "eth0",
}
fields1 := map[string]interface{}{
"bytes_sent": uint64(1123),
"bytes_recv": uint64(8734422),
"packets_sent": uint64(781),
"packets_recv": uint64(23456),
"err_in": uint64(832),
"err_out": uint64(8),
"drop_in": uint64(7),
"drop_out": uint64(1),
}
acc.AssertContainsTaggedFields(t, "net", fields1, ntags)
fields2 := map[string]interface{}{
"udp_noports": int64(892592),
"udp_indatagrams": int64(4655),
}
ntags = map[string]string{
"interface": "all",
}
acc.AssertContainsTaggedFields(t, "net", fields2, ntags)
acc.Metrics = nil
err = (&NetStats{&mps}).Gather(&acc)
require.NoError(t, err)
fields3 := map[string]interface{}{
"tcp_established": 2,
"tcp_syn_sent": 0,
"tcp_syn_recv": 0,
"tcp_fin_wait1": 0,
"tcp_fin_wait2": 0,
"tcp_time_wait": 0,
"tcp_close": 1,
"tcp_close_wait": 0,
"tcp_last_ack": 0,
"tcp_listen": 0,
"tcp_closing": 0,
"tcp_none": 0,
"udp_socket": 1,
}
acc.AssertContainsTaggedFields(t, "netstat", fields3, make(map[string]string))
// netport
err = (&NetPorts{&mps, []uint32{2003}}).Gather(&acc)
acc.AssertContainsTaggedFields(t, "netport", fields3, map[string]string{"port": "2003"})
acc.Metrics = nil
err = (&NetIOStats{ps: &mps, IgnoreProtocolStats: true}).Gather(&acc)
require.NoError(t, err)
acc.AssertDoesNotContainsTaggedFields(t, "netstat", fields3, make(map[string]string))
}