-
Notifications
You must be signed in to change notification settings - Fork 32
/
counter_record_encode_test.go
105 lines (88 loc) · 2.01 KB
/
counter_record_encode_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
package sflow
import (
"bytes"
"testing"
)
func TestEncodeDecodeGenericInterfaceCountersRecord(t *testing.T) {
rec := GenericInterfaceCounters{
Index: 9,
Type: 6,
Speed: 100000000,
Direction: 1,
Status: 3,
InOctets: 79282473,
InUnicastPackets: 329128,
InMulticastPackets: 0,
InBroadcastPackets: 1493,
InDiscards: 0,
InErrors: 0,
InUnknownProtocols: 0,
OutOctets: 764247430,
OutUnicastPackets: 9470970,
OutMulticastPackets: 780342,
OutBroadcastPackets: 877721,
OutDiscards: 0,
OutErrors: 0,
PromiscuousMode: 1,
}
b := &bytes.Buffer{}
err := rec.encode(b)
if err != nil {
t.Fatal(err)
}
// Skip the header section. It's 8 bytes.
var headerBytes [8]byte
_, err = b.Read(headerBytes[:])
if err != nil {
t.Fatal(err)
}
decoded, err := decodeGenericInterfaceCountersRecord(b, uint32(b.Len()))
if err != nil {
t.Fatal(err)
}
if decoded != rec {
t.Errorf("expected\n%+#v\n, got\n%+#v", rec, decoded)
}
}
func TestEncodeDecodeHostCPUCountersRecord(t *testing.T) {
rec := HostCPUCounters{
Load1m: 0.1,
Load5m: 0.2,
Load15m: 0.3,
ProcessesRunning: 4,
ProcessesTotal: 5,
NumCPU: 6,
SpeedCPU: 7,
Uptime: 8,
CPUUser: 9,
CPUNice: 10,
CPUSys: 11,
CPUIdle: 12,
CPUWio: 13,
CPUIntr: 14,
CPUSoftIntr: 15,
Interrupts: 16,
ContextSwitches: 17,
CPUSteal: 18,
CPUGuest: 19,
CPUGuestNice: 20,
}
b := &bytes.Buffer{}
err := rec.encode(b)
if err != nil {
t.Fatal(err)
}
// Skip the header section. It's 8 bytes.
var headerBytes [8]byte
_, err = b.Read(headerBytes[:])
if err != nil {
t.Fatal(err)
}
decoded, err := decodeHostCPUCountersRecord(b, uint32(b.Len()))
if err != nil {
t.Fatal(err)
}
if decoded != rec {
t.Errorf("expected\n%+#v\n, got\n%+#v", rec, decoded)
}
}