From 6519c399b1a2b6f59ba3c1af1cc19f89eab36756 Mon Sep 17 00:00:00 2001 From: Tyler Reid Date: Mon, 14 Jun 2021 22:14:12 -0500 Subject: [PATCH] Add some tests for sns receiver Signed-off-by: Tyler Reid --- config/config_test.go | 16 ++++- config/testdata/conf.sns-invalid.yml | 14 ++++ ...nf.sns-test.yml => conf.sns-topic-arn.yml} | 1 - notify/sns/sns_test.go | 71 ++++++++----------- 4 files changed, 57 insertions(+), 45 deletions(-) create mode 100644 config/testdata/conf.sns-invalid.yml rename config/testdata/{conf.sns-test.yml => conf.sns-topic-arn.yml} (93%) diff --git a/config/config_test.go b/config/config_test.go index 9bae3b71..6687c7bd 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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 diff --git a/config/testdata/conf.sns-invalid.yml b/config/testdata/conf.sns-invalid.yml new file mode 100644 index 00000000..148c71e6 --- /dev/null +++ b/config/testdata/conf.sns-invalid.yml @@ -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 diff --git a/config/testdata/conf.sns-test.yml b/config/testdata/conf.sns-topic-arn.yml similarity index 93% rename from config/testdata/conf.sns-test.yml rename to config/testdata/conf.sns-topic-arn.yml index 5cfe317a..ab83ed9e 100644 --- a/config/testdata/conf.sns-test.yml +++ b/config/testdata/conf.sns-topic-arn.yml @@ -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 diff --git a/notify/sns/sns_test.go b/notify/sns/sns_test.go index 7476e61a..bfa702fd 100644 --- a/notify/sns/sns_test.go +++ b/notify/sns/sns_test.go @@ -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) }