mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
72 lines
1.8 KiB
Go
72 lines
1.8 KiB
Go
package alertmanager_test
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/cloudflare/unsee/alertmanager"
|
|
"github.com/cloudflare/unsee/config"
|
|
"github.com/cloudflare/unsee/mock"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func init() {
|
|
log.SetLevel(log.ErrorLevel)
|
|
for i, uri := range mock.ListAllMockURIs() {
|
|
alertmanager.NewAlertmanager(fmt.Sprintf("dedup-mock-%d", i), uri, time.Second)
|
|
}
|
|
}
|
|
|
|
func pullAlerts() error {
|
|
for _, am := range alertmanager.GetAlertmanagers() {
|
|
err := am.Pull()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func TestDedupAlerts(t *testing.T) {
|
|
if err := pullAlerts(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
alertGroups := alertmanager.DedupAlerts()
|
|
if len(alertGroups) != 10 {
|
|
t.Errorf("Expected %d alert groups, got %d", 10, len(alertGroups))
|
|
}
|
|
}
|
|
|
|
func TestDedupAutocomplete(t *testing.T) {
|
|
if err := pullAlerts(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
ac := alertmanager.DedupAutocomplete()
|
|
// since we have alertmanager instance per mock adding new mocks will increase
|
|
// the number of hints, so we need to calculate the expected value here
|
|
// there should be 56 hints excluding @alertmanager ones, use that as our base
|
|
// and add 2 hints per alertmanager instance (= and != hints)
|
|
mockCount := len(mock.ListAllMockURIs())
|
|
expected := 56 + mockCount*2
|
|
if len(ac) != expected {
|
|
t.Errorf("Expected %d autocomplete hints, got %d", expected, len(ac))
|
|
}
|
|
}
|
|
|
|
func TestDedupColors(t *testing.T) {
|
|
os.Setenv("COLOR_LABELS_UNIQUE", "cluster instance @receiver")
|
|
os.Setenv("ALERTMANAGER_URIS", "default:http://localhost")
|
|
config.Config.Read()
|
|
if err := pullAlerts(); err != nil {
|
|
t.Error(err)
|
|
}
|
|
colors := alertmanager.DedupColors()
|
|
expected := 3
|
|
if len(colors) != expected {
|
|
t.Errorf("Expected %d color keys, got %d", expected, len(colors))
|
|
}
|
|
}
|