2022-11-08 12:54:38 +00:00
|
|
|
// Copyright 2022 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 webex
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-kit/log"
|
|
|
|
commoncfg "github.com/prometheus/common/config"
|
|
|
|
"github.com/prometheus/common/model"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/prometheus/alertmanager/notify"
|
|
|
|
"github.com/prometheus/alertmanager/notify/test"
|
|
|
|
"github.com/prometheus/alertmanager/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestWebexRetry(t *testing.T) {
|
|
|
|
testWebhookURL, err := url.Parse("https://api.ciscospark.com/v1/message")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
notifier, err := New(
|
|
|
|
&config.WebexConfig{
|
|
|
|
HTTPConfig: &commoncfg.HTTPClientConfig{},
|
2022-11-09 14:16:00 +00:00
|
|
|
APIURL: &config.URL{URL: testWebhookURL},
|
2022-11-08 12:54:38 +00:00
|
|
|
},
|
|
|
|
test.CreateTmpl(t),
|
|
|
|
log.NewNopLogger(),
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
for statusCode, expected := range test.RetryTests(test.DefaultRetryCodes()) {
|
|
|
|
actual, _ := notifier.retrier.Check(statusCode, nil)
|
|
|
|
require.Equal(t, expected, actual, fmt.Sprintf("error on status %d", statusCode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWebexTemplating(t *testing.T) {
|
|
|
|
tc := []struct {
|
|
|
|
name string
|
|
|
|
|
2022-11-09 14:38:59 +00:00
|
|
|
cfg *config.WebexConfig
|
|
|
|
Message string
|
|
|
|
expJSON string
|
|
|
|
commonCfg *commoncfg.HTTPClientConfig
|
|
|
|
|
|
|
|
retry bool
|
|
|
|
errMsg string
|
|
|
|
expHeader string
|
2022-11-08 12:54:38 +00:00
|
|
|
}{
|
2022-11-09 14:38:59 +00:00
|
|
|
{
|
|
|
|
name: "with a valid message and a set http_config.authorization, it is formatted as expected",
|
|
|
|
cfg: &config.WebexConfig{
|
|
|
|
Message: `{{ template "webex.default.message" . }}`,
|
|
|
|
},
|
|
|
|
commonCfg: &commoncfg.HTTPClientConfig{
|
|
|
|
Authorization: &commoncfg.Authorization{Type: "Bearer", Credentials: "anewsecret"},
|
|
|
|
},
|
|
|
|
|
|
|
|
expJSON: `{"markdown":"\n\nAlerts Firing:\nLabels:\n - lbl1 = val1\n - lbl3 = val3\nAnnotations:\nSource: \nLabels:\n - lbl1 = val1\n - lbl2 = val2\nAnnotations:\nSource: \n\n\n\n"}`,
|
|
|
|
retry: false,
|
|
|
|
expHeader: "Bearer anewsecret",
|
2022-11-08 12:54:38 +00:00
|
|
|
},
|
|
|
|
{
|
2024-05-30 13:20:38 +00:00
|
|
|
name: "with message templating errors, it fails.",
|
2022-11-08 12:54:38 +00:00
|
|
|
cfg: &config.WebexConfig{
|
|
|
|
Message: "{{ ",
|
|
|
|
},
|
2022-11-09 14:38:59 +00:00
|
|
|
commonCfg: &commoncfg.HTTPClientConfig{},
|
|
|
|
errMsg: "template: :1: unclosed action",
|
2022-11-08 12:54:38 +00:00
|
|
|
},
|
2024-05-30 13:20:38 +00:00
|
|
|
{
|
|
|
|
name: "with a valid roomID set, the roomID is used accordingly.",
|
|
|
|
cfg: &config.WebexConfig{
|
|
|
|
RoomID: "my-room-id",
|
|
|
|
},
|
|
|
|
commonCfg: &commoncfg.HTTPClientConfig{},
|
|
|
|
expJSON: `{"markdown":"", "roomId":"my-room-id"}`,
|
|
|
|
retry: false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "with a valid roomID template, the roomID is used accordingly.",
|
|
|
|
cfg: &config.WebexConfig{
|
|
|
|
RoomID: "{{.GroupLabels.webex_room_id}}",
|
|
|
|
},
|
|
|
|
commonCfg: &commoncfg.HTTPClientConfig{},
|
|
|
|
expJSON: `{"markdown":"", "roomId":"group-label-room-id"}`,
|
|
|
|
retry: false,
|
|
|
|
},
|
2022-11-08 12:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tc {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var out []byte
|
2022-11-09 14:16:00 +00:00
|
|
|
var header http.Header
|
2022-11-08 12:54:38 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
|
|
|
out, err = io.ReadAll(r.Body)
|
2022-11-09 14:16:00 +00:00
|
|
|
header = r.Header.Clone()
|
2022-11-08 12:54:38 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
u, _ := url.Parse(srv.URL)
|
|
|
|
|
2022-11-09 14:16:00 +00:00
|
|
|
tt.cfg.APIURL = &config.URL{URL: u}
|
2022-11-09 14:38:59 +00:00
|
|
|
tt.cfg.HTTPConfig = tt.commonCfg
|
2022-11-08 12:54:38 +00:00
|
|
|
notifierWebex, err := New(tt.cfg, test.CreateTmpl(t), log.NewNopLogger())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
ctx = notify.WithGroupKey(ctx, "1")
|
2024-05-30 13:20:38 +00:00
|
|
|
ctx = notify.WithGroupLabels(ctx, model.LabelSet{"webex_room_id": "group-label-room-id"})
|
2022-11-08 12:54:38 +00:00
|
|
|
|
|
|
|
ok, err := notifierWebex.Notify(ctx, []*types.Alert{
|
|
|
|
{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"lbl1": "val1",
|
|
|
|
"lbl3": "val3",
|
|
|
|
},
|
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(time.Hour),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Alert: model.Alert{
|
|
|
|
Labels: model.LabelSet{
|
|
|
|
"lbl1": "val1",
|
|
|
|
"lbl2": "val2",
|
|
|
|
},
|
|
|
|
StartsAt: time.Now(),
|
|
|
|
EndsAt: time.Now().Add(time.Hour),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}...)
|
|
|
|
|
|
|
|
if tt.errMsg == "" {
|
|
|
|
require.NoError(t, err)
|
2022-11-09 14:38:59 +00:00
|
|
|
require.Equal(t, tt.expHeader, header.Get("Authorization"))
|
2022-11-08 12:54:38 +00:00
|
|
|
require.JSONEq(t, tt.expJSON, string(out))
|
|
|
|
} else {
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), tt.errMsg)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, tt.retry, ok)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|