github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/layer3/floatingips/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 9 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/layer3/floatingips" 10 "github.com/huaweicloud/golangsdk/pagination" 11 th "github.com/huaweicloud/golangsdk/testhelper" 12 ) 13 14 func TestList(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "GET") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, ` 26 { 27 "floatingips": [ 28 { 29 "floating_network_id": "6d67c30a-ddb4-49a1-bec3-a65b286b4170", 30 "router_id": null, 31 "fixed_ip_address": null, 32 "floating_ip_address": "192.0.0.4", 33 "tenant_id": "017d8de156df4177889f31a9bd6edc00", 34 "status": "DOWN", 35 "port_id": null, 36 "id": "2f95fd2b-9f6a-4e8e-9e9a-2cbe286cbf9e", 37 "router_id": "1117c30a-ddb4-49a1-bec3-a65b286b4170" 38 }, 39 { 40 "floating_network_id": "90f742b1-6d17-487b-ba95-71881dbc0b64", 41 "router_id": "0a24cb83-faf5-4d7f-b723-3144ed8a2167", 42 "fixed_ip_address": "192.0.0.2", 43 "floating_ip_address": "10.0.0.3", 44 "tenant_id": "017d8de156df4177889f31a9bd6edc00", 45 "status": "DOWN", 46 "port_id": "74a342ce-8e07-4e91-880c-9f834b68fa25", 47 "id": "ada25a95-f321-4f59-b0e0-f3a970dd3d63", 48 "router_id": "2227c30a-ddb4-49a1-bec3-a65b286b4170" 49 } 50 ] 51 } 52 `) 53 }) 54 55 count := 0 56 57 floatingips.List(fake.ServiceClient(), floatingips.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 58 count++ 59 actual, err := floatingips.ExtractFloatingIPs(page) 60 if err != nil { 61 t.Errorf("Failed to extract floating IPs: %v", err) 62 return false, err 63 } 64 65 expected := []floatingips.FloatingIP{ 66 { 67 FloatingNetworkID: "6d67c30a-ddb4-49a1-bec3-a65b286b4170", 68 FixedIP: "", 69 FloatingIP: "192.0.0.4", 70 TenantID: "017d8de156df4177889f31a9bd6edc00", 71 Status: "DOWN", 72 PortID: "", 73 ID: "2f95fd2b-9f6a-4e8e-9e9a-2cbe286cbf9e", 74 RouterID: "1117c30a-ddb4-49a1-bec3-a65b286b4170", 75 }, 76 { 77 FloatingNetworkID: "90f742b1-6d17-487b-ba95-71881dbc0b64", 78 FixedIP: "192.0.0.2", 79 FloatingIP: "10.0.0.3", 80 TenantID: "017d8de156df4177889f31a9bd6edc00", 81 Status: "DOWN", 82 PortID: "74a342ce-8e07-4e91-880c-9f834b68fa25", 83 ID: "ada25a95-f321-4f59-b0e0-f3a970dd3d63", 84 RouterID: "2227c30a-ddb4-49a1-bec3-a65b286b4170", 85 }, 86 } 87 88 th.CheckDeepEquals(t, expected, actual) 89 90 return true, nil 91 }) 92 93 if count != 1 { 94 t.Errorf("Expected 1 page, got %d", count) 95 } 96 } 97 98 func TestInvalidNextPageURLs(t *testing.T) { 99 th.SetupHTTP() 100 defer th.TeardownHTTP() 101 102 th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) { 103 w.Header().Add("Content-Type", "application/json") 104 w.WriteHeader(http.StatusOK) 105 fmt.Fprintf(w, `{"floatingips": [{}], "floatingips_links": {}}`) 106 }) 107 108 floatingips.List(fake.ServiceClient(), floatingips.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 109 floatingips.ExtractFloatingIPs(page) 110 return true, nil 111 }) 112 } 113 114 func TestRequiredFieldsForCreate(t *testing.T) { 115 res1 := floatingips.Create(fake.ServiceClient(), floatingips.CreateOpts{FloatingNetworkID: ""}) 116 if res1.Err == nil { 117 t.Fatalf("Expected error, got none") 118 } 119 120 res2 := floatingips.Create(fake.ServiceClient(), floatingips.CreateOpts{FloatingNetworkID: "foo", PortID: ""}) 121 if res2.Err == nil { 122 t.Fatalf("Expected error, got none") 123 } 124 } 125 126 func TestCreate(t *testing.T) { 127 th.SetupHTTP() 128 defer th.TeardownHTTP() 129 130 th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) { 131 th.TestMethod(t, r, "POST") 132 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 133 th.TestHeader(t, r, "Content-Type", "application/json") 134 th.TestHeader(t, r, "Accept", "application/json") 135 th.TestJSONRequest(t, r, ` 136 { 137 "floatingip": { 138 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 139 "port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab" 140 } 141 } 142 `) 143 144 w.Header().Add("Content-Type", "application/json") 145 w.WriteHeader(http.StatusCreated) 146 147 fmt.Fprintf(w, ` 148 { 149 "floatingip": { 150 "router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f", 151 "tenant_id": "4969c491a3c74ee4af974e6d800c62de", 152 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 153 "fixed_ip_address": "10.0.0.3", 154 "floating_ip_address": "", 155 "port_id": "ce705c24-c1ef-408a-bda3-7bbd946164ab", 156 "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7" 157 } 158 } 159 `) 160 }) 161 162 options := floatingips.CreateOpts{ 163 FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57", 164 PortID: "ce705c24-c1ef-408a-bda3-7bbd946164ab", 165 } 166 167 ip, err := floatingips.Create(fake.ServiceClient(), options).Extract() 168 th.AssertNoErr(t, err) 169 170 th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID) 171 th.AssertEquals(t, "4969c491a3c74ee4af974e6d800c62de", ip.TenantID) 172 th.AssertEquals(t, "376da547-b977-4cfe-9cba-275c80debf57", ip.FloatingNetworkID) 173 th.AssertEquals(t, "", ip.FloatingIP) 174 th.AssertEquals(t, "ce705c24-c1ef-408a-bda3-7bbd946164ab", ip.PortID) 175 th.AssertEquals(t, "10.0.0.3", ip.FixedIP) 176 } 177 178 func TestCreateEmptyPort(t *testing.T) { 179 th.SetupHTTP() 180 defer th.TeardownHTTP() 181 182 th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) { 183 th.TestMethod(t, r, "POST") 184 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 185 th.TestHeader(t, r, "Content-Type", "application/json") 186 th.TestHeader(t, r, "Accept", "application/json") 187 th.TestJSONRequest(t, r, ` 188 { 189 "floatingip": { 190 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57" 191 } 192 } 193 `) 194 195 w.Header().Add("Content-Type", "application/json") 196 w.WriteHeader(http.StatusCreated) 197 198 fmt.Fprintf(w, ` 199 { 200 "floatingip": { 201 "router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f", 202 "tenant_id": "4969c491a3c74ee4af974e6d800c62de", 203 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 204 "fixed_ip_address": "10.0.0.3", 205 "floating_ip_address": "", 206 "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7" 207 } 208 } 209 `) 210 }) 211 212 options := floatingips.CreateOpts{ 213 FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57", 214 } 215 216 ip, err := floatingips.Create(fake.ServiceClient(), options).Extract() 217 th.AssertNoErr(t, err) 218 219 th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID) 220 th.AssertEquals(t, "4969c491a3c74ee4af974e6d800c62de", ip.TenantID) 221 th.AssertEquals(t, "376da547-b977-4cfe-9cba-275c80debf57", ip.FloatingNetworkID) 222 th.AssertEquals(t, "", ip.FloatingIP) 223 th.AssertEquals(t, "", ip.PortID) 224 th.AssertEquals(t, "10.0.0.3", ip.FixedIP) 225 } 226 227 func TestCreateWithSubnetID(t *testing.T) { 228 th.SetupHTTP() 229 defer th.TeardownHTTP() 230 231 th.Mux.HandleFunc("/v2.0/floatingips", func(w http.ResponseWriter, r *http.Request) { 232 th.TestMethod(t, r, "POST") 233 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 234 th.TestHeader(t, r, "Content-Type", "application/json") 235 th.TestHeader(t, r, "Accept", "application/json") 236 th.TestJSONRequest(t, r, ` 237 { 238 "floatingip": { 239 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 240 "subnet_id": "37adf01c-24db-467a-b845-7ab1e8216c01" 241 } 242 } 243 `) 244 245 w.Header().Add("Content-Type", "application/json") 246 w.WriteHeader(http.StatusCreated) 247 248 fmt.Fprintf(w, ` 249 { 250 "floatingip": { 251 "router_id": null, 252 "tenant_id": "4969c491a3c74ee4af974e6d800c62de", 253 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 254 "fixed_ip_address": null, 255 "floating_ip_address": "172.24.4.3", 256 "port_id": null, 257 "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7" 258 } 259 } 260 `) 261 }) 262 263 options := floatingips.CreateOpts{ 264 FloatingNetworkID: "376da547-b977-4cfe-9cba-275c80debf57", 265 SubnetID: "37adf01c-24db-467a-b845-7ab1e8216c01", 266 } 267 268 ip, err := floatingips.Create(fake.ServiceClient(), options).Extract() 269 th.AssertNoErr(t, err) 270 271 th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID) 272 th.AssertEquals(t, "4969c491a3c74ee4af974e6d800c62de", ip.TenantID) 273 th.AssertEquals(t, "376da547-b977-4cfe-9cba-275c80debf57", ip.FloatingNetworkID) 274 th.AssertEquals(t, "172.24.4.3", ip.FloatingIP) 275 th.AssertEquals(t, "", ip.PortID) 276 th.AssertEquals(t, "", ip.FixedIP) 277 } 278 279 func TestGet(t *testing.T) { 280 th.SetupHTTP() 281 defer th.TeardownHTTP() 282 283 th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) { 284 th.TestMethod(t, r, "GET") 285 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 286 287 w.Header().Add("Content-Type", "application/json") 288 w.WriteHeader(http.StatusOK) 289 290 fmt.Fprintf(w, ` 291 { 292 "floatingip": { 293 "floating_network_id": "90f742b1-6d17-487b-ba95-71881dbc0b64", 294 "fixed_ip_address": "192.0.0.2", 295 "floating_ip_address": "10.0.0.3", 296 "tenant_id": "017d8de156df4177889f31a9bd6edc00", 297 "status": "DOWN", 298 "port_id": "74a342ce-8e07-4e91-880c-9f834b68fa25", 299 "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7", 300 "router_id": "1117c30a-ddb4-49a1-bec3-a65b286b4170" 301 } 302 } 303 `) 304 }) 305 306 ip, err := floatingips.Get(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7").Extract() 307 th.AssertNoErr(t, err) 308 309 th.AssertEquals(t, "90f742b1-6d17-487b-ba95-71881dbc0b64", ip.FloatingNetworkID) 310 th.AssertEquals(t, "10.0.0.3", ip.FloatingIP) 311 th.AssertEquals(t, "74a342ce-8e07-4e91-880c-9f834b68fa25", ip.PortID) 312 th.AssertEquals(t, "192.0.0.2", ip.FixedIP) 313 th.AssertEquals(t, "017d8de156df4177889f31a9bd6edc00", ip.TenantID) 314 th.AssertEquals(t, "DOWN", ip.Status) 315 th.AssertEquals(t, "2f245a7b-796b-4f26-9cf9-9e82d248fda7", ip.ID) 316 th.AssertEquals(t, "1117c30a-ddb4-49a1-bec3-a65b286b4170", ip.RouterID) 317 } 318 319 func TestAssociate(t *testing.T) { 320 th.SetupHTTP() 321 defer th.TeardownHTTP() 322 323 th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) { 324 th.TestMethod(t, r, "PUT") 325 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 326 th.TestHeader(t, r, "Content-Type", "application/json") 327 th.TestHeader(t, r, "Accept", "application/json") 328 th.TestJSONRequest(t, r, ` 329 { 330 "floatingip": { 331 "port_id": "423abc8d-2991-4a55-ba98-2aaea84cc72e" 332 } 333 } 334 `) 335 336 w.Header().Add("Content-Type", "application/json") 337 w.WriteHeader(http.StatusOK) 338 339 fmt.Fprintf(w, ` 340 { 341 "floatingip": { 342 "router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f", 343 "tenant_id": "4969c491a3c74ee4af974e6d800c62de", 344 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 345 "fixed_ip_address": null, 346 "floating_ip_address": "172.24.4.228", 347 "port_id": "423abc8d-2991-4a55-ba98-2aaea84cc72e", 348 "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7" 349 } 350 } 351 `) 352 }) 353 354 portID := "423abc8d-2991-4a55-ba98-2aaea84cc72e" 355 ip, err := floatingips.Update(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7", floatingips.UpdateOpts{PortID: &portID}).Extract() 356 th.AssertNoErr(t, err) 357 358 th.AssertDeepEquals(t, portID, ip.PortID) 359 } 360 361 func TestDisassociate(t *testing.T) { 362 th.SetupHTTP() 363 defer th.TeardownHTTP() 364 365 th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) { 366 th.TestMethod(t, r, "PUT") 367 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 368 th.TestHeader(t, r, "Content-Type", "application/json") 369 th.TestHeader(t, r, "Accept", "application/json") 370 th.TestJSONRequest(t, r, ` 371 { 372 "floatingip": { 373 "port_id": null 374 } 375 } 376 `) 377 378 w.Header().Add("Content-Type", "application/json") 379 w.WriteHeader(http.StatusOK) 380 381 fmt.Fprintf(w, ` 382 { 383 "floatingip": { 384 "router_id": "d23abc8d-2991-4a55-ba98-2aaea84cc72f", 385 "tenant_id": "4969c491a3c74ee4af974e6d800c62de", 386 "floating_network_id": "376da547-b977-4cfe-9cba-275c80debf57", 387 "fixed_ip_address": null, 388 "floating_ip_address": "172.24.4.228", 389 "port_id": null, 390 "id": "2f245a7b-796b-4f26-9cf9-9e82d248fda7" 391 } 392 } 393 `) 394 }) 395 396 ip, err := floatingips.Update(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7", floatingips.UpdateOpts{PortID: nil}).Extract() 397 th.AssertNoErr(t, err) 398 399 th.AssertDeepEquals(t, "", ip.FixedIP) 400 th.AssertDeepEquals(t, "", ip.PortID) 401 } 402 403 func TestDelete(t *testing.T) { 404 th.SetupHTTP() 405 defer th.TeardownHTTP() 406 407 th.Mux.HandleFunc("/v2.0/floatingips/2f245a7b-796b-4f26-9cf9-9e82d248fda7", func(w http.ResponseWriter, r *http.Request) { 408 th.TestMethod(t, r, "DELETE") 409 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 410 w.WriteHeader(http.StatusNoContent) 411 }) 412 413 res := floatingips.Delete(fake.ServiceClient(), "2f245a7b-796b-4f26-9cf9-9e82d248fda7") 414 th.AssertNoErr(t, res.Err) 415 }