| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | package plugins | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2016-03-12 17:13:49 +08:00
										 |  |  | 	"io/ioutil" | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 07:13:06 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2020-02-28 19:51:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/dashboards" | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							|  |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							|  |  |  | 	"gopkg.in/ini.v1" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestDashboardImport(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2017-11-28 19:49:10 +08:00
										 |  |  | 	pluginScenario("When importing a plugin dashboard", t, func() { | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 		origNewDashboardService := dashboards.NewService | 
					
						
							|  |  |  | 		mock := &dashboards.FakeDashboardService{} | 
					
						
							|  |  |  | 		dashboards.MockDashboardService(mock) | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		cmd := ImportDashboardCommand{ | 
					
						
							|  |  |  | 			PluginId: "test-app", | 
					
						
							|  |  |  | 			Path:     "dashboards/connections.json", | 
					
						
							|  |  |  | 			OrgId:    1, | 
					
						
							| 
									
										
										
										
											2020-02-28 19:51:21 +08:00
										 |  |  | 			User:     &models.SignedInUser{UserId: 1, OrgRole: models.ROLE_ADMIN}, | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 			Inputs: []ImportDashboardInput{ | 
					
						
							|  |  |  | 				{Name: "*", Type: "datasource", Value: "graphite"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 19:49:10 +08:00
										 |  |  | 		err := ImportDashboard(&cmd) | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 		So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("should install dashboard", func() { | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 			So(cmd.Result, ShouldNotBeNil) | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 			resultStr, _ := mock.SavedDashboards[0].Dashboard.Data.EncodePretty() | 
					
						
							| 
									
										
										
										
											2018-09-17 23:29:11 +08:00
										 |  |  | 			expectedBytes, _ := ioutil.ReadFile("testdata/test-app/dashboards/connections_result.json") | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 			expectedJson, _ := simplejson.NewJson(expectedBytes) | 
					
						
							|  |  |  | 			expectedStr, _ := expectedJson.EncodePretty() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(string(resultStr), ShouldEqual, string(expectedStr)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 			panel := mock.SavedDashboards[0].Dashboard.Data.Get("rows").GetIndex(0).Get("panels").GetIndex(0) | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 			So(panel.Get("datasource").MustString(), ShouldEqual, "graphite") | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2018-02-19 18:12:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Reset(func() { | 
					
						
							|  |  |  | 			dashboards.NewService = origNewDashboardService | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2017-11-28 01:23:57 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 	Convey("When evaling dashboard template", t, func() { | 
					
						
							| 
									
										
										
										
											2016-03-12 07:13:06 +08:00
										 |  |  | 		template, _ := simplejson.NewJson([]byte(`{ | 
					
						
							| 
									
										
										
										
											2017-11-28 00:08:39 +08:00
										 |  |  | 		"__inputs": [ | 
					
						
							|  |  |  | 			{ | 
					
						
							|  |  |  | 						"name": "DS_NAME", | 
					
						
							|  |  |  | 			"type": "datasource" | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		], | 
					
						
							|  |  |  | 		"test": { | 
					
						
							|  |  |  | 			"prop": "${DS_NAME}" | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		}`)) | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		evaluator := &DashTemplateEvaluator{ | 
					
						
							|  |  |  | 			template: template, | 
					
						
							|  |  |  | 			inputs: []ImportDashboardInput{ | 
					
						
							|  |  |  | 				{Name: "*", Type: "datasource", Value: "my-server"}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		res, err := evaluator.Eval() | 
					
						
							|  |  |  | 		So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("should render template", func() { | 
					
						
							| 
									
										
										
										
											2016-03-12 07:13:06 +08:00
										 |  |  | 			So(res.GetPath("test", "prop").MustString(), ShouldEqual, "my-server") | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-03-12 06:28:33 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("should not include inputs in output", func() { | 
					
						
							| 
									
										
										
										
											2016-03-12 07:13:06 +08:00
										 |  |  | 			inputs := res.Get("__inputs") | 
					
						
							|  |  |  | 			So(inputs.Interface(), ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-03-12 06:28:33 +08:00
										 |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-12 00:31:57 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-11-28 19:49:10 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func pluginScenario(desc string, t *testing.T, fn func()) { | 
					
						
							|  |  |  | 	Convey("Given a plugin", t, func() { | 
					
						
							| 
									
										
										
										
											2018-04-30 22:21:04 +08:00
										 |  |  | 		setting.Raw = ini.Empty() | 
					
						
							|  |  |  | 		sec, _ := setting.Raw.NewSection("plugin.test-app") | 
					
						
							| 
									
										
										
										
											2019-10-12 03:02:15 +08:00
										 |  |  | 		_, err := sec.NewKey("path", "testdata/test-app") | 
					
						
							|  |  |  | 		So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2017-11-28 19:49:10 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-25 19:25:39 +08:00
										 |  |  | 		pm := &PluginManager{ | 
					
						
							|  |  |  | 			Cfg: &setting.Cfg{ | 
					
						
							|  |  |  | 				FeatureToggles: map[string]bool{}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-10-12 03:02:15 +08:00
										 |  |  | 		err = pm.Init() | 
					
						
							| 
									
										
										
										
											2018-04-27 21:11:55 +08:00
										 |  |  | 		So(err, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2019-10-12 03:02:15 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 19:49:10 +08:00
										 |  |  | 		Convey(desc, fn) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } |