2019-07-26 19:38:30 +00:00
|
|
|
package endpoint_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/influxdata/influxdb"
|
|
|
|
"github.com/influxdata/influxdb/mock"
|
|
|
|
"github.com/influxdata/influxdb/notification/endpoint"
|
|
|
|
influxTesting "github.com/influxdata/influxdb/testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
id1 = "020f755c3c082000"
|
|
|
|
id3 = "020f755c3c082002"
|
|
|
|
)
|
|
|
|
|
|
|
|
var goodBase = endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
Description: "desc1",
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidEndpoint(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
src influxdb.NotificationEndpoint
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "invalid endpoint id",
|
|
|
|
src: &endpoint.Slack{},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
Msg: "Notification Endpoint ID is invalid",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid status",
|
|
|
|
src: &endpoint.PagerDuty{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
Msg: "invalid status",
|
|
|
|
},
|
|
|
|
},
|
2019-09-11 09:53:44 +00:00
|
|
|
{
|
|
|
|
name: "empty name",
|
|
|
|
src: &endpoint.PagerDuty{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-09-11 09:53:44 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
},
|
|
|
|
ClientURL: "https://events.pagerduty.com/v2/enqueue",
|
|
|
|
RoutingKey: influxdb.SecretField{Key: id1 + "-routing-key"},
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
Msg: "Notification Endpoint Name can't be empty",
|
|
|
|
},
|
|
|
|
},
|
2019-07-26 19:38:30 +00:00
|
|
|
{
|
2019-12-17 19:23:30 +00:00
|
|
|
name: "empty slack url",
|
2019-07-26 19:38:30 +00:00
|
|
|
src: &endpoint.Slack{
|
|
|
|
Base: goodBase,
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
2019-12-17 19:23:30 +00:00
|
|
|
Msg: "slack endpoint URL must be provided",
|
2019-07-26 19:38:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid slack url",
|
|
|
|
src: &endpoint.Slack{
|
|
|
|
Base: goodBase,
|
|
|
|
URL: "posts://er:{DEf1=ghi@:5432/db?ssl",
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
|
|
|
Msg: "slack endpoint URL is invalid: parse posts://er:{DEf1=ghi@:5432/db?ssl: net/url: invalid userinfo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "empty slack token",
|
|
|
|
src: &endpoint.Slack{
|
|
|
|
Base: goodBase,
|
|
|
|
URL: "localhost",
|
|
|
|
},
|
2019-09-05 20:48:34 +00:00
|
|
|
err: nil,
|
2019-07-26 19:38:30 +00:00
|
|
|
},
|
|
|
|
{
|
2019-08-27 17:29:49 +00:00
|
|
|
name: "empty http http method",
|
|
|
|
src: &endpoint.HTTP{
|
2019-07-26 19:38:30 +00:00
|
|
|
Base: goodBase,
|
|
|
|
URL: "localhost",
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
2019-08-27 17:29:49 +00:00
|
|
|
Msg: "invalid http http method",
|
2019-07-26 19:38:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-08-27 17:29:49 +00:00
|
|
|
name: "empty http token",
|
|
|
|
src: &endpoint.HTTP{
|
2019-07-26 19:38:30 +00:00
|
|
|
Base: goodBase,
|
|
|
|
URL: "localhost",
|
|
|
|
Method: "GET",
|
|
|
|
AuthMethod: "bearer",
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
2019-08-27 17:29:49 +00:00
|
|
|
Msg: "invalid http token for bearer auth",
|
2019-07-26 19:38:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-08-27 17:29:49 +00:00
|
|
|
name: "empty http username",
|
|
|
|
src: &endpoint.HTTP{
|
2019-07-26 19:38:30 +00:00
|
|
|
Base: goodBase,
|
|
|
|
URL: "localhost",
|
|
|
|
Method: http.MethodGet,
|
|
|
|
AuthMethod: "basic",
|
|
|
|
},
|
|
|
|
err: &influxdb.Error{
|
|
|
|
Code: influxdb.EInvalid,
|
2019-08-27 17:29:49 +00:00
|
|
|
Msg: "invalid http username/password for basic auth",
|
2019-07-26 19:38:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
2019-09-09 23:09:35 +00:00
|
|
|
t.Run(c.name, func(t *testing.T) {
|
|
|
|
got := c.src.Valid()
|
|
|
|
influxTesting.ErrorsEqual(t, got, c.err)
|
|
|
|
})
|
2019-07-26 19:38:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var timeGen1 = mock.TimeGenerator{FakeValue: time.Date(2006, time.July, 13, 4, 19, 10, 0, time.UTC)}
|
|
|
|
var timeGen2 = mock.TimeGenerator{FakeValue: time.Date(2006, time.July, 14, 5, 23, 53, 10, time.UTC)}
|
|
|
|
|
|
|
|
func TestJSON(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
src influxdb.NotificationEndpoint
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple Slack",
|
|
|
|
src: &endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
URL: "https://slack.com/api/chat.postMessage",
|
|
|
|
Token: influxdb.SecretField{Key: "token-key-1"},
|
|
|
|
},
|
|
|
|
},
|
2019-09-11 09:53:44 +00:00
|
|
|
{
|
|
|
|
name: "Slack without token",
|
|
|
|
src: &endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-09-11 09:53:44 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-09-11 09:53:44 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
URL: "https://hooks.slack.com/services/x/y/z",
|
|
|
|
},
|
|
|
|
},
|
2019-07-26 19:38:30 +00:00
|
|
|
{
|
|
|
|
name: "simple pagerduty",
|
|
|
|
src: &endpoint.PagerDuty{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
2019-09-09 23:09:35 +00:00
|
|
|
ClientURL: "https://events.pagerduty.com/v2/enqueue",
|
2019-07-26 19:38:30 +00:00
|
|
|
RoutingKey: influxdb.SecretField{Key: "pagerduty-routing-key"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-08-27 17:29:49 +00:00
|
|
|
name: "simple http",
|
|
|
|
src: &endpoint.HTTP{
|
2019-07-26 19:38:30 +00:00
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
2019-09-06 16:32:57 +00:00
|
|
|
Headers: map[string]string{
|
|
|
|
"x-header-1": "header 1",
|
|
|
|
"x-header-2": "header 2",
|
|
|
|
},
|
2019-07-26 19:38:30 +00:00
|
|
|
AuthMethod: "basic",
|
|
|
|
URL: "http://example.com",
|
|
|
|
Username: influxdb.SecretField{Key: "username-key"},
|
|
|
|
Password: influxdb.SecretField{Key: "password-key"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
b, err := json.Marshal(c.src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s marshal failed, err: %s", c.name, err.Error())
|
|
|
|
}
|
|
|
|
got, err := endpoint.UnmarshalJSON(b)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s unmarshal failed, err: %s", c.name, err.Error())
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(got, c.src); diff != "" {
|
|
|
|
t.Errorf("failed %s, NotificationEndpoint are different -got/+want\ndiff %s", c.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBackFill(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
name string
|
|
|
|
src influxdb.NotificationEndpoint
|
|
|
|
target influxdb.NotificationEndpoint
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple Slack",
|
|
|
|
src: &endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
URL: "https://slack.com/api/chat.postMessage",
|
|
|
|
Token: influxdb.SecretField{
|
|
|
|
Value: strPtr("token-value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: &endpoint.Slack{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
URL: "https://slack.com/api/chat.postMessage",
|
|
|
|
Token: influxdb.SecretField{
|
|
|
|
Key: id1 + "-token",
|
|
|
|
Value: strPtr("token-value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple pagerduty",
|
|
|
|
src: &endpoint.PagerDuty{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
2019-09-09 23:09:35 +00:00
|
|
|
ClientURL: "https://events.pagerduty.com/v2/enqueue",
|
2019-07-26 19:38:30 +00:00
|
|
|
RoutingKey: influxdb.SecretField{
|
|
|
|
Value: strPtr("routing-key-value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
target: &endpoint.PagerDuty{
|
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
2019-09-09 23:09:35 +00:00
|
|
|
ClientURL: "https://events.pagerduty.com/v2/enqueue",
|
2019-07-26 19:38:30 +00:00
|
|
|
RoutingKey: influxdb.SecretField{
|
|
|
|
Key: id1 + "-routing-key",
|
|
|
|
Value: strPtr("routing-key-value"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-08-27 17:29:49 +00:00
|
|
|
name: "http with token",
|
|
|
|
src: &endpoint.HTTP{
|
2019-07-26 19:38:30 +00:00
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AuthMethod: "basic",
|
|
|
|
URL: "http://example.com",
|
|
|
|
Username: influxdb.SecretField{
|
|
|
|
Value: strPtr("username1"),
|
|
|
|
},
|
|
|
|
Password: influxdb.SecretField{
|
|
|
|
Value: strPtr("password1"),
|
|
|
|
},
|
|
|
|
},
|
2019-08-27 17:29:49 +00:00
|
|
|
target: &endpoint.HTTP{
|
2019-07-26 19:38:30 +00:00
|
|
|
Base: endpoint.Base{
|
2019-12-12 17:31:49 +00:00
|
|
|
ID: influxTesting.MustIDBase16Ptr(id1),
|
2019-07-26 19:38:30 +00:00
|
|
|
Name: "name1",
|
2019-12-12 17:31:49 +00:00
|
|
|
OrgID: influxTesting.MustIDBase16Ptr(id3),
|
2019-07-26 19:38:30 +00:00
|
|
|
Status: influxdb.Active,
|
|
|
|
CRUDLog: influxdb.CRUDLog{
|
|
|
|
CreatedAt: timeGen1.Now(),
|
|
|
|
UpdatedAt: timeGen2.Now(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
AuthMethod: "basic",
|
|
|
|
URL: "http://example.com",
|
|
|
|
Username: influxdb.SecretField{
|
|
|
|
Key: id1 + "-username",
|
|
|
|
Value: strPtr("username1"),
|
|
|
|
},
|
|
|
|
Password: influxdb.SecretField{
|
|
|
|
Key: id1 + "-password",
|
|
|
|
Value: strPtr("password1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, c := range cases {
|
|
|
|
c.src.BackfillSecretKeys()
|
|
|
|
if diff := cmp.Diff(c.target, c.src); diff != "" {
|
|
|
|
t.Errorf("failed %s, NotificationEndpoint are different -got/+want\ndiff %s", c.name, diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func strPtr(s string) *string {
|
|
|
|
ss := new(string)
|
|
|
|
*ss = s
|
|
|
|
return ss
|
|
|
|
}
|