forked from nytm/go-grafana-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_test.go
162 lines (135 loc) · 3.15 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
package gapi
import (
"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"
}
}`
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) {
server, client := gapiTestTools(200, createdAndUpdateDashboardResponse)
defer server.Close()
dashboard := Dashboard{
Model: map[string]interface{}{
"title": "test",
},
Folder: 0,
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} {
server.code = code
_, err = client.NewDashboard(dashboard)
if err == nil {
t.Errorf("%d not detected", code)
}
}
}
func TestDashboardGet(t *testing.T) {
server, client := gapiTestTools(200, getDashboardResponse)
defer server.Close()
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")
}
resp, err = client.DashboardByUid("cIBgcSjkk")
if err != nil {
t.Error(err)
}
uid, ok = resp.Model["uid"]
if !ok || uid != "cIBgcSjkk" {
t.Errorf("Invalid uid - %s, Expected %s", uid, "cIBgcSjkk")
}
for _, code := range []int{401, 403, 404} {
server.code = code
_, 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) {
server, client := gapiTestTools(200, "")
defer server.Close()
err := client.DeleteDashboard("test")
if err != nil {
t.Error(err)
}
err = client.DeleteDashboardByUid("cIBgcSjkk")
if err != nil {
t.Error(err)
}
for _, code := range []int{401, 403, 404, 412} {
server.code = code
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) {
server, client := gapiTestTools(200, getDashboardsJSON)
defer server.Close()
dashboards, err := client.Dashboards()
if err != nil {
t.Error(err)
}
t.Log(pretty.PrettyFormat(dashboards))
if len(dashboards) != 1 {
t.Error("Length of returned dashboards should be 1")
}
if dashboards[0].Id != 1 || dashboards[0].Title != "Grafana Stats" {
t.Error("Not correctly parsing returned dashboards.")
}
}