-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard-unmarshal_test.go
180 lines (140 loc) · 4.8 KB
/
dashboard-unmarshal_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
package sdk_test
/*
Copyright 2016 Alexander I.Grafov <grafov@gmail.com>
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 (
"encoding/json"
"io/ioutil"
"testing"
"github.com/grafana-tools/sdk"
)
func TestUnmarshal_NewEmptyDashboard26(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/new-empty-dashboard-2.6.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
}
func TestUnmarshal_EmptyDashboardWithTemplating26(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/empty-dashboard-with-templating-2.6.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
}
func TestUnmarshal_EmptyDashboardWithAnnotation26(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/empty-dashboard-with-annotation-2.6.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
}
func TestUnmarshal_EmptyDashboardWithLinks26(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/empty-dashboard-with-links-2.6.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
}
func TestUnmarshal_DashboardWithDefaultPanelsIn2Rows26(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/default-panels-all-types-2-rows-dashboard-2.6.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
}
func TestUnmarshal_DashboardWithGraphWithTargets26(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/default-panels-graph-with-targets-2.6.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
if len(board.Rows) != 1 {
t.Errorf("there are 1 row defined but got %d", len(board.Rows))
}
if len(board.Rows[0].Panels) != 1 {
t.Errorf("there are 1 panel defined but got %d", len(board.Rows[0].Panels))
}
panel := board.Rows[0].Panels[0]
if panel.OfType != sdk.GraphType {
t.Errorf("panel type should be %d (\"graph\") type but got %d", sdk.GraphType, panel.OfType)
}
if *panel.Datasource != sdk.MixedSource {
t.Errorf("panel Datasource should be \"%s\" but got \"%s\"", sdk.MixedSource, *panel.Datasource)
}
if len(panel.GraphPanel.Targets) != 2 {
t.Errorf("panel has 2 targets but got %d", len(panel.GraphPanel.Targets))
}
}
func TestUnmarshal_DashboardWithEmptyPanels30(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/dashboard-with-default-panels-grafana-3.0.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
}
func TestUnmarshal_DashboardWithHiddenTemplates(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/empty-dashboard-with-templating-4.0.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
if board.Templating.List[1].Hide != sdk.TemplatingHideVariable {
t.Errorf("templating has hidden variable '%d', got %d", sdk.TemplatingHideVariable, board.Templating.List[1].Hide)
}
}
func TestUnmarshal_DashboardWithMixedYaxes(t *testing.T) {
var board sdk.Board
raw, _ := ioutil.ReadFile("testdata/dashboard-with-panels-with-mixed-yaxes.json")
err := json.Unmarshal(raw, &board)
if err != nil {
t.Error(err)
}
p1, p2 := board.Rows[0].Panels[0], board.Rows[0].Panels[1]
min1, max1 := p1.Yaxes[0].Min, p1.Yaxes[0].Max
min2, max2 := p1.Yaxes[1].Min, p1.Yaxes[1].Max
min3, max3 := p2.Yaxes[0].Min, p2.Yaxes[0].Max
min4, max4 := p2.Yaxes[1].Min, p2.Yaxes[1].Max
if min1.Value != 0 || min1.Valid != true {
t.Errorf("panel #1 has wrong min value: %d, expected: %d", min1.Value, 0)
}
if max1.Value != 100 || max1.Valid != true {
t.Errorf("panel #1 has wrong max value: %d, expected: %d", max1.Value, 100)
}
if min2 != nil {
t.Errorf("panel #1 has wrong min value: %v, expected: %v", min2, nil)
}
if max2 != nil {
t.Errorf("panel #1 has wrong max value: %v, expected: %v", max2, nil)
}
if min3.Value != 0 || min3.Valid != true {
t.Errorf("panel #2 has wrong min value: %d, expected: %d", min3.Value, 0)
}
if max3 != nil {
t.Errorf("panel #2 has wrong max value: %v, expected: %v", max3, nil)
}
if min4 != nil {
t.Errorf("panel #2 has wrong min value: %v, expected: %v", min4, nil)
}
if max4.Value != 50 || max4.Valid != true {
t.Errorf("panel #1 has wrong max value: %d, expected: %d", max4.Value, 100)
}
}