-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathstore_test.go
232 lines (207 loc) · 5.68 KB
/
store_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
package main
import (
"fmt"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
type storeTest struct {
storetype string
uri string
}
var testStores = []storeTest{
{"sqlite", ":memory:"},
}
func init() {
pgTestUrl := os.Getenv("PG_TEST_URL")
if pgTestUrl != "" {
testStores = append(testStores, storeTest{"postgresql", pgTestUrl})
}
chTestUrl := os.Getenv("CH_TEST_URL")
if chTestUrl != "" {
testStores = append(testStores, storeTest{"clickhouse", chTestUrl})
}
}
func doTestLogIndexed(t *testing.T, s Store) {
s.Clear()
s.Init()
testFilename := "test.log"
indexed, err := s.IsLogIndexed(testFilename)
if err != nil {
t.Fatal(err)
}
if indexed != false {
t.Errorf("IsLogIndexed(%q) == %t, want false", testFilename, indexed)
}
var ar aggregationResult
var ur UpdateResult
err = s.SetLogIndexed(testFilename, ar, ur)
if err != nil {
t.Fatal(err)
}
indexed, err = s.IsLogIndexed(testFilename)
if err != nil {
t.Fatal(err)
}
if indexed != true {
t.Errorf("IsLogIndexed(%q) == %t, want true", testFilename, indexed)
}
}
func LoadFile(t *testing.T, s Store, fn string) UpdateResult {
aggregator := NewDNSAggregator()
err := aggregate(aggregator, fn)
if err != nil {
t.Fatal(err)
}
aggregated := aggregator.GetResult()
result, err := s.Update(aggregated)
if err != nil {
t.Fatal(err)
}
return result
}
func doTestUpdating(t *testing.T, s Store, forward bool) {
s.Clear()
s.Init()
var files []string
if forward {
files = []string{"test_data/reddit_1.txt", "test_data/reddit_2.txt"}
} else {
files = []string{"test_data/reddit_2.txt", "test_data/reddit_1.txt"}
}
result_a := LoadFile(t, s, files[0])
result_b := LoadFile(t, s, files[1])
// Hack for now. Clickhouse store doesn't report inserted vs updated
//TODO: add a method to Store interface to return a bool for this
expected_inserted := 31
expected_updated := 0
if _, ok := s.(*CHStore); ok {
expected_inserted = 0
expected_updated = 31
}
assert.EqualValues(t, result_a.Inserted, expected_inserted)
assert.EqualValues(t, result_a.Updated, expected_updated)
assert.EqualValues(t, result_b.Inserted, 0)
assert.EqualValues(t, result_b.Updated, 31)
recs, err := s.FindIndividual("www.reddit.com")
if err != nil {
t.Fatalf("Failed to find: %v", err)
return
}
//www.reddit.com Q 2 2016-04-01 00:03:03 2016-04-01 21:55:04
if assert.Equal(t, len(recs), 1) {
rec := recs[0]
assert.Equal(t, rec.Value, "www.reddit.com")
assert.Equal(t, rec.Which, "Q")
assert.EqualValues(t, rec.Count, 2)
//This is stupid, but I need to fix things so that they return actual dates
//and get a handle on the timezone BS.
//So for now, ignore the ' ' vs 'T' difference, and the hour
assert.Regexp(t, "2016-04-01...:03:03", rec.First)
assert.Regexp(t, "2016-04-01...:55:04", rec.Last)
}
//www.reddit.com A 198.41.208.138 2 300 2016-04-01 00:03:03 2016-04-01 21:55:04
trecs, err := s.FindTuples("198.41.208.138")
if err != nil {
t.Fatalf("Failed to find: %v", err)
return
}
if assert.Equal(t, len(trecs), 1) {
rec := trecs[0]
assert.Equal(t, rec.Query, "www.reddit.com")
assert.Equal(t, rec.Type, "A")
assert.Equal(t, rec.Answer, "198.41.208.138")
assert.EqualValues(t, rec.Count, 2)
//This is stupid, but I need to fix things so that they return actual dates
//and get a handle on the timezone BS.
//So for now, ignore the ' ' vs 'T' difference, and the hour
assert.Regexp(t, "2016-04-01...:03:03", rec.First)
assert.Regexp(t, "2016-04-01...:55:04", rec.Last)
}
}
// Output:
//A: Inserted=31 Updated=0
//B: Inserted=0 Updated=31
//Individual records: 1
//www.reddit.com Q 2 2016-04-01 00:03:03 2016-04-01 21:55:04
//Tuple records: 1
//www.reddit.com A 198.41.208.138 2 300 2016-04-01 00:03:03 2016-04-01 21:55:04
func BenchmarkUpdateStores(b *testing.B) {
for _, ts := range testStores {
b.Run(fmt.Sprintf("Indexing/%s", ts.storetype), func(b *testing.B) {
store, err := NewStore(ts.storetype, ts.uri)
if err != nil {
b.Fatalf("NewStore failed: %s", err)
}
aggregator := NewDNSAggregator()
err = aggregate(aggregator, "big.log.gz")
if err != nil {
b.Fatal(err)
}
aggregated := aggregator.GetResult()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := store.Update(aggregated)
if err != nil {
b.Fatalf("store.Update failed: %s", err)
}
}
})
}
}
func testFile(t *testing.T, s Store, fn string) error {
aggregator := NewDNSAggregator()
err := aggregate(aggregator, fn)
if err != nil {
return err
}
aggregated := aggregator.GetResult()
_, err = s.Update(aggregated)
if err != nil {
return err
}
return nil
}
func TestIndexingFiles(t *testing.T) {
allFiles := []string{
"./test_data/nbtstat.log",
"./test_data/garbage.log",
"./test_data/bad_ttl.log",
"./test_data/dns_json_iso8601.json",
}
for _, ts := range testStores {
t.Run(fmt.Sprintf("Indexing/%s", ts.storetype), func(t *testing.T) {
store, err := NewStore(ts.storetype, ts.uri)
if err != nil {
t.Fatalf("can't create store at %s: %v", ts.uri, err)
}
for _, fn := range allFiles {
t.Run(fn, func(t *testing.T) {
testFile(t, store, fn)
})
}
})
}
}
func doTestStore(t *testing.T, store Store) {
t.Run("testLogIndexed", func(t *testing.T) {
doTestLogIndexed(t, store)
})
t.Run("forward", func(t *testing.T) {
doTestUpdating(t, store, true)
})
t.Run("reverse", func(t *testing.T) {
doTestUpdating(t, store, false)
})
}
func TestStoreIndexing(t *testing.T) {
for _, ts := range testStores {
t.Run(ts.storetype, func(t *testing.T) {
store, err := NewStore(ts.storetype, ts.uri)
if err != nil {
t.Fatalf("can't create store at %s: %v", ts.uri, err)
}
doTestStore(t, store)
})
}
}