-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add YAML unmarshalers for Alertmanager configuration structs (#186)
Add YAML unmarshalers for Alertmanager configuration struct
- Loading branch information
1 parent
bb4f4f4
commit b986cf0
Showing
2 changed files
with
90 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,12 @@ func Test_ApiReceiver_Marshaling(t *testing.T) { | |
desc: "success AM", | ||
input: PostableApiReceiver{ | ||
Receiver: config.Receiver{ | ||
Name: "foo", | ||
EmailConfigs: []*config.EmailConfig{{}}, | ||
Name: "foo", | ||
EmailConfigs: []*config.EmailConfig{{ | ||
To: "[email protected]", | ||
HTML: config.DefaultEmailConfig.HTML, | ||
Headers: map[string]string{}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -42,8 +46,12 @@ func Test_ApiReceiver_Marshaling(t *testing.T) { | |
desc: "failure mixed", | ||
input: PostableApiReceiver{ | ||
Receiver: config.Receiver{ | ||
Name: "foo", | ||
EmailConfigs: []*config.EmailConfig{{}}, | ||
Name: "foo", | ||
EmailConfigs: []*config.EmailConfig{{ | ||
To: "[email protected]", | ||
HTML: config.DefaultEmailConfig.HTML, | ||
Headers: map[string]string{}, | ||
}}, | ||
}, | ||
PostableGrafanaReceivers: PostableGrafanaReceivers{ | ||
GrafanaManagedReceivers: []*PostableGrafanaReceiver{{}}, | ||
|
@@ -88,8 +96,12 @@ func Test_APIReceiverType(t *testing.T) { | |
desc: "am", | ||
input: PostableApiReceiver{ | ||
Receiver: config.Receiver{ | ||
Name: "foo", | ||
EmailConfigs: []*config.EmailConfig{{}}, | ||
Name: "foo", | ||
EmailConfigs: []*config.EmailConfig{{ | ||
To: "[email protected]", | ||
HTML: config.DefaultEmailConfig.HTML, | ||
Headers: map[string]string{}, | ||
}}, | ||
}, | ||
}, | ||
expected: AlertmanagerReceiverType, | ||
|
@@ -140,6 +152,7 @@ func Test_AllReceivers(t *testing.T) { | |
} | ||
|
||
func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | ||
defaultGlobalConfig := config.DefaultGlobalConfig() | ||
for _, tc := range []struct { | ||
desc string | ||
input PostableApiAlertingConfig | ||
|
@@ -149,6 +162,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "success am", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "am", | ||
Routes: []*Route{ | ||
|
@@ -161,8 +175,12 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
Receivers: []*PostableApiReceiver{ | ||
{ | ||
Receiver: config.Receiver{ | ||
Name: "am", | ||
EmailConfigs: []*config.EmailConfig{{}}, | ||
Name: "am", | ||
EmailConfigs: []*config.EmailConfig{{ | ||
To: "[email protected]", | ||
HTML: config.DefaultEmailConfig.HTML, | ||
Headers: map[string]string{}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -172,6 +190,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "success graf", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "graf", | ||
Routes: []*Route{ | ||
|
@@ -197,6 +216,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "failure undefined am receiver", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "am", | ||
Routes: []*Route{ | ||
|
@@ -209,8 +229,12 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
Receivers: []*PostableApiReceiver{ | ||
{ | ||
Receiver: config.Receiver{ | ||
Name: "am", | ||
EmailConfigs: []*config.EmailConfig{{}}, | ||
Name: "am", | ||
EmailConfigs: []*config.EmailConfig{{ | ||
To: "[email protected]", | ||
HTML: config.DefaultEmailConfig.HTML, | ||
Headers: map[string]string{}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -221,6 +245,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "failure undefined graf receiver", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "graf", | ||
Routes: []*Route{ | ||
|
@@ -263,6 +288,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "failure graf no default receiver", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Routes: []*Route{ | ||
{ | ||
|
@@ -288,6 +314,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "failure graf root route with matchers", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "graf", | ||
Routes: []*Route{ | ||
|
@@ -315,6 +342,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "failure graf nested route duplicate group by labels", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "graf", | ||
Routes: []*Route{ | ||
|
@@ -342,6 +370,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "success undefined am receiver in autogenerated route is ignored", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "am", | ||
Routes: []*Route{ | ||
|
@@ -365,8 +394,12 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
Receivers: []*PostableApiReceiver{ | ||
{ | ||
Receiver: config.Receiver{ | ||
Name: "am", | ||
EmailConfigs: []*config.EmailConfig{{}}, | ||
Name: "am", | ||
EmailConfigs: []*config.EmailConfig{{ | ||
To: "[email protected]", | ||
HTML: config.DefaultEmailConfig.HTML, | ||
Headers: map[string]string{}, | ||
}}, | ||
}, | ||
}, | ||
}, | ||
|
@@ -377,6 +410,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
desc: "success undefined graf receiver in autogenerated route is ignored", | ||
input: PostableApiAlertingConfig{ | ||
Config: Config{ | ||
Global: &defaultGlobalConfig, | ||
Route: &Route{ | ||
Receiver: "graf", | ||
Routes: []*Route{ | ||
|
@@ -411,7 +445,7 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
err: false, | ||
}, | ||
} { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
t.Run(tc.desc+" (json)", func(t *testing.T) { | ||
encoded, err := json.Marshal(tc.input) | ||
require.Nil(t, err) | ||
|
||
|
@@ -425,6 +459,21 @@ func Test_ApiAlertingConfig_Marshaling(t *testing.T) { | |
require.Equal(t, tc.input, out) | ||
} | ||
}) | ||
|
||
t.Run(tc.desc+" (yaml)", func(t *testing.T) { | ||
encoded, err := yaml.Marshal(tc.input) | ||
require.Nil(t, err) | ||
|
||
var out PostableApiAlertingConfig | ||
err = yaml.Unmarshal(encoded, &out) | ||
|
||
if tc.err { | ||
require.Error(t, err) | ||
} else { | ||
require.Nil(t, err) | ||
require.Equal(t, tc.input, out) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
|