-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_test.go
180 lines (151 loc) · 3.64 KB
/
send_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package libkflow
import (
"fmt"
"io"
"math/rand"
"net"
"testing"
"time"
"github.com/kentik/libkflow/agg"
"github.com/kentik/libkflow/api"
"github.com/kentik/libkflow/api/test"
"github.com/kentik/libkflow/chf"
"github.com/kentik/libkflow/flow"
"github.com/kentik/libkflow/metrics"
"github.com/stretchr/testify/assert"
capnp "zombiezen.com/go/capnproto2"
)
func TestSender(t *testing.T) {
sender, server, assert := setup(t)
expected := flow.Flow{
DeviceId: uint32(sender.Device.ID),
SrcAs: rand.Uint32(),
DstAs: rand.Uint32(),
SampleAdj: true,
}
sender.Send(&expected)
msgs, err := receive(server)
if err != nil {
t.Fatal(err)
}
assert.True(sender.Stop(100 * time.Millisecond))
assert.Greater(sender.Metrics.BytesSent.Count(), int64(0))
assert.Equal(flowToCHF(expected, t).String(), msgs.At(0).String())
}
func TestSenderStop(t *testing.T) {
sender, _, assert := setup(t)
stopped := sender.Stop(100 * time.Millisecond)
assert.True(stopped)
}
func TestSendDNS(t *testing.T) {
sender, server, assert := setup(t)
url := server.URL(test.DNS)
sender.StartDNS(url, 1*time.Millisecond)
expected := &api.DNSResponse{
Question: api.DNSQuestion{
Name: "foo.com",
Host: net.ParseIP("127.0.0.1"),
},
Answers: []api.DNSResourceRecord{
{
Name: "",
CNAME: "",
IP: net.ParseIP("10.0.0.1"),
TTL: 16,
},
},
}
sender.SendDNS(expected)
select {
case res := <-server.Dns():
assert.Equal(expected, res)
case <-time.After(100 * time.Millisecond):
t.Fatal("failed to receive DNS data")
}
}
func BenchmarkSenderSend(b *testing.B) {
sender, _, _ := setup(b)
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
sender.Send(&flow.Flow{
SrcAs: uint32(b.N),
DstAs: uint32(b.N),
})
}
}
func setup(t testing.TB) (*Sender, *test.Server, *assert.Assertions) {
metrics := metrics.New(100, 200, "send_test", "1.0.0")
agg, err := agg.NewAgg(10*time.Millisecond, 100, metrics)
if err != nil {
t.Fatal(err)
}
client, server, device, err := test.NewClientServer()
if err != nil {
t.Fatal(err)
}
server.Log.SetOutput(io.Discard)
url := server.URL(test.FLOW)
sender := newSender(url, 1*time.Second)
sender.Metrics = metrics
sender.start(agg, client, device, 1)
return sender, server, assert.New(t)
}
func receive(s *test.Server) (*chf.CHF_List, error) {
interval := 100 * time.Millisecond
select {
case flow := <-s.Flows():
msgs, err := flow.Msgs()
return &msgs, err
case <-time.After(interval):
return nil, fmt.Errorf("failed to receive flow within %s", interval)
}
}
func flowToCHF(flow flow.Flow, t testing.TB) chf.CHF {
_, seg, err := capnp.NewMessage(capnp.SingleSegment(nil))
kflow, err := chf.NewCHF(seg)
if err != nil {
t.Fatal(err)
}
list, err := chf.NewCustom_List(seg, int32(len(flow.Customs)))
if err != nil {
t.Fatal(err)
}
flow.FillCHF(kflow, list)
return kflow
}
func TestCompress(t *testing.T) {
sender, server, assert := setup(t)
expected := []flow.Flow{
flow.Flow{
DeviceId: uint32(sender.Device.ID),
SrcAs: uint32(1),
DstAs: rand.Uint32(),
SampleAdj: true,
},
flow.Flow{
DeviceId: uint32(sender.Device.ID),
SrcAs: uint32(2),
DstAs: rand.Uint32(),
SampleAdj: true,
},
flow.Flow{
DeviceId: uint32(sender.Device.ID),
SrcAs: uint32(3),
DstAs: rand.Uint32(),
SampleAdj: true,
},
}
// Send them all at once
for _, e := range expected {
sender.Send(&e)
}
// Itterate through, looking at each one recieved.
for i, e := range expected {
msgs, err := receive(server)
if err != nil {
t.Fatal(err)
}
assert.Equal(flowToCHF(e, t).String(), msgs.At(0).String(), "%d", i)
}
}