This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/
series_test.go
134 lines (125 loc) · 3.3 KB
/
series_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
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/timescale/promscale/pkg/log"
"github.com/timescale/promscale/pkg/query"
)
func TestSeries(t *testing.T) {
_ = log.Init(log.Config{
Level: "debug",
})
testCases := []struct {
name string
querier *mockQuerier
matchers []string
start string
end string
expectCode int
expectError string
}{
{
name: "match[] is not sent",
expectCode: http.StatusBadRequest,
matchers: []string{},
start: "1",
end: "2",
expectError: "bad_data",
querier: &mockQuerier{},
}, {
name: "Start is unparsable",
expectCode: http.StatusBadRequest,
matchers: []string{"m"},
start: "unparsable",
expectError: "bad_data",
querier: &mockQuerier{},
}, {
name: "End is unparsable",
expectCode: http.StatusBadRequest,
matchers: []string{"m"},
start: "1.1",
end: "unparsable",
expectError: "bad_data",
querier: &mockQuerier{},
}, {
name: "End is before start",
expectCode: http.StatusBadRequest,
matchers: []string{"m"},
start: "1.1",
end: "1",
expectError: "bad_data",
querier: &mockQuerier{},
}, {
name: "Matcher is unparsable",
expectCode: http.StatusBadRequest,
matchers: []string{"wrong_matcher{"},
start: "1",
end: "2",
expectError: "bad_data",
querier: &mockQuerier{},
}, {
name: "Select error",
start: "1",
end: "2",
expectCode: http.StatusUnprocessableEntity,
expectError: "execution",
matchers: []string{"m"},
querier: &mockQuerier{selectErr: fmt.Errorf("some error")},
}, {
name: "All good",
start: "1",
end: "2",
expectCode: http.StatusOK,
matchers: []string{"m", `m{a="1"}`},
querier: &mockQuerier{},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
handler := series(query.NewQueryable(tc.querier))
queryUrl := constructSeriesRequest(tc.start, tc.end, tc.matchers)
w := doSeriesRequest(t, handler, queryUrl)
if w.Code != tc.expectCode {
t.Errorf("Unexpected HTTP status code received: got %d wanted %d", w.Code, tc.expectCode)
return
}
if tc.expectError != "" {
var er errResponse
_ = json.NewDecoder(bytes.NewReader(w.Body.Bytes())).Decode(&er)
if tc.expectError != er.ErrorType {
t.Errorf("expected error of type %s, got %s", tc.expectError, er.ErrorType)
}
}
})
}
}
func constructSeriesRequest(start, end string, matchers []string) string {
var matcherParams []string
for _, m := range matchers {
matcherParams = append(matcherParams, "match[]="+m)
}
match := strings.Join(matcherParams, "&")
return fmt.Sprintf(
"http://localhost:9090/series?start=%s&end=%s&%s",
start, end, match,
)
}
func doSeriesRequest(t *testing.T, queryHandler http.Handler, url string) *httptest.ResponseRecorder {
req, err := http.NewRequestWithContext(context.Background(), "GET", url, nil)
if err != nil {
t.Errorf("%v", err)
}
req.Header.Set(
"Content-Type",
"application/x-www-form-urlencoded; param=value",
)
w := httptest.NewRecorder()
queryHandler.ServeHTTP(w, req)
return w
}