@@ -7,93 +7,119 @@ import (
7
7
8
8
"github.com/grafana/grafana/pkg/components/simplejson"
9
9
m "github.com/grafana/grafana/pkg/models"
10
+ "github.com/grafana/grafana/pkg/services/alerting/alertstates"
10
11
. "github.com/smartystreets/goconvey/convey"
11
12
)
12
13
13
14
func TestAlertNotificationExtraction (t * testing.T ) {
15
+ Convey ("Notifier tests" , t , func () {
16
+ Convey ("rules for sending notifications" , func () {
17
+ dummieNotifier := NotifierImpl {}
18
+
19
+ result := & AlertResult {
20
+ State : alertstates .Critical ,
21
+ }
22
+
23
+ notifier := & Notification {
24
+ Name : "Test Notifier" ,
25
+ Type : "TestType" ,
26
+ SendCritical : true ,
27
+ SendWarning : true ,
28
+ }
29
+
30
+ Convey ("Should send notification" , func () {
31
+ So (dummieNotifier .ShouldDispath (result , notifier ), ShouldBeTrue )
32
+ })
14
33
15
- Convey ("Parsing alert notification from settings" , t , func () {
16
- Convey ("Parsing email" , func () {
17
- Convey ("empty settings should return error" , func () {
18
- json := `{ }`
34
+ Convey ("warn:false and state:warn should not send" , func () {
35
+ result .State = alertstates .Warn
36
+ notifier .SendWarning = false
37
+ So (dummieNotifier .ShouldDispath (result , notifier ), ShouldBeFalse )
38
+ })
39
+ })
19
40
20
- settingsJSON , _ := simplejson .NewJson ([]byte (json ))
21
- model := & m.AlertNotification {
22
- Name : "ops" ,
23
- Type : "email" ,
24
- Settings : settingsJSON ,
25
- }
41
+ Convey ("Parsing alert notification from settings" , func () {
42
+ Convey ("Parsing email" , func () {
43
+ Convey ("empty settings should return error" , func () {
44
+ json := `{ }`
26
45
27
- _ , err := NewNotificationFromDBModel (model )
28
- So (err , ShouldNotBeNil )
29
- })
46
+ settingsJSON , _ := simplejson .NewJson ([]byte (json ))
47
+ model := & m.AlertNotification {
48
+ Name : "ops" ,
49
+ Type : "email" ,
50
+ Settings : settingsJSON ,
51
+ }
30
52
31
- Convey ("from settings" , func () {
32
- json := `
53
+ _ , err := NewNotificationFromDBModel (model )
54
+ So (err , ShouldNotBeNil )
55
+ })
56
+
57
+ Convey ("from settings" , func () {
58
+ json := `
33
59
{
34
60
"to": "ops@grafana.org"
35
61
}`
36
62
37
- settingsJSON , _ := simplejson .NewJson ([]byte (json ))
38
- model := & m.AlertNotification {
39
- Name : "ops" ,
40
- Type : "email" ,
41
- Settings : settingsJSON ,
42
- }
63
+ settingsJSON , _ := simplejson .NewJson ([]byte (json ))
64
+ model := & m.AlertNotification {
65
+ Name : "ops" ,
66
+ Type : "email" ,
67
+ Settings : settingsJSON ,
68
+ }
43
69
44
- not , err := NewNotificationFromDBModel (model )
70
+ not , err := NewNotificationFromDBModel (model )
45
71
46
- So (err , ShouldBeNil )
47
- So (not .Name , ShouldEqual , "ops" )
48
- So (not .Type , ShouldEqual , "email" )
49
- So (reflect .TypeOf (not .Notifierr ).Elem ().String (), ShouldEqual , "alerting.EmailNotifier" )
72
+ So (err , ShouldBeNil )
73
+ So (not .Name , ShouldEqual , "ops" )
74
+ So (not .Type , ShouldEqual , "email" )
75
+ So (reflect .TypeOf (not .Notifierr ).Elem ().String (), ShouldEqual , "alerting.EmailNotifier" )
50
76
51
- email := not .Notifierr .(* EmailNotifier )
52
- So (email .To , ShouldEqual , "ops@grafana.org" )
77
+ email := not .Notifierr .(* EmailNotifier )
78
+ So (email .To , ShouldEqual , "ops@grafana.org" )
79
+ })
53
80
})
54
- })
55
81
56
- Convey ("Parsing webhook" , func () {
57
- Convey ("empty settings should return error" , func () {
58
- json := `{ }`
82
+ Convey ("Parsing webhook" , func () {
83
+ Convey ("empty settings should return error" , func () {
84
+ json := `{ }`
59
85
60
- settingsJSON , _ := simplejson .NewJson ([]byte (json ))
61
- model := & m.AlertNotification {
62
- Name : "ops" ,
63
- Type : "webhook" ,
64
- Settings : settingsJSON ,
65
- }
86
+ settingsJSON , _ := simplejson .NewJson ([]byte (json ))
87
+ model := & m.AlertNotification {
88
+ Name : "ops" ,
89
+ Type : "webhook" ,
90
+ Settings : settingsJSON ,
91
+ }
66
92
67
- _ , err := NewNotificationFromDBModel (model )
68
- So (err , ShouldNotBeNil )
69
- })
93
+ _ , err := NewNotificationFromDBModel (model )
94
+ So (err , ShouldNotBeNil )
95
+ })
70
96
71
- Convey ("from settings" , func () {
72
- json := `
97
+ Convey ("from settings" , func () {
98
+ json := `
73
99
{
74
100
"url": "http://localhost:3000",
75
101
"username": "username",
76
102
"password": "password"
77
103
}`
78
104
79
- settingsJSON , _ := simplejson .NewJson ([]byte (json ))
80
- model := & m.AlertNotification {
81
- Name : "slack" ,
82
- Type : "webhook" ,
83
- Settings : settingsJSON ,
84
- }
105
+ settingsJSON , _ := simplejson .NewJson ([]byte (json ))
106
+ model := & m.AlertNotification {
107
+ Name : "slack" ,
108
+ Type : "webhook" ,
109
+ Settings : settingsJSON ,
110
+ }
85
111
86
- not , err := NewNotificationFromDBModel (model )
112
+ not , err := NewNotificationFromDBModel (model )
87
113
88
- So (err , ShouldBeNil )
89
- So (not .Name , ShouldEqual , "slack" )
90
- So (not .Type , ShouldEqual , "webhook" )
91
- So (reflect .TypeOf (not .Notifierr ).Elem ().String (), ShouldEqual , "alerting.WebhookNotifier" )
114
+ So (err , ShouldBeNil )
115
+ So (not .Name , ShouldEqual , "slack" )
116
+ So (not .Type , ShouldEqual , "webhook" )
117
+ So (reflect .TypeOf (not .Notifierr ).Elem ().String (), ShouldEqual , "alerting.WebhookNotifier" )
92
118
93
- webhook := not .Notifierr .(* WebhookNotifier )
94
- So (webhook .Url , ShouldEqual , "http://localhost:3000" )
119
+ webhook := not .Notifierr .(* WebhookNotifier )
120
+ So (webhook .Url , ShouldEqual , "http://localhost:3000" )
121
+ })
95
122
})
96
123
})
97
-
98
124
})
99
125
}
0 commit comments