github.com/gophercloud/gophercloud@v1.14.1/openstack/networking/v2/extensions/bgpvpns/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/bgpvpns" 10 "github.com/gophercloud/gophercloud/pagination" 11 th "github.com/gophercloud/gophercloud/testhelper" 12 ) 13 14 func TestList(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 filterProjectID := []string{"b7549121395844bea941bb92feb3fad9"} 19 fields := []string{"id", "name"} 20 listOpts := bgpvpns.ListOpts{ 21 Fields: fields, 22 ProjectID: filterProjectID[0], 23 } 24 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns", 25 func(w http.ResponseWriter, r *http.Request) { 26 th.TestMethod(t, r, "GET") 27 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 28 29 r.ParseForm() 30 th.AssertDeepEquals(t, r.Form["fields"], fields) 31 th.AssertDeepEquals(t, r.Form["project_id"], filterProjectID) 32 33 w.Header().Add("Content-Type", "application/json") 34 w.WriteHeader(http.StatusOK) 35 fmt.Fprintf(w, ListBGPVPNsResult) 36 }) 37 count := 0 38 39 err := bgpvpns.List(fake.ServiceClient(), listOpts).EachPage( 40 func(page pagination.Page) (bool, error) { 41 count++ 42 actual, err := bgpvpns.ExtractBGPVPNs(page) 43 if err != nil { 44 t.Errorf("Failed to extract BGP VPNs: %v", err) 45 return false, nil 46 } 47 48 expected := []bgpvpns.BGPVPN{BGPVPN} 49 th.CheckDeepEquals(t, expected, actual) 50 51 return true, nil 52 }) 53 th.AssertNoErr(t, err) 54 } 55 56 func TestGet(t *testing.T) { 57 th.SetupHTTP() 58 defer th.TeardownHTTP() 59 60 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 61 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID, func(w http.ResponseWriter, r *http.Request) { 62 th.TestMethod(t, r, "GET") 63 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 64 w.Header().Add("Content-Type", "application/json") 65 w.WriteHeader(http.StatusOK) 66 fmt.Fprintf(w, GetBGPVPNResult) 67 }) 68 69 r, err := bgpvpns.Get(fake.ServiceClient(), bgpVpnID).Extract() 70 th.AssertNoErr(t, err) 71 th.CheckDeepEquals(t, GetBGPVPN, *r) 72 } 73 74 func TestCreate(t *testing.T) { 75 th.SetupHTTP() 76 defer th.TeardownHTTP() 77 78 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns", func(w http.ResponseWriter, r *http.Request) { 79 th.TestMethod(t, r, "POST") 80 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 81 th.TestHeader(t, r, "Content-Type", "application/json") 82 th.TestHeader(t, r, "Accept", "application/json") 83 th.TestJSONRequest(t, r, CreateRequest) 84 w.Header().Add("Content-Type", "application/json") 85 w.WriteHeader(http.StatusCreated) 86 fmt.Fprintf(w, CreateResponse) 87 }) 88 89 opts := bgpvpns.CreateOpts{ 90 TenantID: "b7549121395844bea941bb92feb3fad9", 91 RouteTargets: []string{ 92 "64512:1444", 93 }, 94 ImportTargets: []string{ 95 "64512:1555", 96 }, 97 ExportTargets: []string{ 98 "64512:1666", 99 }, 100 RouteDistinguishers: []string{ 101 "64512:1777", 102 "64512:1888", 103 "64512:1999", 104 }, 105 Type: "l3", 106 VNI: 1000, 107 } 108 109 r, err := bgpvpns.Create(fake.ServiceClient(), opts).Extract() 110 th.AssertNoErr(t, err) 111 th.AssertDeepEquals(t, CreateBGPVPN, *r) 112 } 113 114 func TestDelete(t *testing.T) { 115 bgpVpnID := "0f9d472a-908f-40f5-8574-b4e8a63ccbf0" 116 th.SetupHTTP() 117 defer th.TeardownHTTP() 118 119 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID, func(w http.ResponseWriter, r *http.Request) { 120 th.TestMethod(t, r, "DELETE") 121 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 122 th.TestHeader(t, r, "Accept", "application/json") 123 124 w.Header().Add("Content-Type", "application/json") 125 w.WriteHeader(http.StatusNoContent) 126 }) 127 128 err := bgpvpns.Delete(fake.ServiceClient(), bgpVpnID).ExtractErr() 129 th.AssertNoErr(t, err) 130 } 131 132 func TestUpdate(t *testing.T) { 133 bgpVpnID := "4d627abf-06dd-45ab-920b-8e61422bb984" 134 th.SetupHTTP() 135 defer th.TeardownHTTP() 136 137 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID, func(w http.ResponseWriter, r *http.Request) { 138 th.TestMethod(t, r, "PUT") 139 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 140 th.TestHeader(t, r, "Content-Type", "application/json") 141 th.TestHeader(t, r, "Accept", "application/json") 142 th.TestJSONRequest(t, r, UpdateBGPVPNRequest) 143 144 w.Header().Add("Content-Type", "application/json") 145 w.WriteHeader(http.StatusOK) 146 147 fmt.Fprintf(w, UpdateBGPVPNResponse) 148 }) 149 150 name := "foo" 151 routeTargets := []string{"64512:1444"} 152 emptyTarget := []string{} 153 opts := bgpvpns.UpdateOpts{ 154 Name: &name, 155 RouteTargets: &routeTargets, 156 ImportTargets: &emptyTarget, 157 ExportTargets: &emptyTarget, 158 } 159 160 r, err := bgpvpns.Update(fake.ServiceClient(), bgpVpnID, opts).Extract() 161 th.AssertNoErr(t, err) 162 th.AssertDeepEquals(t, *opts.Name, r.Name) 163 } 164 165 func TestListNetworkAssociations(t *testing.T) { 166 th.SetupHTTP() 167 defer th.TeardownHTTP() 168 169 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 170 fields := []string{"id", "name"} 171 listOpts := bgpvpns.ListNetworkAssociationsOpts{ 172 Fields: fields, 173 } 174 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/network_associations", func(w http.ResponseWriter, r *http.Request) { 175 th.TestMethod(t, r, "GET") 176 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 177 178 r.ParseForm() 179 th.AssertDeepEquals(t, fields, r.Form["fields"]) 180 181 w.Header().Add("Content-Type", "application/json") 182 w.WriteHeader(http.StatusOK) 183 fmt.Fprintf(w, ListNetworkAssociationsResult) 184 }) 185 186 count := 0 187 err := bgpvpns.ListNetworkAssociations(fake.ServiceClient(), bgpVpnID, listOpts).EachPage( 188 func(page pagination.Page) (bool, error) { 189 count++ 190 actual, err := bgpvpns.ExtractNetworkAssociations(page) 191 if err != nil { 192 t.Errorf("Failed to extract network associations: %v", err) 193 return false, nil 194 } 195 196 expected := []bgpvpns.NetworkAssociation{NetworkAssociation} 197 th.CheckDeepEquals(t, expected, actual) 198 199 return true, nil 200 }) 201 202 th.AssertNoErr(t, err) 203 th.AssertEquals(t, 1, count) 204 } 205 206 func TestCreateNetworkAssociation(t *testing.T) { 207 th.SetupHTTP() 208 defer th.TeardownHTTP() 209 210 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 211 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/network_associations", func(w http.ResponseWriter, r *http.Request) { 212 th.TestMethod(t, r, "POST") 213 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 214 th.TestHeader(t, r, "Content-Type", "application/json") 215 th.TestHeader(t, r, "Accept", "application/json") 216 th.TestJSONRequest(t, r, CreateNetworkAssociationRequest) 217 w.Header().Add("Content-Type", "application/json") 218 w.WriteHeader(http.StatusCreated) 219 fmt.Fprintf(w, CreateNetworkAssociationResponse) 220 }) 221 222 opts := bgpvpns.CreateNetworkAssociationOpts{ 223 NetworkID: "8c5d88dc-60ac-4b02-a65a-36b65888ddcd", 224 } 225 r, err := bgpvpns.CreateNetworkAssociation(fake.ServiceClient(), bgpVpnID, opts).Extract() 226 th.AssertNoErr(t, err) 227 th.AssertDeepEquals(t, CreateNetworkAssociation, *r) 228 } 229 230 func TestGetNetworkAssociation(t *testing.T) { 231 th.SetupHTTP() 232 defer th.TeardownHTTP() 233 234 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 235 networkAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 236 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/network_associations/"+networkAssociationID, func(w http.ResponseWriter, r *http.Request) { 237 th.TestMethod(t, r, "GET") 238 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 239 w.Header().Add("Content-Type", "application/json") 240 w.WriteHeader(http.StatusOK) 241 fmt.Fprintf(w, GetNetworkAssociationResult) 242 }) 243 244 r, err := bgpvpns.GetNetworkAssociation(fake.ServiceClient(), bgpVpnID, networkAssociationID).Extract() 245 th.AssertNoErr(t, err) 246 th.CheckDeepEquals(t, GetNetworkAssociation, *r) 247 } 248 249 func TestDeleteNetworkAssociation(t *testing.T) { 250 th.SetupHTTP() 251 defer th.TeardownHTTP() 252 253 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 254 networkAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 255 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/network_associations/"+networkAssociationID, func(w http.ResponseWriter, r *http.Request) { 256 th.TestMethod(t, r, "DELETE") 257 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 258 w.WriteHeader(http.StatusNoContent) 259 }) 260 261 err := bgpvpns.DeleteNetworkAssociation(fake.ServiceClient(), bgpVpnID, networkAssociationID).ExtractErr() 262 th.AssertNoErr(t, err) 263 } 264 265 func TestListRouterAssociations(t *testing.T) { 266 th.SetupHTTP() 267 defer th.TeardownHTTP() 268 269 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 270 fields := []string{"id", "name"} 271 listOpts := bgpvpns.ListRouterAssociationsOpts{ 272 Fields: fields, 273 } 274 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/router_associations", func(w http.ResponseWriter, r *http.Request) { 275 th.TestMethod(t, r, "GET") 276 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 277 278 r.ParseForm() 279 th.AssertDeepEquals(t, fields, r.Form["fields"]) 280 281 w.Header().Add("Content-Type", "application/json") 282 w.WriteHeader(http.StatusOK) 283 fmt.Fprintf(w, ListRouterAssociationsResult) 284 }) 285 286 count := 0 287 err := bgpvpns.ListRouterAssociations(fake.ServiceClient(), bgpVpnID, listOpts).EachPage( 288 func(page pagination.Page) (bool, error) { 289 count++ 290 actual, err := bgpvpns.ExtractRouterAssociations(page) 291 if err != nil { 292 t.Errorf("Failed to extract router associations: %v", err) 293 return false, nil 294 } 295 296 expected := []bgpvpns.RouterAssociation{RouterAssociation} 297 th.CheckDeepEquals(t, expected, actual) 298 299 return true, nil 300 }) 301 302 th.AssertNoErr(t, err) 303 th.AssertEquals(t, 1, count) 304 } 305 306 func TestCreateRouterAssociation(t *testing.T) { 307 th.SetupHTTP() 308 defer th.TeardownHTTP() 309 310 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 311 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/router_associations", func(w http.ResponseWriter, r *http.Request) { 312 th.TestMethod(t, r, "POST") 313 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 314 th.TestHeader(t, r, "Content-Type", "application/json") 315 th.TestHeader(t, r, "Accept", "application/json") 316 th.TestJSONRequest(t, r, CreateRouterAssociationRequest) 317 w.Header().Add("Content-Type", "application/json") 318 w.WriteHeader(http.StatusCreated) 319 fmt.Fprintf(w, CreateRouterAssociationResponse) 320 }) 321 322 opts := bgpvpns.CreateRouterAssociationOpts{ 323 RouterID: "8c5d88dc-60ac-4b02-a65a-36b65888ddcd", 324 } 325 r, err := bgpvpns.CreateRouterAssociation(fake.ServiceClient(), bgpVpnID, opts).Extract() 326 th.AssertNoErr(t, err) 327 th.AssertDeepEquals(t, CreateRouterAssociation, *r) 328 } 329 330 func TestGetRouterAssociation(t *testing.T) { 331 th.SetupHTTP() 332 defer th.TeardownHTTP() 333 334 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 335 routerAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 336 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/router_associations/"+routerAssociationID, func(w http.ResponseWriter, r *http.Request) { 337 th.TestMethod(t, r, "GET") 338 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 339 w.Header().Add("Content-Type", "application/json") 340 w.WriteHeader(http.StatusOK) 341 fmt.Fprintf(w, GetRouterAssociationResult) 342 }) 343 344 r, err := bgpvpns.GetRouterAssociation(fake.ServiceClient(), bgpVpnID, routerAssociationID).Extract() 345 th.AssertNoErr(t, err) 346 th.CheckDeepEquals(t, GetRouterAssociation, *r) 347 } 348 349 func TestUpdateRouterAssociation(t *testing.T) { 350 bgpVpnID := "4d627abf-06dd-45ab-920b-8e61422bb984" 351 routerAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 352 th.SetupHTTP() 353 defer th.TeardownHTTP() 354 355 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/router_associations/"+routerAssociationID, func(w http.ResponseWriter, r *http.Request) { 356 th.TestMethod(t, r, "PUT") 357 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 358 th.TestHeader(t, r, "Content-Type", "application/json") 359 th.TestHeader(t, r, "Accept", "application/json") 360 th.TestJSONRequest(t, r, UpdateRouterAssociationRequest) 361 w.Header().Add("Content-Type", "application/json") 362 w.WriteHeader(http.StatusOK) 363 fmt.Fprintf(w, UpdateRouterAssociationResponse) 364 }) 365 366 opts := bgpvpns.UpdateRouterAssociationOpts{ 367 AdvertiseExtraRoutes: new(bool), 368 } 369 r, err := bgpvpns.UpdateRouterAssociation(fake.ServiceClient(), bgpVpnID, routerAssociationID, opts).Extract() 370 th.AssertNoErr(t, err) 371 th.AssertDeepEquals(t, UpdateRouterAssociation, *r) 372 } 373 374 func TestDeleteRouterAssociation(t *testing.T) { 375 th.SetupHTTP() 376 defer th.TeardownHTTP() 377 378 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 379 routerAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 380 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/router_associations/"+routerAssociationID, func(w http.ResponseWriter, r *http.Request) { 381 th.TestMethod(t, r, "DELETE") 382 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 383 w.WriteHeader(http.StatusNoContent) 384 }) 385 386 err := bgpvpns.DeleteRouterAssociation(fake.ServiceClient(), bgpVpnID, routerAssociationID).ExtractErr() 387 th.AssertNoErr(t, err) 388 } 389 390 func TestListPortAssociations(t *testing.T) { 391 th.SetupHTTP() 392 defer th.TeardownHTTP() 393 394 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 395 fields := []string{"id", "name"} 396 listOpts := bgpvpns.ListPortAssociationsOpts{ 397 Fields: fields, 398 } 399 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/port_associations", func(w http.ResponseWriter, r *http.Request) { 400 th.TestMethod(t, r, "GET") 401 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 402 403 r.ParseForm() 404 th.AssertDeepEquals(t, fields, r.Form["fields"]) 405 406 w.Header().Add("Content-Type", "application/json") 407 w.WriteHeader(http.StatusOK) 408 fmt.Fprintf(w, ListPortAssociationsResult) 409 }) 410 411 count := 0 412 err := bgpvpns.ListPortAssociations(fake.ServiceClient(), bgpVpnID, listOpts).EachPage( 413 func(page pagination.Page) (bool, error) { 414 count++ 415 actual, err := bgpvpns.ExtractPortAssociations(page) 416 if err != nil { 417 t.Errorf("Failed to extract port associations: %v", err) 418 return false, nil 419 } 420 421 expected := []bgpvpns.PortAssociation{PortAssociation} 422 th.CheckDeepEquals(t, expected, actual) 423 424 return true, nil 425 }) 426 427 th.AssertNoErr(t, err) 428 th.AssertEquals(t, 1, count) 429 } 430 431 func TestCreatePortAssociation(t *testing.T) { 432 th.SetupHTTP() 433 defer th.TeardownHTTP() 434 435 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 436 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/port_associations", func(w http.ResponseWriter, r *http.Request) { 437 th.TestMethod(t, r, "POST") 438 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 439 th.TestHeader(t, r, "Content-Type", "application/json") 440 th.TestHeader(t, r, "Accept", "application/json") 441 th.TestJSONRequest(t, r, CreatePortAssociationRequest) 442 w.Header().Add("Content-Type", "application/json") 443 w.WriteHeader(http.StatusCreated) 444 fmt.Fprintf(w, CreatePortAssociationResponse) 445 }) 446 447 opts := bgpvpns.CreatePortAssociationOpts{ 448 PortID: "8c5d88dc-60ac-4b02-a65a-36b65888ddcd", 449 } 450 r, err := bgpvpns.CreatePortAssociation(fake.ServiceClient(), bgpVpnID, opts).Extract() 451 th.AssertNoErr(t, err) 452 th.AssertDeepEquals(t, CreatePortAssociation, *r) 453 } 454 455 func TestGetPortAssociation(t *testing.T) { 456 th.SetupHTTP() 457 defer th.TeardownHTTP() 458 459 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 460 portAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 461 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/port_associations/"+portAssociationID, func(w http.ResponseWriter, r *http.Request) { 462 th.TestMethod(t, r, "GET") 463 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 464 w.Header().Add("Content-Type", "application/json") 465 w.WriteHeader(http.StatusOK) 466 fmt.Fprintf(w, GetPortAssociationResult) 467 }) 468 469 r, err := bgpvpns.GetPortAssociation(fake.ServiceClient(), bgpVpnID, portAssociationID).Extract() 470 th.AssertNoErr(t, err) 471 th.CheckDeepEquals(t, GetPortAssociation, *r) 472 } 473 474 func TestUpdatePortAssociation(t *testing.T) { 475 bgpVpnID := "4d627abf-06dd-45ab-920b-8e61422bb984" 476 portAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 477 th.SetupHTTP() 478 defer th.TeardownHTTP() 479 480 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/port_associations/"+portAssociationID, func(w http.ResponseWriter, r *http.Request) { 481 th.TestMethod(t, r, "PUT") 482 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 483 th.TestHeader(t, r, "Content-Type", "application/json") 484 th.TestHeader(t, r, "Accept", "application/json") 485 th.TestJSONRequest(t, r, UpdatePortAssociationRequest) 486 w.Header().Add("Content-Type", "application/json") 487 w.WriteHeader(http.StatusOK) 488 fmt.Fprintf(w, UpdatePortAssociationResponse) 489 }) 490 491 opts := bgpvpns.UpdatePortAssociationOpts{ 492 AdvertiseFixedIPs: new(bool), 493 } 494 r, err := bgpvpns.UpdatePortAssociation(fake.ServiceClient(), bgpVpnID, portAssociationID, opts).Extract() 495 th.AssertNoErr(t, err) 496 th.AssertDeepEquals(t, UpdatePortAssociation, *r) 497 } 498 499 func TestDeletePortAssociation(t *testing.T) { 500 th.SetupHTTP() 501 defer th.TeardownHTTP() 502 503 bgpVpnID := "460ac411-3dfb-45bb-8116-ed1a7233d143" 504 portAssociationID := "73238ca1-e05d-4c7a-b4d4-70407b4b8730" 505 th.Mux.HandleFunc("/v2.0/bgpvpn/bgpvpns/"+bgpVpnID+"/port_associations/"+portAssociationID, func(w http.ResponseWriter, r *http.Request) { 506 th.TestMethod(t, r, "DELETE") 507 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 508 w.WriteHeader(http.StatusNoContent) 509 }) 510 511 err := bgpvpns.DeletePortAssociation(fake.ServiceClient(), bgpVpnID, portAssociationID).ExtractErr() 512 th.AssertNoErr(t, err) 513 }