grafana/pkg/infra/usagestats/statscollector/prometheus_flavor_test.go
Serge Zaitsev 9cdb6b07c7
Chore: Move stats service into a standalone packge from sqlstore (#59574)
* move original stats service into a separate package

* add stats service to wire

* move GetAdminStats

* switch to using stats.Service

* add missing package

* fix api tests
2022-11-30 18:11:07 +01:00

90 lines
2.4 KiB
Go

package statscollector
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/services/datasources"
"github.com/grafana/grafana/pkg/services/sqlstore/mockstore"
"github.com/grafana/grafana/pkg/services/stats/statstest"
"github.com/grafana/grafana/pkg/setting"
)
func TestDetectPrometheusVariant(t *testing.T) {
vanilla := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"status":"success","data":{"version":"","revision":"","branch":"","buildUser":"","buildDate":"","goVersion":"go1.17.6"}}`)
}))
t.Cleanup(vanilla.Close)
mimir := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprint(w, `{"status":"success","data":{"application":"Grafana Mimir","version":"2.0.0","revision":"9fd2da5","branch":"HEAD","goVersion":"go1.17.8","features":{"ruler_config_api":"true","alertmanager_config_api":"true","query_sharding":"false","federated_rules":"false"}}}`)
}))
t.Cleanup(mimir.Close)
cortex := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}))
t.Cleanup(cortex.Close)
sqlStore := mockstore.NewSQLStoreMock()
statsService := statstest.NewFakeService()
s := createService(
t,
setting.NewCfg(),
sqlStore,
statsService,
withDatasources(mockDatasourceService{datasources: []*datasources.DataSource{
{
Id: 1,
OrgId: 1,
Version: 1,
Name: "Vanilla",
Type: "prometheus",
Access: "proxy",
Url: vanilla.URL,
},
{
Id: 2,
OrgId: 1,
Version: 1,
Name: "Mimir",
Type: "prometheus",
Access: "proxy",
Url: mimir.URL,
},
{
Id: 3,
OrgId: 1,
Version: 1,
Name: "Another Mimir",
Type: "prometheus",
Access: "proxy",
Url: mimir.URL,
},
{
Id: 4,
OrgId: 1,
Version: 1,
Name: "Cortex",
Type: "prometheus",
Access: "proxy",
Url: cortex.URL,
},
}}),
)
flavors, err := s.detectPrometheusVariants(context.Background())
require.NoError(t, err)
assert.Equal(t, int64(2), flavors["mimir"])
assert.Equal(t, int64(1), flavors["vanilla"])
assert.Equal(t, int64(1), flavors["cortex-like"])
}