forked from thousandeyes/thousandeyes-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert_test.go
163 lines (143 loc) · 5.21 KB
/
alert_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
package thousandeyes
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_GetAlertRule(t *testing.T) {
out := `{"alertRules" : [ {"RuleID":1, "ruleName": "test" }]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.Write([]byte(out))
})
// Define expected values from the API (based on the JSON we print out above)
expected := AlertRules{
AlertRule{RuleID: Int(1), RuleName: String("test")},
}
res, err := client.GetAlertRules()
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}
func TestClient_GetAlertError(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})
_, err := client.GetAlertRules()
teardown()
assert.Error(t, err)
}
func TestClient_DeleteAlertRule(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/alert-rules/1/delete.json", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
assert.Equal(t, "POST", r.Method)
})
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
id := 1
err := client.DeleteAlertRule(id)
if err != nil {
t.Fatal(err)
}
}
func TestClient_UpdateAlertRule(t *testing.T) {
setup()
out := `{"RuleID":1, "ruleName": "test", "roundsViolatingOutOf": 2, "roundsViolatingRequired": 1}`
mux.HandleFunc("/alert-rules/1/update.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
_, _ = w.Write([]byte(out))
})
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
id := 1
u := AlertRule{RoundsViolatingOutOf: Int(2)}
res, err := client.UpdateAlertRule(id, u)
if err != nil {
t.Fatal(err)
}
expected := AlertRule{RuleID: Int(1), RuleName: String("test"), RoundsViolatingOutOf: Int(2), RoundsViolatingRequired: Int(1)}
assert.Equal(t, &expected, res)
}
func TestClient_CreateAlertRule(t *testing.T) {
setup()
out := `{"alertRuleId": 1, "ruleName": "test", "roundsViolatingOutOf": 2, "roundsViolatingRequired": 1}`
mux.HandleFunc("/alert-rules/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(out))
})
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
u := AlertRule{RuleName: String("test"), RoundsViolatingOutOf: Int(2), RoundsViolatingRequired: Int(1)}
res, err := client.CreateAlertRule(u)
if err != nil {
t.Fatal(err)
}
expected := AlertRule{RuleID: Int(1), RuleName: String("test"), RoundsViolatingOutOf: Int(2), RoundsViolatingRequired: Int(1)}
assert.Equal(t, &expected, res)
}
func TestClient_AlertJsonError(t *testing.T) {
out := `{"alertRules": [test]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte(out))
})
_, err := client.GetAlertRules()
assert.Error(t, err)
assert.EqualError(t, err, "could not decode JSON response: invalid character 'e' in literal true (expecting 'r')")
}
func TestClient_GetAlertStatusCode(t *testing.T) {
setup()
out := `{"alertRules":[{"ruleId":1,"ruleName":"test123"}]}`
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(out))
})
_, err := client.GetAlertRules()
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{<nil>}")
}
func TestClient_CreateAlertStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{}`))
})
_, err := client.CreateAlertRule(AlertRule{})
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{<nil>}")
}
func TestClient_UpdateAlertRuleStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/1/update.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{}`))
})
_, err := client.UpdateAlertRule(1, AlertRule{})
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{<nil>}")
}
func TestClient_DeleteAlertRuleStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/alert-rules/1/delete.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(`{}`))
})
err := client.DeleteAlertRule(1)
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{<nil>}")
}