Skip to content

Commit

Permalink
Tests for notifier config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexweav committed Mar 6, 2025
1 parent f8fd2e3 commit 1276bfd
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 3 deletions.
6 changes: 3 additions & 3 deletions pkg/ruler/notifier/notifier_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
package notifier

import (
"encoding/json"
"flag"
"fmt"
"hash/fnv"

"github.com/grafana/dskit/crypto/tls"
"github.com/grafana/dskit/flagext"
"gopkg.in/yaml.v3"

"github.com/grafana/mimir/pkg/util"
)
Expand All @@ -28,7 +28,7 @@ type AlertmanagerClientConfig struct {
}

func (acc *AlertmanagerClientConfig) String() string {
out, err := json.Marshal(acc)
out, err := yaml.Marshal(acc)
if err != nil {
return fmt.Sprintf("failed to marshal: %v", err)
}
Expand All @@ -37,7 +37,7 @@ func (acc *AlertmanagerClientConfig) String() string {

func (acc *AlertmanagerClientConfig) Set(s string) error {
cfg := AlertmanagerClientConfig{}
if err := json.Unmarshal([]byte(s), &cfg); err != nil {
if err := yaml.Unmarshal([]byte(s), &cfg); err != nil {
return err
}
*acc = cfg
Expand Down
300 changes: 300 additions & 0 deletions pkg/ruler/notifier/notifier_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,300 @@
// SPDX-License-Identifier: AGPL-3.0-only

package notifier

import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

"github.com/grafana/dskit/crypto/tls"
"github.com/grafana/dskit/flagext"
)

func TestAlertmanagerClientConfig(t *testing.T) {
t.Run("IsDefault", func(t *testing.T) {
tc := []struct {
name string
cfg *AlertmanagerClientConfig
exp bool
}{
{
name: "default",
cfg: &DefaultAlertmanagerClientConfig,
exp: true,
},
{
name: "initialized limits map",
cfg: &AlertmanagerClientConfig{
NotifierConfig: Config{
OAuth2: OAuth2Config{
EndpointParams: flagext.NewLimitsMap[string](nil),
},
},
},
exp: true,
},
{
name: "empty scopes",
cfg: &AlertmanagerClientConfig{
NotifierConfig: Config{
OAuth2: OAuth2Config{
Scopes: []string{},
},
},
},
exp: true,
},
{
name: "golang default",
cfg: &AlertmanagerClientConfig{},
exp: true,
},
{
name: "custom TLS reader ignored",
cfg: &AlertmanagerClientConfig{
NotifierConfig: Config{
TLS: tls.ClientConfig{
Reader: &fakeSecretReader{},
},
},
},
exp: true,
},
{
name: "modified field",
cfg: &AlertmanagerClientConfig{
AlertmanagerURL: "test",
},
exp: false,
},
{
name: "modified endpoint param",
cfg: &AlertmanagerClientConfig{
NotifierConfig: Config{
OAuth2: OAuth2Config{
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{"k1": "v1"}, nil),
},
},
},
exp: false,
},
{
name: "modified scope",
cfg: &AlertmanagerClientConfig{
NotifierConfig: Config{
OAuth2: OAuth2Config{
Scopes: []string{"asdf"},
},
},
},
exp: false,
},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.exp, tt.cfg.IsDefault(), tt.cfg)
})
}
})

