Skip to content

Commit eba0ec4

Browse files
authored
Add test for APIs (#332)
* Add test for status and metrics apis Signed-off-by: Drumil Patel <drumilpatel720@gmail.com>
1 parent 4beed75 commit eba0ec4

File tree

3 files changed

+283
-42
lines changed

3 files changed

+283
-42
lines changed

api/v1/api_test.go

+221
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
// Copyright 2020 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
package v1
14+
15+
import (
16+
"bytes"
17+
"encoding/json"
18+
"net/http"
19+
"net/http/httptest"
20+
"reflect"
21+
"testing"
22+
"time"
23+
24+
dto "github.com/prometheus/client_model/go"
25+
26+
"github.com/go-kit/kit/log"
27+
"github.com/golang/protobuf/proto"
28+
"github.com/prometheus/pushgateway/storage"
29+
"github.com/prometheus/pushgateway/testutil"
30+
)
31+
32+
var (
33+
logger = log.NewNopLogger()
34+
testFlags = map[string]string{
35+
"flag1": "value1",
36+
"flag2": "value2",
37+
"flag3": "value3",
38+
}
39+
testBuildInfo = map[string]string{
40+
"build1": "value1",
41+
"build2": "value2",
42+
"build3": "value3",
43+
}
44+
45+
mf1 = &dto.MetricFamily{
46+
Name: proto.String("mf1"),
47+
Type: dto.MetricType_SUMMARY.Enum(),
48+
Metric: []*dto.Metric{
49+
{
50+
Label: []*dto.LabelPair{
51+
{
52+
Name: proto.String("instance"),
53+
Value: proto.String(`inst'a"n\ce1`),
54+
},
55+
{
56+
Name: proto.String("job"),
57+
Value: proto.String("Björn"),
58+
},
59+
},
60+
Summary: &dto.Summary{
61+
SampleCount: proto.Uint64(0),
62+
SampleSum: proto.Float64(0),
63+
},
64+
},
65+
},
66+
}
67+
68+
grouping1 = map[string]string{
69+
"job": "Björn",
70+
"instance": `inst'a"n\ce1`,
71+
}
72+
)
73+
74+
func convertMap(m map[string]string) map[string]interface{} {
75+
result := map[string]interface{}{}
76+
for k, v := range m {
77+
result[k] = v
78+
}
79+
return result
80+
}
81+
82+
func TestStatusAPI(t *testing.T) {
83+
dms := storage.NewDiskMetricStore("", 100*time.Millisecond, nil, logger)
84+
testAPI := New(logger, dms, testFlags, testBuildInfo)
85+
86+
req, err := http.NewRequest("GET", "http://example.org/", &bytes.Buffer{})
87+
if err != nil {
88+
t.Fatal(err)
89+
}
90+
91+
w := httptest.NewRecorder()
92+
93+
testResponse := response{}
94+
testAPI.status(w, req)
95+
json.Unmarshal(w.Body.Bytes(), &testResponse)
96+
jsonData := testResponse.Data.(map[string]interface{})
97+
responseFlagData := jsonData["flags"].(map[string]interface{})
98+
responseBuildInfo := jsonData["build_information"].(map[string]interface{})
99+
100+
if expected, got := http.StatusOK, w.Code; expected != got {
101+
t.Errorf("Wanted status code %v, got %v.", expected, got)
102+
}
103+
104+
if !reflect.DeepEqual(responseFlagData, convertMap(testFlags)) {
105+
t.Errorf("Wanted following flags %q, got %q.", testFlags, responseFlagData)
106+
}
107+
108+
if !reflect.DeepEqual(responseBuildInfo, convertMap(testBuildInfo)) {
109+
t.Errorf("Wanted following build info %q, got %q.", testBuildInfo, responseBuildInfo)
110+
}
111+
}
112+
113+
func TestMetricsAPI(t *testing.T) {
114+
dms := storage.NewDiskMetricStore("", 100*time.Millisecond, nil, logger)
115+
testAPI := New(logger, dms, testFlags, testBuildInfo)
116+
117+
req, err := http.NewRequest("GET", "http://example.org/", &bytes.Buffer{})
118+
if err != nil {
119+
t.Fatal(err)
120+
}
121+
122+
w := httptest.NewRecorder()
123+
124+
testAPI.metrics(w, req)
125+
126+
if expected, got := http.StatusOK, w.Code; expected != got {
127+
t.Errorf("Wanted status code %v, got %v.", expected, got)
128+
}
129+
130+
requiredResponse := `{"status":"success","data":[]}`
131+
132+
if expected, got := requiredResponse, w.Body.String(); expected != got {
133+
t.Errorf("Wanted response %q, got %q.", requiredResponse, w.Body.String())
134+
}
135+
136+
testTime, _ := time.Parse(time.RFC3339Nano, "2020-03-10T00:54:08.025744841+05:30")
137+
138+
errCh := make(chan error, 1)
139+
140+
dms.SubmitWriteRequest(storage.WriteRequest{
141+
Labels: grouping1,
142+
Timestamp: testTime,
143+
MetricFamilies: testutil.MetricFamiliesMap(mf1),
144+
Done: errCh,
145+
})
146+
147+
for err := range errCh {
148+
t.Fatal("Unexpected error:", err)
149+
}
150+
151+
w = httptest.NewRecorder()
152+
153+
testAPI.metrics(w, req)
154+
155+
var prettyJSON bytes.Buffer
156+
json.Indent(&prettyJSON, w.Body.Bytes(), "", "\t")
157+
158+
requiredResponse = `{
159+
"status": "success",
160+
"data": [
161+
{
162+
"labels": {
163+
"instance": "inst'a\"n\\ce1",
164+
"job": "Björn"
165+
},
166+
"last_push_successful": true,
167+
"mf1": {
168+
"time_stamp": "2020-03-10T00:54:08.025744841+05:30",
169+
"type": "SUMMARY",
170+
"metrics": [
171+
{
172+
"count": "0",
173+
"labels": {
174+
"instance": "inst'a\"n\\ce1",
175+
"job": "Björn"
176+
},
177+
"quantiles": {},
178+
"sum": "0"
179+
}
180+
]
181+
},
182+
"push_failure_time_seconds": {
183+
"time_stamp": "2020-03-10T00:54:08.025744841+05:30",
184+
"type": "GAUGE",
185+
"help": "Last Unix time when changing this group in the Pushgateway failed.",
186+
"metrics": [
187+
{
188+
"labels": {
189+
"instance": "inst'a\"n\\ce1",
190+
"job": "Björn"
191+
},
192+
"value": "0"
193+
}
194+
]
195+
},
196+
"push_time_seconds": {
197+
"time_stamp": "2020-03-10T00:54:08.025744841+05:30",
198+
"type": "GAUGE",
199+
"help": "Last Unix time when changing this group in the Pushgateway succeeded.",
200+
"metrics": [
201+
{
202+
"labels": {
203+
"instance": "inst'a\"n\\ce1",
204+
"job": "Björn"
205+
},
206+
"value": "1.583781848025745e+09"
207+
}
208+
]
209+
}
210+
}
211+
]
212+
}`
213+
214+
if expected, got := http.StatusOK, w.Code; expected != got {
215+
t.Errorf("Wanted status code %v, got %v.", expected, got)
216+
}
217+
218+
if expected, got := requiredResponse, prettyJSON.String(); expected != got {
219+
t.Errorf("Wanted response %q, got %q.", expected, got)
220+
}
221+
}

0 commit comments

Comments
 (0)