2019-08-16 19:43:15 +00:00
|
|
|
package rule_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/flux/parser"
|
2019-08-27 17:29:49 +00:00
|
|
|
"github.com/influxdata/influxdb"
|
2019-08-16 19:43:15 +00:00
|
|
|
"github.com/influxdata/influxdb/notification"
|
2019-08-27 17:29:49 +00:00
|
|
|
"github.com/influxdata/influxdb/notification/endpoint"
|
2019-08-16 19:43:15 +00:00
|
|
|
"github.com/influxdata/influxdb/notification/rule"
|
|
|
|
)
|
|
|
|
|
|
|
|
func mustDuration(d string) *notification.Duration {
|
|
|
|
dur, err := parser.ParseDuration(d)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*notification.Duration)(dur)
|
|
|
|
}
|
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
func statusRulePtr(r notification.CheckLevel) *notification.CheckLevel {
|
|
|
|
return &r
|
|
|
|
}
|
|
|
|
|
2019-08-16 19:43:15 +00:00
|
|
|
func TestSlack_GenerateFlux(t *testing.T) {
|
|
|
|
want := `package main
|
|
|
|
// foo
|
2019-08-23 21:46:03 +00:00
|
|
|
import "influxdata/influxdb/monitor"
|
2019-08-16 19:43:15 +00:00
|
|
|
import "slack"
|
2019-08-27 17:29:49 +00:00
|
|
|
import "influxdata/influxdb/secrets"
|
2019-09-03 20:58:50 +00:00
|
|
|
import "experimental"
|
2019-09-05 00:33:53 +00:00
|
|
|
import "influxdata/influxdb/v1"
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
option task = {name: "foo", every: 2h}
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
slack_secret = secrets.get(key: "slack_token")
|
|
|
|
slack_endpoint = slack.endpoint(token: slack_secret, url: "http://localhost:7777")
|
|
|
|
notification = {
|
|
|
|
_notification_rule_id: "0000000000000001",
|
|
|
|
_notification_rule_name: "foo",
|
|
|
|
_notification_endpoint_id: "0000000000000002",
|
|
|
|
_notification_endpoint_name: "foo",
|
|
|
|
}
|
2019-09-03 20:58:50 +00:00
|
|
|
statuses = monitor.from(start: -2h, fn: (r) =>
|
2019-08-27 17:29:49 +00:00
|
|
|
(r.foo == "bar" and r.baz == "bang"))
|
2019-09-05 00:33:53 +00:00
|
|
|
|> v1.fieldsAsCols()
|
2019-09-03 20:58:50 +00:00
|
|
|
any_to_crit = statuses
|
|
|
|
|> monitor.stateChanges(fromLevel: "any", toLevel: "crit")
|
|
|
|
info_to_warn = statuses
|
|
|
|
|> monitor.stateChanges(fromLevel: "info", toLevel: "warn")
|
|
|
|
all_statuses = union(tables: [any_to_crit, info_to_warn])
|
|
|
|
|> sort(columns: ["_time"])
|
|
|
|
|> filter(fn: (r) =>
|
|
|
|
(r._time > experimental.subDuration(from: now(), d: 1h)))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-09-03 20:58:50 +00:00
|
|
|
all_statuses
|
2019-08-27 17:29:49 +00:00
|
|
|
|> monitor.notify(data: notification, endpoint: slack_endpoint(mapFn: (r) =>
|
2019-09-04 21:28:42 +00:00
|
|
|
({channel: "bar", text: "blah", color: if r._level == "crit" then "danger" else if r._level == "warn" then "warning" else "good"})))`
|
2019-08-16 19:43:15 +00:00
|
|
|
|
|
|
|
s := &rule.Slack{
|
|
|
|
Channel: "bar",
|
|
|
|
MessageTemplate: "blah",
|
|
|
|
Base: rule.Base{
|
2019-08-27 17:29:49 +00:00
|
|
|
ID: 1,
|
|
|
|
EndpointID: 2,
|
|
|
|
Name: "foo",
|
|
|
|
Every: mustDuration("1h"),
|
2019-08-16 19:43:15 +00:00
|
|
|
TagRules: []notification.TagRule{
|
|
|
|
{
|
2019-08-28 16:25:54 +00:00
|
|
|
Tag: influxdb.Tag{
|
2019-08-16 19:43:15 +00:00
|
|
|
Key: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
},
|
|
|
|
Operator: notification.Equal,
|
|
|
|
},
|
|
|
|
{
|
2019-08-28 16:25:54 +00:00
|
|
|
Tag: influxdb.Tag{
|
2019-08-16 19:43:15 +00:00
|
|
|
Key: "baz",
|
|
|
|
Value: "bang",
|
|
|
|
},
|
|
|
|
Operator: notification.Equal,
|
|
|
|
},
|
|
|
|
},
|
2019-09-03 20:58:50 +00:00
|
|
|
StatusRules: []notification.StatusRule{
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.Critical,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.Warn,
|
|
|
|
PreviousLevel: statusRulePtr(notification.Info),
|
|
|
|
},
|
|
|
|
},
|
2019-08-16 19:43:15 +00:00
|
|
|
},
|
|
|
|
}
|
2019-08-27 17:29:49 +00:00
|
|
|
e := &endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
|
|
|
ID: 2,
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
URL: "http://localhost:7777",
|
|
|
|
Token: influxdb.SecretField{
|
|
|
|
Key: "slack_token",
|
|
|
|
},
|
|
|
|
}
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
f, err := s.GenerateFlux(e)
|
2019-08-16 19:43:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if f != want {
|
|
|
|
t.Errorf("scripts did not match. want:\n%v\n\ngot:\n%v", want, f)
|
|
|
|
}
|
|
|
|
}
|