github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/qos/policies/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/qos/policies" 12 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/networks" 13 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/ports" 14 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 15 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 16 ) 17 18 func TestGetPort(t *testing.T) { 19 th.SetupHTTP() 20 defer th.TeardownHTTP() 21 22 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 23 th.TestMethod(t, r, "GET") 24 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 25 26 w.Header().Add("Content-Type", "application/json") 27 w.WriteHeader(http.StatusOK) 28 29 _, err := fmt.Fprint(w, GetPortResponse) 30 th.AssertNoErr(t, err) 31 }) 32 33 var p struct { 34 ports.Port 35 policies.QoSPolicyExt 36 } 37 err := ports.Get(context.TODO(), fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d").ExtractInto(&p) 38 th.AssertNoErr(t, err) 39 40 th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 41 th.AssertEquals(t, p.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08") 42 } 43 44 func TestCreatePort(t *testing.T) { 45 th.SetupHTTP() 46 defer th.TeardownHTTP() 47 48 th.Mux.HandleFunc("/v2.0/ports", func(w http.ResponseWriter, r *http.Request) { 49 th.TestMethod(t, r, "POST") 50 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 51 th.TestHeader(t, r, "Content-Type", "application/json") 52 th.TestHeader(t, r, "Accept", "application/json") 53 th.TestJSONRequest(t, r, CreatePortRequest) 54 55 w.Header().Add("Content-Type", "application/json") 56 w.WriteHeader(http.StatusCreated) 57 58 _, err := fmt.Fprint(w, CreatePortResponse) 59 th.AssertNoErr(t, err) 60 }) 61 62 var p struct { 63 ports.Port 64 policies.QoSPolicyExt 65 } 66 portCreateOpts := ports.CreateOpts{ 67 NetworkID: "a87cc70a-3e15-4acf-8205-9b711a3531b7", 68 } 69 createOpts := policies.PortCreateOptsExt{ 70 CreateOptsBuilder: portCreateOpts, 71 QoSPolicyID: "591e0597-39a6-4665-8149-2111d8de9a08", 72 } 73 err := ports.Create(context.TODO(), fake.ServiceClient(), createOpts).ExtractInto(&p) 74 th.AssertNoErr(t, err) 75 76 th.AssertEquals(t, p.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 77 th.AssertEquals(t, p.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 78 th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 79 th.AssertEquals(t, p.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08") 80 } 81 82 func TestUpdatePortWithPolicy(t *testing.T) { 83 th.SetupHTTP() 84 defer th.TeardownHTTP() 85 86 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 87 th.TestMethod(t, r, "PUT") 88 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 89 th.TestHeader(t, r, "Content-Type", "application/json") 90 th.TestHeader(t, r, "Accept", "application/json") 91 th.TestJSONRequest(t, r, UpdatePortWithPolicyRequest) 92 93 w.Header().Add("Content-Type", "application/json") 94 w.WriteHeader(http.StatusOK) 95 96 _, err := fmt.Fprint(w, UpdatePortWithPolicyResponse) 97 th.AssertNoErr(t, err) 98 }) 99 100 policyID := "591e0597-39a6-4665-8149-2111d8de9a08" 101 102 var p struct { 103 ports.Port 104 policies.QoSPolicyExt 105 } 106 portUpdateOpts := ports.UpdateOpts{} 107 updateOpts := policies.PortUpdateOptsExt{ 108 UpdateOptsBuilder: portUpdateOpts, 109 QoSPolicyID: &policyID, 110 } 111 err := ports.Update(context.TODO(), fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&p) 112 th.AssertNoErr(t, err) 113 114 th.AssertEquals(t, p.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 115 th.AssertEquals(t, p.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 116 th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 117 th.AssertEquals(t, p.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08") 118 } 119 120 func TestUpdatePortWithoutPolicy(t *testing.T) { 121 th.SetupHTTP() 122 defer th.TeardownHTTP() 123 124 th.Mux.HandleFunc("/v2.0/ports/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 125 th.TestMethod(t, r, "PUT") 126 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 127 th.TestHeader(t, r, "Content-Type", "application/json") 128 th.TestHeader(t, r, "Accept", "application/json") 129 th.TestJSONRequest(t, r, UpdatePortWithoutPolicyRequest) 130 131 w.Header().Add("Content-Type", "application/json") 132 w.WriteHeader(http.StatusOK) 133 134 _, err := fmt.Fprint(w, UpdatePortWithoutPolicyResponse) 135 th.AssertNoErr(t, err) 136 }) 137 138 policyID := "" 139 140 var p struct { 141 ports.Port 142 policies.QoSPolicyExt 143 } 144 portUpdateOpts := ports.UpdateOpts{} 145 updateOpts := policies.PortUpdateOptsExt{ 146 UpdateOptsBuilder: portUpdateOpts, 147 QoSPolicyID: &policyID, 148 } 149 err := ports.Update(context.TODO(), fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&p) 150 th.AssertNoErr(t, err) 151 152 th.AssertEquals(t, p.NetworkID, "a87cc70a-3e15-4acf-8205-9b711a3531b7") 153 th.AssertEquals(t, p.TenantID, "d6700c0c9ffa4f1cb322cd4a1f3906fa") 154 th.AssertEquals(t, p.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 155 th.AssertEquals(t, p.QoSPolicyID, "") 156 } 157 158 func TestGetNetwork(t *testing.T) { 159 th.SetupHTTP() 160 defer th.TeardownHTTP() 161 162 th.Mux.HandleFunc("/v2.0/networks/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 163 th.TestMethod(t, r, "GET") 164 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 165 166 w.Header().Add("Content-Type", "application/json") 167 w.WriteHeader(http.StatusOK) 168 169 _, err := fmt.Fprint(w, GetNetworkResponse) 170 th.AssertNoErr(t, err) 171 }) 172 173 var n struct { 174 networks.Network 175 policies.QoSPolicyExt 176 } 177 err := networks.Get(context.TODO(), fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d").ExtractInto(&n) 178 th.AssertNoErr(t, err) 179 180 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 181 th.AssertEquals(t, n.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08") 182 } 183 184 func TestCreateNetwork(t *testing.T) { 185 th.SetupHTTP() 186 defer th.TeardownHTTP() 187 188 th.Mux.HandleFunc("/v2.0/networks", func(w http.ResponseWriter, r *http.Request) { 189 th.TestMethod(t, r, "POST") 190 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 191 th.TestHeader(t, r, "Content-Type", "application/json") 192 th.TestHeader(t, r, "Accept", "application/json") 193 th.TestJSONRequest(t, r, CreateNetworkRequest) 194 195 w.Header().Add("Content-Type", "application/json") 196 w.WriteHeader(http.StatusCreated) 197 198 _, err := fmt.Fprint(w, CreateNetworkResponse) 199 th.AssertNoErr(t, err) 200 }) 201 202 var n struct { 203 networks.Network 204 policies.QoSPolicyExt 205 } 206 networkCreateOpts := networks.CreateOpts{ 207 Name: "private", 208 } 209 createOpts := policies.NetworkCreateOptsExt{ 210 CreateOptsBuilder: networkCreateOpts, 211 QoSPolicyID: "591e0597-39a6-4665-8149-2111d8de9a08", 212 } 213 err := networks.Create(context.TODO(), fake.ServiceClient(), createOpts).ExtractInto(&n) 214 th.AssertNoErr(t, err) 215 216 th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869") 217 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 218 th.AssertEquals(t, n.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08") 219 } 220 221 func TestUpdateNetworkWithPolicy(t *testing.T) { 222 th.SetupHTTP() 223 defer th.TeardownHTTP() 224 225 th.Mux.HandleFunc("/v2.0/networks/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 226 th.TestMethod(t, r, "PUT") 227 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 228 th.TestHeader(t, r, "Content-Type", "application/json") 229 th.TestHeader(t, r, "Accept", "application/json") 230 th.TestJSONRequest(t, r, UpdateNetworkWithPolicyRequest) 231 232 w.Header().Add("Content-Type", "application/json") 233 w.WriteHeader(http.StatusOK) 234 235 _, err := fmt.Fprint(w, UpdateNetworkWithPolicyResponse) 236 th.AssertNoErr(t, err) 237 }) 238 239 policyID := "591e0597-39a6-4665-8149-2111d8de9a08" 240 name := "updated" 241 242 var n struct { 243 networks.Network 244 policies.QoSPolicyExt 245 } 246 networkUpdateOpts := networks.UpdateOpts{ 247 Name: &name, 248 } 249 updateOpts := policies.NetworkUpdateOptsExt{ 250 UpdateOptsBuilder: networkUpdateOpts, 251 QoSPolicyID: &policyID, 252 } 253 err := networks.Update(context.TODO(), fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&n) 254 th.AssertNoErr(t, err) 255 256 th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869") 257 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 258 th.AssertEquals(t, n.Name, "updated") 259 th.AssertEquals(t, n.QoSPolicyID, "591e0597-39a6-4665-8149-2111d8de9a08") 260 } 261 262 func TestUpdateNetworkWithoutPolicy(t *testing.T) { 263 th.SetupHTTP() 264 defer th.TeardownHTTP() 265 266 th.Mux.HandleFunc("/v2.0/networks/65c0ee9f-d634-4522-8954-51021b570b0d", func(w http.ResponseWriter, r *http.Request) { 267 th.TestMethod(t, r, "PUT") 268 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 269 th.TestHeader(t, r, "Content-Type", "application/json") 270 th.TestHeader(t, r, "Accept", "application/json") 271 th.TestJSONRequest(t, r, UpdateNetworkWithoutPolicyRequest) 272 273 w.Header().Add("Content-Type", "application/json") 274 w.WriteHeader(http.StatusOK) 275 276 _, err := fmt.Fprint(w, UpdateNetworkWithoutPolicyResponse) 277 th.AssertNoErr(t, err) 278 }) 279 280 policyID := "" 281 282 var n struct { 283 networks.Network 284 policies.QoSPolicyExt 285 } 286 networkUpdateOpts := networks.UpdateOpts{} 287 updateOpts := policies.NetworkUpdateOptsExt{ 288 UpdateOptsBuilder: networkUpdateOpts, 289 QoSPolicyID: &policyID, 290 } 291 err := networks.Update(context.TODO(), fake.ServiceClient(), "65c0ee9f-d634-4522-8954-51021b570b0d", updateOpts).ExtractInto(&n) 292 th.AssertNoErr(t, err) 293 294 th.AssertEquals(t, n.TenantID, "4fd44f30292945e481c7b8a0c8908869") 295 th.AssertEquals(t, n.ID, "65c0ee9f-d634-4522-8954-51021b570b0d") 296 th.AssertEquals(t, n.QoSPolicyID, "") 297 } 298 299 func TestListPolicies(t *testing.T) { 300 th.SetupHTTP() 301 defer th.TeardownHTTP() 302 303 th.Mux.HandleFunc("/v2.0/qos/policies", func(w http.ResponseWriter, r *http.Request) { 304 th.TestMethod(t, r, "GET") 305 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 306 307 w.Header().Add("Content-Type", "application/json") 308 w.WriteHeader(http.StatusOK) 309 310 fmt.Fprint(w, ListPoliciesResponse) 311 }) 312 313 count := 0 314 315 err := policies.List(fake.ServiceClient(), policies.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 316 count++ 317 actual, err := policies.ExtractPolicies(page) 318 if err != nil { 319 t.Errorf("Failed to extract policies: %v", err) 320 return false, nil 321 } 322 323 expected := []policies.Policy{ 324 Policy1, 325 Policy2, 326 } 327 328 th.CheckDeepEquals(t, expected, actual) 329 330 return true, nil 331 }) 332 th.AssertNoErr(t, err) 333 334 if count != 1 { 335 t.Errorf("Expected 1 page, got %d", count) 336 } 337 } 338 339 func TestGetPolicy(t *testing.T) { 340 th.SetupHTTP() 341 defer th.TeardownHTTP() 342 343 th.Mux.HandleFunc("/v2.0/qos/policies/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 344 th.TestMethod(t, r, "GET") 345 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 346 347 w.Header().Add("Content-Type", "application/json") 348 w.WriteHeader(http.StatusOK) 349 350 fmt.Fprint(w, GetPolicyResponse) 351 }) 352 353 p, err := policies.Get(context.TODO(), fake.ServiceClient(), "30a57f4a-336b-4382-8275-d708babd2241").Extract() 354 th.AssertNoErr(t, err) 355 356 th.AssertEquals(t, "bw-limiter", p.Name) 357 th.AssertDeepEquals(t, []string{}, p.Tags) 358 th.AssertDeepEquals(t, []map[string]any{ 359 { 360 "max_kbps": float64(3000), 361 "direction": "egress", 362 "qos_policy_id": "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", 363 "type": "bandwidth_limit", 364 "id": "30a57f4a-336b-4382-8275-d708babd2241", 365 "max_burst_kbps": float64(300), 366 }, 367 }, p.Rules) 368 th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID) 369 th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID) 370 th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt) 371 th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 57, 0, time.UTC), p.UpdatedAt) 372 th.AssertEquals(t, 1, p.RevisionNumber) 373 th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID) 374 } 375 376 func TestCreatePolicy(t *testing.T) { 377 th.SetupHTTP() 378 defer th.TeardownHTTP() 379 380 th.Mux.HandleFunc("/v2.0/qos/policies", func(w http.ResponseWriter, r *http.Request) { 381 th.TestMethod(t, r, "POST") 382 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 383 th.TestHeader(t, r, "Content-Type", "application/json") 384 th.TestHeader(t, r, "Accept", "application/json") 385 th.TestJSONRequest(t, r, CreatePolicyRequest) 386 387 w.Header().Add("Content-Type", "application/json") 388 w.WriteHeader(http.StatusCreated) 389 390 fmt.Fprint(w, CreatePolicyResponse) 391 }) 392 393 opts := policies.CreateOpts{ 394 Name: "shared-default-policy", 395 Shared: true, 396 IsDefault: true, 397 Description: "use-me", 398 } 399 p, err := policies.Create(context.TODO(), fake.ServiceClient(), opts).Extract() 400 th.AssertNoErr(t, err) 401 402 th.AssertEquals(t, "shared-default-policy", p.Name) 403 th.AssertEquals(t, true, p.Shared) 404 th.AssertEquals(t, true, p.IsDefault) 405 th.AssertEquals(t, "use-me", p.Description) 406 th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID) 407 th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID) 408 th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt) 409 th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 57, 0, time.UTC), p.UpdatedAt) 410 th.AssertEquals(t, 0, p.RevisionNumber) 411 th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID) 412 } 413 414 func TestUpdatePolicy(t *testing.T) { 415 th.SetupHTTP() 416 defer th.TeardownHTTP() 417 418 th.Mux.HandleFunc("/v2.0/qos/policies/d6ae28ce-fcb5-4180-aa62-d260a27e09ae", func(w http.ResponseWriter, r *http.Request) { 419 th.TestMethod(t, r, "PUT") 420 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 421 th.TestHeader(t, r, "Content-Type", "application/json") 422 th.TestHeader(t, r, "Accept", "application/json") 423 th.TestJSONRequest(t, r, UpdatePolicyRequest) 424 425 w.Header().Add("Content-Type", "application/json") 426 w.WriteHeader(http.StatusOK) 427 428 fmt.Fprint(w, UpdatePolicyResponse) 429 }) 430 431 shared := true 432 description := "" 433 opts := policies.UpdateOpts{ 434 Name: "new-name", 435 Shared: &shared, 436 Description: &description, 437 } 438 p, err := policies.Update(context.TODO(), fake.ServiceClient(), "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", opts).Extract() 439 th.AssertNoErr(t, err) 440 441 th.AssertEquals(t, "new-name", p.Name) 442 th.AssertEquals(t, true, p.Shared) 443 th.AssertEquals(t, false, p.IsDefault) 444 th.AssertEquals(t, "", p.Description) 445 th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.TenantID) 446 th.AssertEquals(t, "a77cbe0998374aed9a6798ad6c61677e", p.ProjectID) 447 th.AssertEquals(t, time.Date(2019, 5, 19, 11, 17, 50, 0, time.UTC), p.CreatedAt) 448 th.AssertEquals(t, time.Date(2019, 6, 1, 13, 17, 57, 0, time.UTC), p.UpdatedAt) 449 th.AssertEquals(t, 1, p.RevisionNumber) 450 th.AssertEquals(t, "d6ae28ce-fcb5-4180-aa62-d260a27e09ae", p.ID) 451 } 452 453 func TestDeletePolicy(t *testing.T) { 454 th.SetupHTTP() 455 defer th.TeardownHTTP() 456 457 th.Mux.HandleFunc("/v2.0/qos/policies/d6ae28ce-fcb5-4180-aa62-d260a27e09ae", func(w http.ResponseWriter, r *http.Request) { 458 th.TestMethod(t, r, "DELETE") 459 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 460 w.WriteHeader(http.StatusNoContent) 461 }) 462 463 res := policies.Delete(context.TODO(), fake.ServiceClient(), "d6ae28ce-fcb5-4180-aa62-d260a27e09ae") 464 th.AssertNoErr(t, res.Err) 465 }