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