-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathgraph_annotation_test.go
328 lines (275 loc) · 10 KB
/
graph_annotation_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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package mackerel
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestCreateGraphAnnotation(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-annotations" {
t.Error("request URL should be /api/v0/graph-annotations but: ", req.URL.Path)
}
if req.Method != "POST" {
t.Error("request method should be POST but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var data struct {
Service string `json:"service,omitempty"`
Roles []string `json:"roles,omitempty"`
From int64 `json:"from,omitempty"`
To int64 `json:"to,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
}
err := json.Unmarshal(body, &data)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}
if data.Service != "My-Blog" {
t.Errorf("request sends json including Service but: %s", data.Service)
}
if !reflect.DeepEqual(data.Roles, []string{"Role1", "Role2"}) {
t.Error("request sends json including Roles but: ", data.Roles)
}
if data.From != 1485675275 {
t.Errorf("request sends json including From but: %d", data.From)
}
if data.To != 1485675299 {
t.Errorf("request sends json including To but: %d", data.To)
}
if data.Title != "Deployed" {
t.Errorf("request sends json including Title but: %s", data.Title)
}
if data.Description != "Deployed my blog" {
t.Errorf("request sends json including Description but: %s", data.Description)
}
respJSON, _ := json.Marshal(map[string]interface{}{
"service": "My-Blog",
"roles": []string{"Role1", "Role2"},
"from": 1485675275,
"to": 1485675299,
"title": "Deployed",
"description": "Deployed my blog",
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
annotation, err := client.CreateGraphAnnotation(&GraphAnnotation{
Service: "My-Blog",
Roles: []string{"Role1", "Role2"},
From: 1485675275,
To: 1485675299,
Title: "Deployed",
Description: "Deployed my blog",
})
if err != nil {
t.Error("err should be nil but: ", err)
}
if annotation.Service != "My-Blog" {
t.Error("request sends json including Service but: ", annotation.Service)
}
if !reflect.DeepEqual(annotation.Roles, []string{"Role1", "Role2"}) {
t.Error("request sends json including Roles but: ", annotation.Roles)
}
if annotation.From != 1485675275 {
t.Error("request sends json including From but: ", annotation.From)
}
if annotation.To != 1485675299 {
t.Error("request sends json including To but: ", annotation.To)
}
if annotation.Title != "Deployed" {
t.Error("request sends json including Title but: ", annotation.Title)
}
if annotation.Description != "Deployed my blog" {
t.Error("request sends json including Description but: ", annotation.Description)
}
}
func TestFindGraphAnnotations(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-annotations" {
t.Error("request URL should be /api/v0/graph-annotations but: ", req.URL.Path)
}
if req.Method != "GET" {
t.Error("request method should be GET but: ", req.Method)
}
query := req.URL.Query()
if query.Get("service") != "My-Blog" {
t.Error("request query 'service' param should be My-Blog but: ", query.Get("service"))
}
if query.Get("from") != "1485675275" {
t.Error("request query 'from' param should be 1485675275 but: ", query.Get("from"))
}
if query.Get("to") != "1485675299" {
t.Error("request query 'from' param should be 1485675299 but: ", query.Get("from"))
}
respJSON, _ := json.Marshal(map[string][]map[string]interface{}{
"graphAnnotations": {
{
"service": "My-Blog",
"roles": []string{"Role1", "Role2"},
"from": 1485675275,
"to": 1485675299,
"title": "Deployed",
"description": "Deployed my blog",
},
},
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
annotations, err := client.FindGraphAnnotations("My-Blog", 1485675275, 1485675299)
if err != nil {
t.Error("err should be nil but: ", err)
}
if annotations[0].Service != "My-Blog" {
t.Error("request sends json including Service but: ", annotations[0].Service)
}
if !reflect.DeepEqual(annotations[0].Roles, []string{"Role1", "Role2"}) {
t.Error("request sends json including Roles but: ", annotations[0].Roles)
}
if annotations[0].From != 1485675275 {
t.Error("request sends json including From but: ", annotations[0].From)
}
if annotations[0].To != 1485675299 {
t.Error("request sends json including To but: ", annotations[0].To)
}
if annotations[0].Title != "Deployed" {
t.Error("request sends json including Title but: ", annotations[0].Title)
}
if annotations[0].Description != "Deployed my blog" {
t.Error("request sends json including Description but: ", annotations[0].Description)
}
}
func TestUpdateGraphAnnotations(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-annotations/123456789" {
t.Error("request URL should be /api/v0/graph-annotations/123456789 but: ", req.URL.Path)
}
if req.Method != "PUT" {
t.Error("request method should be PUT but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string]interface{}{
"service": "My-Blog",
"roles": []string{"Role1", "Role2"},
"from": 1485675275,
"to": 1485675299,
"title": "Deployed",
"description": "Deployed my blog",
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
annotation, err := client.UpdateGraphAnnotation("123456789",
&GraphAnnotation{
Service: "My-Blog",
Roles: []string{"Role1", "Role2"},
From: 1485675275,
To: 1485675299,
Title: "Deployed",
Description: "Deployed my blog",
})
if err != nil {
t.Error("err should be nil but: ", err)
}
if annotation.Service != "My-Blog" {
t.Error("request sends json including Service but: ", annotation.Service)
}
if !reflect.DeepEqual(annotation.Roles, []string{"Role1", "Role2"}) {
t.Error("request sends json including Roles but: ", annotation.Roles)
}
if annotation.From != 1485675275 {
t.Error("request sends json including From but: ", annotation.From)
}
if annotation.To != 1485675299 {
t.Error("request sends json including To but: ", annotation.To)
}
if annotation.Title != "Deployed" {
t.Error("request sends json including Title but: ", annotation.Title)
}
if annotation.Description != "Deployed my blog" {
t.Error("request sends json including Description but: ", annotation.Description)
}
}
func TestDeleteGraphAnnotations(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-annotations/123456789" {
t.Error("request URL should be /api/v0/graph-annotations/123456789 but: ", req.URL.Path)
}
if req.Method != "DELETE" {
t.Error("request method should be DELETE but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string]interface{}{
"service": "My-Blog",
"roles": []string{"Role1", "Role2"},
"from": 1485675275,
"to": 1485675299,
"title": "Deployed",
"description": "Deployed my blog",
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
annotation, err := client.DeleteGraphAnnotation("123456789")
if err != nil {
t.Error("err should be nil but: ", err)
}
if annotation.Service != "My-Blog" {
t.Error("request sends json including Service but: ", annotation.Service)
}
if !reflect.DeepEqual(annotation.Roles, []string{"Role1", "Role2"}) {
t.Error("request sends json including Roles but: ", annotation.Roles)
}
if annotation.From != 1485675275 {
t.Error("request sends json including From but: ", annotation.From)
}
if annotation.To != 1485675299 {
t.Error("request sends json including To but: ", annotation.To)
}
if annotation.Title != "Deployed" {
t.Error("request sends json including Title but: ", annotation.Title)
}
if annotation.Description != "Deployed my blog" {
t.Error("request sends json including Description but: ", annotation.Description)
}
}
func TestDeleteGraphAnnotations_NotFound(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-annotations/123456789" {
t.Error("request URL should be /api/v0/graph-annotations/123456789 but: ", req.URL.Path)
}
if req.Method != "DELETE" {
t.Error("request method should be DELETE but: ", req.Method)
}
respJSON, _ := json.Marshal(map[string]map[string]string{
"error": {"message": "Graph annotation not found"},
})
res.Header()["Content-Type"] = []string{"application/json"}
res.WriteHeader(http.StatusNotFound)
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
_, err := client.DeleteGraphAnnotation("123456789")
if err == nil {
t.Error("err should not be nil but: ", err)
}
apiErr := err.(*APIError)
if expected := 404; apiErr.StatusCode != expected {
t.Errorf("api error StatusCode should be %d but got: %d", expected, apiErr.StatusCode)
}
if expected := "API request failed: Graph annotation not found"; apiErr.Error() != expected {
t.Errorf("api error string should be %s but got: %s", expected, apiErr.Error())
}
}