This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
/
labels_test.go
86 lines (78 loc) · 2.09 KB
/
labels_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
package api
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"github.com/timescale/promscale/pkg/log"
"github.com/timescale/promscale/pkg/query"
)
func TestLabels(t *testing.T) {
_ = log.Init(log.Config{
Level: "debug",
})
testCases := []struct {
name string
querier *mockQuerier
expectCode int
expectError string
}{
{
name: "Error on get label names",
expectCode: http.StatusInternalServerError,
expectError: "internal",
querier: &mockQuerier{labelNamesErr: fmt.Errorf("error on label names")},
}, {
name: "All good",
expectCode: http.StatusOK,
querier: &mockQuerier{labelNames: []string{"a"}},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
handler := labelsHandler(query.NewQueryable(tc.querier))
w := doLabels(t, handler)
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)
}
return
}
var res response
_ = json.NewDecoder(bytes.NewReader(w.Body.Bytes())).Decode(&res)
if len(res.Warnings) > 0 {
t.Errorf("unexpected warnings: %v", res.Warnings)
}
var resStr []string
for _, s := range res.Data.([]interface{}) {
resStr = append(resStr, s.(string))
}
if !reflect.DeepEqual(resStr, tc.querier.labelNames) {
t.Errorf("expected: %v, got: %v", tc.querier.labelNames, res.Data)
}
})
}
}
func doLabels(t *testing.T, queryHandler http.Handler) *httptest.ResponseRecorder {
req, err := http.NewRequestWithContext(context.Background(), "GET", "http://localhost:9090/labels", 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
}