-
Notifications
You must be signed in to change notification settings - Fork 4
/
main_test.go
133 lines (116 loc) · 3.38 KB
/
main_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
package main
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
pb2 "github.com/dgryski/carbonzipper/carbonzipperpb"
pb3 "github.com/dgryski/carbonzipper/carbonzipperpb3"
m "github.com/kanatohodets/carbonsearch/consumer/message"
"github.com/kanatohodets/carbonsearch/database"
"github.com/kanatohodets/carbonsearch/util"
)
func TestFindHandler(t *testing.T) {
stats := util.InitStats()
initTimeBuckets(Config.Buckets + 1)
db := database.New(
100,
1000,
"custom",
"text",
map[string][]string{
"fqdn": []string{"servers"},
},
stats,
)
populateDb(db)
mux := createMux(db, stats, "virt.v1.*.")
pb2FindTest(t, mux)
pb3FindTest(t, mux)
jsonFindTest(t, mux)
}
func pb2FindTest(t *testing.T, mux *http.ServeMux) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/metrics/find/?query=virt.v1.*.servers-status:live&format=protobuf", nil)
if err != nil {
t.Errorf("pb2 test: could not create http request: %v", err)
return
}
name := "virt.v1.*.servers-status:live"
path := "host.foohost_prod_example_com.cpu.loadavg"
leaf := true
mux.ServeHTTP(recorder, req)
expected := &pb2.GlobResponse{
Name: &name,
Matches: []*pb2.GlobMatch{
&pb2.GlobMatch{
Path: &path,
IsLeaf: &leaf,
},
},
}
expectedBytes, err := expected.Marshal()
if err != nil {
t.Errorf("pb2 test: failed to marshal expected into bytes: %v", err)
}
if !reflect.DeepEqual(expectedBytes, recorder.Body.Bytes()) {
t.Errorf("pb2 test: bad response! expected %q, got %q", expected.String(), string(recorder.Body.Bytes()))
}
}
func pb3FindTest(t *testing.T, mux *http.ServeMux) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/metrics/find/?query=virt.v1.*.servers-status:live&format=protobuf3", nil)
if err != nil {
t.Errorf("pb3 test: could not create http request: %v", err)
return
}
mux.ServeHTTP(recorder, req)
expected := &pb3.GlobResponse{
Name: "virt.v1.*.servers-status:live",
Matches: []*pb3.GlobMatch{
&pb3.GlobMatch{
Path: "host.foohost_prod_example_com.cpu.loadavg",
IsLeaf: true,
},
},
}
expectedBytes, err := expected.Marshal()
if err != nil {
t.Errorf("pb3 test: failed to marshal expected into bytes: %v", err)
}
if !reflect.DeepEqual(expectedBytes, recorder.Body.Bytes()) {
t.Errorf("pb3 test: bad response! expected %q, got %q", expected.String(), string(recorder.Body.Bytes()))
}
}
func jsonFindTest(t *testing.T, mux *http.ServeMux) {
recorder := httptest.NewRecorder()
req, err := http.NewRequest("GET", "/metrics/find/?query=virt.v1.*.servers-status:live&format=json", nil)
if err != nil {
t.Errorf("JSON test: could not create http request: %v", err)
return
}
mux.ServeHTTP(recorder, req)
expected := `{"name":"virt.v1.*.servers-status:live","matches":[{"path":"host.foohost_prod_example_com.cpu.loadavg","isLeaf":true}]}` + "\n"
if expected != string(recorder.Body.Bytes()) {
t.Errorf("JSON test: bad response! expected %q, got %q", expected, string(recorder.Body.Bytes()))
}
}
func populateDb(db *database.Database) {
metrics := &m.KeyMetric{
Key: "fqdn",
Value: "foohost.prod.example.com",
Metrics: []string{
"host.foohost_prod_example_com.cpu.loadavg",
},
}
tags := &m.KeyTag{
Key: "fqdn",
Value: "foohost.prod.example.com",
Tags: []string{
"servers-status:live",
},
}
db.InsertMetrics(metrics)
db.InsertTags(tags)
db.MaterializeIndexes()
}