github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/pkg/ruler/base/notifier_test.go (about) 1 package base 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/prometheus/prometheus/model/labels" 14 "github.com/prometheus/prometheus/model/relabel" 15 "github.com/stretchr/testify/require" 16 17 "github.com/grafana/loki/pkg/util" 18 ) 19 20 func TestBuildNotifierConfig(t *testing.T) { 21 tests := []struct { 22 name string 23 cfg *Config 24 ncfg *config.Config 25 err error 26 }{ 27 { 28 name: "with no valid hosts, returns an empty config", 29 cfg: &Config{}, 30 ncfg: &config.Config{}, 31 }, 32 { 33 name: "with a single URL and no service discovery", 34 cfg: &Config{ 35 AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager", 36 }, 37 ncfg: &config.Config{ 38 AlertingConfig: config.AlertingConfig{ 39 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 40 { 41 APIVersion: "v1", 42 Scheme: "http", 43 PathPrefix: "/alertmanager", 44 ServiceDiscoveryConfigs: discovery.Configs{ 45 discovery.StaticConfig{ 46 { 47 Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}}, 48 }, 49 }, 50 }, 51 }, 52 }, 53 }, 54 }, 55 }, 56 { 57 name: "with a single URL and service discovery", 58 cfg: &Config{ 59 AlertmanagerURL: "http://_http._tcp.alertmanager.default.svc.cluster.local/alertmanager", 60 AlertmanagerDiscovery: true, 61 AlertmanagerRefreshInterval: time.Duration(60), 62 }, 63 ncfg: &config.Config{ 64 AlertingConfig: config.AlertingConfig{ 65 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 66 { 67 APIVersion: "v1", 68 Scheme: "http", 69 PathPrefix: "/alertmanager", 70 ServiceDiscoveryConfigs: discovery.Configs{ 71 &dns.SDConfig{ 72 Names: []string{"_http._tcp.alertmanager.default.svc.cluster.local"}, 73 RefreshInterval: 60, 74 Type: "SRV", 75 Port: 0, 76 }, 77 }, 78 }, 79 }, 80 }, 81 }, 82 }, 83 { 84 name: "with service discovery and an invalid URL", 85 cfg: &Config{ 86 AlertmanagerURL: "http://_http.default.svc.cluster.local/alertmanager", 87 AlertmanagerDiscovery: true, 88 }, 89 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\")"), 90 }, 91 { 92 name: "with multiple URLs and no service discovery", 93 cfg: &Config{ 94 AlertmanagerURL: "http://alertmanager-0.default.svc.cluster.local/alertmanager,http://alertmanager-1.default.svc.cluster.local/alertmanager", 95 }, 96 ncfg: &config.Config{ 97 AlertingConfig: config.AlertingConfig{ 98 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 99 { 100 APIVersion: "v1", 101 Scheme: "http", 102 PathPrefix: "/alertmanager", 103 ServiceDiscoveryConfigs: discovery.Configs{ 104 discovery.StaticConfig{{ 105 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 106 }, 107 }, 108 }, 109 }, 110 { 111 APIVersion: "v1", 112 Scheme: "http", 113 PathPrefix: "/alertmanager", 114 ServiceDiscoveryConfigs: discovery.Configs{ 115 discovery.StaticConfig{{ 116 Targets: []model.LabelSet{{"__address__": "alertmanager-1.default.svc.cluster.local"}}, 117 }, 118 }, 119 }, 120 }, 121 }, 122 }, 123 }, 124 }, 125 { 126 name: "with multiple URLs and service discovery", 127 cfg: &Config{ 128 AlertmanagerURL: "http://_http._tcp.alertmanager-0.default.svc.cluster.local/alertmanager,http://_http._tcp.alertmanager-1.default.svc.cluster.local/alertmanager", 129 AlertmanagerDiscovery: true, 130 AlertmanagerRefreshInterval: time.Duration(60), 131 }, 132 ncfg: &config.Config{ 133 AlertingConfig: config.AlertingConfig{ 134 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 135 { 136 APIVersion: "v1", 137 Scheme: "http", 138 PathPrefix: "/alertmanager", 139 ServiceDiscoveryConfigs: discovery.Configs{ 140 &dns.SDConfig{ 141 Names: []string{"_http._tcp.alertmanager-0.default.svc.cluster.local"}, 142 RefreshInterval: 60, 143 Type: "SRV", 144 Port: 0, 145 }, 146 }, 147 }, 148 { 149 APIVersion: "v1", 150 Scheme: "http", 151 PathPrefix: "/alertmanager", 152 ServiceDiscoveryConfigs: discovery.Configs{ 153 &dns.SDConfig{ 154 Names: []string{"_http._tcp.alertmanager-1.default.svc.cluster.local"}, 155 RefreshInterval: 60, 156 Type: "SRV", 157 Port: 0, 158 }, 159 }, 160 }, 161 }, 162 }, 163 }, 164 }, 165 { 166 name: "with Basic Authentication URL", 167 cfg: &Config{ 168 AlertmanagerURL: "http://marco:hunter2@alertmanager-0.default.svc.cluster.local/alertmanager", 169 }, 170 ncfg: &config.Config{ 171 AlertingConfig: config.AlertingConfig{ 172 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 173 { 174 HTTPClientConfig: config_util.HTTPClientConfig{ 175 BasicAuth: &config_util.BasicAuth{Username: "marco", Password: "hunter2"}, 176 }, 177 APIVersion: "v1", 178 Scheme: "http", 179 PathPrefix: "/alertmanager", 180 ServiceDiscoveryConfigs: discovery.Configs{ 181 discovery.StaticConfig{ 182 { 183 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 184 }, 185 }, 186 }, 187 }, 188 }, 189 }, 190 }, 191 }, 192 { 193 name: "with Basic Authentication URL and Explicit", 194 cfg: &Config{ 195 AlertmanagerURL: "http://marco:hunter2@alertmanager-0.default.svc.cluster.local/alertmanager", 196 Notifier: NotifierConfig{ 197 BasicAuth: util.BasicAuth{ 198 Username: "jacob", 199 Password: "test", 200 }, 201 }, 202 }, 203 ncfg: &config.Config{ 204 AlertingConfig: config.AlertingConfig{ 205 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 206 { 207 HTTPClientConfig: config_util.HTTPClientConfig{ 208 BasicAuth: &config_util.BasicAuth{Username: "jacob", Password: "test"}, 209 }, 210 APIVersion: "v1", 211 Scheme: "http", 212 PathPrefix: "/alertmanager", 213 ServiceDiscoveryConfigs: discovery.Configs{ 214 discovery.StaticConfig{ 215 { 216 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 217 }, 218 }, 219 }, 220 }, 221 }, 222 }, 223 }, 224 }, 225 { 226 name: "with Header Authorization", 227 cfg: &Config{ 228 AlertmanagerURL: "http://alertmanager-0.default.svc.cluster.local/alertmanager", 229 Notifier: NotifierConfig{ 230 HeaderAuth: util.HeaderAuth{ 231 Type: "Bearer", 232 Credentials: "jacob", 233 }, 234 }, 235 }, 236 ncfg: &config.Config{ 237 AlertingConfig: config.AlertingConfig{ 238 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 239 { 240 HTTPClientConfig: config_util.HTTPClientConfig{ 241 Authorization: &config_util.Authorization{ 242 Type: "Bearer", 243 Credentials: config_util.Secret("jacob"), 244 }, 245 }, 246 APIVersion: "v1", 247 Scheme: "http", 248 PathPrefix: "/alertmanager", 249 ServiceDiscoveryConfigs: discovery.Configs{ 250 discovery.StaticConfig{ 251 { 252 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 253 }, 254 }, 255 }, 256 }, 257 }, 258 }, 259 }, 260 }, 261 { 262 name: "with Header Authorization and credentials file", 263 cfg: &Config{ 264 AlertmanagerURL: "http://alertmanager-0.default.svc.cluster.local/alertmanager", 265 Notifier: NotifierConfig{ 266 HeaderAuth: util.HeaderAuth{ 267 Type: "Bearer", 268 CredentialsFile: "/path/to/secret/file", 269 }, 270 }, 271 }, 272 ncfg: &config.Config{ 273 AlertingConfig: config.AlertingConfig{ 274 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 275 { 276 HTTPClientConfig: config_util.HTTPClientConfig{ 277 Authorization: &config_util.Authorization{ 278 Type: "Bearer", 279 CredentialsFile: "/path/to/secret/file", 280 }, 281 }, 282 APIVersion: "v1", 283 Scheme: "http", 284 PathPrefix: "/alertmanager", 285 ServiceDiscoveryConfigs: discovery.Configs{ 286 discovery.StaticConfig{ 287 { 288 Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, 289 }, 290 }, 291 }, 292 }, 293 }, 294 }, 295 }, 296 }, 297 { 298 name: "with external labels", 299 cfg: &Config{ 300 AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager", 301 ExternalLabels: []labels.Label{ 302 {Name: "region", Value: "us-east-1"}, 303 }, 304 }, 305 ncfg: &config.Config{ 306 AlertingConfig: config.AlertingConfig{ 307 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 308 { 309 APIVersion: "v1", 310 Scheme: "http", 311 PathPrefix: "/alertmanager", 312 ServiceDiscoveryConfigs: discovery.Configs{ 313 discovery.StaticConfig{ 314 { 315 Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}}, 316 }, 317 }, 318 }, 319 }, 320 }, 321 }, 322 GlobalConfig: config.GlobalConfig{ 323 ExternalLabels: []labels.Label{ 324 {Name: "region", Value: "us-east-1"}, 325 }, 326 }, 327 }, 328 }, 329 { 330 name: "with alert relabel config", 331 cfg: &Config{ 332 AlertmanagerURL: "http://alertmanager.default.svc.cluster.local/alertmanager", 333 ExternalLabels: []labels.Label{ 334 {Name: "region", Value: "us-east-1"}, 335 }, 336 AlertRelabelConfigs: []*relabel.Config{ 337 { 338 SourceLabels: model.LabelNames{"severity"}, 339 Regex: relabel.MustNewRegexp("high"), 340 TargetLabel: "priority", 341 Replacement: "p1", 342 }, 343 }, 344 }, 345 ncfg: &config.Config{ 346 AlertingConfig: config.AlertingConfig{ 347 AlertmanagerConfigs: []*config.AlertmanagerConfig{ 348 { 349 APIVersion: "v1", 350 Scheme: "http", 351 PathPrefix: "/alertmanager", 352 ServiceDiscoveryConfigs: discovery.Configs{ 353 discovery.StaticConfig{ 354 { 355 Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}}, 356 }, 357 }, 358 }, 359 }, 360 }, 361 AlertRelabelConfigs: []*relabel.Config{ 362 { 363 SourceLabels: model.LabelNames{"severity"}, 364 Regex: relabel.MustNewRegexp("high"), 365 TargetLabel: "priority", 366 Replacement: "p1", 367 }, 368 }, 369 }, 370 GlobalConfig: config.GlobalConfig{ 371 ExternalLabels: []labels.Label{ 372 {Name: "region", Value: "us-east-1"}, 373 }, 374 }, 375 }, 376 }, 377 } 378 379 for _, tt := range tests { 380 t.Run(tt.name, func(t *testing.T) { 381 ncfg, err := buildNotifierConfig(tt.cfg) 382 if tt.err == nil { 383 require.NoError(t, err) 384 require.Equal(t, tt.ncfg, ncfg) 385 } else { 386 require.Error(t, tt.err, err) 387 } 388 }) 389 } 390 }