From 5988b236cbdb0b4213d8ca39f20307dcb48b7f40 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Thu, 6 Jun 2024 15:01:41 +0100 Subject: [PATCH] Fix silence tests (#8283) 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. --- pkg/alertmanager/alertmanager_test.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/pkg/alertmanager/alertmanager_test.go b/pkg/alertmanager/alertmanager_test.go index 51389027e5d..0ca0cbf3d94 100644 --- a/pkg/alertmanager/alertmanager_test.go +++ b/pkg/alertmanager/alertmanager_test.go @@ -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() @@ -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"}}, @@ -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) @@ -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{