...

Source file src/github.com/prometheus/alertmanager/notify/sns/sns_test.go

Documentation: github.com/prometheus/alertmanager/notify/sns

     1  // Copyright 2021 Prometheus Team
     2  // Licensed under the Apache License, Version 2.0 (the "License");
     3  // you may not use this file except in compliance with the License.
     4  // You may obtain a copy of the License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package sns
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  func TestValidateAndTruncateMessage(t *testing.T) {
    23  	sBuff := make([]byte, 257*1024)
    24  	for i := range sBuff {
    25  		sBuff[i] = byte(33)
    26  	}
    27  	truncatedMessage, isTruncated, err := validateAndTruncateMessage(string(sBuff), 256*1024)
    28  	require.True(t, isTruncated)
    29  	require.NoError(t, err)
    30  	require.NotEqual(t, sBuff, truncatedMessage)
    31  	require.Equal(t, len(truncatedMessage), 256*1024)
    32  
    33  	sBuff = make([]byte, 100)
    34  	for i := range sBuff {
    35  		sBuff[i] = byte(33)
    36  	}
    37  	truncatedMessage, isTruncated, err = validateAndTruncateMessage(string(sBuff), 100)
    38  	require.False(t, isTruncated)
    39  	require.NoError(t, err)
    40  	require.Equal(t, string(sBuff), truncatedMessage)
    41  
    42  	invalidUtf8String := "\xc3\x28"
    43  	_, _, err = validateAndTruncateMessage(invalidUtf8String, 100)
    44  	require.Error(t, err)
    45  }
    46  

View as plain text