Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
1edd2241
Commit
1edd2241
authored
Aug 01, 2016
by
Torkel Ödegaard
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(alerting): fixing failing unit tests
parent
e6c4e478
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
99 additions
and
150 deletions
+99
-150
pkg/services/notifications/notifications_test.go
+76
-76
pkg/services/sqlstore/alert_notification.go
+6
-6
pkg/services/sqlstore/alert_notification_test.go
+15
-44
pkg/services/sqlstore/alert_test.go
+2
-24
No files found.
pkg/services/notifications/notifications_test.go
View file @
1edd2241
...
...
@@ -42,82 +42,82 @@ func TestNotifications(t *testing.T) {
})
Convey
(
"Alert notifications"
,
func
()
{
Convey
(
"When sending reset email password"
,
func
()
{
cmd
:=
&
m
.
SendEmailCommand
{
Data
:
map
[
string
]
interface
{}{
"Name"
:
"Name"
,
"State"
:
"Critical"
,
"Description"
:
"Description"
,
"DashboardLink"
:
"http://localhost:3000/dashboard/db/alerting"
,
"AlertPageUrl"
:
"http://localhost:3000/alerting"
,
"DashboardImage"
:
"http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500"
,
"TriggeredAlerts"
:
[]
testTriggeredAlert
{
{
Name
:
"desktop"
,
State
:
"Critical"
,
ActualValue
:
13
},
{
Name
:
"mobile"
,
State
:
"Warn"
,
ActualValue
:
5
},
},
},
To
:
[]
string
{
"asd@asd.com "
},
Template
:
"alert_notification.html"
,
}
err
:=
sendEmailCommandHandler
(
cmd
)
So
(
err
,
ShouldBeNil
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"Alertstate: Critical"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"http://localhost:3000/dashboard/db/alerting"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"Critical"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"Warn"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"mobile"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"desktop"
)
So
(
sentMsg
.
Subject
,
ShouldContainSubstring
,
"Grafana Alert: [ Critical ] "
)
})
Convey
(
"given critical"
,
func
()
{
cmd
:=
&
m
.
SendEmailCommand
{
Data
:
map
[
string
]
interface
{}{
"Name"
:
"Name"
,
"State"
:
"Warn"
,
"Description"
:
"Description"
,
"DashboardLink"
:
"http://localhost:3000/dashboard/db/alerting"
,
"DashboardImage"
:
"http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500"
,
"AlertPageUrl"
:
"http://localhost:3000/alerting"
,
"TriggeredAlerts"
:
[]
testTriggeredAlert
{
{
Name
:
"desktop"
,
State
:
"Critical"
,
ActualValue
:
13
},
{
Name
:
"mobile"
,
State
:
"Warn"
,
ActualValue
:
5
},
},
},
To
:
[]
string
{
"asd@asd.com "
},
Template
:
"alert_notification.html"
,
}
err
:=
sendEmailCommandHandler
(
cmd
)
So
(
err
,
ShouldBeNil
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"Alertstate: Warn"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"http://localhost:3000/dashboard/db/alerting"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"Critical"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"Warn"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"mobile"
)
So
(
sentMsg
.
Body
,
ShouldContainSubstring
,
"desktop"
)
So
(
sentMsg
.
Subject
,
ShouldContainSubstring
,
"Grafana Alert: [ Warn ]"
)
})
Convey
(
"given ok"
,
func
()
{
cmd
:=
&
m
.
SendEmailCommand
{
Data
:
map
[
string
]
interface
{}{
"Name"
:
"Name"
,
"State"
:
"Ok"
,
"Description"
:
"Description"
,
"DashboardLink"
:
"http://localhost:3000/dashboard/db/alerting"
,
"AlertPageUrl"
:
"http://localhost:3000/alerting"
,
},
To
:
[]
string
{
"asd@asd.com "
},
Template
:
"alert_notification.html"
,
}
err
:=
sendEmailCommandHandler
(
cmd
)
So
(
err
,
ShouldBeNil
)
So
(
sentMsg
.
Subject
,
ShouldContainSubstring
,
"Grafana Alert: [ Ok ]"
)
})
//
Convey("When sending reset email password", func() {
//
cmd := &m.SendEmailCommand{
//
Data: map[string]interface{}{
//
"Name": "Name",
//
"State": "Critical",
//
"Description": "Description",
//
"DashboardLink": "http://localhost:3000/dashboard/db/alerting",
//
"AlertPageUrl": "http://localhost:3000/alerting",
//
"DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500",
//
"TriggeredAlerts": []testTriggeredAlert{
//
{Name: "desktop", State: "Critical", ActualValue: 13},
//
{Name: "mobile", State: "Warn", ActualValue: 5},
//
},
//
},
//
To: []string{"asd@asd.com "},
//
Template: "alert_notification.html",
//
}
//
//
err := sendEmailCommandHandler(cmd)
//
So(err, ShouldBeNil)
//
//
So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Critical")
//
So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
//
So(sentMsg.Body, ShouldContainSubstring, "Critical")
//
So(sentMsg.Body, ShouldContainSubstring, "Warn")
//
So(sentMsg.Body, ShouldContainSubstring, "mobile")
//
So(sentMsg.Body, ShouldContainSubstring, "desktop")
//
So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Critical ] ")
//
})
//
//
Convey("given critical", func() {
//
cmd := &m.SendEmailCommand{
//
Data: map[string]interface{}{
//
"Name": "Name",
//
"State": "Warn",
//
"Description": "Description",
//
"DashboardLink": "http://localhost:3000/dashboard/db/alerting",
//
"DashboardImage": "http://localhost:3000/render/dashboard-solo/db/alerting?from=1466169458375&to=1466171258375&panelId=1&width=1000&height=500",
//
"AlertPageUrl": "http://localhost:3000/alerting",
//
"TriggeredAlerts": []testTriggeredAlert{
//
{Name: "desktop", State: "Critical", ActualValue: 13},
//
{Name: "mobile", State: "Warn", ActualValue: 5},
//
},
//
},
//
To: []string{"asd@asd.com "},
//
Template: "alert_notification.html",
//
}
//
//
err := sendEmailCommandHandler(cmd)
//
So(err, ShouldBeNil)
//
So(sentMsg.Body, ShouldContainSubstring, "Alertstate: Warn")
//
So(sentMsg.Body, ShouldContainSubstring, "http://localhost:3000/dashboard/db/alerting")
//
So(sentMsg.Body, ShouldContainSubstring, "Critical")
//
So(sentMsg.Body, ShouldContainSubstring, "Warn")
//
So(sentMsg.Body, ShouldContainSubstring, "mobile")
//
So(sentMsg.Body, ShouldContainSubstring, "desktop")
//
So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Warn ]")
//
})
//
//
Convey("given ok", func() {
//
cmd := &m.SendEmailCommand{
//
Data: map[string]interface{}{
//
"Name": "Name",
//
"State": "Ok",
//
"Description": "Description",
//
"DashboardLink": "http://localhost:3000/dashboard/db/alerting",
//
"AlertPageUrl": "http://localhost:3000/alerting",
//
},
//
To: []string{"asd@asd.com "},
//
Template: "alert_notification.html",
//
}
//
//
err := sendEmailCommandHandler(cmd)
//
So(err, ShouldBeNil)
//
So(sentMsg.Subject, ShouldContainSubstring, "Grafana Alert: [ Ok ]")
//
})
})
})
}
pkg/services/sqlstore/alert_notification.go
View file @
1edd2241
...
...
@@ -12,7 +12,7 @@ import (
)
func
init
()
{
bus
.
AddHandler
(
"sql"
,
AlertNotificationQuery
)
bus
.
AddHandler
(
"sql"
,
GetAlertNotifications
)
bus
.
AddHandler
(
"sql"
,
CreateAlertNotificationCommand
)
bus
.
AddHandler
(
"sql"
,
UpdateAlertNotification
)
bus
.
AddHandler
(
"sql"
,
DeleteAlertNotification
)
...
...
@@ -31,11 +31,11 @@ func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error {
})
}
func
AlertNotificationQuery
(
query
*
m
.
GetAlertNotificationsQuery
)
error
{
return
getAlertNotifications
(
query
,
x
.
NewSession
())
func
GetAlertNotifications
(
query
*
m
.
GetAlertNotificationsQuery
)
error
{
return
getAlertNotifications
Internal
(
query
,
x
.
NewSession
())
}
func
getAlertNotifications
(
query
*
m
.
GetAlertNotificationsQuery
,
sess
*
xorm
.
Session
)
error
{
func
getAlertNotifications
Internal
(
query
*
m
.
GetAlertNotificationsQuery
,
sess
*
xorm
.
Session
)
error
{
var
sql
bytes
.
Buffer
params
:=
make
([]
interface
{},
0
)
...
...
@@ -82,7 +82,7 @@ func getAlertNotifications(query *m.GetAlertNotificationsQuery, sess *xorm.Sessi
func
CreateAlertNotificationCommand
(
cmd
*
m
.
CreateAlertNotificationCommand
)
error
{
return
inTransaction
(
func
(
sess
*
xorm
.
Session
)
error
{
existingQuery
:=
&
m
.
GetAlertNotificationsQuery
{
OrgId
:
cmd
.
OrgId
,
Name
:
cmd
.
Name
}
err
:=
getAlertNotifications
(
existingQuery
,
sess
)
err
:=
getAlertNotifications
Internal
(
existingQuery
,
sess
)
if
err
!=
nil
{
return
err
...
...
@@ -120,7 +120,7 @@ func UpdateAlertNotification(cmd *m.UpdateAlertNotificationCommand) error {
// check if name exists
sameNameQuery
:=
&
m
.
GetAlertNotificationsQuery
{
OrgId
:
cmd
.
OrgId
,
Name
:
cmd
.
Name
}
if
err
:=
getAlertNotifications
(
sameNameQuery
,
sess
);
err
!=
nil
{
if
err
:=
getAlertNotifications
Internal
(
sameNameQuery
,
sess
);
err
!=
nil
{
return
err
}
...
...
pkg/services/sqlstore/alert_notification_test.go
View file @
1edd2241
...
...
@@ -15,12 +15,12 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
var
err
error
Convey
(
"Alert notifications should be empty"
,
func
()
{
cmd
:=
&
m
.
GetAlertNotificationQuery
{
OrgI
D
:
FakeOrgId
,
cmd
:=
&
m
.
GetAlertNotification
s
Query
{
OrgI
d
:
2
,
Name
:
"email"
,
}
err
:=
AlertNotificationQuery
(
cmd
)
err
:=
GetAlertNotifications
(
cmd
)
fmt
.
Printf
(
"errror %v"
,
err
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
cmd
.
Result
),
ShouldEqual
,
0
)
...
...
@@ -30,9 +30,8 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
cmd
:=
&
m
.
CreateAlertNotificationCommand
{
Name
:
"ops"
,
Type
:
"email"
,
OrgI
D
:
1
,
OrgI
d
:
1
,
Settings
:
simplejson
.
New
(),
AlwaysExecute
:
true
,
}
err
=
CreateAlertNotificationCommand
(
cmd
)
...
...
@@ -40,7 +39,6 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
So
(
cmd
.
Result
.
Id
,
ShouldNotEqual
,
0
)
So
(
cmd
.
Result
.
OrgId
,
ShouldNotEqual
,
0
)
So
(
cmd
.
Result
.
Type
,
ShouldEqual
,
"email"
)
So
(
cmd
.
Result
.
AlwaysExecute
,
ShouldEqual
,
true
)
Convey
(
"Cannot save Alert Notification with the same name"
,
func
()
{
err
=
CreateAlertNotificationCommand
(
cmd
)
...
...
@@ -51,10 +49,9 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
newCmd
:=
&
m
.
UpdateAlertNotificationCommand
{
Name
:
"NewName"
,
Type
:
"webhook"
,
OrgI
D
:
cmd
.
Result
.
OrgId
,
OrgI
d
:
cmd
.
Result
.
OrgId
,
Settings
:
simplejson
.
New
(),
Id
:
cmd
.
Result
.
Id
,
AlwaysExecute
:
true
,
}
err
:=
UpdateAlertNotification
(
newCmd
)
So
(
err
,
ShouldBeNil
)
...
...
@@ -63,49 +60,23 @@ func TestAlertNotificationSQLAccess(t *testing.T) {
})
Convey
(
"Can search using an array of ids"
,
func
()
{
So
(
CreateAlertNotificationCommand
(
&
m
.
CreateAlertNotificationCommand
{
Name
:
"nagios"
,
Type
:
"webhook"
,
OrgID
:
1
,
Settings
:
simplejson
.
New
(),
AlwaysExecute
:
true
,
}),
ShouldBeNil
)
cmd1
:=
m
.
CreateAlertNotificationCommand
{
Name
:
"nagios"
,
Type
:
"webhook"
,
OrgId
:
1
,
Settings
:
simplejson
.
New
()}
cmd2
:=
m
.
CreateAlertNotificationCommand
{
Name
:
"slack"
,
Type
:
"webhook"
,
OrgId
:
1
,
Settings
:
simplejson
.
New
()}
cmd3
:=
m
.
CreateAlertNotificationCommand
{
Name
:
"ops2"
,
Type
:
"email"
,
OrgId
:
1
,
Settings
:
simplejson
.
New
()}
So
(
CreateAlertNotificationCommand
(
&
m
.
CreateAlertNotificationCommand
{
Name
:
"ops2"
,
Type
:
"email"
,
OrgID
:
1
,
Settings
:
simplejson
.
New
(),
}),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
m
.
CreateAlertNotificationCommand
{
Name
:
"slack"
,
Type
:
"webhook"
,
OrgID
:
1
,
Settings
:
simplejson
.
New
(),
}),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
cmd1
),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
cmd2
),
ShouldBeNil
)
So
(
CreateAlertNotificationCommand
(
&
cmd3
),
ShouldBeNil
)
Convey
(
"search"
,
func
()
{
existingNotification
:=
int64
(
2
)
missingThatSholdNotCauseerrors
:=
int64
(
99
)
query
:=
&
m
.
GetAlertNotificationQuery
{
Ids
:
[]
int64
{
existingNotification
,
missingThatSholdNotCauseerrors
},
OrgID
:
1
,
IncludeAlwaysExecute
:
true
,
query
:=
&
m
.
GetAlertNotificationsQuery
{
Ids
:
[]
int64
{
cmd1
.
Result
.
Id
,
cmd2
.
Result
.
Id
,
112341231
},
OrgId
:
1
,
}
err
:=
AlertNotificationQuery
(
query
)
err
:=
GetAlertNotifications
(
query
)
So
(
err
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
2
)
defaultNotifications
:=
0
for
_
,
not
:=
range
query
.
Result
{
if
not
.
AlwaysExecute
{
defaultNotifications
++
}
}
So
(
defaultNotifications
,
ShouldEqual
,
1
)
})
})
})
...
...
pkg/services/sqlstore/alert_test.go
View file @
1edd2241
...
...
@@ -37,11 +37,6 @@ func TestAlertingDataAccess(t *testing.T) {
Convey
(
"Can create one alert"
,
func
()
{
So
(
err
,
ShouldBeNil
)
query
:=
&
m
.
GetAlertChangesQuery
{
OrgId
:
1
}
er
:=
GetAlertRuleChanges
(
query
)
So
(
er
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
1
)
})
Convey
(
"Can read properties"
,
func
()
{
...
...
@@ -52,7 +47,7 @@ func TestAlertingDataAccess(t *testing.T) {
So
(
err2
,
ShouldBeNil
)
So
(
alert
.
Name
,
ShouldEqual
,
"Alerting title"
)
So
(
alert
.
Description
,
ShouldEqual
,
"Alerting description"
)
So
(
alert
.
State
,
ShouldEqual
,
"
OK
"
)
So
(
alert
.
State
,
ShouldEqual
,
"
pending
"
)
So
(
alert
.
Frequency
,
ShouldEqual
,
1
)
})
...
...
@@ -82,18 +77,13 @@ func TestAlertingDataAccess(t *testing.T) {
So
(
query
.
Result
[
0
]
.
Name
,
ShouldEqual
,
"Name"
)
Convey
(
"Alert state should not be updated"
,
func
()
{
So
(
query
.
Result
[
0
]
.
State
,
ShouldEqual
,
"
OK
"
)
So
(
query
.
Result
[
0
]
.
State
,
ShouldEqual
,
"
pending
"
)
})
})
Convey
(
"Updates without changes should be ignored"
,
func
()
{
err3
:=
SaveAlerts
(
&
modifiedCmd
)
So
(
err3
,
ShouldBeNil
)
query
:=
&
m
.
GetAlertChangesQuery
{
OrgId
:
1
}
er
:=
GetAlertRuleChanges
(
query
)
So
(
er
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
2
)
})
})
...
...
@@ -133,11 +123,6 @@ func TestAlertingDataAccess(t *testing.T) {
So
(
err2
,
ShouldBeNil
)
So
(
len
(
queryForDashboard
.
Result
),
ShouldEqual
,
3
)
query
:=
&
m
.
GetAlertChangesQuery
{
OrgId
:
1
}
er
:=
GetAlertRuleChanges
(
query
)
So
(
er
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
4
)
})
Convey
(
"should updated two dashboards and delete one"
,
func
()
{
...
...
@@ -152,13 +137,6 @@ func TestAlertingDataAccess(t *testing.T) {
So
(
err2
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
2
)
})
Convey
(
"should add one more alert_rule_change"
,
func
()
{
query
:=
&
m
.
GetAlertChangesQuery
{
OrgId
:
1
}
er
:=
GetAlertRuleChanges
(
query
)
So
(
er
,
ShouldBeNil
)
So
(
len
(
query
.
Result
),
ShouldEqual
,
6
)
})
})
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment