| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | package sqlstore | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | func mockTimeNow() { | 
					
						
							|  |  |  | 	var timeSeed int64 | 
					
						
							|  |  |  | 	timeNow = func() time.Time { | 
					
						
							|  |  |  | 		fakeNow := time.Unix(timeSeed, 0) | 
					
						
							| 
									
										
										
										
											2018-07-18 04:17:00 +08:00
										 |  |  | 		timeSeed++ | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 		return fakeNow | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func resetTimeNow() { | 
					
						
							|  |  |  | 	timeNow = time.Now | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | func TestAlertingDataAccess(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 	mockTimeNow() | 
					
						
							|  |  |  | 	defer resetTimeNow() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-19 22:52:20 +08:00
										 |  |  | 	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") | 
					
						
							| 
									
										
										
										
											2018-07-18 04:17:00 +08:00
										 |  |  | 		evalData, _ := simplejson.NewJson([]byte(`{"test": "test"}`)) | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							| 
									
										
										
										
											2018-07-18 04:17:00 +08:00
										 |  |  | 				EvalData:    evalData, | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 21:31:03 +08:00
										 |  |  | 			alert, _ := getAlertById(1) | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 			stateDateBeforePause := alert.NewStateDate | 
					
						
							| 
									
										
										
										
											2016-12-19 20:24:45 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 			Convey("can pause all alerts", func() { | 
					
						
							|  |  |  | 				pauseAllAlerts(true) | 
					
						
							| 
									
										
										
										
											2016-12-19 20:24:45 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Convey("cannot updated paused alert", func() { | 
					
						
							|  |  |  | 					cmd := &m.SetAlertStateCommand{ | 
					
						
							|  |  |  | 						AlertId: 1, | 
					
						
							|  |  |  | 						State:   m.AlertStateOK, | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					err = SetAlertState(cmd) | 
					
						
							|  |  |  | 					So(err, ShouldNotBeNil) | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Convey("pausing alerts should update their NewStateDate", func() { | 
					
						
							|  |  |  | 					alert, _ = getAlertById(1) | 
					
						
							|  |  |  | 					stateDateAfterPause := alert.NewStateDate | 
					
						
							|  |  |  | 					So(stateDateBeforePause, ShouldHappenBefore, stateDateAfterPause) | 
					
						
							|  |  |  | 				}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Convey("unpausing alerts should update their NewStateDate again", func() { | 
					
						
							|  |  |  | 					pauseAllAlerts(false) | 
					
						
							|  |  |  | 					alert, _ = getAlertById(1) | 
					
						
							|  |  |  | 					stateDateAfterUnpause := alert.NewStateDate | 
					
						
							|  |  |  | 					So(stateDateBeforePause, ShouldHappenBefore, stateDateAfterUnpause) | 
					
						
							|  |  |  | 				}) | 
					
						
							| 
									
										
										
										
											2016-12-19 20:24:45 +08:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 		Convey("Can read properties", func() { | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 			alertQuery := m.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, PanelId: 1, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}} | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 			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) | 
					
						
							| 
									
										
										
										
											2018-07-18 04:17:00 +08:00
										 |  |  | 			So(alert.Id, ShouldBeGreaterThan, 0) | 
					
						
							|  |  |  | 			So(alert.DashboardId, ShouldEqual, testDash.Id) | 
					
						
							|  |  |  | 			So(alert.PanelId, ShouldEqual, 1) | 
					
						
							| 
									
										
										
										
											2016-06-06 23:11:46 +08:00
										 |  |  | 			So(alert.Name, ShouldEqual, "Alerting title") | 
					
						
							| 
									
										
										
										
											2018-11-05 17:23:43 +08:00
										 |  |  | 			So(alert.State, ShouldEqual, m.AlertStateUnknown) | 
					
						
							| 
									
										
										
										
											2018-07-18 04:17:00 +08:00
										 |  |  | 			So(alert.NewStateDate, ShouldNotBeNil) | 
					
						
							|  |  |  | 			So(alert.EvalData, ShouldNotBeNil) | 
					
						
							|  |  |  | 			So(alert.EvalData.Get("test").MustString(), ShouldEqual, "test") | 
					
						
							|  |  |  | 			So(alert.EvalDate, ShouldNotBeNil) | 
					
						
							|  |  |  | 			So(alert.ExecutionError, ShouldEqual, "") | 
					
						
							|  |  |  | 			So(alert.DashboardUid, ShouldNotBeNil) | 
					
						
							|  |  |  | 			So(alert.DashboardSlug, ShouldEqual, "dashboard-with-alerts") | 
					
						
							| 
									
										
										
										
											2016-04-20 22:46:24 +08:00
										 |  |  | 		}) | 
					
						
							| 
									
										
										
										
											2016-04-22 22:51:24 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-16 20:54:59 +08:00
										 |  |  | 		Convey("Viewer cannot read alerts", func() { | 
					
						
							| 
									
										
										
										
											2018-06-01 18:39:44 +08:00
										 |  |  | 			viewerUser := &m.SignedInUser{OrgRole: m.ROLE_VIEWER, OrgId: 1} | 
					
						
							|  |  |  | 			alertQuery := m.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, PanelId: 1, OrgId: 1, User: viewerUser} | 
					
						
							| 
									
										
										
										
											2018-02-16 20:54:59 +08:00
										 |  |  | 			err2 := HandleAlertsQuery(&alertQuery) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			So(err2, ShouldBeNil) | 
					
						
							| 
									
										
										
										
											2018-06-01 18:39:44 +08:00
										 |  |  | 			So(alertQuery.Result, ShouldHaveLength, 1) | 
					
						
							| 
									
										
										
										
											2018-02-16 20:54:59 +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() { | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 				query := m.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}} | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 				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() { | 
					
						
							| 
									
										
										
										
											2018-11-05 17:23:43 +08:00
										 |  |  | 					So(query.Result[0].State, ShouldEqual, m.AlertStateUnknown) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 				queryForDashboard := m.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}} | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 				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() { | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 					query := m.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}} | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 					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() { | 
					
						
							| 
									
										
										
										
											2018-06-01 20:36:40 +08:00
										 |  |  | 				query := m.GetAlertsQuery{DashboardIDs: []int64{testDash.Id}, OrgId: 1, User: &m.SignedInUser{OrgRole: m.ROLE_ADMIN}} | 
					
						
							| 
									
										
										
										
											2016-05-10 15:45:56 +08:00
										 |  |  | 				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
										 |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestPausingAlerts(t *testing.T) { | 
					
						
							|  |  |  | 	mockTimeNow() | 
					
						
							|  |  |  | 	defer resetTimeNow() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Convey("Given an alert", t, func() { | 
					
						
							|  |  |  | 		InitTestDB(t) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		testDash := insertTestDashboard("dashboard with alerts", 1, 0, false, "alert") | 
					
						
							|  |  |  | 		alert, _ := insertTestAlert("Alerting title", "Alerting message", testDash.OrgId, testDash.Id, simplejson.New()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		stateDateBeforePause := alert.NewStateDate | 
					
						
							|  |  |  | 		stateDateAfterPause := stateDateBeforePause | 
					
						
							|  |  |  | 		Convey("when paused", func() { | 
					
						
							|  |  |  | 			pauseAlert(testDash.OrgId, 1, true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("the NewStateDate should be updated", func() { | 
					
						
							| 
									
										
										
										
											2018-02-16 21:31:03 +08:00
										 |  |  | 				alert, _ := getAlertById(1) | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				stateDateAfterPause = alert.NewStateDate | 
					
						
							|  |  |  | 				So(stateDateBeforePause, ShouldHappenBefore, stateDateAfterPause) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Convey("when unpaused", func() { | 
					
						
							|  |  |  | 			pauseAlert(testDash.OrgId, 1, false) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			Convey("the NewStateDate should be updated again", func() { | 
					
						
							| 
									
										
										
										
											2018-02-16 21:31:03 +08:00
										 |  |  | 				alert, _ := getAlertById(1) | 
					
						
							| 
									
										
										
										
											2018-02-16 20:06:28 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				stateDateAfterUnpause := alert.NewStateDate | 
					
						
							|  |  |  | 				So(stateDateAfterPause, ShouldHappenBefore, stateDateAfterUnpause) | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		}) | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func pauseAlert(orgId int64, alertId int64, pauseState bool) (int64, error) { | 
					
						
							|  |  |  | 	cmd := &m.PauseAlertCommand{ | 
					
						
							|  |  |  | 		OrgId:    orgId, | 
					
						
							|  |  |  | 		AlertIds: []int64{alertId}, | 
					
						
							|  |  |  | 		Paused:   pauseState, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err := PauseAlert(cmd) | 
					
						
							|  |  |  | 	So(err, ShouldBeNil) | 
					
						
							|  |  |  | 	return cmd.ResultCount, err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | func insertTestAlert(title string, message string, orgId int64, dashId int64, settings *simplejson.Json) (*m.Alert, error) { | 
					
						
							|  |  |  | 	items := []*m.Alert{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			PanelId:     1, | 
					
						
							|  |  |  | 			DashboardId: dashId, | 
					
						
							|  |  |  | 			OrgId:       orgId, | 
					
						
							|  |  |  | 			Name:        title, | 
					
						
							|  |  |  | 			Message:     message, | 
					
						
							|  |  |  | 			Settings:    settings, | 
					
						
							|  |  |  | 			Frequency:   1, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	cmd := m.SaveAlertsCommand{ | 
					
						
							|  |  |  | 		Alerts:      items, | 
					
						
							|  |  |  | 		DashboardId: dashId, | 
					
						
							|  |  |  | 		OrgId:       orgId, | 
					
						
							|  |  |  | 		UserId:      1, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	err := SaveAlerts(&cmd) | 
					
						
							|  |  |  | 	return cmd.Alerts[0], err | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func getAlertById(id int64) (*m.Alert, error) { | 
					
						
							|  |  |  | 	q := &m.GetAlertByIdQuery{ | 
					
						
							|  |  |  | 		Id: id, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err := GetAlertById(q) | 
					
						
							|  |  |  | 	So(err, ShouldBeNil) | 
					
						
							|  |  |  | 	return q.Result, err | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2018-02-16 21:31:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | func pauseAllAlerts(pauseState bool) error { | 
					
						
							|  |  |  | 	cmd := &m.PauseAllAlertCommand{ | 
					
						
							|  |  |  | 		Paused: pauseState, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	err := PauseAllAlerts(cmd) | 
					
						
							|  |  |  | 	So(err, ShouldBeNil) | 
					
						
							|  |  |  | 	return err | 
					
						
							|  |  |  | } |