grafana/pkg/promlib/heuristics_test.go
Andreas Christou 2616366a0a
Prometheus: Reintroduce Azure audience override feature flag (#90339)
* Re-add feature flag with deprecation note

* Hide the field in frontend if ff disabled

* Block scope overriding if ff is disabled in backend

- Update promlib to forward logger to extendOptions
- Add warning
- Update tests

* Default toggle to true for now

* Update description

* Update prom tests

* Fix lint
2024-07-17 15:09:55 +01:00

94 lines
2.9 KiB
Go

package promlib
import (
"context"
"io"
"net/http"
"strconv"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/backend/datasource"
sdkhttpclient "github.com/grafana/grafana-plugin-sdk-go/backend/httpclient"
"github.com/grafana/grafana-plugin-sdk-go/backend/log"
)
type heuristicsSuccessRoundTripper struct {
res io.ReadCloser
status int
}
func (rt *heuristicsSuccessRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return &http.Response{
Status: strconv.Itoa(rt.status),
StatusCode: rt.status,
Header: nil,
Body: rt.res,
ContentLength: 0,
Request: req,
}, nil
}
func newHeuristicsSDKProvider(hrt heuristicsSuccessRoundTripper) *sdkhttpclient.Provider {
anotherFN := func(o sdkhttpclient.Options, next http.RoundTripper) http.RoundTripper {
return &hrt
}
fn := sdkhttpclient.MiddlewareFunc(anotherFN)
mid := sdkhttpclient.NamedMiddlewareFunc("mock", fn)
return sdkhttpclient.NewProvider(sdkhttpclient.ProviderOptions{Middlewares: []sdkhttpclient.Middleware{mid}})
}
func mockExtendClientOpts(ctx context.Context, settings backend.DataSourceInstanceSettings, clientOpts *sdkhttpclient.Options, log log.Logger) error {
return nil
}
func Test_GetHeuristics(t *testing.T) {
t.Run("should return Prometheus", func(t *testing.T) {
rt := heuristicsSuccessRoundTripper{
res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"version\":\"1.0\"}}")),
status: http.StatusOK,
}
httpProvider := newHeuristicsSDKProvider(rt)
logger := backend.NewLoggerWith("logger", "test")
s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, logger, mockExtendClientOpts)),
logger: logger,
}
req := HeuristicsRequest{
PluginContext: getPluginContext(),
}
res, err := s.GetHeuristics(context.Background(), req)
assert.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, KindPrometheus, res.Application)
assert.Equal(t, Features{RulerApiEnabled: false}, res.Features)
})
t.Run("should return Mimir", func(t *testing.T) {
rt := heuristicsSuccessRoundTripper{
res: io.NopCloser(strings.NewReader("{\"status\":\"success\",\"data\":{\"features\":{\"foo\":\"bar\"},\"version\":\"1.0\"}}")),
status: http.StatusOK,
}
httpProvider := newHeuristicsSDKProvider(rt)
logger := backend.NewLoggerWith("logger", "test")
s := &Service{
im: datasource.NewInstanceManager(newInstanceSettings(httpProvider, logger, mockExtendClientOpts)),
logger: logger,
}
req := HeuristicsRequest{
PluginContext: getPluginContext(),
}
res, err := s.GetHeuristics(context.Background(), req)
assert.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, KindMimir, res.Application)
assert.Equal(t, Features{RulerApiEnabled: true}, res.Features)
})
}