|
5 | 5 | "context"
|
6 | 6 | "fmt"
|
7 | 7 | "io/ioutil"
|
| 8 | + "net/http" |
8 | 9 | "net/http/httptest"
|
9 | 10 | "os"
|
10 | 11 | "testing"
|
@@ -48,7 +49,8 @@ func (m *mockAlertStore) GetAlertConfig(ctx context.Context, user string) (alert
|
48 | 49 | }
|
49 | 50 |
|
50 | 51 | func (m *mockAlertStore) SetAlertConfig(ctx context.Context, cfg alerts.AlertConfigDesc) error {
|
51 |
| - return fmt.Errorf("not implemented") |
| 52 | + m.configs[cfg.User] = cfg |
| 53 | + return nil |
52 | 54 | }
|
53 | 55 |
|
54 | 56 | func (m *mockAlertStore) DeleteAlertConfig(ctx context.Context, user string) error {
|
@@ -241,3 +243,69 @@ func TestAlertmanager_ServeHTTP(t *testing.T) {
|
241 | 243 | body, _ = ioutil.ReadAll(resp.Body)
|
242 | 244 | require.Equal(t, "the Alertmanager is not configured\n", string(body))
|
243 | 245 | }
|
| 246 | + |
| 247 | +func TestAlertmanager_ServeHTTPWithFallbackConfig(t *testing.T) { |
| 248 | + mockStore := &mockAlertStore{ |
| 249 | + configs: map[string]alerts.AlertConfigDesc{}, |
| 250 | + } |
| 251 | + |
| 252 | + externalURL := flagext.URLValue{} |
| 253 | + err := externalURL.Set("http://localhost:8080/alertmanager") |
| 254 | + require.NoError(t, err) |
| 255 | + |
| 256 | + tempDir, err := ioutil.TempDir(os.TempDir(), "alertmanager") |
| 257 | + require.NoError(t, err) |
| 258 | + defer os.RemoveAll(tempDir) |
| 259 | + |
| 260 | + fallbackCfg := ` |
| 261 | +global: |
| 262 | + smtp_smarthost: 'localhost:25' |
| 263 | + |
| 264 | +route: |
| 265 | + receiver: example-email |
| 266 | +receivers: |
| 267 | + - name: example-email |
| 268 | + email_configs: |
| 269 | + |
| 270 | +` |
| 271 | + |
| 272 | + // Create the Multitenant Alertmanager. |
| 273 | + reg := prometheus.NewPedanticRegistry() |
| 274 | + am := createMultitenantAlertmanager(&MultitenantAlertmanagerConfig{ |
| 275 | + ExternalURL: externalURL, |
| 276 | + DataDir: tempDir, |
| 277 | + }, nil, nil, mockStore, log.NewNopLogger(), reg) |
| 278 | + am.fallbackConfig = fallbackCfg |
| 279 | + |
| 280 | + // Request when no user configuration is present. |
| 281 | + req := httptest.NewRequest("GET", externalURL.String()+"/api/v1/status", nil) |
| 282 | + req.Header.Add(user.OrgIDHeaderName, "user1") |
| 283 | + w := httptest.NewRecorder() |
| 284 | + |
| 285 | + am.ServeHTTP(w, req) |
| 286 | + |
| 287 | + resp := w.Result() |
| 288 | + |
| 289 | + // It succeeds and the Alertmanager is started |
| 290 | + require.Equal(t, http.StatusOK, resp.StatusCode) |
| 291 | + require.Len(t, am.alertmanagers, 1) |
| 292 | + require.True(t, am.alertmanagers["user1"].IsActive()) |
| 293 | + |
| 294 | + // Even after a poll it does not pause your Alertmanager |
| 295 | + err = am.updateConfigs() |
| 296 | + require.NoError(t, err) |
| 297 | + |
| 298 | + require.True(t, am.alertmanagers["user1"].IsActive()) |
| 299 | + require.Len(t, am.alertmanagers, 1) |
| 300 | + |
| 301 | + // Pause the alertmanager |
| 302 | + am.alertmanagers["user1"].Pause() |
| 303 | + |
| 304 | + // Request when user configuration is paused. |
| 305 | + w = httptest.NewRecorder() |
| 306 | + am.ServeHTTP(w, req) |
| 307 | + |
| 308 | + resp = w.Result() |
| 309 | + body, _ := ioutil.ReadAll(resp.Body) |
| 310 | + require.Equal(t, "the Alertmanager is not configured\n", string(body)) |
| 311 | +} |
0 commit comments