forked from spaceapegames/go-wavefront
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dashboard_test.go
140 lines (122 loc) · 3.23 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
package wavefront
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"testing"
)
type MockDashboardClient struct {
Client
InvokedCount int
T *testing.T
}
type MockCrudDashboardClient struct {
Client
method string
T *testing.T
}
func (m *MockDashboardClient) Do(req *http.Request) (io.ReadCloser, error) {
response, err := ioutil.ReadFile(fmt.Sprintf("./fixtures/paginated-dashboard-%d.json", m.InvokedCount))
if err != nil {
m.T.Fatal(err)
}
body, _ := ioutil.ReadAll(req.Body)
search := SearchParams{}
err = json.Unmarshal(body, &search)
if err != nil {
m.T.Fatal(err)
}
if search.Offset != search.Limit*m.InvokedCount {
m.T.Errorf("offset, expected %d, got %d", search.Limit*m.InvokedCount, search.Offset)
}
m.InvokedCount++
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
func TestDashboards_PaginatedFind(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
a := &Dashboards{
client: &MockDashboardClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
T: t,
},
}
dashboards, err := a.Find(nil)
if err != nil {
t.Fatal(err)
}
invoked := ((a.client).(*MockDashboardClient)).InvokedCount
if invoked != 2 {
t.Errorf("paginated search, expected 2, got %d", invoked)
}
if dashboards[0].Name != "test 1" {
t.Errorf("dashboard name incorrect: %s", dashboards[0].Name)
}
if len(dashboards[0].Tags) != 2 {
t.Errorf("dashboard tags, expected 2, got %d", len(dashboards[0].Tags))
}
}
func (m *MockCrudDashboardClient) Do(req *http.Request) (io.ReadCloser, error) {
response, err := ioutil.ReadFile("./fixtures/create-dashboard-response.json")
if err != nil {
m.T.Fatal(err)
}
if req.Method != m.method {
m.T.Errorf("request method expected '%s' got '%s'", m.method, req.Method)
}
body, _ := ioutil.ReadAll(req.Body)
dashboard := Dashboard{}
err = json.Unmarshal(body, &dashboard)
if err != nil {
m.T.Fatal(err)
}
return ioutil.NopCloser(bytes.NewReader(response)), nil
}
func TestDashboards_CreateUpdateDeleteDashboard(t *testing.T) {
baseurl, _ := url.Parse("http://testing.wavefront.com")
a := &Dashboards{
client: &MockCrudDashboardClient{
Client: Client{
Config: &Config{Token: "1234-5678-9977"},
BaseURL: baseurl,
httpClient: http.DefaultClient,
debug: true,
},
method: "PUT",
T: t,
},
}
dashboard := Dashboard{
Name: "Dashboard API example",
Tags: []string{"test"},
Description: "Dashboard Description",
Url: "api-example",
}
if err := a.Update(&dashboard); err == nil {
t.Errorf("expected dashboard update to error with no ID")
}
a.client.(*MockCrudDashboardClient).method = "POST"
a.Create(&dashboard)
if dashboard.ID != "api-example" {
t.Errorf("dashboard ID expected api-example, got %s", dashboard.ID)
}
a.client.(*MockCrudDashboardClient).method = "PUT"
if err := a.Update(&dashboard); err != nil {
t.Error(err)
}
a.client.(*MockCrudDashboardClient).method = "DELETE"
if err := a.Delete(&dashboard); err != nil {
t.Error(err)
}
if dashboard.ID != "" {
t.Error("expected dashboard ID to be reset after deletion")
}
}