...

Source file src/github.com/prometheus/alertmanager/notify/slack/slack_test.go

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

     1  // Copyright 2019 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 slack
    15  
    16  import (
    17  	"fmt"
    18  	"os"
    19  	"testing"
    20  
    21  	"github.com/go-kit/log"
    22  	commoncfg "github.com/prometheus/common/config"
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"github.com/prometheus/alertmanager/config"
    26  	"github.com/prometheus/alertmanager/notify/test"
    27  )
    28  
    29  func TestSlackRetry(t *testing.T) {
    30  	notifier, err := New(
    31  		&config.SlackConfig{
    32  			HTTPConfig: &commoncfg.HTTPClientConfig{},
    33  		},
    34  		test.CreateTmpl(t),
    35  		log.NewNopLogger(),
    36  	)
    37  	require.NoError(t, err)
    38  
    39  	for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) {
    40  		actual, _ := notifier.retrier.Check(statusCode, nil)
    41  		require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
    42  	}
    43  }
    44  
    45  func TestSlackRedactedURL(t *testing.T) {
    46  	ctx, u, fn := test.GetContextWithCancelingURL()
    47  	defer fn()
    48  
    49  	notifier, err := New(
    50  		&config.SlackConfig{
    51  			APIURL:     &config.SecretURL{URL: u},
    52  			HTTPConfig: &commoncfg.HTTPClientConfig{},
    53  		},
    54  		test.CreateTmpl(t),
    55  		log.NewNopLogger(),
    56  	)
    57  	require.NoError(t, err)
    58  
    59  	test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
    60  }
    61  
    62  func TestGettingSlackURLFromFile(t *testing.T) {
    63  	ctx, u, fn := test.GetContextWithCancelingURL()
    64  	defer fn()
    65  
    66  	f, err := os.CreateTemp("", "slack_test")
    67  	require.NoError(t, err, "creating temp file failed")
    68  	_, err = f.WriteString(u.String())
    69  	require.NoError(t, err, "writing to temp file failed")
    70  
    71  	notifier, err := New(
    72  		&config.SlackConfig{
    73  			APIURLFile: f.Name(),
    74  			HTTPConfig: &commoncfg.HTTPClientConfig{},
    75  		},
    76  		test.CreateTmpl(t),
    77  		log.NewNopLogger(),
    78  	)
    79  	require.NoError(t, err)
    80  
    81  	test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
    82  }
    83  
    84  func TestTrimmingSlackURLFromFile(t *testing.T) {
    85  	ctx, u, fn := test.GetContextWithCancelingURL()
    86  	defer fn()
    87  
    88  	f, err := os.CreateTemp("", "slack_test_newline")
    89  	require.NoError(t, err, "creating temp file failed")
    90  	_, err = f.WriteString(u.String() + "\n\n")
    91  	require.NoError(t, err, "writing to temp file failed")
    92  
    93  	notifier, err := New(
    94  		&config.SlackConfig{
    95  			APIURLFile: f.Name(),
    96  			HTTPConfig: &commoncfg.HTTPClientConfig{},
    97  		},
    98  		test.CreateTmpl(t),
    99  		log.NewNopLogger(),
   100  	)
   101  	require.NoError(t, err)
   102  
   103  	test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
   104  }
   105  

View as plain text