forked from grafana/grafana-api-golang-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_test.go
174 lines (145 loc) · 3.75 KB
/
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
package gapi
import (
"strings"
"testing"
"github.com/gobs/pretty"
)
const (
createdAndUpdateDashboardResponse = `{
"slug": "test",
"id": 1,
"uid": "nErXDvCkzz",
"status": "success",
"version": 1
}`
getDashboardResponse = `{
"dashboard": {
"id": 1,
"uid": "cIBgcSjkk",
"title": "Production Overview",
"version": 0
},
"meta": {
"isStarred": false,
"url": "/d/cIBgcSjkk/production-overview",
"slug": "production-overview",
"folderID": 3,
"folderUid": "test"
}
}`
getDashboardsJSON = `{
"id": 1,
"uid": "RGAPB1cZz",
"title": "Grafana Stats",
"uri": "db/grafana-stats",
"url": "/dashboards/d/RGAPB1cZz/grafana-stat",
"slug": "",
"type": "dash-db",
"tags": [],
"isStarred": false
}`
)
func TestDashboardCreateAndUpdate(t *testing.T) {
client := gapiTestTools(t, 200, createdAndUpdateDashboardResponse)
dashboard := Dashboard{
Model: map[string]interface{}{
"title": "test",
},
FolderID: 0,
FolderUID: "l3KqBxCMz",
Overwrite: false,
}
resp, err := client.NewDashboard(dashboard)
if err != nil {
t.Fatal(err)
}
t.Log(pretty.PrettyFormat(resp))
if resp.UID != "nErXDvCkzz" {
t.Errorf("Invalid uid - %s, Expected %s", resp.UID, "nErXDvCkzz")
}
for _, code := range []int{400, 401, 403, 412} {
client = gapiTestTools(t, code, "error")
_, err = client.NewDashboard(dashboard)
if err == nil {
t.Errorf("%d not detected", code)
}
}
}
func TestDashboardGet(t *testing.T) {
client := gapiTestTools(t, 200, getDashboardResponse)
resp, err := client.Dashboard("test")
if err != nil {
t.Error(err)
}
uid, ok := resp.Model["uid"]
if !ok || uid != "cIBgcSjkk" {
t.Errorf("Invalid uid - %s, Expected %s", uid, "cIBgcSjkk")
}
client = gapiTestTools(t, 200, getDashboardResponse)
resp, err = client.DashboardByUID("cIBgcSjkk")
if err != nil {
t.Fatal(err)
}
uid, ok = resp.Model["uid"]
if !ok || uid != "cIBgcSjkk" {
t.Fatalf("Invalid UID - %s, Expected %s", uid, "cIBgcSjkk")
}
for _, code := range []int{401, 403, 404} {
client = gapiTestTools(t, code, "error")
_, err = client.Dashboard("test")
if err == nil {
t.Errorf("%d not detected", code)
}
_, err = client.DashboardByUID("cIBgcSjkk")
if err == nil {
t.Errorf("%d not detected", code)
}
}
}
func TestDashboardDelete(t *testing.T) {
client := gapiTestTools(t, 200, "")
err := client.DeleteDashboard("test")
if err != nil {
t.Error(err)
}
client = gapiTestTools(t, 200, "")
err = client.DeleteDashboardByUID("cIBgcSjkk")
if err != nil {
t.Fatal(err)
}
for _, code := range []int{401, 403, 404, 412} {
client = gapiTestTools(t, code, "error")
err = client.DeleteDashboard("test")
if err == nil {
t.Errorf("%d not detected", code)
}
err = client.DeleteDashboardByUID("cIBgcSjkk")
if err == nil {
t.Errorf("%d not detected", code)
}
}
}
func TestDashboards(t *testing.T) {
mockData := strings.Repeat(getDashboardsJSON+",", 1000) // make 1000 dashboards.
mockData = "[" + mockData[:len(mockData)-1] + "]" // remove trailing comma; make a json list.
// This creates 1000 + 1000 + 1 (2001, 3 calls) worth of dashboards.
client := gapiTestToolsFromCalls(t, []mockServerCall{
{200, mockData},
{200, mockData},
{200, "[" + getDashboardsJSON + "]"},
})
const dashCount = 2001
dashboards, err := client.Dashboards()
if err != nil {
t.Fatal(err)
}
if len(dashboards) != dashCount {
t.Errorf("Length of returned dashboards should be %d", dashCount)
}
if dashboards[0].ID != 1 || dashboards[0].Title != "Grafana Stats" {
t.Error("Not correctly parsing returned dashboards.")
}
if dashboards[dashCount-1].ID != 1 || dashboards[dashCount-1].Title != "Grafana Stats" {
t.Error("Not correctly parsing returned dashboards.")
}
}