forked from Cistern/sflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
94 lines (77 loc) · 2.31 KB
/
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
package sflow
import (
"bytes"
"os"
"testing"
)
func TestDecodeAndEncodeGenericEthernetCounterDatagram(t *testing.T) {
f, err := os.Open("_test/counter_sample.dump")
if err != nil {
t.Fatal(err)
}
d := NewDecoder(f)
dgram, err := d.Decode()
if err != nil {
t.Fatal(err)
}
buf := &bytes.Buffer{}
enc := NewEncoder(dgram.IpAddress, dgram.SubAgentId, dgram.SequenceNumber)
enc.Encode(buf, dgram.Samples)
d.Use(bytes.NewReader(buf.Bytes()))
dgram, err = d.Decode()
if err != nil {
t.Fatal(err)
}
if dgram.Version != 5 {
t.Errorf("Expected datagram version %v, got %v", 5, dgram.Version)
}
if int(dgram.NumSamples) != len(dgram.Samples) {
t.Fatalf("expected NumSamples to be %d, but len(Samples) is %d", dgram.NumSamples, len(dgram.Samples))
}
if len(dgram.Samples) != 1 {
t.Fatalf("expected 1 sample, got %d", len(dgram.Samples))
}
sample, ok := dgram.Samples[0].(*CounterSample)
if !ok {
t.Fatalf("expected a CounterSample, got %T", dgram.Samples[0])
}
if len(sample.Records) != 2 {
t.Fatalf("expected 2 records, got %d", len(sample.Records))
}
ethCounters, ok := sample.Records[0].(EthernetCounters)
if !ok {
t.Fatalf("expected a EthernetCounters record, got %T", sample.Records[0])
}
expectedEthCountersRec := EthernetCounters{}
if ethCounters != expectedEthCountersRec {
t.Errorf("expected\n%#v, got\n%#v", expectedEthCountersRec, ethCounters)
}
genericInterfaceCounters, ok := sample.Records[1].(GenericInterfaceCounters)
if !ok {
t.Fatalf("expected a GenericInterfaceCounters record, got %T", sample.Records[1])
}
expectedGenericInterfaceCounters := 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,
}
if genericInterfaceCounters != expectedGenericInterfaceCounters {
t.Errorf("expected\n%#v, got\n%#v", expectedGenericInterfaceCounters, genericInterfaceCounters)
}
}