forked from grafana-tools/sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
panel_test.go
766 lines (699 loc) · 15.8 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
package sdk_test
/*
Copyright 2016 Alexander I.Grafov <[email protected]>
Copyright 2016-2019 The Grafana SDK authors
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}`)
if err := json.Unmarshal(sampleIn, &sampleOut); err != nil {
t.Fatal(err)
}
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}`)
if err := json.Unmarshal(sampleIn, &sampleOut); err != nil {
t.Fatal(err)
}
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"}`)
if err := json.Unmarshal(sampleIn, &sampleOut); err != nil {
t.Fatal(err)
}
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, err := json.Marshal(sampleInp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, sampleOut) {
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, err := json.Marshal(sampleInp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, sampleOut) {
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, err := json.Marshal(sampleInp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, sampleOut) {
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}`)
if err := json.Unmarshal(sampleIn, &sampleOut); err != nil {
t.Fatal(err)
}
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}`)
if err := json.Unmarshal(sampleIn, &sampleOut); err != nil {
t.Fatal(err)
}
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}`)
if err := json.Unmarshal(sampleIn, &sampleOut); err != nil {
t.Fatal(err)
}
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, err := json.Marshal(sampleInp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, sampleOut) {
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, err := json.Marshal(sampleInp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, sampleOut) {
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, err := json.Marshal(sampleInp)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data, sampleOut) {
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 TestNewTimeseries(t *testing.T) {
var title = "Sample Title"
timeseries := sdk.NewTimeseries(title)
if timeseries.TimeseriesPanel == nil {
t.Error("should be not nil")
}
if timeseries.GraphPanel != nil {
t.Error("should be nil")
}
if timeseries.TextPanel != nil {
t.Error("should be nil")
}
if timeseries.DashlistPanel != nil {
t.Error("should be nil")
}
if timeseries.SinglestatPanel != nil {
t.Error("should be nil")
}
if timeseries.Title != title {
t.Errorf("title should be %s but %s", title, timeseries.Title)
}
}
func TestGraph_AddTarget(t *testing.T) {
var target = sdk.Target{
RefID: "A",
Datasource: &sdk.DatasourceRef{LegacyName: "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: &sdk.DatasourceRef{LegacyName: "Sample Source 1"},
Expr: "sample request 1",
}
target2 = sdk.Target{
RefID: "B",
Datasource: &sdk.DatasourceRef{LegacyName: "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: &sdk.DatasourceRef{LegacyName: "Sample Source 1"},
Expr: "sample request 1",
}
target2 = sdk.Target{
RefID: "A",
Datasource: &sdk.DatasourceRef{LegacyName: "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)
}
}
// Test on the panel sample with stackdriver datasource.
func TestPanel_Stackdriver_ParsedTargets(t *testing.T) {
var rawPanel = []byte(`{
"aliasColors": {},
"bars": false,
"dashLength": 10,
"dashes": false,
"datasource": "awesome-stackdriver",
"fill": 1,
"gridPos": {
"h": 8,
"w": 12,
"x": 0,
"y": 0
},
"id": 2,
"legend": {
"avg": false,
"current": false,
"max": false,
"min": false,
"show": true,
"total": false,
"values": false
},
"lines": true,
"linewidth": 1,
"links": [],
"nullPointMode": "null",
"options": {},
"percentage": false,
"pointradius": 2,
"points": false,
"renderer": "flot",
"seriesOverrides": [],
"spaceLength": 10,
"stack": false,
"steppedLine": false,
"targets": [
{
"aliasBy": "",
"alignOptions": [
{
"expanded": true,
"label": "Alignment options",
"options": [
{
"label": "delta",
"metricKinds": [
"CUMULATIVE",
"DELTA"
],
"text": "delta",
"value": "ALIGN_DELTA",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY",
"DISTRIBUTION"
]
},
{
"label": "rate",
"metricKinds": [
"CUMULATIVE",
"DELTA"
],
"text": "rate",
"value": "ALIGN_RATE",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY"
]
},
{
"label": "min",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "min",
"value": "ALIGN_MIN",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY"
]
},
{
"label": "max",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "max",
"value": "ALIGN_MAX",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY"
]
},
{
"label": "mean",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "mean",
"value": "ALIGN_MEAN",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY"
]
},
{
"label": "count",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "count",
"value": "ALIGN_COUNT",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY",
"BOOL"
]
},
{
"label": "sum",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "sum",
"value": "ALIGN_SUM",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY",
"DISTRIBUTION"
]
},
{
"label": "stddev",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "stddev",
"value": "ALIGN_STDDEV",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY"
]
},
{
"label": "percent change",
"metricKinds": [
"GAUGE",
"DELTA"
],
"text": "percent change",
"value": "ALIGN_PERCENT_CHANGE",
"valueTypes": [
"INT64",
"DOUBLE",
"MONEY"
]
}
]
}
],
"alignmentPeriod": "stackdriver-auto",
"crossSeriesReducer": "REDUCE_MEAN",
"defaultProject": "loading project...",
"filters": [
"resource.label.subscription_id",
"=",
"some_subscription_id"
],
"groupBy": [],
"metricKind": "DELTA",
"metricType": "pubsub.googleapis.com/subscription/ack_message_count",
"perSeriesAligner": "ALIGN_DELTA",
"refId": "A",
"unit": "1",
"usedAlignmentPeriod": 60,
"valueType": "INT64"
}
],
"thresholds": [],
"timeFrom": null,
"timeRegions": [],
"timeShift": null,
"title": "Pubsub Ack msg count",
"tooltip": {
"shared": true,
"sort": 0,
"value_type": "individual"
},
"type": "graph",
"xaxis": {
"buckets": null,
"mode": "time",
"name": null,
"show": true,
"values": []
},
"yaxes": [
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
},
{
"format": "short",
"label": null,
"logBase": 1,
"max": null,
"min": null,
"show": true
}
],
"yaxis": {
"align": false,
"alignLevel": null
}
}`)
var graph sdk.Panel
err := json.Unmarshal(rawPanel, &graph)
if err != nil {
t.Fatalf("%s", err)
}
if len(graph.GraphPanel.Targets) != 1 {
t.Fatalf("should be 1 but %d", len(graph.GraphPanel.Targets))
}
if graph.GraphPanel.Targets[0].MetricType != "pubsub.googleapis.com/subscription/ack_message_count" {
t.Fatalf("should be \"pubsub.googleapis.com/subscription/ack_message_count\" but is not")
}
}
// TestCustomPanelOutput_MarshalJSON marshals new custom panel to JSON,
// then marshals that json to map[string]interface{},\
// and then checks both custom and non-custom keys are present and correct.
func TestCustomPanelOutput_MarshalJSON(t *testing.T) {
var (
titleKey = "title"
titleValue = "test title"
customKey = "test_key"
customValue = "bar_value"
)
p := sdk.NewCustom(titleValue)
custom := map[string]interface{}(*p.CustomPanel)
custom[customKey] = customValue
b, err := json.Marshal(p)
if err != nil {
t.Fatalf("failed to marshal custom panel: %v", err)
}
var j = make(map[string]interface{})
err = json.Unmarshal(b, &j)
if err != nil {
t.Fatalf("failed to unmarshal: %v", err)
}
val, ok := j[customKey]
if !ok {
t.Fatalf("failed to find key %s in map %v", customKey, j)
}
if val != customValue {
t.Fatalf("wrong value of %s: got %s, expected %s", customKey, val, customValue)
}
val, ok = j[titleKey]
if !ok {
t.Fatalf("failed to find key %s in map %v", titleKey, j)
}
if val != titleValue {
t.Fatalf("wrong value of %s: got %s, expected %s", titleKey, val, titleValue)
}
}