From 3b8ceda26a47d898f84773d608a7765ea6c37d66 Mon Sep 17 00:00:00 2001 From: Owen Diehl Date: Wed, 24 Feb 2021 10:35:03 -0500 Subject: [PATCH] receiver unmarshaling tests --- go.mod | 1 + pkg/api/alertmanager.go | 50 +++++++++++++++++++++++++++ pkg/api/alertmanager_test.go | 65 ++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 pkg/api/alertmanager_test.go diff --git a/go.mod b/go.mod index d8502021986..d4de040570e 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/prometheus/alertmanager v0.21.1-0.20210211203738-a7ca7b1d2951 github.com/prometheus/client_golang v1.9.0 github.com/prometheus/common v0.15.0 + github.com/stretchr/testify v1.7.0 golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect golang.org/x/oauth2 v0.0.0-20210210192628-66670185b0cd // indirect gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect diff --git a/pkg/api/alertmanager.go b/pkg/api/alertmanager.go index c54c7f5f8f2..581c59614b7 100644 --- a/pkg/api/alertmanager.go +++ b/pkg/api/alertmanager.go @@ -1,6 +1,9 @@ package api import ( + "encoding/json" + "fmt" + "github.com/grafana/grafana/pkg/models" amv2 "github.com/prometheus/alertmanager/api/v2/models" "github.com/prometheus/alertmanager/config" @@ -177,11 +180,58 @@ type ApiAlertingConfig struct { type GrafanaReceiver models.CreateAlertNotificationCommand +type ReceiverType int + +const ( + GrafanaManagedReceiver ReceiverType = iota + AMReceiver +) + type ApiReceiver struct { config.Receiver GrafanaReceivers } +func (r *ApiReceiver) UnmarshalJSON(b []byte) error { + type plain ApiReceiver + if err := json.Unmarshal(b, (*plain)(r)); err != nil { + return err + } + + hasGrafanaReceivers := len(r.GrafanaReceivers.GrafanaManagedReceivers) > 0 + + if hasGrafanaReceivers { + if len(r.EmailConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager EmailConfigs & Grafana receivers together") + } + if len(r.PagerdutyConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager PagerdutyConfigs & Grafana receivers together") + } + if len(r.SlackConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager SlackConfigs & Grafana receivers together") + } + if len(r.WebhookConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager WebhookConfigs & Grafana receivers together") + } + if len(r.OpsGenieConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager OpsGenieConfigs & Grafana receivers together") + } + if len(r.WechatConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager WechatConfigs & Grafana receivers together") + } + if len(r.PushoverConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager PushoverConfigs & Grafana receivers together") + } + if len(r.VictorOpsConfigs) > 0 { + return fmt.Errorf("cannot have both Alertmanager VictorOpsConfigs & Grafana receivers together") + } + + } + + return nil + +} + type GrafanaReceivers struct { GrafanaManagedReceivers []*GrafanaReceiver `yaml:"grafana_managed_receiver_configs,omitempty" json:"grafana_managed_receiver_configs,omitempty"` } diff --git a/pkg/api/alertmanager_test.go b/pkg/api/alertmanager_test.go new file mode 100644 index 00000000000..1a47516d581 --- /dev/null +++ b/pkg/api/alertmanager_test.go @@ -0,0 +1,65 @@ +package api + +import ( + "encoding/json" + "testing" + + "github.com/prometheus/alertmanager/config" + "github.com/stretchr/testify/require" +) + +func Test_ApiReceiver_Marshaling(t *testing.T) { + for _, tc := range []struct { + desc string + input ApiReceiver + err bool + }{ + { + desc: "success AM", + input: ApiReceiver{ + Receiver: config.Receiver{ + Name: "foo", + EmailConfigs: []*config.EmailConfig{{}}, + }, + }, + }, + { + desc: "success GM", + input: ApiReceiver{ + Receiver: config.Receiver{ + Name: "foo", + }, + GrafanaReceivers: GrafanaReceivers{ + GrafanaManagedReceivers: []*GrafanaReceiver{{}}, + }, + }, + }, + { + desc: "failure mixed", + input: ApiReceiver{ + Receiver: config.Receiver{ + Name: "foo", + EmailConfigs: []*config.EmailConfig{{}}, + }, + GrafanaReceivers: GrafanaReceivers{ + GrafanaManagedReceivers: []*GrafanaReceiver{{}}, + }, + }, + err: true, + }, + } { + t.Run(tc.desc, func(t *testing.T) { + encoded, err := json.Marshal(tc.input) + require.Nil(t, err) + + var out ApiReceiver + err = json.Unmarshal(encoded, &out) + + if tc.err { + require.Error(t, err) + } else { + require.Equal(t, tc.input, out) + } + }) + } +}