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