From b1df15c4b920f8c3099c7ec4397371e44a54c0d1 Mon Sep 17 00:00:00 2001 From: Joe Blubaugh Date: Thu, 6 Oct 2022 17:38:54 +0800 Subject: [PATCH] Alerting: Add basic test wrappers to notification channel API test. (#56432) Adds t.Run calls that supply the name of a receiver and its integrations when the API response is being verified. This test has a rare flake and it's difficult to track down without more context for the test failure. --- .../alerting/api_notification_channel_test.go | 80 ++++++++++--------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/pkg/tests/api/alerting/api_notification_channel_test.go b/pkg/tests/api/alerting/api_notification_channel_test.go index 051697e26ba..152bf6c38d6 100644 --- a/pkg/tests/api/alerting/api_notification_channel_test.go +++ b/pkg/tests/api/alerting/api_notification_channel_test.go @@ -828,49 +828,53 @@ func TestNotificationChannels(t *testing.T) { err := json.Unmarshal([]byte(b), &receivers) require.NoError(t, err) for _, rcv := range receivers { - var expActive bool - if _, ok := expInactiveReceivers[*rcv.Name]; !ok { - expActive = true - } - var expErr bool - if _, ok := expNotificationErrors[*rcv.Name]; ok { - expErr = true - } + t.Run("Receiver "+*rcv.Name, func(t *testing.T) { + var expActive bool + if _, ok := expInactiveReceivers[*rcv.Name]; !ok { + expActive = true + } + var expErr bool + if _, ok := expNotificationErrors[*rcv.Name]; ok { + expErr = true + } - require.NotNil(t, rcv.Name) - require.NotNil(t, rcv.Active) - require.NotEmpty(t, rcv.Integrations) - if expActive { - require.True(t, *rcv.Active) - } - - // We don't have test alerts for the default notifier, continue iterating. - if *rcv.Name == "grafana-default-email" { - continue - } - - for _, integration := range rcv.Integrations { - require.NotNil(t, integration.Name) - require.NotNil(t, integration.SendResolved) - - // If the receiver is not active, no attempts to send notifications should be registered. + require.NotNil(t, rcv.Name) + require.NotNil(t, rcv.Active) + require.NotEmpty(t, rcv.Integrations) if expActive { - require.NotZero(t, integration.LastNotifyAttempt) - require.NotEqual(t, "0s", integration.LastNotifyAttemptDuration) - } else { - require.Zero(t, integration.LastNotifyAttempt) - require.Equal(t, "0s", integration.LastNotifyAttemptDuration) + require.True(t, *rcv.Active) } - // Check whether we're expecting an error on this integration. - if expErr { - for _, integration := range rcv.Integrations { - require.Equal(t, expNotificationErrors[*rcv.Name], integration.LastNotifyAttemptError) - } - } else { - require.Equal(t, "", integration.LastNotifyAttemptError) + // We don't have test alerts for the default notifier, continue iterating. + if *rcv.Name == "grafana-default-email" { + return } - } + + for _, integration := range rcv.Integrations { + require.NotNil(t, integration.Name) + t.Run("Integration "+*integration.Name, func(t *testing.T) { + require.NotNil(t, integration.SendResolved) + + // If the receiver is not active, no attempts to send notifications should be registered. + if expActive { + require.NotZero(t, integration.LastNotifyAttempt) + require.NotEqual(t, "0s", integration.LastNotifyAttemptDuration) + } else { + require.Zero(t, integration.LastNotifyAttempt) + require.Equal(t, "0s", integration.LastNotifyAttemptDuration) + } + + // Check whether we're expecting an error on this integration. + if expErr { + for _, integration := range rcv.Integrations { + require.Equal(t, expNotificationErrors[*rcv.Name], integration.LastNotifyAttemptError) + } + } else { + require.Equal(t, "", integration.LastNotifyAttemptError) + } + }) + } + }) } {