mirror of
https://github.com/prometheus/alertmanager
synced 2025-02-17 19:17:07 +00:00
Add some tests for sns receiver
Signed-off-by: Tyler Reid <tyler.reid@grafana.com>
This commit is contained in:
parent
72d63a5d72
commit
6519c399b1
@ -26,7 +26,7 @@ import (
|
|||||||
commoncfg "github.com/prometheus/common/config"
|
commoncfg "github.com/prometheus/common/config"
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
yaml "gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLoadEmptyString(t *testing.T) {
|
func TestLoadEmptyString(t *testing.T) {
|
||||||
@ -904,6 +904,20 @@ func TestSlackGlobalAPIURLFile(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidSNSConfig(t *testing.T) {
|
||||||
|
_, err := LoadFile("testdata/conf.sns-topic-arn.yml")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Error parsing %s: %s", "testdata/conf.sns-topic-arn.yml\"", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInvalidSNSConfig(t *testing.T) {
|
||||||
|
_, err := LoadFile("testdata/conf.sns-invalid.yml")
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected error with missing fields on SNS config")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestUnmarshalHostPort(t *testing.T) {
|
func TestUnmarshalHostPort(t *testing.T) {
|
||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
in string
|
in string
|
||||||
|
14
config/testdata/conf.sns-invalid.yml
vendored
Normal file
14
config/testdata/conf.sns-invalid.yml
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
route:
|
||||||
|
receiver: 'sns-api-notifications'
|
||||||
|
group_by: [alertname]
|
||||||
|
|
||||||
|
receivers:
|
||||||
|
- name: 'sns-api-notifications'
|
||||||
|
sns_configs:
|
||||||
|
- api_url: https://sns.us-east-2.amazonaws.com
|
||||||
|
sigv4:
|
||||||
|
region: us-east-2
|
||||||
|
access_key: access_key
|
||||||
|
secret_key: secret_ket
|
||||||
|
attributes:
|
||||||
|
severity: Sev2
|
@ -7,7 +7,6 @@ receivers:
|
|||||||
sns_configs:
|
sns_configs:
|
||||||
- api_url: https://sns.us-east-2.amazonaws.com
|
- api_url: https://sns.us-east-2.amazonaws.com
|
||||||
topic_arn: arn:aws:sns:us-east-2:123456789012:My-Topic
|
topic_arn: arn:aws:sns:us-east-2:123456789012:My-Topic
|
||||||
is_fifo_topic: true
|
|
||||||
sigv4:
|
sigv4:
|
||||||
region: us-east-2
|
region: us-east-2
|
||||||
access_key: access_key
|
access_key: access_key
|
@ -15,51 +15,36 @@ package sns
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-kit/kit/log"
|
|
||||||
"github.com/prometheus/alertmanager/config"
|
|
||||||
"github.com/prometheus/alertmanager/notify/test"
|
|
||||||
"github.com/prometheus/alertmanager/types"
|
|
||||||
commoncfg "github.com/prometheus/common/config"
|
|
||||||
"github.com/prometheus/common/model"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNotifier_Notify(t *testing.T) {
|
func TestIsFIFO(t *testing.T) {
|
||||||
ctx, _, fn := test.GetContextWithCancelingURL()
|
require.True(t, isFIFOTopic("arn:aws:sns:us-east-2:624413706616:snsTestTopic.fifo"))
|
||||||
defer fn()
|
require.False(t, isFIFOTopic("arn:aws:sns:us-east-2:624413706616:snsTestTopic"))
|
||||||
attrTest := map[string]string{}
|
}
|
||||||
attrTest["key"] = "testVal"
|
|
||||||
// These are fake values
|
func TestValidateAndTruncateMessage(t *testing.T) {
|
||||||
notifier, err := New(
|
sBuff := make([]byte, 257*1024, 257*1024)
|
||||||
&config.SNSConfig{
|
for i := range sBuff {
|
||||||
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
sBuff[i] = byte(33)
|
||||||
Message: `{{ template "sns.default.message" . }}`,
|
}
|
||||||
TopicARN: "arn:aws:sns:us-east-2:123456789012:My-Topic",
|
truncatedMessage, isTruncated, err := validateAndTruncateMessage(string(sBuff))
|
||||||
Sigv4: config.SigV4Config{
|
require.True(t, isTruncated)
|
||||||
Region: "us-east-2",
|
require.NoError(t, err)
|
||||||
AccessKey: "access_key",
|
require.NotEqual(t, sBuff, truncatedMessage)
|
||||||
SecretKey: "secret_key",
|
require.Equal(t, len(truncatedMessage), 256*1024)
|
||||||
},
|
|
||||||
Attributes: attrTest,
|
sBuff = make([]byte, 100, 100)
|
||||||
},
|
for i := range sBuff {
|
||||||
test.CreateTmpl(t),
|
sBuff[i] = byte(33)
|
||||||
log.NewNopLogger(),
|
}
|
||||||
)
|
truncatedMessage, isTruncated, err = validateAndTruncateMessage(string(sBuff))
|
||||||
require.NoError(t, err)
|
require.False(t, isTruncated)
|
||||||
|
require.NoError(t, err)
|
||||||
ok, err := notifier.Notify(ctx, []*types.Alert{
|
require.Equal(t, string(sBuff), truncatedMessage)
|
||||||
&types.Alert{
|
|
||||||
Alert: model.Alert{
|
invalidUtf8String := "\xc3\x28"
|
||||||
Labels: model.LabelSet{
|
_, _, err = validateAndTruncateMessage(invalidUtf8String)
|
||||||
"lbl1": "val1",
|
require.Error(t, err)
|
||||||
},
|
|
||||||
StartsAt: time.Now(),
|
|
||||||
EndsAt: time.Now().Add(time.Hour),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}...)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.False(t, ok)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user