|
| 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 | + sd_config "github.com/prometheus/prometheus/discovery/config" |
| 12 | + "github.com/prometheus/prometheus/discovery/dns" |
| 13 | + "github.com/prometheus/prometheus/discovery/targetgroup" |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | +) |
| 16 | + |
| 17 | +func TestBuildNotifierConfig(t *testing.T) { |
| 18 | + tests := []struct { |
| 19 | + name string |
| 20 | + cfg *Config |
| 21 | + ncfg *config.Config |
| 22 | + err error |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "with no valid hosts, returns an empty config", |
| 26 | + cfg: &Config{}, |
| 27 | + ncfg: &config.Config{}, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "with a single URL and no service discovery", |
| 31 | + cfg: &Config{ |
| 32 | + AlertmanagerURL: []string{"http://alertmanager.default.svc.cluster.local/alertmanager"}, |
| 33 | + }, |
| 34 | + ncfg: &config.Config{ |
| 35 | + AlertingConfig: config.AlertingConfig{ |
| 36 | + AlertmanagerConfigs: []*config.AlertmanagerConfig{ |
| 37 | + { |
| 38 | + APIVersion: "v1", |
| 39 | + Scheme: "http", |
| 40 | + PathPrefix: "/alertmanager", |
| 41 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{StaticConfigs: []*targetgroup.Group{{ |
| 42 | + Targets: []model.LabelSet{{"__address__": "alertmanager.default.svc.cluster.local"}}, |
| 43 | + }}}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "with a single URL and service discovery", |
| 51 | + cfg: &Config{ |
| 52 | + AlertmanagerURL: []string{"http://_http._tcp.alertmanager.default.svc.cluster.local/alertmanager"}, |
| 53 | + AlertmanagerDiscovery: true, |
| 54 | + AlertmanagerRefreshInterval: time.Duration(60), |
| 55 | + }, |
| 56 | + ncfg: &config.Config{ |
| 57 | + AlertingConfig: config.AlertingConfig{ |
| 58 | + AlertmanagerConfigs: []*config.AlertmanagerConfig{ |
| 59 | + { |
| 60 | + APIVersion: "v1", |
| 61 | + Scheme: "http", |
| 62 | + PathPrefix: "/alertmanager", |
| 63 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{DNSSDConfigs: []*dns.SDConfig{{ |
| 64 | + Names: []string{"_http._tcp.alertmanager.default.svc.cluster.local"}, |
| 65 | + RefreshInterval: 60, |
| 66 | + Type: "SRV", |
| 67 | + Port: 0, |
| 68 | + }}}, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "with service discovery and an invalid URL", |
| 76 | + cfg: &Config{ |
| 77 | + AlertmanagerURL: []string{"http://_http.default.svc.cluster.local/alertmanager"}, |
| 78 | + AlertmanagerDiscovery: true, |
| 79 | + }, |
| 80 | + 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\")"), |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "with multiple URLs and no service discovery", |
| 84 | + cfg: &Config{ |
| 85 | + AlertmanagerURL: []string{ |
| 86 | + "http://alertmanager-0.default.svc.cluster.local/alertmanager", |
| 87 | + "http://alertmanager-1.default.svc.cluster.local/alertmanager", |
| 88 | + }, |
| 89 | + }, |
| 90 | + ncfg: &config.Config{ |
| 91 | + AlertingConfig: config.AlertingConfig{ |
| 92 | + AlertmanagerConfigs: []*config.AlertmanagerConfig{ |
| 93 | + { |
| 94 | + APIVersion: "v1", |
| 95 | + Scheme: "http", |
| 96 | + PathPrefix: "/alertmanager", |
| 97 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{StaticConfigs: []*targetgroup.Group{{ |
| 98 | + Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, |
| 99 | + }}}, |
| 100 | + }, |
| 101 | + { |
| 102 | + APIVersion: "v1", |
| 103 | + Scheme: "http", |
| 104 | + PathPrefix: "/alertmanager", |
| 105 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{StaticConfigs: []*targetgroup.Group{{ |
| 106 | + Targets: []model.LabelSet{{"__address__": "alertmanager-1.default.svc.cluster.local"}}, |
| 107 | + }}}, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "with multiple URLs and service discovery", |
| 115 | + cfg: &Config{ |
| 116 | + AlertmanagerURL: []string{ |
| 117 | + "http://_http._tcp.alertmanager-0.default.svc.cluster.local/alertmanager", |
| 118 | + "http://_http._tcp.alertmanager-1.default.svc.cluster.local/alertmanager", |
| 119 | + }, |
| 120 | + AlertmanagerDiscovery: true, |
| 121 | + AlertmanagerRefreshInterval: time.Duration(60), |
| 122 | + }, |
| 123 | + ncfg: &config.Config{ |
| 124 | + AlertingConfig: config.AlertingConfig{ |
| 125 | + AlertmanagerConfigs: []*config.AlertmanagerConfig{ |
| 126 | + { |
| 127 | + APIVersion: "v1", |
| 128 | + Scheme: "http", |
| 129 | + PathPrefix: "/alertmanager", |
| 130 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{DNSSDConfigs: []*dns.SDConfig{{ |
| 131 | + Names: []string{"_http._tcp.alertmanager-0.default.svc.cluster.local"}, |
| 132 | + RefreshInterval: 60, |
| 133 | + Type: "SRV", |
| 134 | + Port: 0, |
| 135 | + }}}, |
| 136 | + }, |
| 137 | + { |
| 138 | + APIVersion: "v1", |
| 139 | + Scheme: "http", |
| 140 | + PathPrefix: "/alertmanager", |
| 141 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{DNSSDConfigs: []*dns.SDConfig{{ |
| 142 | + Names: []string{"_http._tcp.alertmanager-1.default.svc.cluster.local"}, |
| 143 | + RefreshInterval: 60, |
| 144 | + Type: "SRV", |
| 145 | + Port: 0, |
| 146 | + }}}, |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "with Basic Authentication", |
| 154 | + cfg: &Config{ |
| 155 | + AlertmanagerURL: []string{ |
| 156 | + "http://marco:[email protected]/alertmanager", |
| 157 | + }, |
| 158 | + }, |
| 159 | + ncfg: &config.Config{ |
| 160 | + AlertingConfig: config.AlertingConfig{ |
| 161 | + AlertmanagerConfigs: []*config.AlertmanagerConfig{ |
| 162 | + { |
| 163 | + HTTPClientConfig: config_util.HTTPClientConfig{ |
| 164 | + BasicAuth: &config_util.BasicAuth{Username: "marco", Password: "hunter2"}, |
| 165 | + }, |
| 166 | + APIVersion: "v1", |
| 167 | + Scheme: "http", |
| 168 | + PathPrefix: "/alertmanager", |
| 169 | + ServiceDiscoveryConfig: sd_config.ServiceDiscoveryConfig{StaticConfigs: []*targetgroup.Group{{ |
| 170 | + Targets: []model.LabelSet{{"__address__": "alertmanager-0.default.svc.cluster.local"}}, |
| 171 | + }}}, |
| 172 | + }, |
| 173 | + }, |
| 174 | + }, |
| 175 | + }, |
| 176 | + }, |
| 177 | + } |
| 178 | + |
| 179 | + for _, tt := range tests { |
| 180 | + t.Run(tt.name, func(t *testing.T) { |
| 181 | + ncfg, err := buildNotifierConfig(tt.cfg) |
| 182 | + if tt.err == nil { |
| 183 | + require.NoError(t, err) |
| 184 | + require.Equal(t, tt.ncfg, ncfg) |
| 185 | + } else { |
| 186 | + require.Error(t, tt.err, err) |
| 187 | + } |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
0 commit comments