2019-06-18 13:34:46 +00:00
|
|
|
// Copyright 2019 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package slack
|
|
|
|
|
|
|
|
import (
|
2023-04-28 13:05:56 +00:00
|
|
|
"context"
|
2019-06-18 13:34:46 +00:00
|
|
|
"fmt"
|
2023-04-28 13:05:56 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
2022-07-18 12:25:32 +00:00
|
|
|
"os"
|
2023-08-10 09:59:13 +00:00
|
|
|
"strings"
|
2019-06-18 13:34:46 +00:00
|
|
|
"testing"
|
2023-04-28 13:05:56 +00:00
|
|
|
"time"
|
2019-06-18 13:34:46 +00:00
|
|
|
|
|
|
|
commoncfg "github.com/prometheus/common/config"
|
2023-04-28 13:05:56 +00:00
|
|
|
"github.com/prometheus/common/model"
|
2024-11-06 09:09:57 +00:00
|
|
|
"github.com/prometheus/common/promslog"
|
2019-06-18 13:34:46 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
2023-04-28 13:05:56 +00:00
|
|
|
"github.com/prometheus/alertmanager/notify"
|
2019-06-18 13:34:46 +00:00
|
|
|
"github.com/prometheus/alertmanager/notify/test"
|
2023-04-28 13:05:56 +00:00
|
|
|
"github.com/prometheus/alertmanager/types"
|
2019-06-18 13:34:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSlackRetry(t *testing.T) {
|
2019-08-02 14:17:40 +00:00
|
|
|
notifier, err := New(
|
|
|
|
&config.SlackConfig{
|
|
|
|
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
|
|
|
},
|
|
|
|
test.CreateTmpl(t),
|
2024-11-06 09:09:57 +00:00
|
|
|
promslog.NewNopLogger(),
|
2019-08-02 14:17:40 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-12-12 15:35:19 +00:00
|
|
|
for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) {
|
2019-08-02 14:17:40 +00:00
|
|
|
actual, _ := notifier.retrier.Check(statusCode, nil)
|
2019-06-18 13:34:46 +00:00
|
|
|
require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSlackRedactedURL(t *testing.T) {
|
|
|
|
ctx, u, fn := test.GetContextWithCancelingURL()
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
notifier, err := New(
|
|
|
|
&config.SlackConfig{
|
|
|
|
APIURL: &config.SecretURL{URL: u},
|
|
|
|
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
|
|
|
},
|
|
|
|
test.CreateTmpl(t),
|
2024-11-06 09:09:57 +00:00
|
|
|
promslog.NewNopLogger(),
|
2019-06-18 13:34:46 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-25 16:59:51 +00:00
|
|
|
test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
|
2019-06-18 13:34:46 +00:00
|
|
|
}
|
2021-04-02 01:57:55 +00:00
|
|
|
|
|
|
|
func TestGettingSlackURLFromFile(t *testing.T) {
|
|
|
|
ctx, u, fn := test.GetContextWithCancelingURL()
|
|
|
|
defer fn()
|
|
|
|
|
2022-07-18 12:25:32 +00:00
|
|
|
f, err := os.CreateTemp("", "slack_test")
|
2021-04-02 01:57:55 +00:00
|
|
|
require.NoError(t, err, "creating temp file failed")
|
|
|
|
_, err = f.WriteString(u.String())
|
|
|
|
require.NoError(t, err, "writing to temp file failed")
|
|
|
|
|
|
|
|
notifier, err := New(
|
|
|
|
&config.SlackConfig{
|
|
|
|
APIURLFile: f.Name(),
|
|
|
|
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
|
|
|
},
|
|
|
|
test.CreateTmpl(t),
|
2024-11-06 09:09:57 +00:00
|
|
|
promslog.NewNopLogger(),
|
2021-04-02 01:57:55 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-25 16:59:51 +00:00
|
|
|
test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
|
2021-04-02 01:57:55 +00:00
|
|
|
}
|
2022-09-09 14:37:31 +00:00
|
|
|
|
|
|
|
func TestTrimmingSlackURLFromFile(t *testing.T) {
|
|
|
|
ctx, u, fn := test.GetContextWithCancelingURL()
|
|
|
|
defer fn()
|
|
|
|
|
2022-11-30 16:06:57 +00:00
|
|
|
f, err := os.CreateTemp("", "slack_test_newline")
|
2022-09-09 14:37:31 +00:00
|
|
|
require.NoError(t, err, "creating temp file failed")
|
|
|
|
_, err = f.WriteString(u.String() + "\n\n")
|
|
|
|
require.NoError(t, err, "writing to temp file failed")
|
|
|
|
|
|
|
|
notifier, err := New(
|
|
|
|
&config.SlackConfig{
|
|
|
|
APIURLFile: f.Name(),
|
|
|
|
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
|
|
|
},
|
|
|
|
test.CreateTmpl(t),
|
2024-11-06 09:09:57 +00:00
|
|
|
promslog.NewNopLogger(),
|
2022-09-09 14:37:31 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
test.AssertNotifyLeaksNoSecret(ctx, t, notifier, u.String())
|
|
|
|
}
|
2023-04-28 13:05:56 +00:00
|
|
|
|
|
|
|
func TestNotifier_Notify_WithReason(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
statusCode int
|
2023-08-10 09:59:13 +00:00
|
|
|
responseBody string
|
2023-04-28 13:05:56 +00:00
|
|
|
expectedReason notify.Reason
|
2023-08-10 09:59:13 +00:00
|
|
|
expectedErr string
|
|
|
|
expectedRetry bool
|
2023-05-02 16:59:37 +00:00
|
|
|
noError bool
|
2023-04-28 13:05:56 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "with a 4xx status code",
|
|
|
|
statusCode: http.StatusUnauthorized,
|
|
|
|
expectedReason: notify.ClientErrorReason,
|
2023-08-10 09:59:13 +00:00
|
|
|
expectedRetry: false,
|
|
|
|
expectedErr: "unexpected status code 401",
|
2023-04-28 13:05:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with a 5xx status code",
|
|
|
|
statusCode: http.StatusInternalServerError,
|
|
|
|
expectedReason: notify.ServerErrorReason,
|
2023-08-10 09:59:13 +00:00
|
|
|
expectedRetry: true,
|
|
|
|
expectedErr: "unexpected status code 500",
|
2023-04-28 13:05:56 +00:00
|
|
|
},
|
|
|
|
{
|
2023-08-10 09:59:13 +00:00
|
|
|
name: "with a 3xx status code",
|
2023-04-28 13:05:56 +00:00
|
|
|
statusCode: http.StatusTemporaryRedirect,
|
|
|
|
expectedReason: notify.DefaultReason,
|
2023-08-10 09:59:13 +00:00
|
|
|
expectedRetry: false,
|
|
|
|
expectedErr: "unexpected status code 307",
|
2023-04-28 13:05:56 +00:00
|
|
|
},
|
2023-05-02 16:59:37 +00:00
|
|
|
{
|
2023-08-10 09:59:13 +00:00
|
|
|
name: "with a 1xx status code",
|
|
|
|
statusCode: http.StatusSwitchingProtocols,
|
|
|
|
expectedReason: notify.DefaultReason,
|
|
|
|
expectedRetry: false,
|
|
|
|
expectedErr: "unexpected status code 101",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2xx response with invalid JSON",
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
responseBody: `{"not valid json"}`,
|
|
|
|
expectedReason: notify.ClientErrorReason,
|
|
|
|
expectedRetry: true,
|
|
|
|
expectedErr: "could not unmarshal",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2xx response with a JSON error",
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
responseBody: `{"ok":false,"error":"error_message"}`,
|
|
|
|
expectedReason: notify.ClientErrorReason,
|
|
|
|
expectedRetry: false,
|
|
|
|
expectedErr: "error response from Slack: error_message",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "2xx response with a plaintext error",
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
responseBody: "no_channel",
|
|
|
|
expectedReason: notify.ClientErrorReason,
|
|
|
|
expectedRetry: false,
|
|
|
|
expectedErr: "error response from Slack: no_channel",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful JSON response",
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
responseBody: `{"ok":true}`,
|
|
|
|
noError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "successful plaintext response",
|
|
|
|
statusCode: http.StatusOK,
|
|
|
|
responseBody: "ok",
|
|
|
|
noError: true,
|
2023-05-02 16:59:37 +00:00
|
|
|
},
|
2023-04-28 13:05:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
apiurl, _ := url.Parse("https://slack.com/post.Message")
|
|
|
|
notifier, err := New(
|
|
|
|
&config.SlackConfig{
|
|
|
|
NotifierConfig: config.NotifierConfig{},
|
|
|
|
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
|
|
|
APIURL: &config.SecretURL{URL: apiurl},
|
2023-08-10 09:59:13 +00:00
|
|
|
Channel: "channelname",
|
2023-04-28 13:05:56 +00:00
|
|
|
},
|
|
|
|
test.CreateTmpl(t),
|
2024-11-06 09:09:57 +00:00
|
|
|
promslog.NewNopLogger(),
|
2023-04-28 13:05:56 +00:00
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
notifier.postJSONFunc = func(ctx context.Context, client *http.Client, url string, body io.Reader) (*http.Response, error) {
|
|
|
|
resp := httptest.NewRecorder()
|
2023-08-10 09:59:13 +00:00
|
|
|
if strings.HasPrefix(tt.responseBody, "{") {
|
|
|
|
resp.Header().Add("Content-Type", "application/json; charset=utf-8")
|
|
|
|
}
|
2023-04-28 13:05:56 +00:00
|
|
|
resp.WriteHeader(tt.statusCode)
|
2023-08-10 09:59:13 +00:00
|
|
|
resp.WriteString(tt.responseBody)
|
2023-04-28 13:05:56 +00:00
|
|
|
return resp.Result(), nil
|
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = notify.WithGroupKey(ctx, "1")
|
|
|
|
|
|
|
|
alert1 := &types.Alert{
|
|
|
|
Alert: model.Alert{
|
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(time.Hour),
|
|
|
|
},
|
|
|
|
}
|
2023-08-10 09:59:13 +00:00
|
|
|
retry, err := notifier.Notify(ctx, alert1)
|
|
|
|
require.Equal(t, tt.expectedRetry, retry)
|
2023-05-02 16:59:37 +00:00
|
|
|
if tt.noError {
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
2023-11-24 21:17:35 +00:00
|
|
|
var reasonError *notify.ErrorWithReason
|
2023-12-10 08:33:13 +00:00
|
|
|
require.ErrorAs(t, err, &reasonError)
|
2023-05-02 16:59:37 +00:00
|
|
|
require.Equal(t, tt.expectedReason, reasonError.Reason)
|
2023-08-10 09:59:13 +00:00
|
|
|
require.Contains(t, err.Error(), tt.expectedErr)
|
|
|
|
require.Contains(t, err.Error(), "channelname")
|
2023-05-02 16:59:37 +00:00
|
|
|
}
|
2023-04-28 13:05:56 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|