github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/ruler/notifier_test.go (about) 1 package ruler 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 8 config_util "github.com/prometheus/common/config" 9 "github.com/prometheus/common/model" 10 "github.com/prometheus/prometheus/config" 11 "github.com/prometheus/prometheus/discovery" 12 "github.com/prometheus/prometheus/discovery/dns" 13 "github.com/stretchr/testify/require" 14 15 "github.com/cortexproject/cortex/pkg/util" 16 ) 17 18 func TestBuildNotifierConfig(t *testing.T) { 19 tests := []struct { 20 name string 21 cfg *Config 22 ncfg *config.Config 23 err error 24 }{ 25 { 26 name: "with no valid hosts, returns an empty config", 27 cfg: &Config{}, 28 ncfg: &config.Config{}, 29 }, 30 { 31 name: "with a single URL and no service discovery", 32 cfg: &Config{ 33 AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager", 34 }, 35 ncfg: &config.Config{ 36 AlertingConfig: config.AlertingConfig{ 37 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 38 { 39 APIVersion: "v1", 40 Scheme: "http", 41 PathPrefix: "/alertmanager", 42 ServiceDiscoveryConfigs: discovery.Configs{ 43 discovery.StaticConfig{ 44 { 45 Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}}, 46 }, 47 }, 48 }, 49 }, 50 }, 51 }, 52 }, 53 }, 54 { 55 name: "with a single URL and service discovery", 56 cfg: &Config{ 57 AlertmanagerURL: "http://_http._tcp.alertmanager.default.svc.cluster.local/alertmanager", 58 AlertmanagerDiscovery: true, 59 AlertmanagerRefreshInterval: time.Duration(60), 60 }, 61 ncfg: &config.Config{ 62 AlertingConfig: config.AlertingConfig{ 63 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 64 { 65 APIVersion: "v1", 66 Scheme: "http", 67 PathPrefix: "/alertmanager", 68 ServiceDiscoveryConfigs: discovery.Configs{ 69 &dns.SDConfig{ 70 Names: []string{"_http._tcp.alertmanager.default.svc.cluster.local"}, 71 RefreshInterval: 60, 72 Type: "SRV", 73 Port: 0, 74 }, 75 }, 76 }, 77 }, 78 }, 79 }, 80 }, 81 { 82 name: "with service discovery and an invalid URL", 83 cfg: &Config{ 84 AlertmanagerURL: "http://_http.default.svc.cluster.local/alertmanager", 85 AlertmanagerDiscovery: true, 86 }, 87 err: fmt.Errorf("when alertmanager-discovery is on, host name must be of the form _portname._tcp.service.fqdn (is \"alertmanager.default.svc.cluster.local\")"), 88 }, 89 { 90 name: "with multiple URLs and no service discovery", 91 cfg: &Config{ 92 AlertmanagerURL: "http://alertmanager-0.default.svc.cluster.local/alertmanager,http://alertmanager-1.default.svc.cluster.local/alertmanager", 93 }, 94 ncfg: &config.Config{ 95 AlertingConfig: config.AlertingConfig{ 96 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 97 { 98 APIVersion: "v1", 99 Scheme: "http", 100 PathPrefix: "/alertmanager", 101 ServiceDiscoveryConfigs: discovery.Configs{ 102 discovery.StaticConfig{{ 103 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 104 }, 105 }, 106 }, 107 }, 108 { 109 APIVersion: "v1", 110 Scheme: "http", 111 PathPrefix: "/alertmanager", 112 ServiceDiscoveryConfigs: discovery.Configs{ 113 discovery.StaticConfig{{ 114 Targets: []model.LabelSet{{"__address__": "alertmanager-1.default.svc.cluster.local"}}, 115 }, 116 }, 117 }, 118 }, 119 }, 120 }, 121 }, 122 }, 123 { 124 name: "with multiple URLs and service discovery", 125 cfg: &Config{ 126 AlertmanagerURL: "http://_http._tcp.alertmanager-0.default.svc.cluster.local/alertmanager,http://_http._tcp.alertmanager-1.default.svc.cluster.local/alertmanager", 127 AlertmanagerDiscovery: true, 128 AlertmanagerRefreshInterval: time.Duration(60), 129 }, 130 ncfg: &config.Config{ 131 AlertingConfig: config.AlertingConfig{ 132 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 133 { 134 APIVersion: "v1", 135 Scheme: "http", 136 PathPrefix: "/alertmanager", 137 ServiceDiscoveryConfigs: discovery.Configs{ 138 &dns.SDConfig{ 139 Names: []string{"_http._tcp.alertmanager-0.default.svc.cluster.local"}, 140 RefreshInterval: 60, 141 Type: "SRV", 142 Port: 0, 143 }, 144 }, 145 }, 146 { 147 APIVersion: "v1", 148 Scheme: "http", 149 PathPrefix: "/alertmanager", 150 ServiceDiscoveryConfigs: discovery.Configs{ 151 &dns.SDConfig{ 152 Names: []string{"_http._tcp.alertmanager-1.default.svc.cluster.local"}, 153 RefreshInterval: 60, 154 Type: "SRV", 155 Port: 0, 156 }, 157 }, 158 }, 159 }, 160 }, 161 }, 162 }, 163 { 164 name: "with Basic Authentication URL", 165 cfg: &Config{ 166 AlertmanagerURL: "http://marco:hunter2@alertmanager-0.default.svc.cluster.local/alertmanager", 167 }, 168 ncfg: &config.Config{ 169 AlertingConfig: config.AlertingConfig{ 170 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 171 { 172 HTTPClientConfig: config_util.HTTPClientConfig{ 173 BasicAuth: &config_util.BasicAuth{Username: "marco", Password: "hunter2"}, 174 }, 175 APIVersion: "v1", 176 Scheme: "http", 177 PathPrefix: "/alertmanager", 178 ServiceDiscoveryConfigs: discovery.Configs{ 179 discovery.StaticConfig{ 180 { 181 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 182 }, 183 }, 184 }, 185 }, 186 }, 187 }, 188 }, 189 }, 190 { 191 name: "with Basic Authentication URL and Explicit", 192 cfg: &Config{ 193 AlertmanagerURL: "http://marco:hunter2@alertmanager-0.default.svc.cluster.local/alertmanager", 194 Notifier: NotifierConfig{ 195 BasicAuth: util.BasicAuth{ 196 Username: "jacob", 197 Password: "test", 198 }, 199 }, 200 }, 201 ncfg: &config.Config{ 202 AlertingConfig: config.AlertingConfig{ 203 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 204 { 205 HTTPClientConfig: config_util.HTTPClientConfig{ 206 BasicAuth: &config_util.BasicAuth{Username: "jacob", Password: "test"}, 207 }, 208 APIVersion: "v1", 209 Scheme: "http", 210 PathPrefix: "/alertmanager", 211 ServiceDiscoveryConfigs: discovery.Configs{ 212 discovery.StaticConfig{ 213 { 214 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 215 }, 216 }, 217 }, 218 }, 219 }, 220 }, 221 }, 222 }, 223 } 224 225 for _, tt := range tests { 226 t.Run(tt.name, func(t *testing.T) { 227 ncfg, err := buildNotifierConfig(tt.cfg) 228 if tt.err == nil { 229 require.NoError(t, err) 230 require.Equal(t, tt.ncfg, ncfg) 231 } else { 232 require.Error(t, tt.err, err) 233 } 234 }) 235 } 236 }