2021-03-08 07:02:49 +01:00
|
|
|
package manager
|
2016-03-10 19:57:48 +01:00
|
|
|
|
|
|
|
|
import (
|
2021-10-05 13:26:24 +02:00
|
|
|
"context"
|
2016-03-10 19:57:48 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/bus"
|
2016-07-08 18:21:25 +02:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-02-28 12:51:21 +01:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2021-11-01 09:53:33 +00:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/manager/loader"
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/manager/signature"
|
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
2016-03-10 19:57:48 +01:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-03-18 13:53:01 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-03-10 19:57:48 +01:00
|
|
|
)
|
|
|
|
|
|
2021-03-18 13:53:01 +01:00
|
|
|
func TestGetPluginDashboards(t *testing.T) {
|
2021-08-25 15:11:22 +02:00
|
|
|
cfg := &setting.Cfg{
|
2021-03-18 13:53:01 +01:00
|
|
|
FeatureToggles: map[string]bool{},
|
|
|
|
|
PluginSettings: setting.PluginSettings{
|
|
|
|
|
"test-app": map[string]string{
|
|
|
|
|
"path": "testdata/test-app",
|
2020-03-25 12:25:39 +01:00
|
|
|
},
|
2021-03-18 13:53:01 +01:00
|
|
|
},
|
2021-08-25 15:11:22 +02:00
|
|
|
}
|
2021-11-01 09:53:33 +00:00
|
|
|
pm := newManager(cfg, nil, loader.New(nil, cfg, &signature.UnsignedPluginAuthorizer{Cfg: cfg}), &sqlstore.SQLStore{})
|
2021-08-25 15:11:22 +02:00
|
|
|
err := pm.init()
|
2021-03-18 13:53:01 +01:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-12-28 16:08:07 +01:00
|
|
|
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardQuery) error {
|
2021-03-18 13:53:01 +01:00
|
|
|
if query.Slug == "nginx-connections" {
|
|
|
|
|
dash := models.NewDashboard("Nginx Connections")
|
|
|
|
|
dash.Data.Set("revision", "1.1")
|
|
|
|
|
query.Result = dash
|
|
|
|
|
return nil
|
2020-03-25 12:25:39 +01:00
|
|
|
}
|
2016-03-10 19:57:48 +01:00
|
|
|
|
2021-03-18 13:53:01 +01:00
|
|
|
return models.ErrDashboardNotFound
|
|
|
|
|
})
|
2016-03-10 19:57:48 +01:00
|
|
|
|
2021-12-28 16:08:07 +01:00
|
|
|
bus.AddHandler("test", func(ctx context.Context, query *models.GetDashboardsByPluginIdQuery) error {
|
2021-03-18 13:53:01 +01:00
|
|
|
var data = simplejson.New()
|
|
|
|
|
data.Set("title", "Nginx Connections")
|
|
|
|
|
data.Set("revision", 22)
|
2016-07-08 18:21:25 +02:00
|
|
|
|
2021-03-18 13:53:01 +01:00
|
|
|
query.Result = []*models.Dashboard{
|
|
|
|
|
{Slug: "nginx-connections", Data: data},
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
})
|
2016-03-10 19:57:48 +01:00
|
|
|
|
2021-12-02 18:08:59 +01:00
|
|
|
dashboards, err := pm.GetPluginDashboards(context.Background(), 1, "test-app")
|
2021-03-18 13:53:01 +01:00
|
|
|
require.NoError(t, err)
|
2016-03-10 19:57:48 +01:00
|
|
|
|
2021-08-25 15:11:22 +02:00
|
|
|
require.Len(t, dashboards, 2)
|
|
|
|
|
require.Equal(t, "Nginx Connections", dashboards[0].Title)
|
|
|
|
|
require.Equal(t, int64(25), dashboards[0].Revision)
|
|
|
|
|
require.Equal(t, int64(22), dashboards[0].ImportedRevision)
|
|
|
|
|
require.Equal(t, "db/nginx-connections", dashboards[0].ImportedUri)
|
2016-03-10 19:57:48 +01:00
|
|
|
|
2021-08-25 15:11:22 +02:00
|
|
|
require.Equal(t, int64(2), dashboards[1].Revision)
|
|
|
|
|
require.Equal(t, int64(0), dashboards[1].ImportedRevision)
|
2016-03-10 19:57:48 +01:00
|
|
|
}
|