Skip to content

Commit e08a33f

Browse files
committed
add unit test
Signed-off-by: Ben Ye <[email protected]>
1 parent 3340c90 commit e08a33f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## master / unreleased
4+
* [FEATURE] Querier/Query Frontend: support Prometheus /api/v1/status/buildinfo API. #4978
45

56
## 1.14.0 in progress
67

pkg/api/handlers_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
package api
22

33
import (
4+
"encoding/json"
45
"io"
56
"net/http"
67
"net/http/httptest"
78
"strings"
89
"testing"
910

11+
"github.com/prometheus/common/version"
12+
v1 "github.com/prometheus/prometheus/web/api/v1"
1013
"github.com/stretchr/testify/assert"
1114
"github.com/stretchr/testify/require"
15+
"github.com/weaveworks/common/user"
16+
17+
"github.com/cortexproject/cortex/pkg/purger"
1218
)
1319

1420
func TestIndexHandlerPrefix(t *testing.T) {
@@ -189,3 +195,53 @@ func TestConfigOverrideHandler(t *testing.T) {
189195
assert.NoError(t, err)
190196
assert.Equal(t, []byte("config"), body)
191197
}
198+
199+
func TestBuildInfoAPI(t *testing.T) {
200+
type buildInfo struct {
201+
Status string `json:"status"`
202+
Data v1.PrometheusVersion `json:"data"`
203+
}
204+
205+
for _, tc := range []struct {
206+
name string
207+
version string
208+
branch string
209+
revision string
210+
expected buildInfo
211+
}{
212+
{
213+
name: "empty",
214+
expected: buildInfo{Status: "success"},
215+
},
216+
{
217+
name: "set versions",
218+
version: "v0.14.0",
219+
branch: "test",
220+
revision: "foo",
221+
expected: buildInfo{Status: "success", Data: v1.PrometheusVersion{
222+
Version: "v0.14.0",
223+
Branch: "test",
224+
Revision: "foo",
225+
}},
226+
},
227+
} {
228+
t.Run(tc.name, func(t *testing.T) {
229+
cfg := Config{}
230+
version.Version = tc.version
231+
version.Branch = tc.branch
232+
version.Revision = tc.revision
233+
handler := NewQuerierHandler(cfg, nil, nil, nil, nil, purger.NewNoopTombstonesLoader(), nil, &FakeLogger{})
234+
writer := httptest.NewRecorder()
235+
req := httptest.NewRequest("GET", "/api/v1/status/buildinfo", nil)
236+
req = req.WithContext(user.InjectOrgID(req.Context(), "test"))
237+
handler.ServeHTTP(writer, req)
238+
out, err := io.ReadAll(writer.Body)
239+
require.NoError(t, err)
240+
241+
var info buildInfo
242+
err = json.Unmarshal(out, &info)
243+
require.NoError(t, err)
244+
require.Equal(t, tc.expected, info)
245+
})
246+
}
247+
}

0 commit comments

Comments
 (0)