-
Notifications
You must be signed in to change notification settings - Fork 1
/
influx_test.go
253 lines (200 loc) · 6.52 KB
/
influx_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package main
import (
"testing"
"sync"
gomock "github.com/golang/mock/gomock"
dns "github.com/miekg/dns"
cdns "github.com/niclabs/dnszeppelin"
"net"
"strconv"
"time"
)
// Function that creates a batch with 8 elements
func createBatch() []cdns.DNSResult {
rChannel := make(chan cdns.DNSResult, 8)
batch := make([]cdns.DNSResult, 0)
timestamp := time.Now()
ip := net.IP{}
types := []uint16{dns.TypeA, dns.TypeSOA, dns.TypeIXFR, dns.TypeAXFR, dns.TypeA, dns.TypeA, dns.TypeIXFR}
for i := 0; i < 7; i++ {
data := new(dns.Msg)
data.SetQuestion("example"+strconv.Itoa(i)+".com.", types[i])
dnsresult := cdns.DNSResult{timestamp, *data, uint8(0), ip, ip, "tcp", uint16(0)}
rChannel <- dnsresult
d := <-rChannel
batch = append(batch, d)
}
datar := new(dns.Msg)
datar.Response = true
dnsresult := cdns.DNSResult{timestamp, *datar, uint8(0), ip, ip, "tcp", uint16(0)}
rChannel <- dnsresult
d := <-rChannel
batch = append(batch, d)
return batch
}
// Tests InfluxAgg with empty batch
func TestInfluxAgg(t *testing.T) {
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
batch := make([]cdns.DNSResult, 0)
got := InfluxAgg(batch, &testmap)
if got != nil {
t.Errorf("InfluxAgg(batch, &testmap) = %d; want nil", got)
}
if testmap.fields["TOTALR"] != 0 {
t.Errorf("fields[TOTALR] = %d; want 0", testmap.fields["TOTALR"])
}
if testmap.fields["TOTALQ"] != 0 {
t.Errorf("fields[TOTALQ] = %d; want 0", testmap.fields["TOTALQ"])
}
if testmap.fields["NOERROR"] != testmap.responses[0] {
t.Errorf("fields[NOERROR] = %d; want responses[0]", testmap.fields["NOERROR"])
}
if testmap.fields["NXDOMAIN"] != testmap.responses[3] {
t.Errorf("fields[NXDOMAIN] = %d; want responses[3]", testmap.fields["NXDOMAIN"])
}
if testmap.fields["UNIQUERY"] != 0 {
t.Errorf("fields[UNIQUERY] = %d; want 0", testmap.fields["UNIQUERY"])
}
}
// Tests InfluxAgg with a batch created with createBatch
func TestInfluxAgg1(t *testing.T) {
batch := createBatch()
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
got := InfluxAgg(batch, &testmap)
if got != nil {
t.Errorf("InfluxAgg(batch, &testmap) = %d; want nil", got)
}
if testmap.fields["TOTALQ"] != 7 {
t.Errorf("fields[TOTALQ] = %d; want 7", testmap.fields["TOTALQ"])
}
if testmap.fields["TOTALR"] != 1 {
t.Errorf("fields[TOTALR] = %d; want 1", testmap.fields["TOTALR"])
}
for i := 0; i < 4; i++ {
if testmap.domains["example"+strconv.Itoa(i)+".com."] != 1 {
t.Errorf("domains[examplei.com.] = %d; want 1", testmap.domains["example"+strconv.Itoa(i)+".com."])
}
}
if testmap.sources["<nil>"] != 7 {
t.Errorf("sources[<nil>] = %d; want 7", testmap.sources["<nil>"])
}
if testmap.fields["A"] != 3 {
t.Errorf("fields[A] = %d; want 3", testmap.fields["A"])
}
if testmap.fields["SOA"] != 1 {
t.Errorf("fields[SOA] = %d; want 1", testmap.fields["SOA"])
}
if testmap.fields["IXFR"] != 2 {
t.Errorf("fields[IXFR] = %d; want 2", testmap.fields["IXFR"])
}
if testmap.fields["AXFR"] != 1 {
t.Errorf("fields[AXFR] = %d; want 1", testmap.fields["AXFR"])
}
}
// Tests InfluxStore with empty batch
func TestInfluxStore(t *testing.T) {
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
batch := make([]cdns.DNSResult, 0)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := NewMockDatabase(ctrl)
m.
EXPECT().
InfluxStore(&testmap, batch).
Return(nil)
m.InfluxStore(&testmap, batch)
}
// Tests InfluxStore with a batch created with createBatch
func TestInfluxStore1(t *testing.T) {
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
batch := createBatch()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := NewMockDatabase(ctrl)
m.
EXPECT().
InfluxStore(&testmap, batch).
Return(nil)
m.InfluxStore(&testmap, batch)
}
// Tests InfluxStore with an empty resultChannel
func TestInfluxCollect(t *testing.T) {
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
resultChannel := make(chan cdns.DNSResult, *resultChannelSize)
exiting := make(chan bool)
var wg sync.WaitGroup
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := NewMockDatabase(ctrl)
m.
EXPECT().
InfluxCollect(resultChannel, exiting, &wg, *wsize, *batchSize, &testmap)
m.InfluxCollect(resultChannel, exiting, &wg, *wsize, *batchSize, &testmap)
}
// Tests InfluxStore with a non empty resultChannel
func TestInfluxCollect1(t *testing.T) {
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
resultChannel := make(chan cdns.DNSResult, *resultChannelSize)
exiting := make(chan bool)
var wg sync.WaitGroup
ctrl := gomock.NewController(t)
timestamp := time.Now()
data := new(dns.Msg)
data.SetQuestion("example.com.", dns.TypeA)
ip := net.IP{}
dnsresult := cdns.DNSResult{timestamp, *data, uint8(0), ip, ip, "tcp", uint16(0)}
resultChannel <- dnsresult
defer ctrl.Finish()
m := NewMockDatabase(ctrl)
m.
EXPECT().
InfluxCollect(resultChannel, exiting, &wg, *wsize, *batchSize, &testmap)
m.InfluxCollect(resultChannel, exiting, &wg, *wsize, *batchSize, &testmap)
}
// Tests StoreEachMap with the map fields
func TestStoreEachMap(t *testing.T) {
fields := make(map[string]int)
sources := make(map[string]int)
domains := make(map[string]int)
responses := make(map[int]int)
filtermap := make(map[string][]float64)
testmap := maps{fields, sources, domains, responses, filtermap}
now := time.Now()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
m := NewMockDatabase(ctrl)
m.
EXPECT().
StoreEachMap(testmap.fields, "stat", "type", now)
m.StoreEachMap(testmap.fields, "stat", "type", now)
}