-
Notifications
You must be signed in to change notification settings - Fork 817
/
Copy pathqueryable_test.go
199 lines (168 loc) · 5.08 KB
/
queryable_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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package api
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"regexp"
"testing"
"time"
"github.com/prometheus/common/route"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/storage"
v1 "github.com/prometheus/prometheus/web/api/v1"
"github.com/stretchr/testify/require"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/user"
"github.com/cortexproject/cortex/pkg/chunk"
"github.com/cortexproject/cortex/pkg/querier"
"github.com/cortexproject/cortex/pkg/util"
)
func TestApiStatusCodes(t *testing.T) {
for ix, tc := range []struct {
err error
expectedString string
expectedCode int
}{
{
err: errors.New("some random error"),
expectedString: "some random error",
expectedCode: 500,
},
{
err: chunk.QueryError("special handling"), // handled specially by chunk_store_queryable
expectedString: "special handling",
expectedCode: 422,
},
{
err: promql.ErrTooManySamples("query execution"),
expectedString: "too many samples",
expectedCode: 422,
},
{
err: promql.ErrQueryCanceled("query execution"),
expectedString: "query was canceled",
expectedCode: 503,
},
{
err: promql.ErrQueryTimeout("query execution"),
expectedString: "query timed out",
expectedCode: 503,
},
// Status code 400 is remapped to 422 (only choice we have)
{
err: httpgrpc.Errorf(http.StatusBadRequest, "test string"),
expectedString: "test string",
expectedCode: 422,
},
// 404 is also translated to 422
{
err: httpgrpc.Errorf(http.StatusNotFound, "not found"),
expectedString: "not found",
expectedCode: 422,
},
// 505 is translated to 500
{
err: httpgrpc.Errorf(http.StatusHTTPVersionNotSupported, "test"),
expectedString: "test",
expectedCode: 500,
},
} {
for k, q := range map[string]storage.SampleAndChunkQueryable{
"error from queryable": testQueryable{err: tc.err},
"error from querier": testQueryable{q: testQuerier{err: tc.err}},
"error from seriesset": testQueryable{q: testQuerier{s: testSeriesSet{err: tc.err}}},
} {
t.Run(fmt.Sprintf("%s/%d", k, ix), func(t *testing.T) {
r := createPrometheusAPI(errorTranslateQueryable{q: q})
rec := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/api/v1/query?query=up", nil)
req = req.WithContext(user.InjectOrgID(context.Background(), "test org"))
r.ServeHTTP(rec, req)
require.Equal(t, tc.expectedCode, rec.Code)
require.Contains(t, rec.Body.String(), tc.expectedString)
})
}
}
}
func createPrometheusAPI(q storage.SampleAndChunkQueryable) *route.Router {
engine := promql.NewEngine(promql.EngineOpts{
Logger: util.Logger,
Reg: nil,
ActiveQueryTracker: nil,
MaxSamples: 100,
Timeout: 5 * time.Second,
})
api := v1.NewAPI(
engine,
q,
func(context.Context) v1.TargetRetriever { return &querier.DummyTargetRetriever{} },
func(context.Context) v1.AlertmanagerRetriever { return &querier.DummyAlertmanagerRetriever{} },
func() config.Config { return config.Config{} },
map[string]string{}, // TODO: include configuration flags
v1.GlobalURLOptions{},
func(f http.HandlerFunc) http.HandlerFunc { return f },
nil, // Only needed for admin APIs.
"", // This is for snapshots, which is disabled when admin APIs are disabled. Hence empty.
false, // Disable admin APIs.
util.Logger,
func(context.Context) v1.RulesRetriever { return &querier.DummyRulesRetriever{} },
0, 0, 0, // Remote read samples and concurrency limit.
regexp.MustCompile(".*"),
func() (v1.RuntimeInfo, error) { return v1.RuntimeInfo{}, errors.New("not implemented") },
&v1.PrometheusVersion{},
)
promRouter := route.New().WithPrefix("/api/v1")
api.Register(promRouter)
return promRouter
}
type testQueryable struct {
q storage.Querier
err error
}
func (t testQueryable) ChunkQuerier(ctx context.Context, mint, maxt int64) (storage.ChunkQuerier, error) {
return nil, t.err
}
func (t testQueryable) Querier(ctx context.Context, mint, maxt int64) (storage.Querier, error) {
if t.q != nil {
return t.q, nil
}
return nil, t.err
}
type testQuerier struct {
s storage.SeriesSet
err error
}
func (t testQuerier) LabelValues(name string) ([]string, storage.Warnings, error) {
return nil, nil, t.err
}
func (t testQuerier) LabelNames() ([]string, storage.Warnings, error) {
return nil, nil, t.err
}
func (t testQuerier) Close() error {
return nil
}
func (t testQuerier) Select(sortSeries bool, hints *storage.SelectHints, matchers ...*labels.Matcher) storage.SeriesSet {
if t.s != nil {
return t.s
}
return storage.ErrSeriesSet(t.err)
}
type testSeriesSet struct {
err error
}
func (t testSeriesSet) Next() bool {
return false
}
func (t testSeriesSet) At() storage.Series {
return nil
}
func (t testSeriesSet) Err() error {
return t.err
}
func (t testSeriesSet) Warnings() storage.Warnings {
return nil
}