Skip to content

Commit

Permalink
Fix silence tests (#8283)
Browse files Browse the repository at this point in the history
This commit fixes the silence tests, using the exact same tests
in prometheus/alertmanager to avoid regressions due to modules,
but using the New function in pkg/alertmanager/alertmanager.go
to ensure the correct limits are set.
  • Loading branch information
grobinson-grafana authored Jun 6, 2024
1 parent d1ec120 commit 5988b23
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions pkg/alertmanager/alertmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ func testLimiter(t *testing.T, limits Limits, ops []callbackOp) {
}

func TestSilenceLimits(t *testing.T) {
t.Skip()

user := "test"

r := prometheus.NewPedanticRegistry()
Expand All @@ -345,13 +343,16 @@ func TestSilenceLimits(t *testing.T) {
Store: prepareInMemoryAlertStore(),
Replicator: &stubReplicator{},
ReplicationFactor: 1,
// Set the interval to 1s as this test can trigger multiple broadcasts
// creating and expiring silences.
PersisterConfig: PersisterConfig{Interval: time.Second},
// We have set this to 1 hour, but we don't use it in this
// test as we override the broadcast function with SetBroadcast.
PersisterConfig: PersisterConfig{Interval: time.Hour},
}, r)
require.NoError(t, err)
defer am.StopAndWait()

// Override SetBroadcast as we just want to test limits.
am.silences.SetBroadcast(func(_ []byte) {})

// Insert sil1 should succeed without error.
sil1 := &silencepb.Silence{
Matchers: []*silencepb.Matcher{{Name: "a", Pattern: "b"}},
Expand All @@ -373,8 +374,13 @@ func TestSilenceLimits(t *testing.T) {
require.EqualError(t, err, "exceeded maximum number of silences: 1 (limit: 1)")
require.Equal(t, "", id2)

// Expire sil1. This should allow sil2 to be inserted.
// Expire sil1 and run the GC. This should allow sil2 to be
// inserted.
require.NoError(t, am.silences.Expire(id1))
n, err := am.silences.GC()
require.NoError(t, err)
require.Equal(t, 1, n)

id2, err = am.silences.Set(sil2)
require.NoError(t, err)
require.NotEqual(t, "", id2)
Expand All @@ -385,6 +391,9 @@ func TestSilenceLimits(t *testing.T) {

// Expire sil2.
require.NoError(t, am.silences.Expire(id2))
n, err = am.silences.GC()
require.NoError(t, err)
require.Equal(t, 1, n)

// Insert sil3 should fail because it exceeds maximum size.
sil3 := &silencepb.Silence{
Expand Down

0 comments on commit 5988b23

Please sign in to comment.