2023-07-18 13:06:19 +00:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metricset "github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlackHoleNotifier_Send(t *testing.T) {
|
2023-07-18 13:53:37 +00:00
|
|
|
bh := newBlackHoleNotifier()
|
2023-07-18 13:06:19 +00:00
|
|
|
if err := bh.Send(context.Background(), []Alert{{
|
|
|
|
GroupID: 0,
|
|
|
|
Name: "alert0",
|
|
|
|
Start: time.Now().UTC(),
|
|
|
|
End: time.Now().UTC(),
|
|
|
|
Annotations: map[string]string{"a": "b", "c": "d", "e": "f"},
|
|
|
|
}}, nil); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2023-07-18 13:06:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-18 13:53:37 +00:00
|
|
|
alertCount := bh.metrics.alertsSent.Get()
|
2023-07-18 13:06:19 +00:00
|
|
|
if alertCount != 1 {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("expect value 1; instead got %d", alertCount)
|
2023-07-18 13:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlackHoleNotifier_Close(t *testing.T) {
|
2023-07-18 13:53:37 +00:00
|
|
|
bh := newBlackHoleNotifier()
|
2023-07-18 13:06:19 +00:00
|
|
|
if err := bh.Send(context.Background(), []Alert{{
|
|
|
|
GroupID: 0,
|
|
|
|
Name: "alert0",
|
|
|
|
Start: time.Now().UTC(),
|
|
|
|
End: time.Now().UTC(),
|
|
|
|
Annotations: map[string]string{"a": "b", "c": "d", "e": "f"},
|
|
|
|
}}, nil); err != nil {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2023-07-18 13:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bh.Close()
|
|
|
|
|
|
|
|
defaultMetrics := metricset.GetDefaultSet()
|
|
|
|
alertMetricName := "vmalert_alerts_sent_total{addr=\"blackhole\"}"
|
|
|
|
for _, name := range defaultMetrics.ListMetricNames() {
|
|
|
|
if name == alertMetricName {
|
2024-07-12 19:57:56 +00:00
|
|
|
t.Fatalf("Metric name should have unregistered.But still present")
|
2023-07-18 13:06:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|