2019-07-24 21:53:53 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb/notification"
|
|
|
|
|
|
|
|
"github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/mock"
|
|
|
|
"github.com/influxdata/influxdb/notification/rule"
|
|
|
|
influxTesting "github.com/influxdata/influxdb/testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_newNotificationRuleResponses(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
opt influxdb.FindOptions
|
|
|
|
filter influxdb.NotificationRuleFilter
|
|
|
|
nrs []influxdb.NotificationRule
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: args{
|
|
|
|
opt: influxdb.FindOptions{
|
|
|
|
Limit: 50,
|
|
|
|
Offset: 0,
|
|
|
|
Descending: true,
|
|
|
|
},
|
|
|
|
filter: influxdb.NotificationRuleFilter{
|
|
|
|
OrgID: influxTesting.IDPtr(influxdb.ID(2)),
|
|
|
|
},
|
|
|
|
nrs: []influxdb.NotificationRule{
|
|
|
|
&rule.Slack{
|
|
|
|
Channel: "ch1",
|
|
|
|
MessageTemplate: "message 1{var1}",
|
|
|
|
Base: rule.Base{
|
2019-08-15 23:24:22 +00:00
|
|
|
ID: influxdb.ID(1),
|
|
|
|
OrgID: influxdb.ID(2),
|
|
|
|
OwnerID: influxdb.ID(3),
|
2019-08-16 19:43:15 +00:00
|
|
|
EndpointID: 4,
|
2019-08-15 23:24:22 +00:00
|
|
|
Name: "name1",
|
|
|
|
Description: "desc1",
|
|
|
|
Status: influxdb.Active,
|
2019-08-16 19:43:15 +00:00
|
|
|
Every: mustDuration("5m"),
|
|
|
|
Offset: mustDuration("15s"),
|
2019-07-24 21:53:53 +00:00
|
|
|
TagRules: []notification.TagRule{
|
|
|
|
{
|
2019-08-28 16:25:54 +00:00
|
|
|
Tag: influxdb.Tag{Key: "k1", Value: "v1"},
|
2019-07-24 21:53:53 +00:00
|
|
|
Operator: notification.Equal,
|
|
|
|
},
|
|
|
|
{
|
2019-08-28 16:25:54 +00:00
|
|
|
Tag: influxdb.Tag{Key: "k2", Value: "v2"},
|
2019-07-24 21:53:53 +00:00
|
|
|
Operator: notification.NotRegexEqual,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StatusRules: []notification.StatusRule{
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.LevelRule{CheckLevel: notification.Critical, Operation: true},
|
|
|
|
Count: 3,
|
|
|
|
Period: influxdb.Duration{Duration: time.Hour},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.LevelRule{CheckLevel: notification.Warn, Operation: false},
|
|
|
|
Count: 30,
|
|
|
|
Period: influxdb.Duration{Duration: time.Minute * 30},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-08-15 23:24:22 +00:00
|
|
|
&rule.PagerDuty{
|
2019-08-27 17:29:49 +00:00
|
|
|
MessageTemplate: "body 2{var2}",
|
2019-07-24 21:53:53 +00:00
|
|
|
Base: rule.Base{
|
2019-08-15 23:24:22 +00:00
|
|
|
ID: influxdb.ID(11),
|
|
|
|
OrgID: influxdb.ID(2),
|
|
|
|
OwnerID: influxdb.ID(33),
|
2019-08-16 19:43:15 +00:00
|
|
|
EndpointID: 44,
|
2019-08-15 23:24:22 +00:00
|
|
|
Name: "name2",
|
|
|
|
Description: "desc2",
|
|
|
|
Status: influxdb.Inactive,
|
2019-07-24 21:53:53 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: `{
|
|
|
|
"links": {
|
|
|
|
"self": "/api/v2/notificationRules?descending=true&limit=50&offset=0&orgID=0000000000000002"
|
|
|
|
},
|
|
|
|
"notificationRules": [
|
|
|
|
{
|
2019-08-15 23:24:22 +00:00
|
|
|
"ownerID": "0000000000000003",
|
2019-07-24 21:53:53 +00:00
|
|
|
"channel": "ch1",
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"description": "desc1",
|
|
|
|
"endpointID": "0000000000000004",
|
2019-08-16 19:43:15 +00:00
|
|
|
"every": "5m",
|
2019-07-24 21:53:53 +00:00
|
|
|
"id": "0000000000000001",
|
|
|
|
"messageTemplate": "message 1{var1}",
|
|
|
|
"name": "name1",
|
|
|
|
"offset": "15s",
|
|
|
|
"orgID": "0000000000000002",
|
|
|
|
"runbookLink": "",
|
|
|
|
"status": "active",
|
|
|
|
"statusRules": [
|
|
|
|
{
|
|
|
|
"count": 3,
|
|
|
|
"currentLevel": {
|
|
|
|
"level": "CRIT",
|
|
|
|
"operation": "equal"
|
|
|
|
},
|
|
|
|
"period": "1h0m0s",
|
|
|
|
"previousLevel": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"count": 30,
|
|
|
|
"currentLevel": {
|
|
|
|
"level": "WARN",
|
|
|
|
"operation": "notequal"
|
|
|
|
},
|
|
|
|
"period": "30m0s",
|
|
|
|
"previousLevel": null
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"tagRules": [
|
|
|
|
{
|
|
|
|
"key": "k1",
|
|
|
|
"operator": "equal",
|
|
|
|
"value": "v1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "k2",
|
|
|
|
"operator": "notequalregex",
|
|
|
|
"value": "v2"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"type": "slack",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"labels": [],
|
|
|
|
"links": {
|
|
|
|
"labels": "/api/v2/notificationRules/0000000000000001/labels",
|
|
|
|
"members": "/api/v2/notificationRules/0000000000000001/members",
|
|
|
|
"owners": "/api/v2/notificationRules/0000000000000001/owners",
|
|
|
|
"self": "/api/v2/notificationRules/0000000000000001"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2019-08-15 23:24:22 +00:00
|
|
|
"ownerID": "0000000000000021",
|
|
|
|
"messageTemplate": "body 2{var2}",
|
2019-07-24 21:53:53 +00:00
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"description": "desc2",
|
|
|
|
"endpointID": "000000000000002c",
|
|
|
|
"id": "000000000000000b",
|
|
|
|
"name": "name2",
|
|
|
|
"orgID": "0000000000000002",
|
|
|
|
"runbookLink": "",
|
|
|
|
"status": "inactive",
|
2019-08-15 23:24:22 +00:00
|
|
|
"type": "pagerduty",
|
2019-07-24 21:53:53 +00:00
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"labels": [],
|
|
|
|
"links": {
|
|
|
|
"labels": "/api/v2/notificationRules/000000000000000b/labels",
|
|
|
|
"members": "/api/v2/notificationRules/000000000000000b/members",
|
|
|
|
"owners": "/api/v2/notificationRules/000000000000000b/owners",
|
|
|
|
"self": "/api/v2/notificationRules/000000000000000b"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}`},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
res := newNotificationRulesResponse(ctx, tt.args.nrs, mock.NewLabelService(), tt.args.filter, tt.args.opt)
|
|
|
|
got, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("newNotificationRulesResponse() JSON marshal %v", err)
|
|
|
|
}
|
|
|
|
if eq, diff, _ := jsonEqual(string(got), tt.want); tt.want != "" && !eq {
|
|
|
|
t.Errorf("%q. newNotificationRulesResponse() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_newNotificationRuleResponse(t *testing.T) {
|
|
|
|
type args struct {
|
|
|
|
nr influxdb.NotificationRule
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: args{
|
|
|
|
nr: &rule.Slack{
|
|
|
|
Channel: "ch1",
|
|
|
|
MessageTemplate: "message 1{var1}",
|
|
|
|
Base: rule.Base{
|
2019-08-15 23:24:22 +00:00
|
|
|
ID: influxdb.ID(1),
|
|
|
|
OrgID: influxdb.ID(2),
|
|
|
|
OwnerID: influxdb.ID(3),
|
2019-08-16 19:43:15 +00:00
|
|
|
EndpointID: 4,
|
2019-08-15 23:24:22 +00:00
|
|
|
Name: "name1",
|
|
|
|
Description: "desc1",
|
|
|
|
Status: influxdb.Active,
|
2019-08-16 19:43:15 +00:00
|
|
|
Every: mustDuration("5m"),
|
|
|
|
Offset: mustDuration("15s"),
|
2019-07-24 21:53:53 +00:00
|
|
|
TagRules: []notification.TagRule{
|
|
|
|
{
|
2019-08-28 16:25:54 +00:00
|
|
|
Tag: influxdb.Tag{Key: "k1", Value: "v1"},
|
2019-07-24 21:53:53 +00:00
|
|
|
Operator: notification.Equal,
|
|
|
|
},
|
|
|
|
{
|
2019-08-28 16:25:54 +00:00
|
|
|
Tag: influxdb.Tag{Key: "k2", Value: "v2"},
|
2019-07-24 21:53:53 +00:00
|
|
|
Operator: notification.NotRegexEqual,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
StatusRules: []notification.StatusRule{
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.LevelRule{CheckLevel: notification.Critical, Operation: true},
|
|
|
|
Count: 3,
|
|
|
|
Period: influxdb.Duration{Duration: time.Hour},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.LevelRule{CheckLevel: notification.Warn, Operation: true},
|
|
|
|
Count: 30,
|
|
|
|
Period: influxdb.Duration{Duration: time.Minute * 30},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: `{
|
|
|
|
"channel": "ch1",
|
|
|
|
"messageTemplate": "message 1{var1}",
|
|
|
|
"id": "0000000000000001",
|
|
|
|
"orgID": "0000000000000002",
|
2019-08-15 23:24:22 +00:00
|
|
|
"ownerID": "0000000000000003",
|
2019-07-24 21:53:53 +00:00
|
|
|
"endpointID": "0000000000000004",
|
|
|
|
"name": "name1",
|
|
|
|
"description": "desc1",
|
2019-08-16 19:43:15 +00:00
|
|
|
"every": "5m",
|
2019-07-24 21:53:53 +00:00
|
|
|
"offset": "15s",
|
|
|
|
"type": "slack",
|
|
|
|
"runbookLink": "",
|
|
|
|
"status": "active",
|
|
|
|
"statusRules": [
|
|
|
|
{
|
|
|
|
"count": 3,
|
|
|
|
"currentLevel": {
|
|
|
|
"level": "CRIT",
|
|
|
|
"operation": "equal"
|
|
|
|
},
|
|
|
|
"period": "1h0m0s",
|
|
|
|
"previousLevel": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"count": 30,
|
|
|
|
"currentLevel": {
|
|
|
|
"level": "WARN",
|
|
|
|
"operation": "equal"
|
|
|
|
},
|
|
|
|
"period": "30m0s",
|
|
|
|
"previousLevel": null
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"tagRules": [
|
|
|
|
{
|
|
|
|
"key": "k1",
|
|
|
|
"operator": "equal",
|
|
|
|
"value": "v1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"key": "k2",
|
|
|
|
"operator": "notequalregex",
|
|
|
|
"value": "v2"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"createdAt": "0001-01-01T00:00:00Z",
|
|
|
|
"updatedAt": "0001-01-01T00:00:00Z",
|
|
|
|
"labels": [
|
|
|
|
],
|
|
|
|
"links": {
|
|
|
|
"labels": "/api/v2/notificationRules/0000000000000001/labels",
|
|
|
|
"members": "/api/v2/notificationRules/0000000000000001/members",
|
|
|
|
"owners": "/api/v2/notificationRules/0000000000000001/owners",
|
|
|
|
"self": "/api/v2/notificationRules/0000000000000001"
|
|
|
|
}
|
|
|
|
|
|
|
|
}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
res := newNotificationRuleResponse(tt.args.nr, []*influxdb.Label{})
|
|
|
|
got, err := json.Marshal(res)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("newNotificationRuleResponse() JSON marshal %v", err)
|
|
|
|
}
|
|
|
|
if eq, diff, _ := jsonEqual(string(got), tt.want); tt.want != "" && !eq {
|
|
|
|
t.Errorf("%q. newNotificationRuleResponse() = ***%s***", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|