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