Add some tests for sns receiver

Signed-off-by: Tyler Reid <tyler.reid@grafana.com>
This commit is contained in:
Tyler Reid 2021-06-14 22:14:12 -05:00
parent 72d63a5d72
commit 6519c399b1
4 changed files with 57 additions and 45 deletions

View File

@ -26,7 +26,7 @@ import (
commoncfg "github.com/prometheus/common/config"
"github.com/prometheus/common/model"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
"gopkg.in/yaml.v2"
)
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) {
for _, tc := range []struct {
in string

14
config/testdata/conf.sns-invalid.yml vendored Normal file
View 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

View File

@ -7,7 +7,6 @@ receivers:
sns_configs:
- api_url: https://sns.us-east-2.amazonaws.com
topic_arn: arn:aws:sns:us-east-2:123456789012:My-Topic
is_fifo_topic: true
sigv4:
region: us-east-2
access_key: access_key

View File

@ -15,51 +15,36 @@ package sns
import (
"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"
)
func TestNotifier_Notify(t *testing.T) {
ctx, _, fn := test.GetContextWithCancelingURL()
defer fn()
attrTest := map[string]string{}
attrTest["key"] = "testVal"
// These are fake values
notifier, err := New(
&config.SNSConfig{
HTTPConfig: &commoncfg.HTTPClientConfig{},
Message: `{{ template "sns.default.message" . }}`,
TopicARN: "arn:aws:sns:us-east-2:123456789012:My-Topic",
Sigv4: config.SigV4Config{
Region: "us-east-2",
AccessKey: "access_key",
SecretKey: "secret_key",
},
Attributes: attrTest,
},
test.CreateTmpl(t),
log.NewNopLogger(),
)
require.NoError(t, err)
ok, err := notifier.Notify(ctx, []*types.Alert{
&types.Alert{
Alert: model.Alert{
Labels: model.LabelSet{
"lbl1": "val1",
},
StartsAt: time.Now(),
EndsAt: time.Now().Add(time.Hour),
},
},
}...)
require.NoError(t, err)
require.False(t, ok)
func TestIsFIFO(t *testing.T) {
require.True(t, isFIFOTopic("arn:aws:sns:us-east-2:624413706616:snsTestTopic.fifo"))
require.False(t, isFIFOTopic("arn:aws:sns:us-east-2:624413706616:snsTestTopic"))
}
func TestValidateAndTruncateMessage(t *testing.T) {
sBuff := make([]byte, 257*1024, 257*1024)
for i := range sBuff {
sBuff[i] = byte(33)
}
truncatedMessage, isTruncated, err := validateAndTruncateMessage(string(sBuff))
require.True(t, isTruncated)
require.NoError(t, err)
require.NotEqual(t, sBuff, truncatedMessage)
require.Equal(t, len(truncatedMessage), 256*1024)
sBuff = make([]byte, 100, 100)
for i := range sBuff {
sBuff[i] = byte(33)
}
truncatedMessage, isTruncated, err = validateAndTruncateMessage(string(sBuff))
require.False(t, isTruncated)
require.NoError(t, err)
require.Equal(t, string(sBuff), truncatedMessage)
invalidUtf8String := "\xc3\x28"
_, _, err = validateAndTruncateMessage(invalidUtf8String)
require.Error(t, err)
}