forked from thousandeyes/thousandeyes-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dnsp_domain_test.go
249 lines (226 loc) · 9.8 KB
/
dnsp_domain_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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package thousandeyes
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestClient_GetDNSPDomain(t *testing.T) {
out := `{"test":[{"createdDate":"2020-02-06 15:28:07","createdBy":"William Fleming ([email protected])","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"dnsp-domain","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"domain": "webex.com","dnsTransportProtocol": "UDP","apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dnsp-domain/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/122621.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte(out))
})
expected := DNSPDomain{
TestID: 122621,
Enabled: 1,
CreatedBy: "William Fleming ([email protected])",
CreatedDate: "2020-02-06 15:28:07",
SavedEvent: 0,
AlertsEnabled: 1,
TestName: "test123",
Type: "dnsp-domain",
Interval: 300,
Domain: "webex.com",
SharedWithAccounts: []SharedWithAccount{
{
AID: 176592,
AccountGroupName: "Cloudreach",
},
},
APILinks: APILinks{
{
Href: "https://api.thousandeyes.com/v6/tests/1226221",
Rel: "self",
},
{
Href: "https://api.thousandeyes.com/v6/web/dnsp-domain/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/metrics/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/path-vis/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/bgp-metrics/1226221",
Rel: "data",
},
},
}
res, err := client.GetDNSPDomain(122621)
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}
func TestClient_GetDNSPDomainJsonError(t *testing.T) {
out := `{"test":[{"createdDate":"2020-02-06 15:28:07",createdBy":"William Fleming ([email protected])","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"dnsp-domain","interval":300,"alertsEnabled":1,"liveShare":0,"probeMode":"AUTO","agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"domain": "webex.com","dnsTransportProtocol": "UDP"}]"apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dnsp-domain/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/122621.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
_, _ = w.Write([]byte(out))
})
_, err := client.GetDNSPDomain(122621)
assert.Error(t, err)
assert.EqualError(t, err, "Could not decode JSON response: invalid character 'c' looking for beginning of object key string")
}
func TestClient_CreateDNSPDomain(t *testing.T) {
out := `{"test":[{"createdDate":"2020-02-06 15:28:07","createdBy":"William Fleming ([email protected])","enabled":1,"savedEvent":0,"testId":122621,"testName":"test123","type":"dnsp-domain","interval":300,"alertsEnabled":1,"liveShare":0,"agents":[{"agentId":48620,"agentName":"Seattle, WA (Trial) - IPv6","agentType":"Cloud","countryId":"US","ipAddresses":["135.84.184.153"],"location":"Seattle Area","network":"Astute Hosting Inc. (AS 54527)","prefix":"135.84.184.0/22"}],"sharedWithAccounts":[{"aid":176592,"name":"Cloudreach"}],"domain": "webex.com","apiLinks":[{"rel":"self","href":"https://api.thousandeyes.com/v6/tests/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/web/dnsp-domain/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/metrics/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/path-vis/1226221"},{"rel":"data","href":"https://api.thousandeyes.com/v6/net/bgp-metrics/1226221"}]}]}`
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-domain/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusCreated)
_, _ = w.Write([]byte(out))
})
expected := DNSPDomain{
TestID: 122621,
Enabled: 1,
CreatedBy: "William Fleming ([email protected])",
CreatedDate: "2020-02-06 15:28:07",
SavedEvent: 0,
TestName: "test123",
Type: "dnsp-domain",
Interval: 300,
AlertsEnabled: 1,
Domain: "webex.com",
SharedWithAccounts: []SharedWithAccount{
{
AID: 176592,
AccountGroupName: "Cloudreach",
},
},
APILinks: APILinks{
{
Href: "https://api.thousandeyes.com/v6/tests/1226221",
Rel: "self",
},
{
Href: "https://api.thousandeyes.com/v6/web/dnsp-domain/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/metrics/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/path-vis/1226221",
Rel: "data",
},
{
Href: "https://api.thousandeyes.com/v6/net/bgp-metrics/1226221",
Rel: "data",
},
},
}
create := DNSPDomain{
TestName: "test123",
Domain: "webex.com",
Interval: 300,
}
res, err := client.CreateDNSPDomain(create)
teardown()
assert.Nil(t, err)
assert.Equal(t, &expected, res)
}
func TestClient_DeleteDNSPDomain(t *testing.T) {
setup()
mux.HandleFunc("/tests/dnsp-domain/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.DeleteDNSPDomain(id)
if err != nil {
t.Fatal(err)
}
}
func TestClient_AddDNSPDomainAlertRule(t *testing.T) {
test := DNSPDomain{TestName: "test", AlertRules: []AlertRule{}}
expected := DNSPDomain{TestName: "test", AlertRules: []AlertRule{{RuleID: 1}}}
test.AddAlertRule(1)
assert.Equal(t, expected, test)
}
func TestClient_UpdateDNSPDomain(t *testing.T) {
setup()
out := `{"test":[{"testId":1,"testName":"test123","type":"dnsp-domain","domain":"webex.com" }]}`
mux.HandleFunc("/tests/dnsp-domain/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
dnsp := DNSPDomain{Domain: "webex.com"}
res, err := client.UpdateDNSPDomain(id, dnsp)
if err != nil {
t.Fatal(err)
}
expected := DNSPDomain{TestID: 1, TestName: "test123", Type: "dnsp-domain", Domain: "webex.com"}
assert.Equal(t, &expected, res)
}
func TestClient_GetDNSPDomainError(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-domain/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
})
_, err := client.GetDNSPDomain(1)
teardown()
assert.Error(t, err)
}
func TestClient_GetDNSPDomainStatusCode(t *testing.T) {
setup()
out := `{"test":[{"testId":1,"testName":"test123","type":"dnsp-trace"}]}`
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/1.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(out))
})
_, err := client.GetDNSPDomain(1)
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}
func TestClient_CreateDNSPDomainStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-domain/new.json", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "POST", r.Method)
w.WriteHeader(http.StatusBadRequest)
_, _ = w.Write([]byte(`{}`))
})
_, err := client.CreateDNSPDomain(DNSPDomain{})
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}
func TestClient_UpdateDNSPDomainStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-domain/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.UpdateDNSPDomain(1, DNSPDomain{})
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}
func TestClient_DeleteDNSPDomainStatusCode(t *testing.T) {
setup()
var client = &Client{APIEndpoint: server.URL, AuthToken: "foo"}
mux.HandleFunc("/tests/dnsp-domain/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.DeleteDNSPDomain(1)
teardown()
assert.EqualError(t, err, "Failed call API endpoint. HTTP response code: 400. Error: &{}")
}