| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | package manager | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	"path/filepath" | 
					
						
							|  |  |  | 	"strings" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/plugins" | 
					
						
							| 
									
										
										
										
											2022-01-14 20:30:39 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/plugins/backendplugin/provider" | 
					
						
							| 
									
										
										
										
											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/licensing" | 
					
						
							|  |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/assert" | 
					
						
							|  |  |  | 	"github.com/stretchr/testify/require" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"gopkg.in/ini.v1" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestPluginManager_int_init(t *testing.T) { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	staticRootPath, err := filepath.Abs("../../../public/") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bundledPluginsPath, err := filepath.Abs("../../../plugins-bundled/internal") | 
					
						
							|  |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cfg := &setting.Cfg{ | 
					
						
							|  |  |  | 		Raw:                ini.Empty(), | 
					
						
							|  |  |  | 		Env:                setting.Prod, | 
					
						
							|  |  |  | 		StaticRootPath:     staticRootPath, | 
					
						
							|  |  |  | 		BundledPluginsPath: bundledPluginsPath, | 
					
						
							|  |  |  | 		PluginSettings: map[string]map[string]string{ | 
					
						
							|  |  |  | 			"plugin.datasource-id": { | 
					
						
							|  |  |  | 				"path": "testdata/test-app", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	license := &licensing.OSSLicensingService{ | 
					
						
							|  |  |  | 		Cfg: cfg, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-14 20:30:39 +08:00
										 |  |  | 	pmCfg := plugins.FromGrafanaCfg(cfg) | 
					
						
							|  |  |  | 	pm, err := ProvideService(cfg, nil, loader.New(pmCfg, license, | 
					
						
							|  |  |  | 		&signature.UnsignedPluginAuthorizer{Cfg: pmCfg}, &provider.Service{}), nil) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	require.NoError(t, err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	verifyCorePluginCatalogue(t, pm) | 
					
						
							|  |  |  | 	verifyBundledPlugins(t, pm) | 
					
						
							|  |  |  | 	verifyPluginStaticRoutes(t, pm) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func verifyCorePluginCatalogue(t *testing.T, pm *PluginManager) { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expPanels := map[string]struct{}{ | 
					
						
							|  |  |  | 		"alertGroups":    {}, | 
					
						
							|  |  |  | 		"alertlist":      {}, | 
					
						
							|  |  |  | 		"annolist":       {}, | 
					
						
							|  |  |  | 		"barchart":       {}, | 
					
						
							|  |  |  | 		"bargauge":       {}, | 
					
						
							|  |  |  | 		"canvas":         {}, | 
					
						
							|  |  |  | 		"dashlist":       {}, | 
					
						
							|  |  |  | 		"debug":          {}, | 
					
						
							|  |  |  | 		"gauge":          {}, | 
					
						
							|  |  |  | 		"geomap":         {}, | 
					
						
							|  |  |  | 		"gettingstarted": {}, | 
					
						
							|  |  |  | 		"graph":          {}, | 
					
						
							|  |  |  | 		"heatmap":        {}, | 
					
						
							|  |  |  | 		"histogram":      {}, | 
					
						
							|  |  |  | 		"icon":           {}, | 
					
						
							|  |  |  | 		"live":           {}, | 
					
						
							|  |  |  | 		"logs":           {}, | 
					
						
							| 
									
										
										
										
											2021-11-16 06:09:59 +08:00
										 |  |  | 		"candlestick":    {}, | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		"news":           {}, | 
					
						
							|  |  |  | 		"nodeGraph":      {}, | 
					
						
							|  |  |  | 		"piechart":       {}, | 
					
						
							|  |  |  | 		"pluginlist":     {}, | 
					
						
							|  |  |  | 		"stat":           {}, | 
					
						
							|  |  |  | 		"state-timeline": {}, | 
					
						
							|  |  |  | 		"status-history": {}, | 
					
						
							|  |  |  | 		"table":          {}, | 
					
						
							|  |  |  | 		"table-old":      {}, | 
					
						
							|  |  |  | 		"text":           {}, | 
					
						
							|  |  |  | 		"timeseries":     {}, | 
					
						
							|  |  |  | 		"welcome":        {}, | 
					
						
							|  |  |  | 		"xychart":        {}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expDataSources := map[string]struct{}{ | 
					
						
							|  |  |  | 		"alertmanager": {}, | 
					
						
							|  |  |  | 		"dashboard":    {}, | 
					
						
							|  |  |  | 		"input":        {}, | 
					
						
							|  |  |  | 		"jaeger":       {}, | 
					
						
							|  |  |  | 		"mixed":        {}, | 
					
						
							|  |  |  | 		"zipkin":       {}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	expApps := map[string]struct{}{ | 
					
						
							|  |  |  | 		"test-app": {}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	panels := pm.Plugins(context.Background(), plugins.Panel) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	assert.Equal(t, len(expPanels), len(panels)) | 
					
						
							|  |  |  | 	for _, p := range panels { | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 		p, exists := pm.Plugin(context.Background(), p.ID) | 
					
						
							|  |  |  | 		require.NotEqual(t, plugins.PluginDTO{}, p) | 
					
						
							|  |  |  | 		assert.True(t, exists) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		assert.Contains(t, expPanels, p.ID) | 
					
						
							|  |  |  | 		assert.Contains(t, pm.registeredPlugins(), p.ID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	dataSources := pm.Plugins(context.Background(), plugins.DataSource) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	assert.Equal(t, len(expDataSources), len(dataSources)) | 
					
						
							|  |  |  | 	for _, ds := range dataSources { | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 		p, exists := pm.Plugin(context.Background(), ds.ID) | 
					
						
							|  |  |  | 		require.NotEqual(t, plugins.PluginDTO{}, p) | 
					
						
							|  |  |  | 		assert.True(t, exists) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		assert.Contains(t, expDataSources, ds.ID) | 
					
						
							|  |  |  | 		assert.Contains(t, pm.registeredPlugins(), ds.ID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	apps := pm.Plugins(context.Background(), plugins.App) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	assert.Equal(t, len(expApps), len(apps)) | 
					
						
							|  |  |  | 	for _, app := range apps { | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 		p, exists := pm.Plugin(context.Background(), app.ID) | 
					
						
							|  |  |  | 		require.NotEqual(t, plugins.PluginDTO{}, p) | 
					
						
							|  |  |  | 		assert.True(t, exists) | 
					
						
							|  |  |  | 		assert.Contains(t, expApps, app.ID) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		assert.Contains(t, pm.registeredPlugins(), app.ID) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	assert.Equal(t, len(expPanels)+len(expDataSources)+len(expApps), len(pm.Plugins(context.Background()))) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func verifyBundledPlugins(t *testing.T, pm *PluginManager) { | 
					
						
							|  |  |  | 	t.Helper() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dsPlugins := make(map[string]struct{}) | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	for _, p := range pm.Plugins(context.Background(), plugins.DataSource) { | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 		dsPlugins[p.ID] = struct{}{} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	pluginRoutes := make(map[string]*plugins.StaticRoute) | 
					
						
							|  |  |  | 	for _, r := range pm.Routes() { | 
					
						
							|  |  |  | 		pluginRoutes[r.PluginID] = r | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	inputPlugin, exists := pm.Plugin(context.Background(), "input") | 
					
						
							|  |  |  | 	require.NotEqual(t, plugins.PluginDTO{}, inputPlugin) | 
					
						
							|  |  |  | 	assert.True(t, exists) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	assert.NotNil(t, dsPlugins["input"]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, pluginID := range []string{"input"} { | 
					
						
							|  |  |  | 		assert.Contains(t, pluginRoutes, pluginID) | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 		assert.True(t, strings.HasPrefix(pluginRoutes[pluginID].Directory, inputPlugin.PluginDir)) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func verifyPluginStaticRoutes(t *testing.T, pm *PluginManager) { | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	routes := make(map[string]*plugins.StaticRoute) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	for _, route := range pm.Routes() { | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 		routes[route.PluginID] = route | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	assert.Len(t, routes, 2) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	inputPlugin, _ := pm.Plugin(context.Background(), "input") | 
					
						
							|  |  |  | 	assert.NotNil(t, routes["input"]) | 
					
						
							|  |  |  | 	assert.Equal(t, routes["input"].Directory, inputPlugin.PluginDir) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-17 19:04:22 +08:00
										 |  |  | 	testAppPlugin, _ := pm.Plugin(context.Background(), "test-app") | 
					
						
							|  |  |  | 	assert.Contains(t, routes, "test-app") | 
					
						
							|  |  |  | 	assert.Equal(t, routes["test-app"].Directory, testAppPlugin.PluginDir) | 
					
						
							| 
									
										
										
										
											2021-11-01 17:53:33 +08:00
										 |  |  | } |