2019-08-16 19:43:15 +00:00
|
|
|
package rule_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-08-28 16:25:54 +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 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-08-16 19:43:15 +00:00
|
|
|
|
2019-08-19 11:31:46 +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-08-23 21:46:03 +00:00
|
|
|
statuses = monitor.from(start: -1h, fn: (r) =>
|
2019-08-19 11:31:46 +00:00
|
|
|
(r.foo == "bar" and r.baz == "bang"))
|
2019-08-16 19:43:15 +00:00
|
|
|
|
|
|
|
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-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-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)
|
|
|
|
}
|
|
|
|
}
|