grafana/pkg/services/ngalert/sender/sender_test.go
Alexander Weaver 5ee4744d62
Alerting: Improve operational logs in sender package (#57134)
* Audit logs in sender package

* Fix casing and touch up a few key names

* Avoid logging entire alert struct

* Log configuration ID being applied

* Revert change to errorf rather than log

* Tune levels further and remove some redundancies

* Adjust logger naming and standardize log context

* Adjust logger naming in router

* Move log and get rid of dead error handling code
2022-10-20 14:19:04 -05:00

104 lines
3.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package sender
import (
"testing"
"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/prometheus/pkg/labels"
"github.com/stretchr/testify/require"
)
func TestSanitizeLabelName(t *testing.T) {
cases := []struct {
desc string
labelName string
expectedResult string
expectedErr string
}{
{
desc: "Remove whitespace",
labelName: " a\tb\nc\vd\re\ff ",
expectedResult: "abcdef",
},
{
desc: "Replace ASCII with underscore",
labelName: " !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~",
expectedResult: "________________0123456789_______ABCDEFGHIJKLMNOPQRSTUVWXYZ______abcdefghijklmnopqrstuvwxyz____",
},
{
desc: "Replace non-ASCII unicode with hex",
labelName: "_€_ƒ_„_†_‡_œ_Ÿ_®_º_¼_×_ð_þ_¿_±_四_十_二_🔥",
expectedResult: "_0x20ac_0x192_0x201e_0x2020_0x2021_0x153_0x178_0xae_0xba_0xbc_0xd7_0xf0_0xfe_0xbf_0xb1_0x56db_0x5341_0x4e8c_0x1f525",
},
{ // labels starting with a number are invalid, so we have to make sure we don't sanitize to another invalid label.
desc: "If first character is replaced with hex, prefix with underscore",
labelName: "😍😍😍",
expectedResult: "_0x1f60d0x1f60d0x1f60d",
},
{
desc: "Empty string should error",
labelName: "",
expectedErr: "label name cannot be empty",
},
{
desc: "Only whitespace should error",
labelName: " \t\n\v\n\f ",
expectedErr: "label name is empty after removing invalids chars",
},
}
for _, tc := range cases {
am := NewExternalAlertmanagerSender()
t.Run(tc.desc, func(t *testing.T) {
res, err := am.sanitizeLabelName(tc.labelName)
if tc.expectedErr != "" {
require.EqualError(t, err, tc.expectedErr)
}
require.Equal(t, tc.expectedResult, res)
})
}
}
func TestSanitizeLabelSet(t *testing.T) {
cases := []struct {
desc string
labelset models.LabelSet
expectedResult labels.Labels
}{
{
desc: "Duplicate labels after sanitizations, append short has as suffix to duplicates",
labelset: models.LabelSet{
"test-alert": "42",
"test_alert": "43",
"test+alert": "44",
},
expectedResult: labels.Labels{
labels.Label{Name: "test_alert", Value: "44"},
labels.Label{Name: "test_alert_ed6237", Value: "42"},
labels.Label{Name: "test_alert_a67b5e", Value: "43"},
},
},
{
desc: "If sanitize fails for a label, skip it",
labelset: models.LabelSet{
"test-alert": "42",
" \t\n\v\n\f ": "43",
"test+alert": "44",
},
expectedResult: labels.Labels{
labels.Label{Name: "test_alert", Value: "44"},
labels.Label{Name: "test_alert_ed6237", Value: "42"},
},
},
}
for _, tc := range cases {
am := NewExternalAlertmanagerSender()
t.Run(tc.desc, func(t *testing.T) {
require.Equal(t, tc.expectedResult, am.sanitizeLabelSet(tc.labelset))
})
}
}