forked from ekanite/ekanite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index_test.go
466 lines (415 loc) · 11.7 KB
/
index_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package ekanite
import (
"os"
"reflect"
"sort"
"testing"
"time"
"github.com/blevesearch/bleve"
"github.com/ekanite/ekanite/query"
)
type testDoc struct {
id DocID
line string
priority string
}
// ID returns a sufficiently long doc ID, embedding the testDoc's ID.
func (t testDoc) ID() DocID {
return t.id
}
func (t testDoc) Data() interface{} {
data := struct {
Line string
Priority string
}{
Line: t.line,
Priority: t.priority,
}
return data
}
func (t testDoc) Source() []byte { return []byte(t.line) }
func TestIndex_NewIndex(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
start := parseTime("2006-01-02T22:04:00Z")
i, err := NewIndex(path, start.UTC(), start.Add(time.Hour), 4)
if err != nil || i == nil {
t.Fatalf("failed to create new index at %s %s", path, err)
}
defer i.Close()
existOrFail(t, path+"/20060102_2204")
existOrFail(t, path+"/20060102_2204/endtime")
containsOrFail(t, path+"/20060102_2204/endtime", "20060102_2304")
existOrFail(t, path+"/20060102_2204/0")
existOrFail(t, path+"/20060102_2204/1")
existOrFail(t, path+"/20060102_2204/2")
existOrFail(t, path+"/20060102_2204/3")
tests := []struct {
timestamp string
shouldContain bool
}{
{
timestamp: "2006-01-02T22:03:59Z",
shouldContain: false,
},
{
timestamp: "2006-01-02T22:04:00Z",
shouldContain: true,
},
{
timestamp: "2006-01-02T22:04:01Z",
shouldContain: true,
},
{
timestamp: "2006-01-02T23:04:00Z",
shouldContain: false,
},
{
timestamp: "2006-01-02T23:05:00Z",
shouldContain: false,
},
}
for n, tt := range tests {
if tt.shouldContain != i.Contains(parseTime(tt.timestamp)) {
t.Fatalf("timestamp #%d '%s' failed Contains test", n, tt.timestamp)
}
}
}
func TestIndex_Expired(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
retentionPeriod := 24 * time.Hour
startTime := parseTime("2006-01-02T22:04:00Z").UTC()
endTime := parseTime("2006-01-02T22:04:00Z").Add(time.Hour).UTC()
i, _ := NewIndex(path, startTime, endTime, 1)
defer i.Close()
tests := []struct {
timestamp time.Time
expired bool
}{
{
timestamp: endTime,
expired: false,
},
{
timestamp: endTime.Add(time.Hour),
expired: false,
},
{
timestamp: endTime.Add(retentionPeriod),
expired: false,
},
{
timestamp: endTime.Add(2 * retentionPeriod),
expired: true,
},
}
for n, tt := range tests {
if i.Expired(tt.timestamp, retentionPeriod) != tt.expired {
t.Errorf("Expired test #%d: index %s has incorrect expired status for time %s",
n, path, tt.timestamp)
}
}
}
func TestIndex_OpenIndex(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
start := parseTime("2006-01-04T00:04:00Z")
startTime := start.UTC()
endTime := start.Add(time.Hour).UTC()
n, err := NewIndex(path, startTime, endTime, 4)
if err != nil {
t.Fatalf("failed to create new index for Open() test at %s %s", path, err)
}
n.Close() // Close it, or it can't be opened.
i, err := OpenIndex(path + "/20060104_0004")
if err != nil {
t.Fatalf("failed to open index for Open() test at %s %s", path, err)
}
defer i.Close()
if i.Path() != path+"/20060104_0004" {
t.Fatalf("opened index not at expected path, got: %s", i.Path())
}
if i.StartTime() != startTime {
t.Fatalf("start time of opended index is wrong, expected %s, got %s", startTime, i.StartTime())
}
if i.EndTime() != endTime {
t.Fatalf("end time time of opended index is wrong, expected %s, got %s", endTime, i.EndTime())
}
if len(i.Shards) != 4 {
t.Fatalf("wrong number of shards, expected 4, got %d", len(i.Shards))
}
if n, err := i.Total(); n != 0 || err != nil {
t.Fatalf("new index failed doc count check, found %d, %s", n, err.Error())
}
}
func TestIndex_DeleteIndex(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
now := time.Now().UTC()
i, err := NewIndex(path, now, now.Add(time.Hour), 4)
if err != nil {
t.Fatalf("failed to create new index for Open() test at %s %s", path, err)
}
if err := DeleteIndex(i); err != nil {
t.Fatalf("failed to delete index at %s", path)
}
notExistOrFail(t, path+"/20060104_0004")
}
func TestIndex_Index(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
now := time.Now().UTC()
i, _ := NewIndex(path, now, now, 2)
d1 := testDoc{id: DocID("00000000000000000000000000001234"), line: "password accepted for user root"}
d2 := testDoc{id: DocID("00000000000000000000000000005678"), line: "GET /index.html"}
d3 := testDoc{id: DocID("00000000000000000000000000009abc"), line: "sshd version 4.0"}
if err := i.Index([]Document{d1, d2, d3}); err != nil {
t.Fatalf("failed to index batch into index at %s", path)
}
n, err := i.Total()
if err != nil {
t.Fatalf("failed to get number of documents in index at %s", path)
}
if n != 3 {
t.Fatalf("wrong number of documents in index at %s", path)
}
}
func TestIndex_Document(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
now := time.Now().UTC()
i, _ := NewIndex(path, now, now, 2)
id := DocID("1234")
source := "password accepted for user root"
d1 := testDoc{id: DocID(id), line: source}
if err := i.Index([]Document{d1}); err != nil {
t.Fatalf("failed to index batch into index at %s", path)
}
b, err := i.Document(id)
if err != nil {
t.Fatalf(`failed to retrieve document ID "%s": %s`, id, err.Error())
}
if string(b) != source {
t.Fatalf(`source of retrieved document not correct, got: "%s", exp: "%s"`, string(b), source)
}
}
func TestIndex_IndexSimpleSearch(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
now := time.Now().UTC()
i, _ := NewIndex(path, now, now, 4)
d1 := testDoc{id: DocID("00000000000000000000000000001234"), line: "auth password accepted for user philip"}
d2 := testDoc{id: DocID("0000000000000000000000000000ABCD"), line: "auth password invalid for user root"}
d3 := testDoc{id: DocID("00000000000000000000000000005678"), line: "auth GET /index.html"}
d4 := testDoc{id: DocID("0000000000000000000000000000DEAD"), line: "pamd POST", priority: "CRITICAL"}
d5 := testDoc{id: DocID("0000000000000000000000000000BEEF"), line: "pamd PUT", priority: "ERROR"}
if err := i.Index([]Document{d1, d2, d3, d4, d5}); err != nil {
t.Fatalf("failed to index batch into index at %s", path)
}
tests := []struct {
Phrase string
IDs []string
}{
{
Phrase: "missing",
IDs: []string{},
},
{
Phrase: "priority:ERROR",
IDs: []string{},
},
{
Phrase: "philip",
IDs: []string{"00000000000000000000000000001234"},
},
{
Phrase: "Priority:CRITICAL",
IDs: []string{"0000000000000000000000000000DEAD"},
},
{
Phrase: "-Priority:CRITICAL",
IDs: []string{
"00000000000000000000000000001234",
"0000000000000000000000000000ABCD",
"00000000000000000000000000005678",
"0000000000000000000000000000BEEF",
},
},
{
Phrase: "get",
IDs: []string{"00000000000000000000000000005678"},
},
{
Phrase: "password",
IDs: []string{"00000000000000000000000000001234", "0000000000000000000000000000ABCD"},
},
{
Phrase: "+password +accepted",
IDs: []string{"00000000000000000000000000001234"},
},
{
Phrase: "+philip +root",
IDs: []string{},
},
{
Phrase: "+password +get",
IDs: []string{},
},
{
Phrase: "+password -accepted",
IDs: []string{"0000000000000000000000000000ABCD"},
},
{
Phrase: "+password -ACCEPTED",
IDs: []string{"0000000000000000000000000000ABCD"},
},
{
Phrase: "auth",
IDs: []string{
"00000000000000000000000000001234",
"0000000000000000000000000000ABCD",
"00000000000000000000000000005678"},
},
}
f := func(testName string, idx *Index) {
t.Logf("running '%s'", testName)
for _, tt := range tests {
// Perform the search
IDs, err := idx.Search(tt.Phrase)
if err != nil {
t.Errorf("error while searching for '%s': %s", tt.Phrase, err.Error())
}
if len(IDs) != len(tt.IDs) {
t.Errorf("wrong number of hits for search '%s', got %d, expected %d",
tt.Phrase, len(IDs), len(tt.IDs))
continue
}
// Compare doc IDs.
exp := make([]string, len(IDs))
got := make([]string, len(IDs))
for n := range IDs {
exp[n] = tt.IDs[n]
got[n] = string(IDs[n])
}
sort.Strings(exp)
sort.Strings(got)
for n := range exp {
if got[n] != exp[n] {
t.Errorf("search return incorrect doc IDs for search '%s'", tt.Phrase)
t.Logf("got: %v, exp: %v", got, exp)
break // No point checking any further.
}
}
}
}
// Test searching a newly-created index.
f("new index search test", i)
// Close, re-open the index, and try all searches again.
i.Close()
i, err := OpenIndex(i.Path())
if err != nil {
t.Fatalf("failed to re-open index at %s: %s", i.Path(), err.Error())
}
f("re-opened index search test", i)
}
func TestIndex_Shard(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
now := time.Now().UTC()
i, _ := NewIndex(path, now, now.Add(time.Hour), 2)
s := i.Shard(DocID("00000000000000000000000000001234"))
if s == nil {
t.Fatalf("failed to get shard for doc 1234")
}
if s != i.Shards[0] && s != i.Shards[1] {
t.Fatalf("wrong shard returned for doc 1234")
}
u := i.Shard(DocID("00000000000000000000000000001234"))
if u != s {
t.Fatalf("different shard returned for same doc")
}
}
func TestShard_NewOpenClose(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
s := NewShard(path)
if s == nil {
t.Fatalf("failed to create new shard at %s", path)
}
if err := s.Open(); err != nil {
t.Fatalf("failed to open shard at %s: %s", path, err.Error())
}
if err := s.Close(); err != nil {
t.Fatalf("failed to close shard at %s: %s", path, err.Error())
}
return
}
func TestShard_Index(t *testing.T) {
path := tempPath()
defer os.RemoveAll(path)
s := NewShard(path)
s.Open()
var c uint64
var err error
d1 := testDoc{id: DocID("00000000000000000000000000001234"), line: "this is a log line"}
d2 := testDoc{id: DocID("00000000000000000000000000005678"), line: "this is a log line"}
d3 := testDoc{id: DocID("00000000000000000000000000009abc"), line: "this is a log line"}
// Index a single document
if err := s.Index([]Document{d1}); err != nil {
t.Fatalf("failed to index single document: %s", err.Error())
}
if c, _ := s.Total(); c != 1 {
t.Fatalf("shard indexed count incorrect, got %d, expected 1", c)
}
c, err = s.Total()
if err != nil {
t.Fatalf("failed to get shard doc count: %s", err.Error())
}
if c != 1 {
t.Fatalf("shard doc count incorrect, got %d, expected 1", c)
}
// Index a couple more.
if err := s.Index([]Document{d2, d3}); err != nil {
t.Fatalf("failed to index single document: %s", err.Error())
}
if c, _ := s.Total(); c != 3 {
t.Fatalf("shard indexed count incorrect, got %d, expected 1", c)
}
c, err = s.Total()
if err != nil {
t.Fatalf("failed to get shard doc count: %s", err.Error())
}
if c != 3 {
t.Fatalf("shard doc count incorrect, got %d, expected 1", c)
}
// Test fetching the indexed documents by ID.
source, err := s.Document(d1.ID())
if err != nil {
t.Fatalf("failed to get document %s", d1.ID())
}
if string(source) != d1.line {
t.Fatalf("retrieved document is not identical, got: '%s', expected: '%s'", string(source), d1.line)
}
s.Close()
}
func Test_bleveQueryFromExpr(t *testing.T) {
tests := []struct {
expr query.Expr
query bleve.Query
}{
{
&query.FieldExpr{Field: "SearchField", Term: "theTerm"},
bleve.NewPhraseQuery([]string{"theTerm"}, "SearchField"),
},
}
for _, tt := range tests {
q, err := bleveQueryFromExpr(tt.expr)
if err != nil {
t.Fatalf("error transforming Ekanite query to bleve query: %s", err.Error())
}
if !reflect.DeepEqual(tt.query, q) {
t.Fatalf("bleve query is not correct, exp: %v, got: %v", tt.query, q)
}
}
}