-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_test.go
49 lines (46 loc) · 1.47 KB
/
search_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
package chevalier
import (
"encoding/json"
"testing"
)
func TestBuildQuery(t *testing.T) {
engine := NewQueryEngine("localhost", "chevalier_test", "datasource", "chevalier_metadata")
query := new(SourceRequest)
query.Tags = make([]*SourceRequest_Tag, 2)
query.Tags[0] = NewSourceRequestTag("hostname", "*.example.com")
query.Tags[1] = NewSourceRequestTag("metric", "cpu")
q, err := engine.BuildQuery("ABCDEF", query)
if err != nil {
t.Errorf("%v", err)
}
json, err := json.Marshal(q)
if err != nil {
t.Errorf("%v", err)
}
expected := `{"from":0,"query":{"bool":{"must":[{"wildcard":{"datasource.hostname":"*.example.com"}},{"wildcard":{"datasource.metric":"cpu"}},{"query_string":{"fields":["Origin"],"query":"ABCDEF"}}]}},"size":0}`
result := string(json[:])
if result != expected {
t.Errorf("Query marshalling mismatch: expected %v, got %v.", expected, result)
}
}
func TestSanitizeTag(t *testing.T) {
var tagTests = []struct {
k string
v string
outK string
outV string
}{
{"*", "*", "datasource._all", "*"},
{"host*", "[test*]^~", "datasource.host*", `\[test*\]\^\~`},
}
engine := NewQueryEngine("localhost", "chevalier_test", "datasource", "chevalier_metadata")
failIfInvalid := func(f, v, wantF, wantV string) {
resF, resV := engine.sanitizeTag(f, v)
if resF != wantF || resV != wantV {
t.Errorf("Got %v and %v, wanted %v and %v", resF, resV, wantF, wantV)
}
}
for _, tt := range tagTests {
failIfInvalid(tt.k, tt.v, tt.outK, tt.outV)
}
}