-
Notifications
You must be signed in to change notification settings - Fork 0
/
panel_test.go
414 lines (355 loc) · 9.53 KB
/
panel_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
package sdk_test
/*
Copyright 2016 Alexander I.Grafov <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
ॐ तारे तुत्तारे तुरे स्व
*/
import (
"bytes"
"encoding/json"
"testing"
"github.com/grafana-tools/sdk"
)
func TestStackVal_UnmarshalJSON_GotTrue(t *testing.T) {
var sampleOut struct {
Val sdk.BoolString `json:"val"`
}
var sampleIn = []byte(`{"val":true}`)
json.Unmarshal(sampleIn, &sampleOut)
if !sampleOut.Val.Flag {
t.Errorf("should be true but got false")
}
if sampleOut.Val.Value != "" {
t.Error("string value should be empty")
}
}
func TestStackVal_UnmarshalJSON_GotFalse(t *testing.T) {
var sampleOut struct {
Val sdk.BoolString `json:"val"`
}
var sampleIn = []byte(`{"val":false}`)
json.Unmarshal(sampleIn, &sampleOut)
if sampleOut.Val.Flag {
t.Errorf("should be false but got true")
}
if sampleOut.Val.Value != "" {
t.Error("string value should be empty")
}
}
func TestStackVal_UnmarshalJSON_GotString(t *testing.T) {
var sampleOut struct {
Val sdk.BoolString `json:"val"`
}
var sampleIn = []byte(`{"val":"A"}`)
json.Unmarshal(sampleIn, &sampleOut)
if sampleOut.Val.Flag {
t.Error("should be false but got true")
}
if sampleOut.Val.Value != "A" {
t.Errorf("should be 'A' but got '%s'", sampleOut.Val.Value)
}
}
func TestStackVal_MarshalJSON_GotTrue(t *testing.T) {
var sampleInp struct {
Val sdk.BoolString `json:"val"`
}
sampleInp.Val.Flag = true
var sampleOut = []byte(`{"val":true}`)
data, _ := json.Marshal(sampleInp)
if bytes.Compare(data, sampleOut) != 0 {
t.Errorf("should be %s but got %s", sampleOut, data)
}
}
func TestStackVal_MarshalJSON_GotFalse(t *testing.T) {
var sampleInp struct {
Val sdk.BoolString `json:"val"`
}
sampleInp.Val.Flag = false
var sampleOut = []byte(`{"val":false}`)
data, _ := json.Marshal(sampleInp)
if bytes.Compare(data, sampleOut) != 0 {
t.Errorf("should be %s but got %s", sampleOut, data)
}
}
func TestStackVal_MarshalJSON_GotString(t *testing.T) {
var sampleInp struct {
Val sdk.BoolString `json:"val"`
}
sampleInp.Val.Value = "A"
var sampleOut = []byte(`{"val":"A"}`)
data, _ := json.Marshal(sampleInp)
if bytes.Compare(data, sampleOut) != 0 {
t.Errorf("should be %s but got %s", sampleOut, data)
}
}
func TestBoolInt_UnmarshalJSON_GotTrue(t *testing.T) {
var sampleOut struct {
Val sdk.BoolInt `json:"val"`
}
var sampleIn = []byte(`{"val":true}`)
json.Unmarshal(sampleIn, &sampleOut)
if !sampleOut.Val.Flag {
t.Errorf("should be true but got false")
}
if sampleOut.Val.Value != nil {
t.Error("int value should be empty")
}
}
func TestBoolInt_UnmarshalJSON_GotFalse(t *testing.T) {
var sampleOut struct {
Val sdk.BoolInt `json:"val"`
}
var sampleIn = []byte(`{"val":false}`)
json.Unmarshal(sampleIn, &sampleOut)
if sampleOut.Val.Flag {
t.Errorf("should be false but got true")
}
if sampleOut.Val.Value != nil {
t.Error("int value should be empty")
}
}
func TestBoolInt_UnmarshalJSON_GotInt(t *testing.T) {
var sampleOut struct {
Val sdk.BoolInt `json:"val"`
}
var sampleIn = []byte(`{"val":123456789}`)
json.Unmarshal(sampleIn, &sampleOut)
if sampleOut.Val.Flag {
t.Error("should be false but got true")
}
if sampleOut.Val.Value == nil {
t.Fatalf("should be 123456789 but got nil")
}
if *sampleOut.Val.Value != 123456789 {
t.Errorf("should be 123456789 but got %v", sampleOut.Val.Value)
}
}
func TestBoolInt_MarshalJSON_GotTrue(t *testing.T) {
var sampleInp struct {
Val sdk.BoolInt `json:"val"`
}
sampleInp.Val.Flag = true
var sampleOut = []byte(`{"val":true}`)
data, _ := json.Marshal(sampleInp)
if bytes.Compare(data, sampleOut) != 0 {
t.Errorf("should be %s but got %s", sampleOut, data)
}
}
func TestBoolInt_MarshalJSON_GotFalse(t *testing.T) {
var sampleInp struct {
Val sdk.BoolInt `json:"val"`
}
sampleInp.Val.Flag = false
var sampleOut = []byte(`{"val":false}`)
data, _ := json.Marshal(sampleInp)
if bytes.Compare(data, sampleOut) != 0 {
t.Errorf("should be %s but got %s", sampleOut, data)
}
}
func TestBoolInt_MarshalJSON_GotInt(t *testing.T) {
var sampleInp struct {
Val sdk.BoolInt `json:"val"`
}
var i int64 = 123456789
sampleInp.Val.Value = &i
var sampleOut = []byte(`{"val":123456789}`)
data, _ := json.Marshal(sampleInp)
if bytes.Compare(data, sampleOut) != 0 {
t.Errorf("should be %s but got %s", sampleOut, data)
}
}
func TestNewGraph(t *testing.T) {
var title = "Sample Title"
graph := sdk.NewGraph(title)
if graph.GraphPanel == nil {
t.Error("should be not nil")
}
if graph.TextPanel != nil {
t.Error("should be nil")
}
if graph.DashlistPanel != nil {
t.Error("should be nil")
}
if graph.SinglestatPanel != nil {
t.Error("should be nil")
}
if graph.Title != title {
t.Errorf("title should be %s but %s", title, graph.Title)
}
}
func TestGraph_AddTarget(t *testing.T) {
var target = sdk.Target{
RefID: "A",
Datasource: "Sample Source",
Expr: "sample request"}
graph := sdk.NewGraph("")
graph.AddTarget(&target)
if len(graph.GraphPanel.Targets) != 1 {
t.Errorf("should be 1 but %d", len(graph.GraphPanel.Targets))
}
if graph.GraphPanel.Targets[0].RefID != "A" {
t.Errorf("should be equal A but %s", graph.GraphPanel.Targets[0].RefID)
}
}
func TestGraph_SetTargetNew(t *testing.T) {
var (
target1 = sdk.Target{
RefID: "A",
Datasource: "Sample Source 1",
Expr: "sample request 1"}
target2 = sdk.Target{
RefID: "B",
Datasource: "Sample Source 2",
Expr: "sample request 2"}
)
graph := sdk.NewGraph("")
graph.AddTarget(&target1)
graph.SetTarget(&target2)
if len(graph.GraphPanel.Targets) != 2 {
t.Errorf("should be 2 but %d", len(graph.GraphPanel.Targets))
}
if graph.GraphPanel.Targets[0].RefID != "A" {
t.Errorf("should be equal A but %s", graph.GraphPanel.Targets[0].RefID)
}
if graph.GraphPanel.Targets[1].RefID != "B" {
t.Errorf("should be equal B but %s", graph.GraphPanel.Targets[1].RefID)
}
}
func TestGraph_SetTargetUpdate(t *testing.T) {
var (
target1 = sdk.Target{
RefID: "A",
Datasource: "Sample Source 1",
Expr: "sample request 1"}
target2 = sdk.Target{
RefID: "A",
Datasource: "Sample Source 2",
Expr: "sample request 2"}
)
graph := sdk.NewGraph("")
graph.AddTarget(&target1)
graph.SetTarget(&target2)
if len(graph.GraphPanel.Targets) != 1 {
t.Errorf("should be 1 but %d", len(graph.GraphPanel.Targets))
}
if graph.GraphPanel.Targets[0].RefID != "A" {
t.Errorf("should be equal A but %s", graph.GraphPanel.Targets[0].RefID)
}
}
// Test on the panel sample with Elasticsearch datasource with Graylog query. Grafana 2.6.
func TestPanel_ElasticsearchSource_ParsedTargets(t *testing.T) {
var rawPanel = []byte(`{
"aliasColors": {},
"bars": true,
"datasource": "Example GrayLog",
"editable": true,
"error": false,
"fill": 1,
"grid": {
"leftLogBase": 1,
"leftMax": null,
"leftMin": null,
"rightLogBase": 1,
"rightMax": null,
"rightMin": null,
"threshold1": null,
"threshold1Color": "rgba(216, 200, 27, 0.27)",
"threshold2": null,
"threshold2Color": "rgba(234, 112, 112, 0.22)"
},
"id": 37,
"isNew": true,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": false,
"total": false,
"values": false
},
"lines": false,
"linewidth": 2,
"links": [
{
"params": "q=tag%3A%2Fid.*%2F+AND+level%3AERROR&rangetype=relative&relative=300#fields=message%2Csource",
"title": "Example GrayLog Page",
"type": "absolute",
"url": "https://graylog/streams/xxx/messages"
}
],
"nullPointMode": "connected",
"percentage": false,
"pointradius": 5,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"span": 2,
"stack": false,
"steppedLine": false,
"targets": [
{
"bucketAggs": [
{
"field": "timestamp",
"id": "2",
"settings": {
"interval": "5m",
"min_doc_count": 0
},
"type": "date_histogram"
}
],
"dsType": "elasticsearch",
"metrics": [
{
"field": "select field",
"id": "1",
"type": "count"
}
],
"query": "tag:/.*.xxx.filtered/ AND tag:/id.*/ AND level:ERROR",
"refId": "A",
"target": "",
"timeField": "timestamp"
}
],
"timeFrom": null,
"timeShift": null,
"title": "Example GrayLog Errors[5m]",
"tooltip": {
"shared": true,
"value_type": "cumulative"
},
"transparent": true,
"type": "graph",
"x-axis": true,
"y-axis": true,
"y_formats": [
"short",
"short"
]
}`)
var graph sdk.Panel
err := json.Unmarshal(rawPanel, &graph)
if err != nil {
t.Fatalf("%s", err)
}
if len(graph.GraphPanel.Targets) != 1 {
t.Errorf("should be 1 but %d", len(graph.GraphPanel.Targets))
}
if graph.GraphPanel.Targets[0].DsType == nil {
t.Fatalf("should be \"elasticsearch\" but nil")
}
if *graph.GraphPanel.Targets[0].DsType != "elasticsearch" {
t.Errorf("should be \"elasticsearch\" but %s", *graph.GraphPanel.Targets[0].DsType)
}
}