2023-07-18 15:06:19 +02:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
metricset "github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBlackHoleNotifier_Send(t *testing.T) {
|
2023-07-18 15:53:37 +02:00
|
|
|
bh := newBlackHoleNotifier()
|
2023-07-18 15:06:19 +02: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 21:57:56 +02:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2023-07-18 15:06:19 +02:00
|
|
|
}
|
|
|
|
|
2023-07-18 15:53:37 +02:00
|
|
|
alertCount := bh.metrics.alertsSent.Get()
|
2023-07-18 15:06:19 +02:00
|
|
|
if alertCount != 1 {
|
2024-07-12 21:57:56 +02:00
|
|
|
t.Fatalf("expect value 1; instead got %d", alertCount)
|
2023-07-18 15:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBlackHoleNotifier_Close(t *testing.T) {
|
2023-07-18 15:53:37 +02:00
|
|
|
bh := newBlackHoleNotifier()
|
2023-07-18 15:06:19 +02: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 21:57:56 +02:00
|
|
|
t.Fatalf("unexpected error %s", err)
|
2023-07-18 15:06:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bh.Close()
|
|
|
|
|
|
|
|
defaultMetrics := metricset.GetDefaultSet()
|
|
|
|
alertMetricName := "vmalert_alerts_sent_total{addr=\"blackhole\"}"
|
|
|
|
for _, name := range defaultMetrics.ListMetricNames() {
|
|
|
|
if name == alertMetricName {
|
2024-07-12 21:57:56 +02:00
|
|
|
t.Fatalf("Metric name should have unregistered.But still present")
|
2023-07-18 15:06:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|