-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_device_link_test.go
301 lines (279 loc) · 10.8 KB
/
rest_device_link_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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package ne
import (
"context"
"encoding/json"
"fmt"
"net/http"
"testing"
"github.com/equinix/ne-go/internal/api"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestGetDeviceLinkGroups(t *testing.T) {
//Given
var respBody api.DeviceLinkGroupsGetResponse
if err := readJSONData("./test-fixtures/ne_device_links_get_resp.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
limit := respBody.Pagination.Limit
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/links?limit=%d", baseURL, limit),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
c.PageSize = limit
linkGroups, err := c.GetDeviceLinkGroups()
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, linkGroups, "Client should return a response")
assert.Equal(t, len(respBody.Data), len(linkGroups), "Number of objects matches")
for i := range respBody.Data {
verifyDeviceLinkGroup(t, linkGroups[i], respBody.Data[i])
}
}
func TestGetDeviceLinkGroup(t *testing.T) {
//Given
var respBody api.DeviceLinkGroup
if err := readJSONData("./test-fixtures/ne_device_link_get_resp.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
uuid := "testLinkGroup"
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/ne/v1/links/%s", baseURL, uuid),
func(r *http.Request) (*http.Response, error) {
resp, _ := httpmock.NewJsonResponse(200, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
linkGroup, err := c.GetDeviceLinkGroup(uuid)
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, linkGroup, "Client should return a response")
verifyDeviceLinkGroup(t, *linkGroup, respBody)
}
func TestCreateDeviceLinkGroup(t *testing.T) {
//Given
var respBody api.DeviceLinkGroupCreateResponse
if err := readJSONData("./test-fixtures/ne_device_link_create_resp.json", &respBody); err != nil {
assert.Failf(t, "cannot read test response due to %s", err.Error())
}
testLinkGroup := DeviceLinkGroup{
Name: String("testLinkGroup"),
Subnet: String("10.1.2.0/24"),
RedundancyType: String("Primary"),
Devices: []DeviceLinkGroupDevice{
{
DeviceID: String("c9a5c40c-b90f-4156-8460-6cb5dc98f88d"),
ASN: Int(12345),
InterfaceID: Int(5),
},
{
DeviceID: String("7312336a-d508-4ac2-8d99-3070c30aed94"),
ASN: Int(22335),
InterfaceID: Int(4),
},
{
DeviceID: String("01026670-b55e-46db-84ba-004573d7a9d8"),
ASN: Int(12451),
InterfaceID: Int(8),
},
},
Links: []DeviceLinkGroupLink{
{
AccountNumber: String("22314"),
Throughput: String("50"),
ThroughputUnit: String("Mbps"),
SourceMetroCode: String("LD"),
DestinationMetroCode: String("AM"),
SourceZoneCode: String("Zone1"),
DestinationZoneCode: String("Zone1"),
},
{
AccountNumber: String("10314"),
Throughput: String("50"),
ThroughputUnit: String("Mbps"),
SourceMetroCode: String("LD"),
DestinationMetroCode: String("FR"),
SourceZoneCode: String("Zone1"),
DestinationZoneCode: String("Zone1"),
},
},
MetroLinks: []DeviceLinkGroupMetroLink{
{
AccountNumber: String("592205"),
Throughput: String("50"),
ThroughputUnit: String("Mbps"),
MetroCode: String("MX"),
},
{
AccountNumber: String("10314"),
Throughput: String("50"),
ThroughputUnit: String("Mbps"),
MetroCode: String("LD"),
},
},
}
request := api.DeviceLinkGroup{}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/ne/v1/links", baseURL),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&request); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
resp, _ := httpmock.NewJsonResponse(202, respBody)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//When
c := NewClient(context.Background(), baseURL, testHc)
uuid, err := c.CreateDeviceLinkGroup(testLinkGroup)
//Then
assert.Nil(t, err, "Client should not return an error")
assert.NotNil(t, uuid, "Client should return a response")
assert.Equal(t, respBody.UUID, uuid, "UUID matches")
verifyDeviceLinkGroup(t, testLinkGroup, request)
}
func TestUpdateDeviceLinkGroup(t *testing.T) {
//given
groupID := "test"
newGroupName := "newDLGroup"
newSubnet := "20.1.1.0/24"
newRedundancyType := "Primary"
newDevices := []DeviceLinkGroupDevice{
{
DeviceID: String("c9a5c40c-b90f-4156-8460-6cb5dc98f88d"),
ASN: Int(12345),
InterfaceID: Int(5),
},
{
DeviceID: String("7312336a-d508-4ac2-8d99-3070c30aed94"),
ASN: Int(22335),
InterfaceID: Int(4),
},
}
newLinks := []DeviceLinkGroupLink{
{
AccountNumber: String("22314"),
Throughput: String("50"),
ThroughputUnit: String("Mbps"),
SourceMetroCode: String("LD"),
DestinationMetroCode: String("AM"),
SourceZoneCode: String("Zone1"),
DestinationZoneCode: String("Zone1"),
},
}
newMetroLinks := []DeviceLinkGroupMetroLink{
{
AccountNumber: String("22314"),
Throughput: String("50"),
ThroughputUnit: String("Mbps"),
MetroCode: String("SV"),
},
}
req := api.DeviceLinkGroup{}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("PATCH", fmt.Sprintf("%s/ne/v1/links/%s", baseURL, groupID),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
return httpmock.NewStringResponse(204, ""), nil
},
)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
err := c.NewDeviceLinkGroupUpdateRequest(groupID).
WithGroupName(newGroupName).
WithRedundancyType(newRedundancyType).
WithSubnet(newSubnet).
WithDevices(newDevices).
WithLinks(newLinks).
WithMetroLinks(newMetroLinks).
Execute()
//then
assert.Nil(t, err, "Error is not returned")
assert.Equal(t, &newSubnet, req.Subnet, "Subnet matches")
assert.Equal(t, &newGroupName, req.GroupName, "GroupName matches")
assert.Equal(t, len(newDevices), len(req.Devices), "Devices number matches")
assert.Equal(t, &newRedundancyType, req.RedundancyType, "RedundancyType matches")
for i := range newDevices {
verifyDeviceLinkGroupDevice(t, newDevices[i], req.Devices[i])
}
assert.Equal(t, len(newLinks), len(req.Links), "Links number matches")
for i := range newLinks {
verifyDeviceLinkGroupLink(t, newLinks[i], req.Links[i])
}
assert.Equal(t, len(newMetroLinks), len(req.MetroLinks), "Metro Links number matches")
for i := range newMetroLinks {
verifyDeviceLinkGroupMetroLink(t, newMetroLinks[i], req.MetroLinks[i])
}
}
func TestDeleteDeviceLinkGroup(t *testing.T) {
//given
uuid := "testLinkGroup"
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("DELETE", fmt.Sprintf("%s/ne/v1/links/%s", baseURL, uuid),
httpmock.NewStringResponder(204, ""))
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
err := c.DeleteDeviceLinkGroup(uuid)
//then
assert.Nil(t, err, "Error is not returned")
}
func verifyDeviceLinkGroup(t *testing.T, linkGroup DeviceLinkGroup, apiLinkGroup api.DeviceLinkGroup) {
assert.Equal(t, apiLinkGroup.UUID, linkGroup.UUID, "UUID matches")
assert.Equal(t, apiLinkGroup.GroupName, linkGroup.Name, "GroupName matches")
assert.Equal(t, apiLinkGroup.Subnet, linkGroup.Subnet, "Subnet matches")
assert.Equal(t, apiLinkGroup.RedundancyType, linkGroup.RedundancyType, "RedundancyType matches")
assert.Equal(t, len(apiLinkGroup.Devices), len(linkGroup.Devices), "Length of []Devices matches")
assert.Equal(t, apiLinkGroup.ProjectID, linkGroup.ProjectID, "Project ID matches")
for i := range apiLinkGroup.Devices {
verifyDeviceLinkGroupDevice(t, linkGroup.Devices[i], apiLinkGroup.Devices[i])
}
assert.Equal(t, len(apiLinkGroup.Links), len(linkGroup.Links), "Length of []Links matches")
for i := range apiLinkGroup.Links {
verifyDeviceLinkGroupLink(t, linkGroup.Links[i], apiLinkGroup.Links[i])
}
assert.Equal(t, len(apiLinkGroup.MetroLinks), len(linkGroup.MetroLinks), "Length of []MetroLinks matches")
for i := range apiLinkGroup.MetroLinks {
verifyDeviceLinkGroupMetroLink(t, linkGroup.MetroLinks[i], apiLinkGroup.MetroLinks[i])
}
}
func verifyDeviceLinkGroupDevice(t *testing.T, linkGroupDevice DeviceLinkGroupDevice, apiLinkGroupDevice api.DeviceLinkGroupDevice) {
assert.Equal(t, linkGroupDevice.DeviceID, apiLinkGroupDevice.DeviceUUID, "DeviceUUID matches")
assert.Equal(t, linkGroupDevice.ASN, apiLinkGroupDevice.ASN, "ASN matches")
assert.Equal(t, linkGroupDevice.InterfaceID, apiLinkGroupDevice.InterfaceID, "InterfaceID matches")
assert.Equal(t, linkGroupDevice.Status, apiLinkGroupDevice.Status, "Status matches")
assert.Equal(t, linkGroupDevice.IPAddress, apiLinkGroupDevice.IPAddress, "IPAddress matches")
}
func verifyDeviceLinkGroupLink(t *testing.T, linkGroupLink DeviceLinkGroupLink, apiLinkGroupLink api.DeviceLinkGroupLink) {
assert.Equal(t, linkGroupLink.AccountNumber, apiLinkGroupLink.AccountNumber, "AccountNumber matches")
assert.Equal(t, linkGroupLink.Throughput, apiLinkGroupLink.Throughput, "Throughput matches")
assert.Equal(t, linkGroupLink.ThroughputUnit, apiLinkGroupLink.ThroughputUnit, "ThroughputUnit matches")
assert.Equal(t, linkGroupLink.SourceMetroCode, apiLinkGroupLink.SourceMetroCode, "SourceMetroCode matches")
assert.Equal(t, linkGroupLink.DestinationMetroCode, apiLinkGroupLink.DestinationMetroCode, "DestinationMetroCode matches")
assert.Equal(t, linkGroupLink.SourceZoneCode, apiLinkGroupLink.SourceZoneCode, "SourceZoneCode matches")
assert.Equal(t, linkGroupLink.DestinationZoneCode, apiLinkGroupLink.DestinationZoneCode, "DestinationZoneCode matches")
}
func verifyDeviceLinkGroupMetroLink(t *testing.T, linkGroupMetroLink DeviceLinkGroupMetroLink, apiLinkGroupMetroLink api.DeviceLinkGroupMetroLink) {
assert.Equal(t, linkGroupMetroLink.AccountNumber, apiLinkGroupMetroLink.AccountNumber, "AccountNumber matches")
assert.Equal(t, linkGroupMetroLink.Throughput, apiLinkGroupMetroLink.Throughput, "Throughput matches")
assert.Equal(t, linkGroupMetroLink.ThroughputUnit, apiLinkGroupMetroLink.ThroughputUnit, "ThroughputUnit matches")
assert.Equal(t, linkGroupMetroLink.MetroCode, apiLinkGroupMetroLink.MetroCode, "Metrocode matches")
}