2023-04-13 11:25:32 -05:00
|
|
|
package notifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
alertingNotify "github.com/grafana/alerting/notify"
|
|
|
|
"github.com/prometheus/alertmanager/config"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
|
|
)
|
|
|
|
|
2023-04-25 12:39:46 -05:00
|
|
|
func TestPostableGrafanaReceiverToGrafanaIntegrationConfig(t *testing.T) {
|
2023-04-13 11:25:32 -05:00
|
|
|
r := &apimodels.PostableGrafanaReceiver{
|
|
|
|
UID: "test-uid",
|
|
|
|
Name: "test-name",
|
|
|
|
Type: "slack",
|
|
|
|
DisableResolveMessage: false,
|
|
|
|
Settings: apimodels.RawMessage(`{ "data" : "test" }`),
|
|
|
|
SecureSettings: map[string]string{
|
|
|
|
"test": "data",
|
|
|
|
},
|
|
|
|
}
|
2023-04-25 12:39:46 -05:00
|
|
|
actual := PostableGrafanaReceiverToGrafanaIntegrationConfig(r)
|
|
|
|
require.Equal(t, alertingNotify.GrafanaIntegrationConfig{
|
2023-04-13 11:25:32 -05:00
|
|
|
UID: "test-uid",
|
|
|
|
Name: "test-name",
|
|
|
|
Type: "slack",
|
|
|
|
DisableResolveMessage: false,
|
|
|
|
Settings: json.RawMessage(`{ "data" : "test" }`),
|
|
|
|
SecureSettings: map[string]string{
|
|
|
|
"test": "data",
|
|
|
|
},
|
|
|
|
}, *actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostableApiReceiverToApiReceiver(t *testing.T) {
|
|
|
|
t.Run("returns empty when no receivers", func(t *testing.T) {
|
|
|
|
r := &apimodels.PostableApiReceiver{
|
|
|
|
Receiver: config.Receiver{
|
|
|
|
Name: "test-receiver",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := PostableApiReceiverToApiReceiver(r)
|
2023-04-25 12:39:46 -05:00
|
|
|
require.Empty(t, actual.Integrations)
|
2023-04-13 11:25:32 -05:00
|
|
|
require.Equal(t, r.Receiver, actual.ConfigReceiver)
|
|
|
|
})
|
|
|
|
t.Run("converts receivers", func(t *testing.T) {
|
|
|
|
r := &apimodels.PostableApiReceiver{
|
|
|
|
Receiver: config.Receiver{
|
|
|
|
Name: "test-receiver",
|
|
|
|
},
|
|
|
|
PostableGrafanaReceivers: apimodels.PostableGrafanaReceivers{
|
|
|
|
GrafanaManagedReceivers: []*apimodels.PostableGrafanaReceiver{
|
|
|
|
{
|
|
|
|
UID: "test-uid",
|
|
|
|
Name: "test-name",
|
|
|
|
Type: "slack",
|
|
|
|
DisableResolveMessage: false,
|
|
|
|
Settings: apimodels.RawMessage(`{ "data" : "test" }`),
|
|
|
|
SecureSettings: map[string]string{
|
|
|
|
"test": "data",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
UID: "test-uid2",
|
|
|
|
Name: "test-name2",
|
|
|
|
Type: "webhook",
|
|
|
|
DisableResolveMessage: false,
|
|
|
|
Settings: apimodels.RawMessage(`{ "data2" : "test2" }`),
|
|
|
|
SecureSettings: map[string]string{
|
|
|
|
"test2": "data2",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := PostableApiReceiverToApiReceiver(r)
|
2023-04-25 12:39:46 -05:00
|
|
|
require.Len(t, actual.Integrations, 2)
|
2023-04-13 11:25:32 -05:00
|
|
|
require.Equal(t, r.Receiver, actual.ConfigReceiver)
|
2023-04-25 12:39:46 -05:00
|
|
|
require.Equal(t, *PostableGrafanaReceiverToGrafanaIntegrationConfig(r.GrafanaManagedReceivers[0]), *actual.Integrations[0])
|
|
|
|
require.Equal(t, *PostableGrafanaReceiverToGrafanaIntegrationConfig(r.GrafanaManagedReceivers[1]), *actual.Integrations[1])
|
2023-04-13 11:25:32 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostableApiAlertingConfigToApiReceivers(t *testing.T) {
|
|
|
|
t.Run("returns empty when no receivers", func(t *testing.T) {
|
|
|
|
r := apimodels.PostableApiAlertingConfig{
|
|
|
|
Config: apimodels.Config{},
|
|
|
|
}
|
|
|
|
actual := PostableApiAlertingConfigToApiReceivers(r)
|
|
|
|
require.Empty(t, actual)
|
|
|
|
})
|
|
|
|
c := apimodels.PostableApiAlertingConfig{
|
|
|
|
Config: apimodels.Config{},
|
|
|
|
Receivers: []*apimodels.PostableApiReceiver{
|
|
|
|
{
|
|
|
|
Receiver: config.Receiver{
|
|
|
|
Name: "test-receiver",
|
|
|
|
},
|
|
|
|
PostableGrafanaReceivers: apimodels.PostableGrafanaReceivers{
|
|
|
|
GrafanaManagedReceivers: []*apimodels.PostableGrafanaReceiver{
|
|
|
|
{
|
|
|
|
UID: "test-uid",
|
|
|
|
Name: "test-name",
|
|
|
|
Type: "slack",
|
|
|
|
DisableResolveMessage: false,
|
|
|
|
Settings: apimodels.RawMessage(`{ "data" : "test" }`),
|
|
|
|
SecureSettings: map[string]string{
|
|
|
|
"test": "data",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Receiver: config.Receiver{
|
|
|
|
Name: "test-receiver2",
|
|
|
|
},
|
|
|
|
PostableGrafanaReceivers: apimodels.PostableGrafanaReceivers{
|
|
|
|
GrafanaManagedReceivers: []*apimodels.PostableGrafanaReceiver{
|
|
|
|
{
|
|
|
|
UID: "test-uid2",
|
|
|
|
Name: "test-name1",
|
|
|
|
Type: "slack",
|
|
|
|
DisableResolveMessage: false,
|
|
|
|
Settings: apimodels.RawMessage(`{ "data" : "test" }`),
|
|
|
|
SecureSettings: map[string]string{
|
|
|
|
"test": "data",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
actual := PostableApiAlertingConfigToApiReceivers(c)
|
|
|
|
|
|
|
|
require.Len(t, actual, 2)
|
|
|
|
require.Equal(t, PostableApiReceiverToApiReceiver(c.Receivers[0]), actual[0])
|
|
|
|
require.Equal(t, PostableApiReceiverToApiReceiver(c.Receivers[1]), actual[1])
|
|
|
|
}
|