From 8b530b8683b00666ad1aff39dc57527ee59f8037 Mon Sep 17 00:00:00 2001 From: George Robinson Date: Fri, 12 Apr 2024 16:29:27 +0200 Subject: [PATCH] Fix tests Signed-off-by: George Robinson --- notify/discord/discord_test.go | 1 + notify/msteams/msteams_test.go | 2 ++ notify/notify_test.go | 15 +++++++++++++++ notify/opsgenie/opsgenie_test.go | 2 ++ notify/pagerduty/pagerduty_test.go | 2 ++ notify/test/test.go | 1 + notify/victorops/victorops_test.go | 2 ++ 7 files changed, 25 insertions(+) diff --git a/notify/discord/discord_test.go b/notify/discord/discord_test.go index a571ffffe0..1c641aab62 100644 --- a/notify/discord/discord_test.go +++ b/notify/discord/discord_test.go @@ -106,6 +106,7 @@ func TestDiscordTemplating(t *testing.T) { ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) ok, err := pd.Notify(ctx, []*types.Alert{ { diff --git a/notify/msteams/msteams_test.go b/notify/msteams/msteams_test.go index 80f9439173..8598c61fa0 100644 --- a/notify/msteams/msteams_test.go +++ b/notify/msteams/msteams_test.go @@ -117,6 +117,7 @@ func TestMSTeamsTemplating(t *testing.T) { ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) ok, err := pd.Notify(ctx, []*types.Alert{ { @@ -175,6 +176,7 @@ func TestNotifier_Notify_WithReason(t *testing.T) { } ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) alert1 := &types.Alert{ Alert: model.Alert{ diff --git a/notify/notify_test.go b/notify/notify_test.go index d5ce9612e3..98c3ff8788 100644 --- a/notify/notify_test.go +++ b/notify/notify_test.go @@ -236,6 +236,10 @@ func TestDedupStage(t *testing.T) { ctx = WithGroupKey(ctx, "1") + _, _, err = s.Exec(ctx, log.NewNopLogger()) + require.EqualError(t, err, "now missing") + ctx = WithNow(ctx, time.Now()) + _, _, err = s.Exec(ctx, log.NewNopLogger()) require.EqualError(t, err, "repeat interval missing") @@ -404,6 +408,7 @@ func TestRetryStageWithError(t *testing.T) { ctx := context.Background() ctx = WithFiringAlerts(ctx, []uint64{0}) + ctx = WithNow(ctx, time.Now()) // Notify with a recoverable error should retry and succeed. resctx, res, err := r.Exec(ctx, log.NewNopLogger(), alerts...) @@ -457,6 +462,7 @@ func TestRetryStageWithErrorCode(t *testing.T) { ctx := context.Background() ctx = WithFiringAlerts(ctx, []uint64{0}) + ctx = WithNow(ctx, time.Now()) // Notify with a non-recoverable error. resctx, _, err := r.Exec(ctx, log.NewNopLogger(), alerts...) @@ -491,6 +497,7 @@ func TestRetryStageWithContextCanceled(t *testing.T) { } ctx = WithFiringAlerts(ctx, []uint64{0}) + ctx = WithNow(ctx, time.Now()) // Notify with a non-recoverable error. resctx, _, err := r.Exec(ctx, log.NewNopLogger(), alerts...) @@ -529,6 +536,13 @@ func TestRetryStageNoResolved(t *testing.T) { ctx := context.Background() resctx, res, err := r.Exec(ctx, log.NewNopLogger(), alerts...) + require.EqualError(t, err, "now missing") + require.Nil(t, res) + require.NotNil(t, resctx) + + ctx = WithNow(ctx, time.Now()) + + resctx, res, err = r.Exec(ctx, log.NewNopLogger(), alerts...) require.EqualError(t, err, "firing alerts missing") require.Nil(t, res) require.NotNil(t, resctx) @@ -579,6 +593,7 @@ func TestRetryStageSendResolved(t *testing.T) { ctx := context.Background() ctx = WithFiringAlerts(ctx, []uint64{0}) + ctx = WithNow(ctx, time.Now()) resctx, res, err := r.Exec(ctx, log.NewNopLogger(), alerts...) require.NoError(t, err) diff --git a/notify/opsgenie/opsgenie_test.go b/notify/opsgenie/opsgenie_test.go index 59444d03e1..c240aef356 100644 --- a/notify/opsgenie/opsgenie_test.go +++ b/notify/opsgenie/opsgenie_test.go @@ -216,6 +216,7 @@ func TestOpsGenie(t *testing.T) { ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) expectedURL, _ := url.Parse("https://opsgenie/apiv2/alerts") @@ -279,6 +280,7 @@ func TestOpsGenieWithUpdate(t *testing.T) { tmpl := test.CreateTmpl(t) ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) opsGenieConfigWithUpdate := config.OpsGenieConfig{ Message: `{{ .CommonLabels.Message }}`, Description: `{{ .CommonLabels.Description }}`, diff --git a/notify/pagerduty/pagerduty_test.go b/notify/pagerduty/pagerduty_test.go index be4e83dc04..f302050d3a 100644 --- a/notify/pagerduty/pagerduty_test.go +++ b/notify/pagerduty/pagerduty_test.go @@ -259,6 +259,7 @@ func TestPagerDutyTemplating(t *testing.T) { ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) ok, err := pd.Notify(ctx, []*types.Alert{ { @@ -483,6 +484,7 @@ func TestPagerDutyEmptySrcHref(t *testing.T) { ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) _, err = pagerDuty.Notify(ctx, []*types.Alert{ { diff --git a/notify/test/test.go b/notify/test/test.go index 75729b2a4f..7e591b3640 100644 --- a/notify/test/test.go +++ b/notify/test/test.go @@ -142,6 +142,7 @@ func AssertNotifyLeaksNoSecret(ctx context.Context, t *testing.T, n notify.Notif require.NotEmpty(t, secret) ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) ok, err := n.Notify(ctx, []*types.Alert{ { Alert: model.Alert{ diff --git a/notify/victorops/victorops_test.go b/notify/victorops/victorops_test.go index 90bb611d40..b84ece9e11 100644 --- a/notify/victorops/victorops_test.go +++ b/notify/victorops/victorops_test.go @@ -62,6 +62,7 @@ func TestVictorOpsCustomFields(t *testing.T) { ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) alert := &types.Alert{ Alert: model.Alert{ @@ -211,6 +212,7 @@ func TestVictorOpsTemplating(t *testing.T) { require.NoError(t, err) ctx := context.Background() ctx = notify.WithGroupKey(ctx, "1") + ctx = notify.WithNow(ctx, time.Now()) _, err = vo.Notify(ctx, []*types.Alert{ {