receiver unmarshaling tests

This commit is contained in:
Owen Diehl
2021-02-24 10:35:03 -05:00
parent 0e9b78868f
commit 3b8ceda26a
3 changed files with 116 additions and 0 deletions
+1
View File
@@ -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
+50
View File
@@ -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"`
}
+65
View File
@@ -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)
}
})
}
}