-
Notifications
You must be signed in to change notification settings - Fork 33
/
graph_defs_test.go
139 lines (122 loc) · 3.34 KB
/
graph_defs_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
package mackerel
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"
)
func TestCreateGraphDefs(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-defs/create" {
t.Error("request URL should be /api/v0/graph-defs/create but: ", req.URL.Path)
}
if req.Method != "POST" {
t.Error("request method should be POST but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var datas []struct {
Name string `json:"name"`
DisplayName string `json:"displayName"`
Unit string `json:"unit"`
Metrics []*GraphDefsMetric `json:"metrics"`
}
err := json.Unmarshal(body, &datas)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}
data := datas[0]
if data.Name != "mackerel" {
t.Errorf("request sends json including name but: %s", data.Name)
}
if data.DisplayName != "HorseMackerel" {
t.Errorf("request sends json including DisplayName but: %s", data.Name)
}
if !reflect.DeepEqual(
data.Metrics[0],
&GraphDefsMetric{
Name: "saba1",
DisplayName: "aji1",
IsStacked: false,
},
) {
t.Error("request sends json including GraphDefsMetric but: ", data.Metrics[0])
}
respJSON, _ := json.Marshal(map[string]string{
"result": "OK",
})
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
err := client.CreateGraphDefs([]*GraphDefsParam{
{
Name: "mackerel",
DisplayName: "HorseMackerel",
Unit: "percentage",
Metrics: []*GraphDefsMetric{
{
Name: "saba1",
DisplayName: "aji1",
IsStacked: false,
},
{
Name: "saba2",
DisplayName: "aji2",
IsStacked: false,
},
},
},
})
if err != nil {
t.Error("err should be nil but: ", err)
}
}
func TestGraphDefsOmitJSON(t *testing.T) {
g := GraphDefsParam{
Metrics: []*GraphDefsMetric{
{},
},
}
want := `{"name":"","metrics":[{"name":"","isStacked":false}]}`
b, err := json.Marshal(&g)
if err != nil {
t.Fatal(err)
}
if s := string(b); s != want {
t.Errorf("json.Marshal(%#v) = %q; want %q", g, s, want)
}
}
func TestDeleteGraphDef(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.URL.Path != "/api/v0/graph-defs" {
t.Error("request URL should be /api/v0/graph-defs but:", req.URL.Path)
}
if req.Method != "DELETE" {
t.Error("request method should be DELETE but: ", req.Method)
}
body, _ := io.ReadAll(req.Body)
var data struct {
Name string `json:"name"`
}
err := json.Unmarshal(body, &data)
if err != nil {
t.Fatal("request body should be decoded as json", string(body))
}
if data.Name != "mackerel" {
t.Errorf("request sends json including name but: %s", data.Name)
}
respJSON, _ := json.Marshal((map[string]bool{"success": true}))
res.Header()["Content-Type"] = []string{"application/json"}
fmt.Fprint(res, string(respJSON))
}))
defer ts.Close()
client, _ := NewClientWithOptions("dummy-key", ts.URL, false)
err := client.DeleteGraphDef("mackerel")
if err != nil {
t.Error("err should be nil but: ", err)
}
}