| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-10 17:37:03 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/components/simplejson" | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | 	m "github.com/grafana/grafana/pkg/models" | 
					
						
							| 
									
										
										
										
											2016-04-20 15:38:44 +08:00
										 |  |  | 	. "github.com/smartystreets/goconvey/convey" | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestAlertingDataAccess(t *testing.T) { | 
					
						
							|  |  |  | 	Convey("Testing Alerting data access", t, func() { | 
					
						
							|  |  |  | 		InitTestDB(t) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-27 20:36:28 +08:00
										 |  |  | 		testDash := insertTestDashboard("dashboard with alerts", 1, 0, false, "alert") | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-06-11 16:26:48 +08:00
										 |  |  | 		items := []*m.Alert{ | 
					
						
							| 
									
										
										
										
											2016-06-08 14:50:11 +08:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2016-06-10 04:21:28 +08:00
										 |  |  | 				PanelId:     1, | 
					
						
							|  |  |  | 				DashboardId: testDash.Id, | 
					
						
							|  |  |  | 				OrgId:       testDash.OrgId, | 
					
						
							|  |  |  | 				Name:        "Alerting title", | 
					
						
							| 
									
										
										
										
											2016-08-12 16:12:04 +08:00
										 |  |  | 				Message:     "Alerting message", | 
					
						
							| 
									
										
										
										
											2016-06-13 21:18:19 +08:00
										 |  |  | 				Settings:    simplejson.New(), | 
					
						
							| 
									
										
										
										
											2016-06-17 14:27:38 +08:00
										 |  |  | 				Frequency:   1, | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cmd := m.SaveAlertsCommand{ | 
					
						
							| 
									
										
										
										
											2016-05-16 22:20:28 +08:00
										 |  |  | 			Alerts:      items, | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 			DashboardId: testDash.Id, | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 			OrgId:       1, | 
					
						
							|  |  |  | 			UserId:      1, | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 		err := SaveAlerts(&cmd) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 		Convey("Can create one alert", func() { | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 20:24:45 +08:00
										 |  |  | 		Convey("Can set new states", func() { | 
					
						
							|  |  |  | 			Convey("new state ok", func() { | 
					
						
							|  |  |  | 				cmd := &m.SetAlertStateCommand{ | 
					
						
							|  |  |  | 					AlertId: 1, | 
					
						
							|  |  |  | 					State:   m.AlertStateOK, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				err = SetAlertState(cmd) | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("can pause alert", func() { | 
					
						
							|  |  |  | 				cmd := &m.PauseAllAlertCommand{ | 
					
						
							|  |  |  | 					Paused: true, | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 21:36:44 +08:00
										 |  |  | 				err = PauseAllAlerts(cmd) | 
					
						
							| 
									
										
										
										
											2016-12-19 20:24:45 +08:00
										 |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("cannot updated paused alert", func() { | 
					
						
							|  |  |  | 					cmd := &m.SetAlertStateCommand{ | 
					
						
							|  |  |  | 						AlertId: 1, | 
					
						
							|  |  |  | 						State:   m.AlertStateOK, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					err = SetAlertState(cmd) | 
					
						
							|  |  |  | 					So(err, ShouldNotBeNil) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 		Convey("Can read properties", func() { | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 			alertQuery := m.GetAlertsQuery{DashboardId: testDash.Id, PanelId: 1, OrgId: 1} | 
					
						
							|  |  |  | 			err2 := HandleAlertsQuery(&alertQuery) | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 			alert := alertQuery.Result[0] | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 			So(err2, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-06-06 23:11:46 +08:00
										 |  |  | 			So(alert.Name, ShouldEqual, "Alerting title") | 
					
						
							| 
									
										
										
										
											2016-08-12 16:12:04 +08:00
										 |  |  | 			So(alert.Message, ShouldEqual, "Alerting message") | 
					
						
							| 
									
										
										
										
											2016-11-01 22:30:55 +08:00
										 |  |  | 			So(alert.State, ShouldEqual, "pending") | 
					
						
							| 
									
										
										
										
											2016-06-17 14:27:38 +08:00
										 |  |  | 			So(alert.Frequency, ShouldEqual, 1) | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Alerts with same dashboard id and panel id should update", func() { | 
					
						
							|  |  |  | 			modifiedItems := items | 
					
						
							| 
									
										
										
										
											2016-06-10 17:37:03 +08:00
										 |  |  | 			modifiedItems[0].Name = "Name" | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			modifiedCmd := m.SaveAlertsCommand{ | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 				DashboardId: testDash.Id, | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 				OrgId:       1, | 
					
						
							|  |  |  | 				UserId:      1, | 
					
						
							| 
									
										
										
										
											2016-05-16 22:20:28 +08:00
										 |  |  | 				Alerts:      modifiedItems, | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			err := SaveAlerts(&modifiedCmd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Can save alerts with same dashboard and panel id", func() { | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Alerts should be updated", func() { | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 				query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} | 
					
						
							|  |  |  | 				err2 := HandleAlertsQuery(&query) | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				So(err2, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-04-28 21:13:42 +08:00
										 |  |  | 				So(len(query.Result), ShouldEqual, 1) | 
					
						
							| 
									
										
										
										
											2016-06-10 04:21:28 +08:00
										 |  |  | 				So(query.Result[0].Name, ShouldEqual, "Name") | 
					
						
							| 
									
										
										
										
											2016-04-28 17:47:14 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Convey("Alert state should not be updated", func() { | 
					
						
							| 
									
										
										
										
											2016-11-01 22:30:55 +08:00
										 |  |  | 					So(query.Result[0].State, ShouldEqual, "pending") | 
					
						
							| 
									
										
										
										
											2016-04-28 17:47:14 +08:00
										 |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-04-25 22:18:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Updates without changes should be ignored", func() { | 
					
						
							|  |  |  | 				err3 := SaveAlerts(&modifiedCmd) | 
					
						
							|  |  |  | 				So(err3, ShouldBeNil) | 
					
						
							|  |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Convey("Multiple alerts per dashboard", func() { | 
					
						
							| 
									
										
										
										
											2016-06-11 16:26:48 +08:00
										 |  |  | 			multipleItems := []*m.Alert{ | 
					
						
							| 
									
										
										
										
											2016-06-08 14:50:11 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 					DashboardId: testDash.Id, | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 					PanelId:     1, | 
					
						
							| 
									
										
										
										
											2016-06-10 04:21:28 +08:00
										 |  |  | 					Name:        "1", | 
					
						
							| 
									
										
										
										
											2016-04-25 22:18:28 +08:00
										 |  |  | 					OrgId:       1, | 
					
						
							| 
									
										
										
										
											2016-06-13 21:18:19 +08:00
										 |  |  | 					Settings:    simplejson.New(), | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-06-08 14:50:11 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 					DashboardId: testDash.Id, | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 					PanelId:     2, | 
					
						
							| 
									
										
										
										
											2016-06-10 04:21:28 +08:00
										 |  |  | 					Name:        "2", | 
					
						
							| 
									
										
										
										
											2016-04-25 22:18:28 +08:00
										 |  |  | 					OrgId:       1, | 
					
						
							| 
									
										
										
										
											2016-06-13 21:18:19 +08:00
										 |  |  | 					Settings:    simplejson.New(), | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-06-08 14:50:11 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 					DashboardId: testDash.Id, | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 					PanelId:     3, | 
					
						
							| 
									
										
										
										
											2016-06-10 04:21:28 +08:00
										 |  |  | 					Name:        "3", | 
					
						
							| 
									
										
										
										
											2016-04-25 22:18:28 +08:00
										 |  |  | 					OrgId:       1, | 
					
						
							| 
									
										
										
										
											2016-06-13 21:18:19 +08:00
										 |  |  | 					Settings:    simplejson.New(), | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 22:20:28 +08:00
										 |  |  | 			cmd.Alerts = multipleItems | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 			err = SaveAlerts(&cmd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("Should save 3 dashboards", func() { | 
					
						
							|  |  |  | 				So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 				queryForDashboard := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} | 
					
						
							|  |  |  | 				err2 := HandleAlertsQuery(&queryForDashboard) | 
					
						
							| 
									
										
										
										
											2016-04-28 21:13:42 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 				So(err2, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-04-28 21:13:42 +08:00
										 |  |  | 				So(len(queryForDashboard.Result), ShouldEqual, 3) | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("should updated two dashboards and delete one", func() { | 
					
						
							|  |  |  | 				missingOneAlert := multipleItems[:2] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-16 22:20:28 +08:00
										 |  |  | 				cmd.Alerts = missingOneAlert | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 				err = SaveAlerts(&cmd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("should delete the missing alert", func() { | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 					query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} | 
					
						
							|  |  |  | 					err2 := HandleAlertsQuery(&query) | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 					So(err2, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-04-28 21:13:42 +08:00
										 |  |  | 					So(len(query.Result), ShouldEqual, 2) | 
					
						
							| 
									
										
										
										
											2016-04-22 23:49:50 +08:00
										 |  |  | 				}) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 		Convey("When dashboard is removed", func() { | 
					
						
							| 
									
										
										
										
											2016-06-11 16:26:48 +08:00
										 |  |  | 			items := []*m.Alert{ | 
					
						
							| 
									
										
										
										
											2016-06-08 14:50:11 +08:00
										 |  |  | 				{ | 
					
						
							| 
									
										
										
										
											2016-06-10 04:21:28 +08:00
										 |  |  | 					PanelId:     1, | 
					
						
							|  |  |  | 					DashboardId: testDash.Id, | 
					
						
							|  |  |  | 					Name:        "Alerting title", | 
					
						
							| 
									
										
										
										
											2016-08-12 16:12:04 +08:00
										 |  |  | 					Message:     "Alerting message", | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 			cmd := m.SaveAlertsCommand{ | 
					
						
							| 
									
										
										
										
											2016-05-16 22:20:28 +08:00
										 |  |  | 				Alerts:      items, | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 				DashboardId: testDash.Id, | 
					
						
							|  |  |  | 				OrgId:       1, | 
					
						
							|  |  |  | 				UserId:      1, | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 			SaveAlerts(&cmd) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 14:34:48 +08:00
										 |  |  | 			err = DeleteDashboard(&m.DeleteDashboardCommand{ | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 				OrgId: 1, | 
					
						
							| 
									
										
										
										
											2017-06-18 06:24:38 +08:00
										 |  |  | 				Id:    testDash.Id, | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 14:34:48 +08:00
										 |  |  | 			So(err, ShouldBeNil) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 			Convey("Alerts should be removed", func() { | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 				query := m.GetAlertsQuery{DashboardId: testDash.Id, OrgId: 1} | 
					
						
							|  |  |  | 				err2 := HandleAlertsQuery(&query) | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				So(testDash.Id, ShouldEqual, 1) | 
					
						
							|  |  |  | 				So(err2, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2016-04-28 21:13:42 +08:00
										 |  |  | 				So(len(query.Result), ShouldEqual, 0) | 
					
						
							| 
									
										
										
										
											2016-04-23 16:25:51 +08:00
										 |  |  | 			}) | 
					
						
							| 
									
										
										
										
											2016-04-23 20:14:18 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | 	}) | 
					
						
							|  |  |  | } |