forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-dashboard_test.go
421 lines (386 loc) · 11.8 KB
/
rest-dashboard_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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
package sdk_test
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"strconv"
"strings"
"testing"
"github.com/grafana-tools/sdk"
"github.com/stretchr/testify/assert"
)
func TestClient_SearchDashboards(t *testing.T) {
type in struct {
Query string
Starred bool
Tags []string
}
type testCase struct {
In in
Out url.Values
}
dashboard := string(sdk.SearchTypeDashboard)
for i, tc := range []testCase{
{
In: in{},
Out: url.Values{"type": []string{dashboard}, "starred": []string{"false"}},
},
{
In: in{Starred: true},
Out: url.Values{"type": []string{dashboard}, "starred": []string{"true"}},
},
{
In: in{Query: "Foo"},
Out: url.Values{"type": []string{dashboard}, "query": []string{"Foo"}, "starred": []string{"false"}},
},
{
In: in{Tags: []string{"Foo", "Bar"}},
Out: url.Values{"type": []string{dashboard}, "starred": []string{"false"}, "tag": []string{"Foo", "Bar"}},
},
} {
ts := httptest.NewServer(http.HandlerFunc(testSearchQuery(t, i, tc.Out)))
client, _ := sdk.NewClient(ts.URL, "", ts.Client())
ctx := context.Background()
_, err := client.SearchDashboards(ctx, tc.In.Query, tc.In.Starred, tc.In.Tags...)
ts.Close()
if err != nil {
t.Fatalf("SearchDashboards test %d failed: %s", i, err)
}
}
}
func TestClient_Search(t *testing.T) {
type testCase struct {
In []sdk.SearchParam
Out url.Values
}
for i, tc := range []testCase{
{
In: []sdk.SearchParam{},
Out: url.Values{},
},
{
// Test all options given their correct usage.
In: []sdk.SearchParam{
sdk.SearchDashboardID(234),
sdk.SearchDashboardID(432),
sdk.SearchFolderID(123),
sdk.SearchFolderID(321),
sdk.SearchLimit(10),
sdk.SearchPage(99),
sdk.SearchQuery("Q"),
sdk.SearchStarred(true),
sdk.SearchTag("Foo"),
sdk.SearchTag("Bar"),
sdk.SearchType(sdk.SearchTypeFolder),
},
Out: url.Values{
"dashboardIds": []string{"234", "432"},
"folderIds": []string{"123", "321"},
"limit": []string{"10"},
"page": []string{"99"},
"query": []string{"Q"},
"starred": []string{"true"},
"tag": []string{"Foo", "Bar"},
"type": []string{string(sdk.SearchTypeFolder)},
},
},
{
// Test non-repeatable options.
In: []sdk.SearchParam{
sdk.SearchLimit(10),
sdk.SearchLimit(100),
sdk.SearchPage(88),
sdk.SearchPage(99),
sdk.SearchQuery("Q1"),
sdk.SearchQuery("Q2"),
sdk.SearchStarred(true),
sdk.SearchStarred(false),
sdk.SearchType(sdk.SearchTypeFolder),
sdk.SearchType(sdk.SearchTypeDashboard),
},
Out: url.Values{
"limit": []string{"100"},
"page": []string{"99"},
"query": []string{"Q2"},
"starred": []string{"false"},
"type": []string{string(sdk.SearchTypeDashboard)},
},
},
} {
ts := httptest.NewServer(http.HandlerFunc(testSearchQuery(t, i, tc.Out)))
client, _ := sdk.NewClient(ts.URL, "", ts.Client())
ctx := context.Background()
_, err := client.Search(ctx, tc.In...)
ts.Close()
if err != nil {
t.Fatalf("SearchDashboards test %d failed: %s", i, err)
}
}
}
func testSearchQuery(t *testing.T, testID int, exp url.Values) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if m := r.Method; m != http.MethodGet {
t.Fatalf("unexpected http method for case %d: expected %s, got %s", testID, http.MethodGet, m)
}
if e := "/api/search"; r.URL.Path != e {
t.Fatalf("unexpected http handler called for case %d: expected %s, got %s", testID, r.URL.Path, e)
}
qv, err := url.ParseQuery(r.URL.RawQuery)
if err != nil {
t.Fatalf("failed to parse query for case %d: %s, got err %s", testID, r.URL.RawQuery, err)
}
if !reflect.DeepEqual(exp, qv) {
t.Fatalf("unexpected query arguments for case %d: expected %v, got %v", testID, exp, qv)
}
if _, err := w.Write([]byte("[]")); err != nil {
t.Fatalf("failed to write http answer for case %d: %s", testID, err)
}
}
}
func TestSearchQuery(t *testing.T) {
testStringSearchParam(t, sdk.SearchQuery, "query", []string{"foo", "bar"})
}
func TestSearchTag(t *testing.T) {
testRepeatableStringSearchParam(t, sdk.SearchTag, "tag", []string{"foo", "bar"})
}
func TestSearchDashboardID(t *testing.T) {
testRepeatableIntSearchParam(t, sdk.SearchDashboardID, "dashboardIds", []int{100, 200})
}
func TestSearchFolderID(t *testing.T) {
testRepeatableIntSearchParam(t, sdk.SearchFolderID, "folderIds", []int{100, 200})
}
func TestSearchPage(t *testing.T) {
testNonZeroUIntSearchParam(t, sdk.SearchPage, "page", []uint{100, 200})
}
func TestSearchLimit(t *testing.T) {
testNonZeroUIntSearchParam(t, sdk.SearchLimit, "limit", []uint{100, 200})
}
func TestSearchStarred(t *testing.T) {
testBoolSearchParam(t, sdk.SearchStarred, "starred", []bool{true, false})
}
func TestSearchType(t *testing.T) {
var (
sp = sdk.SearchType
key = "type"
testValues = []sdk.SearchParamType{sdk.SearchTypeFolder, sdk.SearchTypeDashboard}
urlValues url.Values
)
request, _ := http.NewRequest("GET", "", nil)
for _, testValue := range testValues {
sp(testValue)(request)
urlValues = request.URL.Query()
expectedLen := 1
gotLen := len(urlValues[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := urlValues.Get(key)
if value != string(testValue) {
t.Errorf("expected value of %s to be %s, but was %s", key, testValue, value)
}
}
}
func testRepeatableStringSearchParam(t *testing.T, sp func(string) sdk.SearchParam, key string, testValues []string) {
request, _ := http.NewRequest("GET", "", nil)
for i, testValue := range testValues {
sp(testValue)(request)
urlValues := request.URL.Query()
expectedLen := i + 1
gotLen := len(urlValues[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
last := urlValues[key][i]
if last != testValue {
t.Errorf("expected last %s to be %s, but was %s", key, testValue, last)
}
}
}
func testStringSearchParam(t *testing.T, sp func(string) sdk.SearchParam, key string, testValues []string) {
request, _ := http.NewRequest("GET", "", nil)
for _, testValue := range testValues {
sp(testValue)(request)
urlValues := request.URL.Query()
expectedLen := 1
gotLen := len(urlValues[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := urlValues.Get(key)
if value != testValue {
t.Errorf("expected value of %s to be %s, but was %s", key, testValue, value)
}
}
}
func testBoolSearchParam(t *testing.T, sp func(bool) sdk.SearchParam, key string, testValues []bool) {
request, _ := http.NewRequest("GET", "", nil)
for _, testValue := range testValues {
sp(testValue)(request)
urlValues := request.URL.Query()
expectedLen := 1
gotLen := len(urlValues[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := urlValues.Get(key)
if value != strconv.FormatBool(testValue) {
t.Errorf("expected value of %s to be %t, but was %s", key, testValue, value)
}
}
}
func testRepeatableIntSearchParam(t *testing.T, sp func(int) sdk.SearchParam, key string, testValues []int) {
request, _ := http.NewRequest("GET", "", nil)
for i, testValue := range testValues {
sp(testValue)(request)
urlValues := request.URL.Query()
expectedLen := i + 1
gotLen := len(urlValues[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
last := urlValues[key][i]
if last != strconv.Itoa(testValue) {
t.Errorf("expected last %s to be %d, but was %s", key, testValue, last)
}
}
}
func testNonZeroUIntSearchParam(t *testing.T, sp func(uint) sdk.SearchParam, key string, testValues []uint) {
request, _ := http.NewRequest("GET", "", nil)
sp(0)(request)
urlValues := request.URL.Query()
value := urlValues.Get(key)
if value != "" {
t.Errorf("expected value of %s to be unset, but was %s", key, value)
}
testUIntSearchParam(t, sp, key, testValues)
}
func testUIntSearchParam(t *testing.T, sp func(uint) sdk.SearchParam, key string, testValues []uint) {
request, _ := http.NewRequest("GET", "", nil)
for _, testValue := range testValues {
sp(testValue)(request)
urlValues := request.URL.Query()
expectedLen := 1
gotLen := len(urlValues[key])
if gotLen != expectedLen {
t.Errorf("expected length of %s to be %d, but was %d", key, expectedLen, gotLen)
}
value := urlValues.Get(key)
if value != strconv.FormatUint(uint64(testValue), 10) {
t.Errorf("expected value of %s to be %d, but was %s", key, testValue, value)
}
}
}
func Test_DecodeRawBoardPreserveId(t *testing.T) {
r := getRawBoardRequest(t, true)
//Serialize object
data, err := json.Marshal(r)
assert.Nil(t, err)
//Deserialize data
var rawData map[string]interface{} = make(map[string]interface{})
assert.Nil(t, json.Unmarshal(data, &rawData))
folderId := rawData["FolderID"]
assert.Equal(t, folderId.(float64), float64(27))
assert.Equal(t, rawData["Overwrite"].(bool), true)
rawData = rawData["dashboard"].(map[string]interface{})
assert.Equal(t, rawData["id"].(float64), float64(25))
assert.Equal(t, rawData["title"].(string), "woot")
}
func Test_DecodeRawBoardWipeId(t *testing.T) {
r := getRawBoardRequest(t, false)
//Serialize object
data, err := json.Marshal(r)
assert.Nil(t, err)
//Deserialize data
var rawData map[string]interface{} = make(map[string]interface{})
err = json.Unmarshal(data, &rawData)
assert.Nil(t, err)
folderId := rawData["FolderID"]
assert.Equal(t, folderId.(float64), float64(27))
assert.Equal(t, rawData["Overwrite"].(bool), true)
rawData = rawData["dashboard"].(map[string]interface{})
assert.Equal(t, rawData["id"].(float64), float64(0))
assert.Equal(t, rawData["title"].(string), "woot")
}
func getRawBoardRequest(t *testing.T, preserveId bool) sdk.RawBoardRequest {
r := sdk.RawBoardRequest{}
board := sdk.Board{}
board.ID = 25
board.Title = "woot"
rawBoard, err := json.Marshal(board)
assert.Nil(t, err)
params := sdk.SetDashboardParams{
FolderID: 27,
Overwrite: true,
PreserveId: preserveId,
}
r.Dashboard = rawBoard
r.Parameters = params
return r
}
// TestCustomPanelSerialization serializes and de-serializes a sample dashboard
// object a few times and prints out final serialized JSON data to highlight
// that CustomPanel was nested in a strange way.
func TestCustomPanelSerialization(t *testing.T) {
board := &sdk.Board{
ID: 0,
UID: "",
Slug: "",
Title: "",
OriginalTitle: "",
Tags: nil,
Style: "",
Timezone: "",
Editable: false,
HideControls: false,
SharedCrosshair: false,
Panels: []*sdk.Panel{
{
CommonPanel: sdk.CommonPanel{},
GraphPanel: nil,
TablePanel: nil,
TextPanel: nil,
SinglestatPanel: nil,
DashlistPanel: nil,
PluginlistPanel: nil,
RowPanel: nil,
AlertlistPanel: nil,
CustomPanel: &sdk.CustomPanel{
"key": "value",
},
},
},
Rows: nil,
Templating: sdk.Templating{},
Annotations: struct {
List []sdk.Annotation `json:"list"`
}{},
Refresh: nil,
SchemaVersion: 0,
Version: 0,
Links: nil,
Time: sdk.Time{},
Timepicker: sdk.Timepicker{},
GraphTooltip: 0,
}
jb, err := json.Marshal(board)
if err != nil {
t.Fatal(err)
}
for i := 0; i < 10; i++ {
newBoard := &sdk.Board{}
if err := json.Unmarshal(jb, newBoard); err != nil {
t.Fatal(err)
}
jb, err = json.MarshalIndent(newBoard, "", " ")
if err != nil {
t.Fatal(err)
}
}
cnt := strings.Count(string(jb), "CustomPanel")
if cnt != 0 {
t.Fatalf("expected to not have any CustomPanel keys, got: %v", cnt)
}
}