t.Run("Equal", func(t *testing.T) {
tc := []struct {
name string
cfg1 AlertmanagerClientConfig
cfg2 AlertmanagerClientConfig
exp bool
}{
{
name: "same values",
cfg1: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
ProxyURL: "http://some-proxy:1234",
TLS: tls.ClientConfig{
CertPath: "cert-path",
KeyPath: "key-path",
CAPath: "ca-path",
ServerName: "server",
InsecureSkipVerify: true,
CipherSuites: "TLS_AES_256_GCM_SHA384",
MinVersion: "1.3",
},
OAuth2: OAuth2Config{
ClientID: "myclient",
ClientSecret: flagext.SecretWithValue("mysecret"),
TokenURL: "http://token-url",
Scopes: []string{"abc", "def"},
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{
"key1": "value1",
}, nil),
},
},
},
cfg2: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
ProxyURL: "http://some-proxy:1234",
TLS: tls.ClientConfig{
CertPath: "cert-path",
KeyPath: "key-path",
CAPath: "ca-path",
ServerName: "server",
InsecureSkipVerify: true,
CipherSuites: "TLS_AES_256_GCM_SHA384",
MinVersion: "1.3",
},
OAuth2: OAuth2Config{
ClientID: "myclient",
ClientSecret: flagext.SecretWithValue("mysecret"),
TokenURL: "http://token-url",
Scopes: []string{"abc", "def"},
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{
"key1": "value1",
}, nil),
},
},
},
exp: true,
},
{
name: "differing value",
cfg1: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
ProxyURL: "http://some-proxy:1234",
TLS: tls.ClientConfig{
CertPath: "cert-path",
KeyPath: "key-path",
CAPath: "ca-path",
ServerName: "server",
InsecureSkipVerify: true,
CipherSuites: "TLS_AES_256_GCM_SHA384",
MinVersion: "1.3",
},
OAuth2: OAuth2Config{
ClientID: "myclient",
ClientSecret: flagext.SecretWithValue("mysecret"),
TokenURL: "http://token-url",
Scopes: []string{"abc", "def"},
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{
"key1": "value1",
}, nil),
},
},
},
cfg2: AlertmanagerClientConfig{
AlertmanagerURL: "http://another-url",
NotifierConfig: Config{
ProxyURL: "http://some-proxy:1234",
TLS: tls.ClientConfig{
CertPath: "cert-path",
KeyPath: "key-path",
CAPath: "ca-path",
ServerName: "server",
InsecureSkipVerify: true,
CipherSuites: "TLS_AES_256_GCM_SHA384",
MinVersion: "1.3",
},
OAuth2: OAuth2Config{
ClientID: "myclient",
ClientSecret: flagext.SecretWithValue("mysecret"),
TokenURL: "http://token-url",
Scopes: []string{"abc", "def"},
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{
"key1": "value1",
}, nil),
},
},
},
exp: false,
},
{
name: "different endpoint params order",
cfg1: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
OAuth2: OAuth2Config{
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{
"key1": "value1",
"key2": "value2",
}, nil),
},
},
},
cfg2: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
OAuth2: OAuth2Config{
EndpointParams: flagext.NewLimitsMapWithData(map[string]string{
"key2": "value2",
"key1": "value1",
}, nil),
},
},
},
exp: true,
},
{
name: "different scopes order",
cfg1: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
OAuth2: OAuth2Config{
Scopes: []string{"s1", "s2"},
},
},
},
cfg2: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
OAuth2: OAuth2Config{
Scopes: []string{"s2", "s1"},
},
},
},
exp: false,
},
{
name: "ignores different secrets reader",
cfg1: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
TLS: tls.ClientConfig{
Reader: &fakeSecretReader{},
},
OAuth2: OAuth2Config{
ClientID: "myclient",
},
},
},
cfg2: AlertmanagerClientConfig{
AlertmanagerURL: "http://some-url",
NotifierConfig: Config{
TLS: tls.ClientConfig{
Reader: nil,
},
OAuth2: OAuth2Config{
ClientID: "myclient",
},
},
},
exp: true,
},
}

for _, tt := range tc {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.exp, tt.cfg1.Equal(tt.cfg2), cmp.Diff(tt.cfg1, tt.cfg2))
})
}
})
}

type fakeSecretReader struct{}

func (fsr *fakeSecretReader) ReadSecret(path string) ([]byte, error) {
return []byte{}, nil
}

0 comments on commit 1276bfd

Please sign in to comment.