|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
4 | 6 | "fmt" |
| 7 | + "github.com/function61/eventhorizon/pkg/ehevent" |
| 8 | + "github.com/function61/eventhorizon/pkg/ehreader" |
| 9 | + "github.com/function61/eventhorizon/pkg/ehreader/ehreadertest" |
5 | 10 | "github.com/function61/gokit/assert" |
| 11 | + "github.com/function61/lambda-alertmanager/pkg/amdomain" |
| 12 | + "github.com/function61/lambda-alertmanager/pkg/amstate" |
| 13 | + "os" |
6 | 14 | "testing" |
7 | 15 | "time" |
8 | 16 | ) |
9 | 17 |
|
10 | | -func TestParseTtlSpec(t *testing.T) { |
11 | | - now := time.Date(2019, 9, 7, 12, 0, 0, 0, time.UTC) |
| 18 | +var t0 = time.Date(2019, 9, 7, 12, 0, 0, 0, time.UTC) |
| 19 | + |
| 20 | +func TestCheckAndAlertForUnnoticedAlerts(t *testing.T) { |
| 21 | + ctx := context.Background() |
| 22 | + |
| 23 | + testStreamName := "/t-42/alertmanager" |
| 24 | + |
| 25 | + // have an alert raised at T+0 |
| 26 | + eventLog := ehreadertest.NewEventLog() |
| 27 | + eventLog.AppendE( |
| 28 | + testStreamName, |
| 29 | + amdomain.NewAlertRaised( |
| 30 | + "a14308bba82f", |
| 31 | + "The building is on fire", |
| 32 | + "Fire sensor in room 456 went off", |
| 33 | + ehevent.MetaSystemUser(t0))) |
| 34 | + |
| 35 | + app, err := amstate.LoadUntilRealtime( |
| 36 | + ctx, |
| 37 | + ehreader.NewTenantCtxWithSnapshots( |
| 38 | + ehreader.TenantId("42"), |
| 39 | + eventLog, |
| 40 | + ehreader.NewInMemSnapshotStore()), |
| 41 | + nil) |
| 42 | + assert.Ok(t, err) |
| 43 | + |
| 44 | + // lame hack to get ackLink() to produce URLs while in testing |
| 45 | + os.Setenv("API_ENDPOINT", "https://alertmanager.com/api") |
| 46 | + |
| 47 | + unnoticedAlertAtT0Plus := func(plus time.Duration) string { // helper |
| 48 | + var publishedAlert *amstate.Alert |
| 49 | + |
| 50 | + // capture maybe-published alert |
| 51 | + assert.Ok(t, checkAndAlertForUnnoticedAlerts(ctx, app, func(alert amstate.Alert) error { |
| 52 | + publishedAlert = &alert |
12 | 53 |
|
| 54 | + return nil |
| 55 | + }, t0.Add(plus))) |
| 56 | + |
| 57 | + publishedAlertAsJson, err := json.MarshalIndent(publishedAlert, "", " ") |
| 58 | + assert.Ok(t, err) |
| 59 | + |
| 60 | + return string(publishedAlertAsJson) |
| 61 | + } |
| 62 | + |
| 63 | + /* we want alert at 4 hour mark when alert is un-acked, then *every* hour again: |
| 64 | +
|
| 65 | + 0:00 (an alert is raised) |
| 66 | + 1:00 |
| 67 | + 2:00 |
| 68 | + 3:00 |
| 69 | + 4:00 => first "unnoticed" alert |
| 70 | + 4:30 |
| 71 | + 5:00 => second "unnoticed" alert |
| 72 | + ... |
| 73 | + */ |
| 74 | + assert.EqualString(t, unnoticedAlertAtT0Plus(0*time.Hour), "null") |
| 75 | + assert.EqualString(t, unnoticedAlertAtT0Plus(1*time.Hour), "null") |
| 76 | + assert.EqualString(t, unnoticedAlertAtT0Plus(2*time.Hour), "null") |
| 77 | + assert.EqualString(t, unnoticedAlertAtT0Plus(3*time.Hour), "null") |
| 78 | + assert.EqualString(t, unnoticedAlertAtT0Plus(4*time.Hour), `{ |
| 79 | + "alert_key": "", |
| 80 | + "subject": "Un-acked alerts", |
| 81 | + "details": "There are 1 un-acked alert(s):\n\nThe building is on fire https://alertmanager.com/api/alerts/acknowledge?id=a14308bba82f\n\nGo take care of them!", |
| 82 | + "timestamp": "2019-09-07T16:00:00Z" |
| 83 | +}`) |
| 84 | + assert.EqualString(t, unnoticedAlertAtT0Plus(4*time.Hour+30*time.Minute), "null") |
| 85 | + assert.EqualString(t, unnoticedAlertAtT0Plus(5*time.Hour), `{ |
| 86 | + "alert_key": "", |
| 87 | + "subject": "Un-acked alerts", |
| 88 | + "details": "There are 1 un-acked alert(s):\n\nThe building is on fire https://alertmanager.com/api/alerts/acknowledge?id=a14308bba82f\n\nGo take care of them!", |
| 89 | + "timestamp": "2019-09-07T17:00:00Z" |
| 90 | +}`) |
| 91 | + assert.EqualString(t, unnoticedAlertAtT0Plus(5*time.Hour+30*time.Minute), "null") |
| 92 | + assert.EqualString(t, unnoticedAlertAtT0Plus(6*time.Hour), `{ |
| 93 | + "alert_key": "", |
| 94 | + "subject": "Un-acked alerts", |
| 95 | + "details": "There are 1 un-acked alert(s):\n\nThe building is on fire https://alertmanager.com/api/alerts/acknowledge?id=a14308bba82f\n\nGo take care of them!", |
| 96 | + "timestamp": "2019-09-07T18:00:00Z" |
| 97 | +}`) |
| 98 | +} |
| 99 | + |
| 100 | +func TestParseTtlSpec(t *testing.T) { |
13 | 101 | tcs := []struct { |
14 | 102 | input string |
15 | 103 | output string |
@@ -51,7 +139,7 @@ func TestParseTtlSpec(t *testing.T) { |
51 | 139 | for _, tc := range tcs { |
52 | 140 | tc := tc // pin |
53 | 141 | t.Run(tc.input, func(t *testing.T) { |
54 | | - ttl, err := parseTtlSpec(tc.input, now) |
| 142 | + ttl, err := parseTtlSpec(tc.input, t0) |
55 | 143 | var actual string |
56 | 144 | if err != nil { |
57 | 145 | actual = fmt.Sprintf("error: %v", err) |
|
0 commit comments