Add basic alertmanager package tests & benchmarks

This commit is contained in:
Łukasz Mierzwa
2017-07-17 18:53:00 -07:00
parent aea6178ecb
commit ae6fcbeeee
3 changed files with 115 additions and 0 deletions

View File

@@ -0,0 +1,39 @@
package alertmanager_test
import (
"os"
"testing"
"github.com/cloudflare/unsee/alertmanager"
"github.com/cloudflare/unsee/config"
)
func BenchmarkDedupAlerts(b *testing.B) {
if err := pullAlerts(); err != nil {
b.Error(err)
}
for n := 0; n < b.N; n++ {
alertmanager.DedupAlerts()
}
}
func BenchmarkDedupAutocomplete(b *testing.B) {
if err := pullAlerts(); err != nil {
b.Error(err)
}
for n := 0; n < b.N; n++ {
alertmanager.DedupAutocomplete()
}
}
func BenchmarkDedupColors(b *testing.B) {
os.Setenv("COLOR_LABELS_UNIQUE", "cluster instance @receiver")
os.Setenv("ALERTMANAGER_URIS", "default:http://localhost")
config.Config.Read()
if err := pullAlerts(); err != nil {
b.Error(err)
}
for n := 0; n < b.N; n++ {
alertmanager.DedupColors()
}
}

View File

@@ -0,0 +1,65 @@
package alertmanager_test
import (
"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(string(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()
expected := 74
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))
}
}

View File

@@ -52,3 +52,14 @@ func ListAllMocks() []string {
}
return dirs
}
// ListAllMockURIs returns a list of mock APIs as file:// URIs
func ListAllMockURIs() []string {
uris := []string{}
_, f, _, _ := runtime.Caller(0)
cwd := filepath.Dir(f)
for _, version := range ListAllMocks() {
uris = append(uris, "file://"+path.Join(cwd, version))
}
return uris
}