github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/vpnaas/siteconnections/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/opentelekomcloud/gophertelekomcloud" 9 fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/common" 10 "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/vpnaas/siteconnections" 11 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 12 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 13 ) 14 15 func TestCreate(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections", func(w http.ResponseWriter, r *http.Request) { 20 th.TestMethod(t, r, "POST") 21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 22 th.TestHeader(t, r, "Content-Type", "application/json") 23 th.TestHeader(t, r, "Accept", "application/json") 24 th.TestJSONRequest(t, r, ` 25 { 26 27 "ipsec_site_connection": { 28 "psk": "secret", 29 "initiator": "bi-directional", 30 "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 31 "admin_state_up": true, 32 "mtu": 1500, 33 "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 34 "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 35 "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 36 "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68", 37 "peer_address": "172.24.4.233", 38 "peer_id": "172.24.4.233", 39 "name": "vpnconnection1" 40 41 } 42 } `) 43 44 w.Header().Add("Content-Type", "application/json") 45 w.WriteHeader(http.StatusCreated) 46 47 _, _ = fmt.Fprint(w, ` 48 { 49 "ipsec_site_connection": { 50 "status": "PENDING_CREATE", 51 "psk": "secret", 52 "initiator": "bi-directional", 53 "name": "vpnconnection1", 54 "admin_state_up": true, 55 "project_id": "10039663455a446d8ba2cbb058b0f578", 56 "tenant_id": "10039663455a446d8ba2cbb058b0f578", 57 "auth_mode": "psk", 58 "peer_cidrs": [], 59 "mtu": 1500, 60 "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 61 "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 62 "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 63 "dpd": { 64 "action": "hold", 65 "interval": 30, 66 "timeout": 120 67 }, 68 "route_mode": "static", 69 "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 70 "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68", 71 "peer_address": "172.24.4.233", 72 "peer_id": "172.24.4.233", 73 "id": "851f280f-5639-4ea3-81aa-e298525ab74b", 74 "description": "" 75 } 76 } 77 `) 78 }) 79 80 options := siteconnections.CreateOpts{ 81 Name: "vpnconnection1", 82 AdminStateUp: golangsdk.Enabled, 83 PSK: "secret", 84 Initiator: siteconnections.InitiatorBiDirectional, 85 IPSecPolicyID: "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 86 MTU: 1500, 87 PeerEPGroupID: "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 88 IKEPolicyID: "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 89 VPNServiceID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 90 LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68", 91 PeerAddress: "172.24.4.233", 92 PeerID: "172.24.4.233", 93 } 94 actual, err := siteconnections.Create(fake.ServiceClient(), options).Extract() 95 th.AssertNoErr(t, err) 96 expectedDPD := siteconnections.DPD{ 97 Action: "hold", 98 Interval: 30, 99 Timeout: 120, 100 } 101 expected := siteconnections.Connection{ 102 TenantID: "10039663455a446d8ba2cbb058b0f578", 103 Name: "vpnconnection1", 104 AdminStateUp: true, 105 PSK: "secret", 106 Initiator: "bi-directional", 107 IPSecPolicyID: "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 108 MTU: 1500, 109 PeerEPGroupID: "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 110 IKEPolicyID: "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 111 VPNServiceID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 112 LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68", 113 PeerAddress: "172.24.4.233", 114 PeerID: "172.24.4.233", 115 Status: "PENDING_CREATE", 116 ProjectID: "10039663455a446d8ba2cbb058b0f578", 117 AuthMode: "psk", 118 PeerCIDRs: []string{}, 119 DPD: expectedDPD, 120 RouteMode: "static", 121 ID: "851f280f-5639-4ea3-81aa-e298525ab74b", 122 Description: "", 123 } 124 th.AssertDeepEquals(t, expected, *actual) 125 } 126 127 func TestDelete(t *testing.T) { 128 th.SetupHTTP() 129 defer th.TeardownHTTP() 130 131 th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 132 th.TestMethod(t, r, "DELETE") 133 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 134 w.WriteHeader(http.StatusNoContent) 135 }) 136 137 res := siteconnections.Delete(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828") 138 th.AssertNoErr(t, res.Err) 139 } 140 141 func TestGet(t *testing.T) { 142 th.SetupHTTP() 143 defer th.TeardownHTTP() 144 145 th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 146 th.TestMethod(t, r, "GET") 147 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 148 149 w.Header().Add("Content-Type", "application/json") 150 w.WriteHeader(http.StatusOK) 151 152 _, _ = fmt.Fprint(w, ` 153 { 154 "ipsec_site_connection": { 155 "status": "PENDING_CREATE", 156 "psk": "secret", 157 "initiator": "bi-directional", 158 "name": "vpnconnection1", 159 "admin_state_up": true, 160 "project_id": "10039663455a446d8ba2cbb058b0f578", 161 "tenant_id": "10039663455a446d8ba2cbb058b0f578", 162 "auth_mode": "psk", 163 "peer_cidrs": [], 164 "mtu": 1500, 165 "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 166 "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 167 "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 168 "dpd": { 169 "action": "hold", 170 "interval": 30, 171 "timeout": 120 172 }, 173 "route_mode": "static", 174 "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 175 "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68", 176 "peer_address": "172.24.4.233", 177 "peer_id": "172.24.4.233", 178 "id": "851f280f-5639-4ea3-81aa-e298525ab74b", 179 "description": "" 180 } 181 } 182 `) 183 }) 184 185 actual, err := siteconnections.Get(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828").Extract() 186 th.AssertNoErr(t, err) 187 expectedDPD := siteconnections.DPD{ 188 Action: "hold", 189 Interval: 30, 190 Timeout: 120, 191 } 192 expected := siteconnections.Connection{ 193 TenantID: "10039663455a446d8ba2cbb058b0f578", 194 Name: "vpnconnection1", 195 AdminStateUp: true, 196 PSK: "secret", 197 Initiator: "bi-directional", 198 IPSecPolicyID: "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 199 MTU: 1500, 200 PeerEPGroupID: "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 201 IKEPolicyID: "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 202 VPNServiceID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 203 LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68", 204 PeerAddress: "172.24.4.233", 205 PeerID: "172.24.4.233", 206 Status: "PENDING_CREATE", 207 ProjectID: "10039663455a446d8ba2cbb058b0f578", 208 AuthMode: "psk", 209 PeerCIDRs: []string{}, 210 DPD: expectedDPD, 211 RouteMode: "static", 212 ID: "851f280f-5639-4ea3-81aa-e298525ab74b", 213 Description: "", 214 } 215 th.AssertDeepEquals(t, expected, *actual) 216 } 217 218 func TestList(t *testing.T) { 219 th.SetupHTTP() 220 defer th.TeardownHTTP() 221 222 th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections", func(w http.ResponseWriter, r *http.Request) { 223 th.TestMethod(t, r, "GET") 224 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 225 226 w.Header().Add("Content-Type", "application/json") 227 w.WriteHeader(http.StatusOK) 228 229 _, _ = fmt.Fprint(w, ` 230 { 231 "ipsec_site_connections":[ 232 { 233 "status": "PENDING_CREATE", 234 "psk": "secret", 235 "initiator": "bi-directional", 236 "name": "vpnconnection1", 237 "admin_state_up": true, 238 "project_id": "10039663455a446d8ba2cbb058b0f578", 239 "tenant_id": "10039663455a446d8ba2cbb058b0f578", 240 "auth_mode": "psk", 241 "peer_cidrs": [], 242 "mtu": 1500, 243 "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 244 "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 245 "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 246 "dpd": { 247 "action": "hold", 248 "interval": 30, 249 "timeout": 120 250 }, 251 "route_mode": "static", 252 "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 253 "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68", 254 "peer_address": "172.24.4.233", 255 "peer_id": "172.24.4.233", 256 "id": "851f280f-5639-4ea3-81aa-e298525ab74b", 257 "description": "" 258 }] 259 } 260 `) 261 }) 262 263 count := 0 264 265 _ = siteconnections.List(fake.ServiceClient(), siteconnections.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 266 count++ 267 actual, err := siteconnections.ExtractConnections(page) 268 if err != nil { 269 t.Errorf("Failed to extract members: %v", err) 270 return false, err 271 } 272 273 expectedDPD := siteconnections.DPD{ 274 Action: "hold", 275 Interval: 30, 276 Timeout: 120, 277 } 278 expected := []siteconnections.Connection{ 279 { 280 TenantID: "10039663455a446d8ba2cbb058b0f578", 281 Name: "vpnconnection1", 282 AdminStateUp: true, 283 PSK: "secret", 284 Initiator: "bi-directional", 285 IPSecPolicyID: "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 286 MTU: 1500, 287 PeerEPGroupID: "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 288 IKEPolicyID: "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 289 VPNServiceID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 290 LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68", 291 PeerAddress: "172.24.4.233", 292 PeerID: "172.24.4.233", 293 Status: "PENDING_CREATE", 294 ProjectID: "10039663455a446d8ba2cbb058b0f578", 295 AuthMode: "psk", 296 PeerCIDRs: []string{}, 297 DPD: expectedDPD, 298 RouteMode: "static", 299 ID: "851f280f-5639-4ea3-81aa-e298525ab74b", 300 Description: "", 301 }, 302 } 303 304 th.CheckDeepEquals(t, expected, actual) 305 306 return true, nil 307 }) 308 309 if count != 1 { 310 t.Errorf("Expected 1 page, got %d", count) 311 } 312 } 313 314 func TestUpdate(t *testing.T) { 315 th.SetupHTTP() 316 defer th.TeardownHTTP() 317 318 th.Mux.HandleFunc("/v2.0/vpn/ipsec-site-connections/5c561d9d-eaea-45f6-ae3e-08d1a7080828", func(w http.ResponseWriter, r *http.Request) { 319 th.TestMethod(t, r, "PUT") 320 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 321 th.TestHeader(t, r, "Content-Type", "application/json") 322 th.TestHeader(t, r, "Accept", "application/json") 323 th.TestJSONRequest(t, r, ` 324 { 325 "ipsec_site_connection": { 326 "psk": "updatedsecret", 327 "initiator": "response-only", 328 "name": "updatedconnection", 329 "description": "updateddescription" 330 } 331 } 332 `) 333 334 w.Header().Add("Content-Type", "application/json") 335 w.WriteHeader(http.StatusOK) 336 337 _, _ = fmt.Fprint(w, ` 338 { 339 "ipsec_site_connection": { 340 "status": "ACTIVE", 341 "psk": "updatedsecret", 342 "initiator": "response-only", 343 "name": "updatedconnection", 344 "admin_state_up": true, 345 "project_id": "10039663455a446d8ba2cbb058b0f578", 346 "tenant_id": "10039663455a446d8ba2cbb058b0f578", 347 "auth_mode": "psk", 348 "peer_cidrs": [], 349 "mtu": 1500, 350 "peer_ep_group_id": "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 351 "ikepolicy_id": "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 352 "vpnservice_id": "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 353 "dpd": { 354 "action": "hold", 355 "interval": 30, 356 "timeout": 120 357 }, 358 "route_mode": "static", 359 "ipsecpolicy_id": "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 360 "local_ep_group_id": "3e1815dd-e212-43d0-8f13-b494fa553e68", 361 "peer_address": "172.24.4.233", 362 "peer_id": "172.24.4.233", 363 "id": "851f280f-5639-4ea3-81aa-e298525ab74b", 364 "description": "updateddescription" 365 } 366 } 367 `) 368 }) 369 updatedName := "updatedconnection" 370 updatedDescription := "updateddescription" 371 options := siteconnections.UpdateOpts{ 372 Name: &updatedName, 373 Description: &updatedDescription, 374 Initiator: siteconnections.InitiatorResponseOnly, 375 PSK: "updatedsecret", 376 } 377 378 actual, err := siteconnections.Update(fake.ServiceClient(), "5c561d9d-eaea-45f6-ae3e-08d1a7080828", options).Extract() 379 th.AssertNoErr(t, err) 380 381 expectedDPD := siteconnections.DPD{ 382 Action: "hold", 383 Interval: 30, 384 Timeout: 120, 385 } 386 387 expected := siteconnections.Connection{ 388 TenantID: "10039663455a446d8ba2cbb058b0f578", 389 Name: "updatedconnection", 390 AdminStateUp: true, 391 PSK: "updatedsecret", 392 Initiator: "response-only", 393 IPSecPolicyID: "e6e23d0c-9519-4d52-8ea4-5b1f96d857b1", 394 MTU: 1500, 395 PeerEPGroupID: "9ad5a7e0-6dac-41b4-b20d-a7b8645fddf1", 396 IKEPolicyID: "9b00d6b0-6c93-4ca5-9747-b8ade7bb514f", 397 VPNServiceID: "5c561d9d-eaea-45f6-ae3e-08d1a7080828", 398 LocalEPGroupID: "3e1815dd-e212-43d0-8f13-b494fa553e68", 399 PeerAddress: "172.24.4.233", 400 PeerID: "172.24.4.233", 401 Status: "ACTIVE", 402 ProjectID: "10039663455a446d8ba2cbb058b0f578", 403 AuthMode: "psk", 404 PeerCIDRs: []string{}, 405 DPD: expectedDPD, 406 RouteMode: "static", 407 ID: "851f280f-5639-4ea3-81aa-e298525ab74b", 408 Description: "updateddescription", 409 } 410 th.AssertDeepEquals(t, expected, *actual) 411 }