-
Notifications
You must be signed in to change notification settings - Fork 25
/
alerts_test.go
173 lines (157 loc) · 4.11 KB
/
alerts_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package alerts
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
"go.sia.tech/core/types"
"go.sia.tech/renterd/webhooks"
"go.uber.org/zap"
)
type testWebhookStore struct {
mu sync.Mutex
added int
deleted int
listed int
}
func (s *testWebhookStore) DeleteWebhook(_ context.Context, wb webhooks.Webhook) error {
s.mu.Lock()
defer s.mu.Unlock()
s.deleted++
return nil
}
func (s *testWebhookStore) AddWebhook(_ context.Context, wb webhooks.Webhook) error {
s.mu.Lock()
defer s.mu.Unlock()
s.added++
return nil
}
func (s *testWebhookStore) Webhooks(_ context.Context) ([]webhooks.Webhook, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.listed++
return nil, nil
}
var _ webhooks.WebhookStore = (*testWebhookStore)(nil)
func TestWebhooks(t *testing.T) {
store := &testWebhookStore{}
mgr, err := webhooks.NewManager(store, zap.NewNop())
if err != nil {
t.Fatal(err)
}
alerts := NewManager()
alerts.RegisterWebhookBroadcaster(mgr)
mux := http.NewServeMux()
var events []webhooks.Event
var mu sync.Mutex
mux.HandleFunc("/event", func(w http.ResponseWriter, r *http.Request) {
var event webhooks.Event
if err := json.NewDecoder(r.Body).Decode(&event); err != nil {
t.Fatal(err)
}
mu.Lock()
events = append(events, event)
mu.Unlock()
})
srv := httptest.NewServer(mux)
defer srv.Close()
// register a hook
wh := webhooks.Webhook{
Module: webhookModule,
URL: fmt.Sprintf("http://%v/event", srv.Listener.Addr().String()),
}
if hookID := wh.String(); hookID != fmt.Sprintf("%v.%v.%v", wh.URL, wh.Module, "") {
t.Fatalf("wrong result for wh.String(): %v != %v", wh.String(), hookID)
}
err = mgr.Register(context.Background(), wh)
if err != nil {
t.Fatal(err)
}
// perform some actions that should trigger the endpoint
a := Alert{
ID: types.Hash256{1},
Message: "test",
Severity: SeverityWarning,
Timestamp: time.Unix(0, 0),
Data: map[string]interface{}{
"origin": "foo",
},
}
if err := alerts.RegisterAlert(context.Background(), a); err != nil {
t.Fatal(err)
}
if err := alerts.DismissAlerts(context.Background(), types.Hash256{1}); err != nil {
t.Fatal(err)
}
// list hooks
hooks, _ := mgr.Info()
if len(hooks) != 1 {
t.Fatal("wrong number of hooks")
} else if hooks[0].URL != wh.URL {
t.Fatal("wrong hook id")
} else if hooks[0].Event != wh.Event {
t.Fatal("wrong event", hooks[0].Event)
} else if hooks[0].Module != wh.Module {
t.Fatal("wrong module", hooks[0].Module)
}
// unregister hook
if err := mgr.Delete(context.Background(), webhooks.Webhook{
Event: hooks[0].Event,
Module: hooks[0].Module,
URL: hooks[0].URL,
}); err != nil {
t.Fatal("hook not deleted", err)
}
// perform an action that should not trigger the endpoint
if err := alerts.RegisterAlert(context.Background(), Alert{
ID: types.Hash256{2},
Message: "test",
Severity: SeverityWarning,
Timestamp: time.Now(),
Data: map[string]interface{}{
"origin": "foo",
},
}); err != nil {
t.Fatal(err)
}
// check events
for i := 0; i < 10; i++ {
mu.Lock()
nEvents := len(events)
mu.Unlock()
if nEvents != 3 {
time.Sleep(100 * time.Millisecond)
continue
}
}
mu.Lock()
defer mu.Unlock()
if len(events) != 3 {
t.Fatal("wrong number of events", len(events))
}
assertEvent := func(event webhooks.Event, module, id string, hasPayload bool) {
t.Helper()
if event.Module != module {
t.Fatal("wrong event module", event.Module, module)
} else if event.Event != id {
t.Fatal("wrong event id", event.Event, id)
} else if hasPayload && event.Payload == nil {
t.Fatal("missing payload")
}
}
assertEvent(events[0], "", webhooks.WebhookEventPing, false)
assertEvent(events[1], webhookModule, webhookEventRegister, true)
assertEvent(events[2], webhookModule, webhookEventDismiss, true)
// check store
if store.added != 1 {
t.Fatalf("wrong number of hooks added: %v != 1", store.added)
} else if store.deleted != 1 {
t.Fatalf("wrong number of hooks deleted: %v != 1", store.deleted)
} else if store.listed != 1 {
t.Fatalf("wrong number of hooks listed: %v != 1", store.listed)
}
}