-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathalert_group_settings_test.go
279 lines (235 loc) · 8.33 KB
/
alert_group_settings_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package mackerel
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"github.com/kylelemons/godebug/pretty"
)
func TestFindAlertGroupSettings(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/alert-group-settings" {
t.Error("request URL should be /api/v0/alert-group-settings but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string][]map[string]interface{}{
"alertGroupSettings": {
{
"id": "xxxxxxxxxxx",
"name": "alert group setting #1",
},
{
"id": "yyyyyyyyyyy",
"name": "alert group setting #2",
"memo": "lorem ipsum...",
"serviceScopes": []string{"my-service"},
"roleScopes": []string{"my-service: db"},
"monitorScopes": []string{"connectivity"},
"notificationInterval": 60,
},
},
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
got, err := client.FindAlertGroupSettings()
if err != nil {
t.Error("err should be nil but: ", err)
}
want := []*AlertGroupSetting{
{
ID: "xxxxxxxxxxx",
Name: "alert group setting #1",
},
{
ID: "yyyyyyyyyyy",
Name: "alert group setting #2",
Memo: "lorem ipsum...",
ServiceScopes: []string{"my-service"},
RoleScopes: []string{"my-service: db"},
MonitorScopes: []string{"connectivity"},
NotificationInterval: 60,
},
}
if diff := pretty.Compare(got, want); diff != "" {
t.Errorf("fail to get correct data: diff: (-got +want)\n%s", diff)
}
}
func TestCreateAlertGroupSetting(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/alert-group-settings" {
t.Error("request URL should be /api/v0/alert-group-settings but: ", req.URL.Path)
}
if req.Method != "POST" {
t.Error("request method should be POST but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var alertGroupSetting AlertGroupSetting
if err := json.Unmarshal(body, &alertGroupSetting); err != nil {
t.Fatal("request body should be decoded as json ", string(body))
}
respJSON, _ := json.Marshal(map[string]interface{}{
"id": "xxxxxxxxxxx",
"name": alertGroupSetting.Name,
"memo": alertGroupSetting.Memo,
"serviceScopes": alertGroupSetting.ServiceScopes,
"roleScopes": alertGroupSetting.RoleScopes,
"monitorScopes": alertGroupSetting.MonitorScopes,
"notificationInterval": alertGroupSetting.NotificationInterval,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
param := &AlertGroupSetting{
Name: "alert group setting",
Memo: "lorem ipsum...",
}
got, err := client.CreateAlertGroupSetting(param)
if err != nil {
t.Error("err should be nil but: ", err)
}
want := &AlertGroupSetting{
ID: "xxxxxxxxxxx",
Name: param.Name,
Memo: param.Memo,
}
if diff := pretty.Compare(got, want); diff != "" {
t.Errorf("fail to get correct data: diff: (-got +want)\n%s", diff)
}
}
func TestGetAlertGroupSetting(t *testing.T) {
id := "xxxxxxxxxxx"
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != fmt.Sprintf("/api/v0/alert-group-settings/%s", id) {
t.Error("request URL should be /api/v0/alert-group-settings/<ID> but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string]interface{}{
"id": "xxxxxxxxxxx",
"name": "alert group setting",
"memo": "lorem ipsum...",
"serviceScopes": []string{"my-service"},
"roleScopes": []string{"my-service: db"},
"monitorScopes": []string{"connectivity"},
"notificationInterval": 60,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
got, err := client.GetAlertGroupSetting(id)
if err != nil {
t.Error("err should be nil but: ", err)
}
want := &AlertGroupSetting{
ID: id,
Name: "alert group setting",
Memo: "lorem ipsum...",
ServiceScopes: []string{"my-service"},
RoleScopes: []string{"my-service: db"},
MonitorScopes: []string{"connectivity"},
NotificationInterval: 60,
}
if diff := pretty.Compare(got, want); diff != "" {
t.Errorf("fail to get correct data: diff (-got +want)\n%s", diff)
}
}
func TestUpdateAlertGroupSetting(t *testing.T) {
id := "xxxxxxxxxxx"
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != fmt.Sprintf("/api/v0/alert-group-settings/%s", id) {
t.Error("request URL should be /api/v0/alert-group-settings/<ID> but: ", req.URL.Path)
}
if req.Method != "PUT" {
t.Error("request method should be PUT but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var alertGroupSetting AlertGroupSetting
if err := json.Unmarshal(body, &alertGroupSetting); err != nil {
t.Fatal("request body should be decoded as json ", string(body))
}
respJSON, _ := json.Marshal(map[string]interface{}{
"id": id,
"name": alertGroupSetting.Name,
"memo": "lorem ipsum...",
"serviceScopes": []string{"my-service"},
"roleScopes": []string{"my-service: db"},
"monitorScopes": []string{"connectivity"},
"notificationInterval": 60,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
param := &AlertGroupSetting{
Name: "alert group notification updated",
}
got, err := client.UpdateAlertGroupSetting(id, param)
if err != nil {
t.Error("err should be nil but: ", err)
}
want := &AlertGroupSetting{
ID: id,
Name: param.Name,
Memo: "lorem ipsum...",
ServiceScopes: []string{"my-service"},
RoleScopes: []string{"my-service: db"},
MonitorScopes: []string{"connectivity"},
NotificationInterval: 60,
}
if diff := pretty.Compare(got, want); diff != "" {
t.Errorf("fail to get correct data: diff: (-got +want)\n%s", diff)
}
}
func TestDeleteAlertGroupSetting(t *testing.T) {
id := "xxxxxxxxxxx"
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != fmt.Sprintf("/api/v0/alert-group-settings/%s", id) {
t.Error("request URL should be /api/v0/alert-group-settings/<ID> but: ", req.URL.Path)
}
if req.Method != "DELETE" {
t.Error("request method should be DELETE but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string]interface{}{
"id": id,
"name": "alert group setting",
"memo": "lorem ipsum...",
"serviceScopes": []string{"my-service"},
"roleScopes": []string{"my-service: db"},
"monitorScopes": []string{"connectivity"},
"notificationInterval": 60,
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
got, err := client.DeleteAlertGroupSetting(id)
if err != nil {
t.Error("err should be nil but: ", err)
}
want := &AlertGroupSetting{
ID: id,
Name: "alert group setting",
Memo: "lorem ipsum...",
ServiceScopes: []string{"my-service"},
RoleScopes: []string{"my-service: db"},
MonitorScopes: []string{"connectivity"},
NotificationInterval: 60,
}
if diff := pretty.Compare(got, want); diff != "" {
t.Errorf("fail to get correct data: diff (-got +want)\n%s", diff)
}
}