-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrender_test.go
More file actions
155 lines (146 loc) · 4.2 KB
/
render_test.go
File metadata and controls
155 lines (146 loc) · 4.2 KB
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
package graphite
import (
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"testing"
"time"
)
var testUnmarshallMetricsCases = []struct {
Json string
Result []Series
Err error
}{
{
Json: "",
Result: []Series{},
Err: nil},
{
Json: "[{\"target\": \"main\", \"datapoints\": [[1, 1468339853], [1.0, 1468339854], [null, 1468339855]]}]",
Result: []Series{
{
Target: "main",
Datapoints: []DataPoint{
{Value: 1.0, Timestamp: time.Unix(1468339853, 0)},
{Value: 1.0, Timestamp: time.Unix(1468339854, 0)},
{Value: 0, Timestamp: time.Unix(1468339855, 0)}},
},
},
Err: nil,
},
}
func TestUnmarshallMetrics(t *testing.T) {
for _, tc := range testUnmarshallMetricsCases {
res, err := unmarshallSeries([]byte(tc.Json))
if !reflect.DeepEqual(res, tc.Result) {
t.Errorf("Result is %+v, \n expected %+v", res, tc.Result)
}
if err != tc.Err {
t.Errorf("E %+v", err)
}
}
}
func TestNewClientFromString(t *testing.T) {
urlString := "http://domain.tld/path"
client, _ := NewFromString(urlString)
testRequest := RenderRequest{}
shouldUrl := urlString + testRequest.toQueryString()
if shouldUrl != client.queryAsString(testRequest) {
t.Errorf("Resulting URL is %v, \n but should be %v", client.queryAsString(testRequest), shouldUrl)
}
}
func TestGraphiteRequest_ToQueryString(t *testing.T) {
testCases := []struct {
Request RenderRequest
Result string
}{
{
Request: RenderRequest{},
Result: "/render/?format=json",
},
{
Request: RenderRequest{Targets: []string{"foo", "bar"}},
Result: "/render/?format=json&target=foo&target=bar",
},
{
Request: RenderRequest{From: time.Unix(1468339853, 0), Until: time.Unix(1468339853, 0)},
Result: "/render/?format=json&from=1468339853&until=1468339853",
},
{
Request: RenderRequest{MaxDataPoints: 10},
Result: "/render/?format=json&maxDataPoints=10",
},
}
for _, tc := range testCases {
res := tc.Request.toQueryString()
if res != tc.Result {
t.Errorf("Result should be \"%v\", but \"%v\" received", tc.Result, res)
}
}
}
func makeTest(t *testing.T, request RenderRequest, expectedQuery, result string, series []Series) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
parsedQuery, _ := url.ParseQuery(expectedQuery)
if !reflect.DeepEqual(r.URL.Query(), parsedQuery) {
t.Errorf("Expected query is %+v but %+v got", parsedQuery, r.URL.Query())
}
if r.URL.Path != "/render/" {
t.Errorf("Path should be `/render/` but %s found", r.URL.Path)
}
fmt.Fprintln(w, result)
}))
defer ts.Close()
client, _ := NewFromString(ts.URL)
res, _ := client.QueryRender(request)
if !reflect.DeepEqual(res, series) {
t.Errorf("Expected series %+v, but %+v got", series, res)
}
}
var queryTestCases = []struct {
Request RenderRequest
ExpectedQuery string
Result string
Series []Series
}{
{
Request: RenderRequest{Targets: []string{"main1", "main2"}},
ExpectedQuery: "format=json&target=main1&target=main2",
Result: "[{\"target\": \"main\", \"datapoints\": [[1, 1468339853], [1.0, 1468339854], [null, 1468339855]]}]",
Series: []Series{
{
Target: "main",
Datapoints: []DataPoint{
{Value: 1.0, Timestamp: time.Unix(1468339853, 0)},
{Value: 1.0, Timestamp: time.Unix(1468339854, 0)},
{Value: 0, Timestamp: time.Unix(1468339855, 0)}},
},
},
},
{
Request: RenderRequest{From: time.Unix(1468339853, 0), Until: time.Unix(1468339854, 0)},
ExpectedQuery: "format=json&from=1468339853&until=1468339854",
Result: "[{\"target\": \"main\", \"datapoints\": [[1, 1468339853], [1.0, 1468339854], [null, 1468339855]]}]",
Series: []Series{
{
Target: "main",
Datapoints: []DataPoint{
{Value: 1.0, Timestamp: time.Unix(1468339853, 0)},
{Value: 1.0, Timestamp: time.Unix(1468339854, 0)},
{Value: 0, Timestamp: time.Unix(1468339855, 0)}},
},
},
},
{
Request: RenderRequest{MaxDataPoints: 1},
ExpectedQuery: "format=json&maxDataPoints=1",
Result: "[]",
Series: []Series{},
},
}
func TestGraphiteClient_Query(t *testing.T) {
for _, tc := range queryTestCases {
makeTest(t, tc.Request, tc.ExpectedQuery, tc.Result, tc.Series)
}
}