-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathhandler_json_test.go
168 lines (137 loc) · 5.08 KB
/
handler_json_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
package find
import (
"io"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/lomik/graphite-clickhouse/config"
"github.com/lomik/graphite-clickhouse/helper/tests/clickhouse"
"github.com/lomik/graphite-clickhouse/metrics"
"github.com/stretchr/testify/assert"
)
func NewRequest(method, url string, body io.Reader) *http.Request {
r, _ := http.NewRequest(method, url, body)
return r
}
type testStruct struct {
request *http.Request
wantCode int
want string
wantContent string
}
func testResponce(t *testing.T, step int, h *Handler, tt *testStruct, wantCachedFind string) {
w := httptest.NewRecorder()
h.ServeHTTP(w, tt.request)
s := w.Body.String()
assert.Equalf(t, tt.wantCode, w.Code, "code mismatch step %d\n,%s", step, s)
if w.Code == http.StatusOK {
if tt.wantContent != "" {
contentType := w.Result().Header["Content-Type"]
assert.Equalf(t, []string{tt.wantContent}, contentType, "content type mismatch, step %d", step)
}
cachedFind := w.Result().Header.Get("X-Cached-Find")
assert.Equalf(t, cachedFind, wantCachedFind, "cached find mismatch, step %d", step)
assert.Equalf(t, tt.want, s, "Step %d", step)
}
}
func TestHandler_ServeValuesJSON(t *testing.T) {
metrics.DisableMetrics()
srv := clickhouse.NewTestServer()
defer srv.Close()
cfg, _ := config.DefaultConfig()
cfg.ClickHouse.URL = srv.URL
h := NewHandler(cfg)
srv.AddResponce(
"SELECT Path FROM graphite_index WHERE ((Level=20003) AND (Path LIKE 'DB.postgres.%')) AND (Date='1970-02-12') GROUP BY Path FORMAT TabSeparatedRaw",
&clickhouse.TestResponse{
Body: []byte("DB.postgres.host1.\nDB.postgres.host2.\n"),
})
srv.AddResponce(
"SELECT Path FROM graphite_index WHERE ((Level=20005) AND (Path LIKE 'DB.postgres.%' AND match(Path, '^DB[.]postgres[.]([^.]*?)[.]cpu[.]load_avg[.]?$'))) AND (Date='1970-02-12') GROUP BY Path FORMAT TabSeparatedRaw",
&clickhouse.TestResponse{
Body: []byte("DB.postgres.host1.cpu.load_avg\nDB.postgres.host2.cpu.load_avg\n"),
})
tests := []testStruct{
{
request: NewRequest("GET", srv.URL+"/metrics/find/?format=json&query=DB.postgres.%2A", nil),
wantCode: http.StatusOK,
want: "[{path=\"DB.postgres.host1\"},{path=\"DB.postgres.host2\"}]\r\n",
wantContent: "text/plain; charset=utf-8",
},
{
request: NewRequest("GET", srv.URL+"/metrics/find/?format=json&query=DB.postgres.%2A.cpu.load_avg", nil),
wantCode: http.StatusOK,
want: "[{path=\"DB.postgres.host1.cpu.load_avg\",leaf=1},{path=\"DB.postgres.host2.cpu.load_avg\",leaf=1}]\r\n",
wantContent: "text/plain; charset=utf-8",
},
}
var queries uint64
for i, tt := range tests {
t.Run(tt.request.URL.RawQuery+"#"+strconv.Itoa(i), func(t *testing.T) {
for i := 0; i < 2; i++ {
testResponce(t, i, h, &tt, "")
}
assert.Equal(t, uint64(2), srv.Queries()-queries)
queries = srv.Queries()
})
}
}
func TestHandler_ServeValuesCachedJSON(t *testing.T) {
srv := clickhouse.NewTestServer()
defer srv.Close()
cfg, _ := config.DefaultConfig()
cfg.ClickHouse.URL = srv.URL
// find cache config
cfg.Common.FindCacheConfig = config.CacheConfig{
Type: "mem",
Size: 8192,
FindTimeoutSec: 1,
}
var err error
cfg.Common.FindCache, err = config.CreateCache("metric-finder", &cfg.Common.FindCacheConfig)
if err != nil {
t.Fatalf("Failed to create find cache: %v", err)
}
h := NewHandler(cfg)
srv.AddResponce(
"SELECT Path FROM graphite_index WHERE ((Level=20003) AND (Path LIKE 'DB.postgres.%')) AND (Date='1970-02-12') GROUP BY Path FORMAT TabSeparatedRaw",
&clickhouse.TestResponse{
Body: []byte("DB.postgres.host1.\nDB.postgres.host2.\n"),
})
srv.AddResponce(
"SELECT Path FROM graphite_index WHERE ((Level=20005) AND (Path LIKE 'DB.postgres.%' AND match(Path, '^DB[.]postgres[.]([^.]*?)[.]cpu[.]load_avg[.]?$'))) AND (Date='1970-02-12') GROUP BY Path FORMAT TabSeparatedRaw",
&clickhouse.TestResponse{
Body: []byte("DB.postgres.host1.cpu.load_avg\nDB.postgres.host2.cpu.load_avg\n"),
})
tests := []testStruct{
{
request: NewRequest("GET", srv.URL+"/metrics/find/?format=json&query=DB.postgres.%2A", nil),
wantCode: http.StatusOK,
want: "[{path=\"DB.postgres.host1\"},{path=\"DB.postgres.host2\"}]\r\n",
wantContent: "text/plain; charset=utf-8",
},
{
request: NewRequest("GET", srv.URL+"/metrics/find/?format=json&query=DB.postgres.%2A.cpu.load_avg", nil),
wantCode: http.StatusOK,
want: "[{path=\"DB.postgres.host1.cpu.load_avg\",leaf=1},{path=\"DB.postgres.host2.cpu.load_avg\",leaf=1}]\r\n",
wantContent: "text/plain; charset=utf-8",
},
}
var queries uint64
for i, tt := range tests {
t.Run(tt.request.URL.RawQuery+"#"+strconv.Itoa(i), func(t *testing.T) {
testResponce(t, 0, h, &tt, "")
assert.Equal(t, uint64(1), srv.Queries()-queries)
// query from cache
testResponce(t, 1, h, &tt, "1")
assert.Equal(t, uint64(1), srv.Queries()-queries)
// wait for expire cache
time.Sleep(time.Second * 2)
testResponce(t, 2, h, &tt, "")
assert.Equal(t, uint64(2), srv.Queries()-queries)
queries = srv.Queries()
})
}
}