github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/ports/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/common" 9 "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/extradhcpopts" 10 "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/extensions/portsecurity" 11 "github.com/opentelekomcloud/gophertelekomcloud/openstack/networking/v2/ports" 12 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 13 th "github.com/opentelekomcloud/gophertelekomcloud/testhelper" 14 ) 15 16 func TestList(t *testing.T) { 17 th.SetupHTTP() 18 defer th.TeardownHTTP() 19 20 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 21 th.TestMethod(t, r, "GET") 22 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 23 24 w.Header().Add("Content-Type", "application/json") 25 w.WriteHeader(http.StatusOK) 26 27 _, _ = fmt.Fprint(w, ListResponse) 28 }) 29 30 count := 0 31 32 _ = ports.List(fake.ServiceClient(), ports.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 33 count++ 34 actual, err := ports.ExtractPorts(page) 35 if err != nil { 36 t.Errorf("Failed to extract subnets: %v", err) 37 return false, nil 38 } 39 40 expected := []ports.Port{ 41 { 42 Status: "ACTIVE", 43 Name: "", 44 AdminStateUp: true, 45 NetworkID: "70c1db1f-b701-45bd-96e0-a313ee3430b3", 46 TenantID: "", 47 DeviceOwner: "network:router_gateway", 48 MACAddress: "fa:16:3e:58:42:ed", 49 FixedIPs: []ports.IP{ 50 { 51 SubnetID: "008ba151-0b8c-4a67-98b5-0d2b87666062", 52 IPAddress: "172.24.4.2", 53 }, 54 }, 55 ID: "d80b1a3b-4fc1-49f3-952e-1e2ab7081d8b", 56 SecurityGroups: []string{}, 57 DeviceID: "9ae135f4-b6e0-4dad-9e91-3c223e385824", 58 }, 59 } 60 61 th.CheckDeepEquals(t, expected, actual) 62 63 return true, nil 64 }) 65 66 if count != 1 { 67 t.Errorf("Expected 1 page, got %d", count) 68 } 69 } 70 71 func TestListWithExtensions(t *testing.T) { 72 th.SetupHTTP() 73 defer th.TeardownHTTP() 74 75 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 76 th.TestMethod(t, r, "GET") 77 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 78 79 w.Header().Add("Content-Type", "application/json") 80 w.WriteHeader(http.StatusOK) 81 82 _, _ = fmt.Fprint(w, ListResponse) 83 }) 84 85 type portWithExt struct { 86 ports.Port 87 portsecurity.PortSecurityExt 88 } 89 90 var allPorts []portWithExt 91 92 allPages, err := ports.List(fake.ServiceClient(), ports.ListOpts{}).AllPages() 93 th.AssertNoErr(t, err) 94 95 err = ports.ExtractPortsInto(allPages, &allPorts) 96 th.AssertNoErr(t, err) 97 98 th.AssertEquals(t, allPorts[0].Status, "ACTIVE") 99 th.AssertEquals(t, allPorts[0].PortSecurityEnabled, false) 100 } 101 102 func TestGet(t *testing.T) { 103 th.SetupHTTP() 104 defer th.TeardownHTTP() 105 106 th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) { 107 th.TestMethod(t, r, "GET") 108 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 109 110 w.Header().Add("Content-Type", "application/json") 111 w.WriteHeader(http.StatusOK) 112 113 _, _ = fmt.Fprint(w, GetResponse) 114 }) 115 116 n, err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").Extract() 117 th.AssertNoErr(t, err) 118 119 th.AssertEquals(t, n.Status, "ACTIVE") 120 th.AssertEquals(t, n.Name, "") 121 th.AssertEquals(t, n.AdminStateUp, true) 122 th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 123 th.AssertEquals(t, n.TenantID, "7e02058126cc4950b75f9970368ba177") 124 th.AssertEquals(t, n.DeviceOwner, "network:router_interface") 125 th.AssertEquals(t, n.MACAddress, "fa:16:3e:23:fd:d7") 126 th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{ 127 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.1"}, 128 }) 129 th.AssertEquals(t, n.ID, "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2") 130 th.AssertDeepEquals(t, n.SecurityGroups, []string{}) 131 th.AssertEquals(t, n.Status, "ACTIVE") 132 th.AssertEquals(t, n.DeviceID, "5e3898d7-11be-483e-9732-b2f5eccd2b2e") 133 } 134 135 func TestGetWithExtensions(t *testing.T) { 136 th.SetupHTTP() 137 defer th.TeardownHTTP() 138 139 th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) { 140 th.TestMethod(t, r, "GET") 141 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 142 143 w.Header().Add("Content-Type", "application/json") 144 w.WriteHeader(http.StatusOK) 145 146 _, _ = fmt.Fprint(w, GetResponse) 147 }) 148 149 var portWithExtensions struct { 150 ports.Port 151 portsecurity.PortSecurityExt 152 } 153 154 err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").ExtractInto(&portWithExtensions) 155 th.AssertNoErr(t, err) 156 157 th.AssertEquals(t, portWithExtensions.Status, "ACTIVE") 158 th.AssertEquals(t, portWithExtensions.PortSecurityEnabled, false) 159 } 160 161 func TestCreate(t *testing.T) { 162 th.SetupHTTP() 163 defer th.TeardownHTTP() 164 165 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 166 th.TestMethod(t, r, "POST") 167 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 168 th.TestHeader(t, r, "Content-Type", "application/json") 169 th.TestHeader(t, r, "Accept", "application/json") 170 th.TestJSONRequest(t, r, CreateRequest) 171 172 w.Header().Add("Content-Type", "application/json") 173 w.WriteHeader(http.StatusCreated) 174 175 _, _ = fmt.Fprint(w, CreateResponse) 176 }) 177 178 asu := true 179 options := ports.CreateOpts{ 180 Name: "private-port", 181 AdminStateUp: &asu, 182 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 183 FixedIPs: []ports.IP{ 184 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 185 }, 186 SecurityGroups: &[]string{"foo"}, 187 AllowedAddressPairs: []ports.AddressPair{ 188 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 189 }, 190 } 191 n, err := ports.Create(fake.ServiceClient(), options).Extract() 192 th.AssertNoErr(t, err) 193 194 th.AssertEquals(t, n.Status, "DOWN") 195 th.AssertEquals(t, n.Name, "private-port") 196 th.AssertEquals(t, n.AdminStateUp, true) 197 th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 198 th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 199 th.AssertEquals(t, n.DeviceOwner, "") 200 th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0") 201 th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{ 202 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 203 }) 204 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 205 th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 206 th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{ 207 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 208 }) 209 } 210 211 func TestCreateOmitSecurityGroups(t *testing.T) { 212 th.SetupHTTP() 213 defer th.TeardownHTTP() 214 215 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 216 th.TestMethod(t, r, "POST") 217 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 218 th.TestHeader(t, r, "Content-Type", "application/json") 219 th.TestHeader(t, r, "Accept", "application/json") 220 th.TestJSONRequest(t, r, CreateOmitSecurityGroupsRequest) 221 222 w.Header().Add("Content-Type", "application/json") 223 w.WriteHeader(http.StatusCreated) 224 225 _, _ = fmt.Fprint(w, CreateOmitSecurityGroupsResponse) 226 }) 227 228 asu := true 229 options := ports.CreateOpts{ 230 Name: "private-port", 231 AdminStateUp: &asu, 232 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 233 FixedIPs: []ports.IP{ 234 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 235 }, 236 AllowedAddressPairs: []ports.AddressPair{ 237 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 238 }, 239 } 240 n, err := ports.Create(fake.ServiceClient(), options).Extract() 241 th.AssertNoErr(t, err) 242 243 th.AssertEquals(t, n.Status, "DOWN") 244 th.AssertEquals(t, n.Name, "private-port") 245 th.AssertEquals(t, n.AdminStateUp, true) 246 th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 247 th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 248 th.AssertEquals(t, n.DeviceOwner, "") 249 th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0") 250 th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{ 251 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 252 }) 253 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 254 th.AssertDeepEquals(t, n.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 255 th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{ 256 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 257 }) 258 } 259 260 func TestCreateWithNoSecurityGroup(t *testing.T) { 261 th.SetupHTTP() 262 defer th.TeardownHTTP() 263 264 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 265 th.TestMethod(t, r, "POST") 266 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 267 th.TestHeader(t, r, "Content-Type", "application/json") 268 th.TestHeader(t, r, "Accept", "application/json") 269 th.TestJSONRequest(t, r, CreateWithNoSecurityGroupsRequest) 270 271 w.Header().Add("Content-Type", "application/json") 272 w.WriteHeader(http.StatusCreated) 273 274 _, _ = fmt.Fprint(w, CreateWithNoSecurityGroupsResponse) 275 }) 276 277 asu := true 278 options := ports.CreateOpts{ 279 Name: "private-port", 280 AdminStateUp: &asu, 281 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 282 FixedIPs: []ports.IP{ 283 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 284 }, 285 SecurityGroups: &[]string{}, 286 AllowedAddressPairs: []ports.AddressPair{ 287 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 288 }, 289 } 290 n, err := ports.Create(fake.ServiceClient(), options).Extract() 291 th.AssertNoErr(t, err) 292 293 th.AssertEquals(t, n.Status, "DOWN") 294 th.AssertEquals(t, n.Name, "private-port") 295 th.AssertEquals(t, n.AdminStateUp, true) 296 th.AssertEquals(t, n.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 297 th.AssertEquals(t, n.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 298 th.AssertEquals(t, n.DeviceOwner, "") 299 th.AssertEquals(t, n.MACAddress, "fa:16:3e:c9:cb:f0") 300 th.AssertDeepEquals(t, n.FixedIPs, []ports.IP{ 301 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 302 }) 303 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 304 th.AssertDeepEquals(t, n.AllowedAddressPairs, []ports.AddressPair{ 305 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 306 }) 307 } 308 309 func TestRequiredCreateOpts(t *testing.T) { 310 res := ports.Create(fake.ServiceClient(), ports.CreateOpts{}) 311 if res.Err == nil { 312 t.Fatalf("Expected error, got none") 313 } 314 } 315 316 func TestCreatePortSecurity(t *testing.T) { 317 th.SetupHTTP() 318 defer th.TeardownHTTP() 319 320 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 321 th.TestMethod(t, r, "POST") 322 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 323 th.TestHeader(t, r, "Content-Type", "application/json") 324 th.TestHeader(t, r, "Accept", "application/json") 325 th.TestJSONRequest(t, r, CreatePortSecurityRequest) 326 327 w.Header().Add("Content-Type", "application/json") 328 w.WriteHeader(http.StatusCreated) 329 330 _, _ = fmt.Fprint(w, CreatePortSecurityResponse) 331 }) 332 333 var portWithExt struct { 334 ports.Port 335 portsecurity.PortSecurityExt 336 } 337 338 asu := true 339 iFalse := false 340 portCreateOpts := ports.CreateOpts{ 341 Name: "private-port", 342 AdminStateUp: &asu, 343 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 344 FixedIPs: []ports.IP{ 345 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 346 }, 347 SecurityGroups: &[]string{"foo"}, 348 AllowedAddressPairs: []ports.AddressPair{ 349 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 350 }, 351 } 352 createOpts := portsecurity.PortCreateOptsExt{ 353 CreateOptsBuilder: portCreateOpts, 354 PortSecurityEnabled: &iFalse, 355 } 356 357 err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&portWithExt) 358 th.AssertNoErr(t, err) 359 360 th.AssertEquals(t, portWithExt.Status, "DOWN") 361 th.AssertEquals(t, portWithExt.PortSecurityEnabled, false) 362 } 363 364 func TestUpdate(t *testing.T) { 365 th.SetupHTTP() 366 defer th.TeardownHTTP() 367 368 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 369 th.TestMethod(t, r, "PUT") 370 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 371 th.TestHeader(t, r, "Content-Type", "application/json") 372 th.TestHeader(t, r, "Accept", "application/json") 373 th.TestJSONRequest(t, r, UpdateRequest) 374 375 w.Header().Add("Content-Type", "application/json") 376 w.WriteHeader(http.StatusOK) 377 378 _, _ = fmt.Fprint(w, UpdateResponse) 379 }) 380 381 options := ports.UpdateOpts{ 382 Name: "new_port_name", 383 FixedIPs: []ports.IP{ 384 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 385 }, 386 SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}, 387 AllowedAddressPairs: &[]ports.AddressPair{ 388 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 389 }, 390 } 391 392 s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract() 393 th.AssertNoErr(t, err) 394 395 th.AssertEquals(t, s.Name, "new_port_name") 396 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 397 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 398 }) 399 th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{ 400 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 401 }) 402 th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 403 } 404 405 func TestUpdateOmitSecurityGroups(t *testing.T) { 406 th.SetupHTTP() 407 defer th.TeardownHTTP() 408 409 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 410 th.TestMethod(t, r, "PUT") 411 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 412 th.TestHeader(t, r, "Content-Type", "application/json") 413 th.TestHeader(t, r, "Accept", "application/json") 414 th.TestJSONRequest(t, r, UpdateOmitSecurityGroupsRequest) 415 416 w.Header().Add("Content-Type", "application/json") 417 w.WriteHeader(http.StatusOK) 418 419 _, _ = fmt.Fprint(w, UpdateOmitSecurityGroupsResponse) 420 }) 421 422 options := ports.UpdateOpts{ 423 Name: "new_port_name", 424 FixedIPs: []ports.IP{ 425 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 426 }, 427 AllowedAddressPairs: &[]ports.AddressPair{ 428 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 429 }, 430 } 431 432 s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract() 433 th.AssertNoErr(t, err) 434 435 th.AssertEquals(t, s.Name, "new_port_name") 436 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 437 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 438 }) 439 th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{ 440 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 441 }) 442 th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 443 } 444 445 func TestUpdatePortSecurity(t *testing.T) { 446 th.SetupHTTP() 447 defer th.TeardownHTTP() 448 449 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 450 th.TestMethod(t, r, "PUT") 451 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 452 th.TestHeader(t, r, "Content-Type", "application/json") 453 th.TestHeader(t, r, "Accept", "application/json") 454 th.TestJSONRequest(t, r, UpdatePortSecurityRequest) 455 456 w.Header().Add("Content-Type", "application/json") 457 w.WriteHeader(http.StatusOK) 458 459 _, _ = fmt.Fprint(w, UpdatePortSecurityResponse) 460 }) 461 462 var portWithExt struct { 463 ports.Port 464 portsecurity.PortSecurityExt 465 } 466 467 iFalse := false 468 portUpdateOpts := ports.UpdateOpts{} 469 updateOpts := portsecurity.PortUpdateOptsExt{ 470 UpdateOptsBuilder: portUpdateOpts, 471 PortSecurityEnabled: &iFalse, 472 } 473 474 err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&portWithExt) 475 th.AssertNoErr(t, err) 476 477 th.AssertEquals(t, portWithExt.Status, "DOWN") 478 th.AssertEquals(t, portWithExt.Name, "private-port") 479 th.AssertEquals(t, portWithExt.PortSecurityEnabled, false) 480 } 481 482 func TestRemoveSecurityGroups(t *testing.T) { 483 th.SetupHTTP() 484 defer th.TeardownHTTP() 485 486 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 487 th.TestMethod(t, r, "PUT") 488 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 489 th.TestHeader(t, r, "Content-Type", "application/json") 490 th.TestHeader(t, r, "Accept", "application/json") 491 th.TestJSONRequest(t, r, RemoveSecurityGroupRequest) 492 493 w.Header().Add("Content-Type", "application/json") 494 w.WriteHeader(http.StatusOK) 495 496 _, _ = fmt.Fprint(w, RemoveSecurityGroupResponse) 497 }) 498 499 options := ports.UpdateOpts{ 500 Name: "new_port_name", 501 FixedIPs: []ports.IP{ 502 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 503 }, 504 SecurityGroups: &[]string{}, 505 AllowedAddressPairs: &[]ports.AddressPair{ 506 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 507 }, 508 } 509 510 s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract() 511 th.AssertNoErr(t, err) 512 513 th.AssertEquals(t, s.Name, "new_port_name") 514 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 515 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 516 }) 517 th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{ 518 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 519 }) 520 th.AssertDeepEquals(t, s.SecurityGroups, []string(nil)) 521 } 522 523 func TestRemoveAllowedAddressPairs(t *testing.T) { 524 th.SetupHTTP() 525 defer th.TeardownHTTP() 526 527 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 528 th.TestMethod(t, r, "PUT") 529 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 530 th.TestHeader(t, r, "Content-Type", "application/json") 531 th.TestHeader(t, r, "Accept", "application/json") 532 th.TestJSONRequest(t, r, RemoveAllowedAddressPairsRequest) 533 534 w.Header().Add("Content-Type", "application/json") 535 w.WriteHeader(http.StatusOK) 536 537 _, _ = fmt.Fprint(w, RemoveAllowedAddressPairsResponse) 538 }) 539 540 options := ports.UpdateOpts{ 541 Name: "new_port_name", 542 FixedIPs: []ports.IP{ 543 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 544 }, 545 SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}, 546 AllowedAddressPairs: &[]ports.AddressPair{}, 547 } 548 549 s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract() 550 th.AssertNoErr(t, err) 551 552 th.AssertEquals(t, s.Name, "new_port_name") 553 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 554 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 555 }) 556 th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair(nil)) 557 th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 558 } 559 560 func TestDontUpdateAllowedAddressPairs(t *testing.T) { 561 th.SetupHTTP() 562 defer th.TeardownHTTP() 563 564 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 565 th.TestMethod(t, r, "PUT") 566 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 567 th.TestHeader(t, r, "Content-Type", "application/json") 568 th.TestHeader(t, r, "Accept", "application/json") 569 th.TestJSONRequest(t, r, DontUpdateAllowedAddressPairsRequest) 570 571 w.Header().Add("Content-Type", "application/json") 572 w.WriteHeader(http.StatusOK) 573 574 _, _ = fmt.Fprint(w, DontUpdateAllowedAddressPairsResponse) 575 }) 576 577 options := ports.UpdateOpts{ 578 Name: "new_port_name", 579 FixedIPs: []ports.IP{ 580 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 581 }, 582 SecurityGroups: &[]string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}, 583 } 584 585 s, err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", options).Extract() 586 th.AssertNoErr(t, err) 587 588 th.AssertEquals(t, s.Name, "new_port_name") 589 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 590 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 591 }) 592 th.AssertDeepEquals(t, s.AllowedAddressPairs, []ports.AddressPair{ 593 {IPAddress: "10.0.0.4", MACAddress: "fa:16:3e:c9:cb:f0"}, 594 }) 595 th.AssertDeepEquals(t, s.SecurityGroups, []string{"f0ac4394-7e4a-4409-9701-ba8be283dbc3"}) 596 } 597 598 func TestDelete(t *testing.T) { 599 th.SetupHTTP() 600 defer th.TeardownHTTP() 601 602 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 603 th.TestMethod(t, r, "DELETE") 604 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 605 w.WriteHeader(http.StatusNoContent) 606 }) 607 608 res := ports.Delete(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d") 609 th.AssertNoErr(t, res.Err) 610 } 611 612 func TestGetWithExtraDHCPOpts(t *testing.T) { 613 th.SetupHTTP() 614 defer th.TeardownHTTP() 615 616 th.Mux.HandleFunc("/v2.0/ports/46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2", func(w http.ResponseWriter, r *http.Request) { 617 th.TestMethod(t, r, "GET") 618 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 619 620 w.Header().Add("Content-Type", "application/json") 621 w.WriteHeader(http.StatusOK) 622 623 _, _ = fmt.Fprint(w, GetWithExtraDHCPOptsResponse) 624 }) 625 626 var s struct { 627 ports.Port 628 extradhcpopts.ExtraDHCPOptsExt 629 } 630 631 err := ports.Get(fake.ServiceClient(), "46d4bfb9-b26e-41f3-bd2e-e6dcc1ccedb2").ExtractInto(&s) 632 th.AssertNoErr(t, err) 633 634 th.AssertEquals(t, s.Status, "ACTIVE") 635 th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 636 th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 637 th.AssertEquals(t, s.AdminStateUp, true) 638 th.AssertEquals(t, s.Name, "port-with-extra-dhcp-opts") 639 th.AssertEquals(t, s.DeviceOwner, "") 640 th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0") 641 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 642 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.4"}, 643 }) 644 th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 645 th.AssertEquals(t, s.DeviceID, "") 646 647 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptName, "option1") 648 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value1") 649 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, "4") 650 th.AssertDeepEquals(t, s.ExtraDHCPOpts[1].OptName, "option2") 651 th.AssertDeepEquals(t, s.ExtraDHCPOpts[1].OptValue, "value2") 652 th.AssertDeepEquals(t, s.ExtraDHCPOpts[1].IPVersion, "4") 653 } 654 655 func TestCreateWithExtraDHCPOpts(t *testing.T) { 656 th.SetupHTTP() 657 defer th.TeardownHTTP() 658 659 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 660 th.TestMethod(t, r, "POST") 661 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 662 th.TestHeader(t, r, "Content-Type", "application/json") 663 th.TestHeader(t, r, "Accept", "application/json") 664 th.TestJSONRequest(t, r, CreateWithExtraDHCPOptsRequest) 665 666 w.Header().Add("Content-Type", "application/json") 667 w.WriteHeader(http.StatusCreated) 668 669 _, _ = fmt.Fprint(w, CreateWithExtraDHCPOptsResponse) 670 }) 671 672 adminStateUp := true 673 portCreateOpts := ports.CreateOpts{ 674 Name: "port-with-extra-dhcp-opts", 675 AdminStateUp: &adminStateUp, 676 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 677 FixedIPs: []ports.IP{ 678 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 679 }, 680 } 681 682 createOpts := extradhcpopts.CreateOptsExt{ 683 CreateOptsBuilder: portCreateOpts, 684 ExtraDHCPOpts: []extradhcpopts.CreateExtraDHCPOpt{ 685 { 686 OptName: "option1", 687 OptValue: "value1", 688 }, 689 }, 690 } 691 692 var s struct { 693 ports.Port 694 extradhcpopts.ExtraDHCPOptsExt 695 } 696 697 err := ports.Create(fake.ServiceClient(), createOpts).ExtractInto(&s) 698 th.AssertNoErr(t, err) 699 700 th.AssertEquals(t, s.Status, "DOWN") 701 th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 702 th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 703 th.AssertEquals(t, s.AdminStateUp, true) 704 th.AssertEquals(t, s.Name, "port-with-extra-dhcp-opts") 705 th.AssertEquals(t, s.DeviceOwner, "") 706 th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0") 707 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 708 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.2"}, 709 }) 710 th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 711 th.AssertEquals(t, s.DeviceID, "") 712 713 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptName, "option1") 714 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value1") 715 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, "4") 716 } 717 718 func TestUpdateWithExtraDHCPOpts(t *testing.T) { 719 th.SetupHTTP() 720 defer th.TeardownHTTP() 721 722 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 723 th.TestMethod(t, r, "PUT") 724 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 725 th.TestHeader(t, r, "Content-Type", "application/json") 726 th.TestHeader(t, r, "Accept", "application/json") 727 th.TestJSONRequest(t, r, UpdateWithExtraDHCPOptsRequest) 728 729 w.Header().Add("Content-Type", "application/json") 730 w.WriteHeader(http.StatusOK) 731 732 _, _ = fmt.Fprint(w, UpdateWithExtraDHCPOptsResponse) 733 }) 734 735 portUpdateOpts := ports.UpdateOpts{ 736 Name: "updated-port-with-dhcp-opts", 737 FixedIPs: []ports.IP{ 738 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 739 }, 740 } 741 742 edoValue2 := "value2" 743 updateOpts := extradhcpopts.UpdateOptsExt{ 744 UpdateOptsBuilder: portUpdateOpts, 745 ExtraDHCPOpts: []extradhcpopts.UpdateExtraDHCPOpt{ 746 { 747 OptName: "option1", 748 }, 749 { 750 OptName: "option2", 751 OptValue: &edoValue2, 752 }, 753 }, 754 } 755 756 var s struct { 757 ports.Port 758 extradhcpopts.ExtraDHCPOptsExt 759 } 760 761 err := ports.Update(fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&s) 762 th.AssertNoErr(t, err) 763 764 th.AssertEquals(t, s.Status, "DOWN") 765 th.AssertEquals(t, s.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 766 th.AssertEquals(t, s.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 767 th.AssertEquals(t, s.AdminStateUp, true) 768 th.AssertEquals(t, s.Name, "updated-port-with-dhcp-opts") 769 th.AssertEquals(t, s.DeviceOwner, "") 770 th.AssertEquals(t, s.MACAddress, "fa:16:3e:c9:cb:f0") 771 th.AssertDeepEquals(t, s.FixedIPs, []ports.IP{ 772 {SubnetID: "a0304c3a-4f08-4c43-88af-d796509c97d2", IPAddress: "10.0.0.3"}, 773 }) 774 th.AssertEquals(t, s.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 775 th.AssertEquals(t, s.DeviceID, "") 776 777 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptName, "option2") 778 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].OptValue, "value2") 779 th.AssertDeepEquals(t, s.ExtraDHCPOpts[0].IPVersion, "4") 780 }