|
1 | 1 | package api
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "encoding/json" |
4 | 5 | "io"
|
5 | 6 | "net/http"
|
6 | 7 | "net/http/httptest"
|
7 | 8 | "strings"
|
8 | 9 | "testing"
|
9 | 10 |
|
| 11 | + "github.com/prometheus/common/version" |
| 12 | + v1 "github.com/prometheus/prometheus/web/api/v1" |
10 | 13 | "github.com/stretchr/testify/assert"
|
11 | 14 | "github.com/stretchr/testify/require"
|
| 15 | + "github.com/weaveworks/common/user" |
| 16 | + |
| 17 | + "github.com/cortexproject/cortex/pkg/purger" |
12 | 18 | )
|
13 | 19 |
|
14 | 20 | func TestIndexHandlerPrefix(t *testing.T) {
|
@@ -189,3 +195,53 @@ func TestConfigOverrideHandler(t *testing.T) {
|
189 | 195 | assert.NoError(t, err)
|
190 | 196 | assert.Equal(t, []byte("config"), body)
|
191 | 197 | }
|
| 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