2019-08-16 19:43:15 +00:00
|
|
|
package rule_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"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 TestHTTP_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 "http"
|
2019-08-19 11:31:46 +00:00
|
|
|
import "json"
|
2019-09-03 20:58:50 +00:00
|
|
|
import "experimental"
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-09-05 00:37:22 +00:00
|
|
|
option task = {name: "foo", every: 1h, offset: 1s}
|
2019-08-16 19:43:15 +00:00
|
|
|
|
2019-08-19 11:31:46 +00:00
|
|
|
endpoint = http.endpoint(url: "http://localhost:7777")
|
|
|
|
notification = {
|
|
|
|
_notification_rule_id: "0000000000000001",
|
|
|
|
_notification_rule_name: "foo",
|
|
|
|
_notification_endpoint_id: "0000000000000002",
|
2019-08-27 17:29:49 +00:00
|
|
|
_notification_endpoint_name: "foo",
|
2019-08-19 11:31:46 +00:00
|
|
|
}
|
2019-09-05 13:52:05 +00:00
|
|
|
statuses = monitor.from(start: -2h)
|
2019-09-03 20:58:50 +00:00
|
|
|
any_to_crit = statuses
|
|
|
|
|> monitor.stateChanges(fromLevel: "any", toLevel: "crit")
|
|
|
|
all_statuses = any_to_crit
|
|
|
|
|> 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-29 00:24:20 +00:00
|
|
|
|> monitor.notify(data: notification, endpoint: endpoint(mapFn: (r) =>
|
2019-08-16 19:43:15 +00:00
|
|
|
({data: json.encode(v: r)})))`
|
|
|
|
|
|
|
|
s := &rule.HTTP{
|
|
|
|
Base: rule.Base{
|
2019-08-19 11:31:46 +00:00
|
|
|
ID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
Every: mustDuration("1h"),
|
|
|
|
Offset: mustDuration("1s"),
|
|
|
|
EndpointID: 2,
|
2019-09-05 13:52:05 +00:00
|
|
|
TagRules: []notification.TagRule{},
|
2019-09-03 20:58:50 +00:00
|
|
|
StatusRules: []notification.StatusRule{
|
|
|
|
{
|
|
|
|
CurrentLevel: notification.Critical,
|
|
|
|
},
|
|
|
|
},
|
2019-08-16 19:43:15 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-08-27 17:29:49 +00:00
|
|
|
e := &endpoint.HTTP{
|
|
|
|
Base: endpoint.Base{
|
|
|
|
ID: 2,
|
|
|
|
Name: "foo",
|
|
|
|
},
|
|
|
|
URL: "http://localhost:7777",
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|