-
Notifications
You must be signed in to change notification settings - Fork 7
/
rest_bgp_test.go
151 lines (135 loc) · 5.18 KB
/
rest_bgp_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
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"
)
var testBGPConfiguration = BGPConfiguration{
ConnectionUUID: String("e8b2e48e-2eba-4412-bc0b-c88dadb48050"),
LocalIPAddress: String("10.0.0.1/30"),
LocalASN: Int(10012),
RemoteIPAddress: String("10.0.0.2"),
RemoteASN: Int(10013),
AuthenticationKey: String("authKey"),
}
func TestCreateBGPConfiguration(t *testing.T) {
//given
resp := api.BGPConfigurationCreateResponse{}
if err := readJSONData("./test-fixtures/ne_bgp_create_resp.json", &resp); err != nil {
assert.Fail(t, "Cannot read test response")
}
bgpConfig := testBGPConfiguration
reqBody := api.BGPConfiguration{}
testHc := &http.Client{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("POST", fmt.Sprintf("%s/ne/v1/bgp", baseURL),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
resp, _ := httpmock.NewJsonResponse(202, resp)
return resp, nil
},
)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
uuid, err := c.CreateBGPConfiguration(bgpConfig)
//then
assert.Nil(t, err, "Error is not returned")
assert.Equal(t, uuid, resp.UUID, "UUID matches")
verifyBGPConfig(t, bgpConfig, reqBody)
}
func TestGetBGPConfiguration(t *testing.T) {
//given
resp := api.BGPConfiguration{}
if err := readJSONData("./test-fixtures/ne_bgp_get_resp.json", &resp); err != nil {
assert.Fail(t, "Cannot read test response")
}
bgpConfID := "e8b2e48e-2eba-4412-bc0b-c88dadb48050"
testHc := setupMockedClient("GET", fmt.Sprintf("%s/ne/v1/bgp/%s", baseURL, bgpConfID), 200, resp)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
bgpConf, err := c.GetBGPConfiguration(bgpConfID)
//then
assert.NotNil(t, bgpConf, "Returned device is not nil")
assert.Nil(t, err, "Error is not returned")
verifyBGPConfig(t, *bgpConf, resp)
}
func TestGetBGPConfigurationForConnection(t *testing.T) {
//given
resp := api.BGPConfiguration{}
if err := readJSONData("./test-fixtures/ne_bgp_get_resp.json", &resp); err != nil {
assert.Fail(t, "Cannot read test response")
}
connID := "e8b2e48e-2eba-4412-bc0b-c88dadb48050"
testHc := setupMockedClient("GET", fmt.Sprintf("%s/ne/v1/bgp/connection/%s", baseURL, connID), 200, resp)
defer httpmock.DeactivateAndReset()
//when
c := NewClient(context.Background(), baseURL, testHc)
bgpConf, err := c.GetBGPConfigurationForConnection(connID)
//then
assert.NotNil(t, bgpConf, "Returned device is not nil")
assert.Nil(t, err, "Error is not returned")
verifyBGPConfig(t, *bgpConf, resp)
}
func TestUpdateBGPConfiguration(t *testing.T) {
//given
resp := api.BGPConfigurationCreateResponse{}
if err := readJSONData("./test-fixtures/ne_bgp_create_resp.json", &resp); err != nil {
assert.Fail(t, "Cannot read test response")
}
bgpConfID := "e8b2e48e-2eba-4412-bc0b-c88dadb48050"
testHc := &http.Client{}
reqBody := api.BGPConfiguration{}
httpmock.ActivateNonDefault(testHc)
httpmock.RegisterResponder("PUT", fmt.Sprintf("%s/ne/v1/bgp/%s", baseURL, bgpConfID),
func(r *http.Request) (*http.Response, error) {
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
return httpmock.NewStringResponse(202, ""), nil
},
)
defer httpmock.DeactivateAndReset()
newLocalASN := 12345
newRemoteASN := 22241
newLocalIPAddress := "1.1.1.1/30"
newRemoteIPAddress := "1.1.1.2"
newAuthenticationKey := "authKey"
//when
c := NewClient(context.Background(), baseURL, testHc)
err := c.NewBGPConfigurationUpdateRequest(bgpConfID).
WithLocalASN(newLocalASN).
WithRemoteASN(newRemoteASN).
WithLocalIPAddress(newLocalIPAddress).
WithRemoteIPAddress(newRemoteIPAddress).
WithAuthenticationKey(newAuthenticationKey).
Execute()
//then
assert.Nil(t, err, "Error is not returned")
verifyBGPConfig(t, BGPConfiguration{
LocalASN: Int(newLocalASN),
RemoteASN: Int(newRemoteASN),
LocalIPAddress: String(newLocalIPAddress),
RemoteIPAddress: String(newRemoteIPAddress),
AuthenticationKey: String(newAuthenticationKey),
}, reqBody)
}
func verifyBGPConfig(t *testing.T, config BGPConfiguration, apiConfig api.BGPConfiguration) {
assert.Equal(t, config.ConnectionUUID, apiConfig.ConnectionUUID, "ConnectionUUID matches")
assert.Equal(t, config.LocalIPAddress, apiConfig.LocalIPAddress, "LocalIPAddress matches")
assert.Equal(t, config.LocalASN, apiConfig.LocalASN, "LocalASN matches")
assert.Equal(t, config.RemoteIPAddress, apiConfig.RemoteIPAddress, "RemoteIPAddress matches")
assert.Equal(t, config.RemoteASN, apiConfig.RemoteASN, "RemoteASN matches")
assert.Equal(t, config.AuthenticationKey, apiConfig.AuthenticationKey, "AuthenticationKey matches")
assert.Equal(t, config.DeviceUUID, apiConfig.VirtualDeviceUUID, "DeviceUUID matches")
assert.Equal(t, config.State, apiConfig.State, "State matches")
assert.Equal(t, config.ProvisioningStatus, apiConfig.ProvisioningStatus, "ProvisioningStatus matches")
}