grafana/pkg/services/updatechecker/plugins_test.go
Giuseppe Guerra a89202eab2
Plugins: Improve instrumentation by adding metrics and tracing (#61035)
* WIP: Plugins tracing

* Trace ID middleware

* Add prometheus metrics and tracing to plugins updater

* Add TODOs

* Add instrumented http client

* Add tracing to grafana update checker

* Goimports

* Moved plugins tracing to middleware

* goimports, fix tests

* Removed X-Trace-Id header

* Fix comment in NewTracingHeaderMiddleware

* Add metrics to instrumented http client

* Add instrumented http client options

* Removed unused function

* Switch to contextual logger

* Refactoring, fix tests

* Moved InstrumentedHTTPClient and PrometheusMetrics to their own package

* Tracing middleware: handle errors

* Report span status codes when recording errors

* Add tests for tracing middleware

* Moved fakeSpan and fakeTracer to pkg/infra/tracing

* Add TestHTTPClientTracing

* Lint

* Changes after PR review

* Tests: Made "ended" in FakeSpan private, allow calling End only once

* Testing: panic in FakeSpan if span already ended

* Refactoring: Simplify Grafana updater checks

* Refactoring: Simplify plugins updater error checks and logs

* Fix wrong call to checkForUpdates -> instrumentedCheckForUpdates

* Tests: Fix wrong call to checkForUpdates -> instrumentedCheckForUpdates

* Log update checks duration, use Info log level for check succeeded logs

* Add plugin context span attributes in tracing_middleware

* Refactor prometheus metrics as httpclient middleware

* Fix call to ProvidePluginsService in plugins_test.go

* Propagate context to update checker outgoing http requests

* Plugin client tracing middleware: Removed operation name in status

* Fix tests

* Goimports tracing_middleware.go

* Goimports

* Fix imports

* Changed span name to plugins client middleware

* Add span name assertion in TestTracingMiddleware

* Removed Prometheus metrics middleware from grafana and plugins updatechecker

* Add span attributes for ds name, type, uid, panel and dashboard ids

* Fix http header reading in tracing middlewares

* Use contexthandler.FromContext, add X-Query-Group-Id

* Add test for RunStream

* Fix imports

* Changes from PR review

* TestTracingMiddleware: Changed assert to require for didPanic assertion

* Lint

* Fix imports
2023-03-28 11:01:06 +02:00

212 lines
4.8 KiB
Go

package updatechecker
import (
"context"
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/plugins"
)
func TestPluginUpdateChecker_HasUpdate(t *testing.T) {
t.Run("update is available", func(t *testing.T) {
svc := PluginsService{
availableUpdates: map[string]string{
"test-ds": "1.0.0",
},
pluginStore: plugins.FakePluginStore{
PluginList: []plugins.PluginDTO{
{
JSONData: plugins.JSONData{
ID: "test-ds",
Info: plugins.Info{Version: "0.9.0"},
},
},
},
},
}
update, exists := svc.HasUpdate(context.Background(), "test-ds")
require.True(t, exists)
require.Equal(t, "1.0.0", update)
})
t.Run("update is not available", func(t *testing.T) {
svc := PluginsService{
availableUpdates: map[string]string{
"test-panel": "0.9.0",
"test-app": "0.0.1",
},
pluginStore: plugins.FakePluginStore{
PluginList: []plugins.PluginDTO{
{
JSONData: plugins.JSONData{
ID: "test-ds",
Info: plugins.Info{Version: "0.9.0"},
},
},
{
JSONData: plugins.JSONData{
ID: "test-panel",
Info: plugins.Info{Version: "0.9.0"},
},
},
{
JSONData: plugins.JSONData{
ID: "test-app",
Info: plugins.Info{Version: "0.9.0"},
},
},
},
},
}
update, exists := svc.HasUpdate(context.Background(), "test-ds")
require.False(t, exists)
require.Empty(t, update)
update, exists = svc.HasUpdate(context.Background(), "test-panel")
require.False(t, exists)
require.Empty(t, update)
update, exists = svc.HasUpdate(context.Background(), "test-app")
require.False(t, exists)
require.Empty(t, update)
})
t.Run("update is available but plugin is not in store", func(t *testing.T) {
svc := PluginsService{
availableUpdates: map[string]string{
"test-panel": "0.9.0",
},
pluginStore: plugins.FakePluginStore{
PluginList: []plugins.PluginDTO{
{
JSONData: plugins.JSONData{
ID: "test-ds",
Info: plugins.Info{Version: "1.0.0"},
},
},
},
},
}
update, exists := svc.HasUpdate(context.Background(), "test-panel")
require.False(t, exists)
require.Empty(t, update)
update, exists = svc.HasUpdate(context.Background(), "test-ds")
require.False(t, exists)
require.Empty(t, update)
})
}
func TestPluginUpdateChecker_checkForUpdates(t *testing.T) {
t.Run("update is available", func(t *testing.T) {
jsonResp := `[
{
"slug": "test-ds",
"version": "1.0.12"
},
{
"slug": "test-panel",
"version": "2.5.7"
},
{
"slug": "test-core-panel",
"version": "1.0.0"
}
]`
svc := PluginsService{
availableUpdates: map[string]string{
"test-app": "1.0.0",
},
pluginStore: plugins.FakePluginStore{
PluginList: []plugins.PluginDTO{
{
JSONData: plugins.JSONData{
ID: "test-ds",
Info: plugins.Info{Version: "0.9.0"},
Type: plugins.DataSource,
},
Class: plugins.External,
},
{
JSONData: plugins.JSONData{
ID: "test-app",
Info: plugins.Info{Version: "0.5.0"},
Type: plugins.App,
},
Class: plugins.External,
},
{
JSONData: plugins.JSONData{
ID: "test-panel",
Info: plugins.Info{Version: "2.5.7"},
Type: plugins.Panel,
},
Class: plugins.Bundled,
},
{
JSONData: plugins.JSONData{
ID: "test-core-panel",
Info: plugins.Info{Version: "0.0.1"},
Type: plugins.Panel,
},
Class: plugins.Core,
},
},
},
httpClient: &fakeHTTPClient{
fakeResp: jsonResp,
},
log: log.NewNopLogger(),
tracer: tracing.InitializeTracerForTest(),
}
svc.instrumentedCheckForUpdates(context.Background())
require.Equal(t, 1, len(svc.availableUpdates))
require.Equal(t, "1.0.12", svc.availableUpdates["test-ds"])
update, exists := svc.HasUpdate(context.Background(), "test-ds")
require.True(t, exists)
require.Equal(t, "1.0.12", update)
require.Empty(t, svc.availableUpdates["test-app"])
update, exists = svc.HasUpdate(context.Background(), "test-app")
require.False(t, exists)
require.Empty(t, update)
require.Empty(t, svc.availableUpdates["test-panel"])
update, exists = svc.HasUpdate(context.Background(), "test-panel")
require.False(t, exists)
require.Empty(t, update)
require.Empty(t, svc.availableUpdates["test-core-panel"])
})
}
type fakeHTTPClient struct {
fakeResp string
requestURL string
}
func (c *fakeHTTPClient) Do(req *http.Request) (*http.Response, error) {
c.requestURL = req.URL.String()
resp := &http.Response{
Body: io.NopCloser(strings.NewReader(c.fakeResp)),
}
return resp, nil
}