forked from go-graphite/carbonapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
142 lines (116 loc) · 3.89 KB
/
main_test.go
File metadata and controls
142 lines (116 loc) · 3.89 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
package main
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-graphite/carbonapi/expr"
pb "github.com/go-graphite/carbonzipper/carbonzipperpb3"
realZipper "github.com/go-graphite/carbonzipper/zipper"
"github.com/lomik/zapwriter"
"github.com/stretchr/testify/assert"
"go.uber.org/zap"
)
type mockCarbonZipper struct {
z *realZipper.Zipper
logger *zap.Logger
statsSender func(*realZipper.Stats)
}
func newMockCarbonZipper() *mockCarbonZipper {
z := &mockCarbonZipper{}
return z
}
func (z mockCarbonZipper) Find(ctx context.Context, metric string) (pb.GlobResponse, error) {
return getGlobResponse(), nil
}
func (z mockCarbonZipper) Info(ctx context.Context, metric string) (map[string]pb.InfoResponse, error) {
response := getMockInfoResponse()
return response, nil
}
func (z mockCarbonZipper) Render(ctx context.Context, metric string, from, until int32) ([]*expr.MetricData, error) {
var result []*expr.MetricData
multiFetchResponse := getMultiFetchResponse()
result = append(result, &expr.MetricData{FetchResponse: multiFetchResponse.Metrics[0]})
return result, nil
}
func getGlobResponse() pb.GlobResponse {
globMtach := pb.GlobMatch{Path: "foo.bar", IsLeaf: true}
var matches []pb.GlobMatch
matches = append(matches, globMtach)
globResponse := pb.GlobResponse{
Name: "foo.bar",
Matches: matches,
}
return globResponse
}
func getMultiFetchResponse() pb.MultiFetchResponse {
mfr := pb.FetchResponse{
Name: "foo.bar",
StartTime: 1510913280,
StopTime: 1510913880,
StepTime: 60,
Values: []float64{0, 1510913759, 1510913818},
IsAbsent: []bool{true, false, false},
}
result := pb.MultiFetchResponse{Metrics: []pb.FetchResponse{mfr}}
return result
}
func getMockInfoResponse() map[string]pb.InfoResponse {
decoded := make(map[string]pb.InfoResponse)
r := pb.Retention{
SecondsPerPoint: 60,
NumberOfPoints: 43200,
}
d := pb.InfoResponse{
Name: "foo.bar",
AggregationMethod: "Average",
MaxRetention: 157680000,
XFilesFactor: 0.5,
Retentions: []pb.Retention{r},
}
decoded["http://127.0.0.1:8080"] = d
return decoded
}
func init() {
logger := zapwriter.Logger("main")
setUpViper(logger)
setUpConfigUpstreams(logger)
setUpConfig(logger, newMockCarbonZipper())
initHandlers()
}
func setUpRequest(t *testing.T, url string) (*http.Request, *httptest.ResponseRecorder) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
t.Fatal(err)
}
// We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
return req, rr
}
func TestRenderHandler(t *testing.T) {
req, rr := setUpRequest(t, "/render/?target=fallbackSeries(foo.bar,foo.baz)&from=-10minutes&format=json")
renderHandler(rr, req)
expected := `[{"target":"foo.bar","datapoints":[[null,1510913280],[1510913759,1510913340],[1510913818,1510913400]]}]`
// Check the status code is what we expect.
assert.Equal(t, rr.Code, http.StatusOK, "HttpStatusCode should be 200 OK.")
assert.Equal(t, expected, rr.Body.String(), "Http response should be same.")
}
func TestFindHandler(t *testing.T) {
req, rr := setUpRequest(t, "/metrics/find/?query=foo.bar&format=json")
findHandler(rr, req)
body := rr.Body.String()
expected := `[{"allowChildren":0,"expandable":0,"leaf":1,"id":"foo.bar","text":"bar","context":{}}]` + "\n"
assert.Equal(t, rr.Code, http.StatusOK, "HttpStatusCode should be 200 OK.")
assert.Equal(t, expected, body, "Http response should be same.")
}
func TestInfoHandler(t *testing.T) {
req, rr := setUpRequest(t, "/info/?target=foo.bar&format=json")
infoHandler(rr, req)
body := rr.Body.String()
expected := getMockInfoResponse()
expectedJson, err := json.Marshal(expected)
assert.Nil(t, err)
assert.Equal(t, rr.Code, http.StatusOK, "HttpStatusCode should be 200 OK.")
assert.Equal(t, string(expectedJson), body, "Http response should be same.")
}