github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/compute/v2/flavors/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "reflect" 8 "testing" 9 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/compute/v2/flavors" 11 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 12 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 13 fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client" 14 ) 15 16 func TestListFlavors(t *testing.T) { 17 th.SetupHTTP() 18 defer th.TeardownHTTP() 19 20 th.Mux.HandleFunc("/flavors/detail", 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 if err := r.ParseForm(); err != nil { 26 t.Errorf("Failed to parse request form %v", err) 27 } 28 marker := r.Form.Get("marker") 29 switch marker { 30 case "": 31 fmt.Fprintf(w, ` 32 { 33 "flavors": [ 34 { 35 "id": "1", 36 "name": "m1.tiny", 37 "vcpus": 1, 38 "disk": 1, 39 "ram": 9216000, 40 "swap":"", 41 "os-flavor-access:is_public": true, 42 "OS-FLV-EXT-DATA:ephemeral": 10, 43 "description": "foo", 44 "extra_specs": 45 { 46 "foo": "bar" 47 } 48 }, 49 { 50 "id": "2", 51 "name": "m1.small", 52 "vcpus": 1, 53 "disk": 20, 54 "ram": 2048, 55 "swap": 1000, 56 "os-flavor-access:is_public": true, 57 "OS-FLV-EXT-DATA:ephemeral": 0 58 }, 59 { 60 "id": "3", 61 "name": "m1.medium", 62 "vcpus": 2, 63 "disk": 40, 64 "ram": 4096, 65 "swap": 1000, 66 "os-flavor-access:is_public": false, 67 "OS-FLV-EXT-DATA:ephemeral": 0 68 } 69 ], 70 "flavors_links": [ 71 { 72 "href": "%s/flavors/detail?marker=2", 73 "rel": "next" 74 } 75 ] 76 } 77 `, th.Server.URL) 78 case "2": 79 fmt.Fprint(w, `{ "flavors": [] }`) 80 default: 81 t.Fatalf("Unexpected marker: [%s]", marker) 82 } 83 }) 84 85 pages := 0 86 // Get public and private flavors 87 err := flavors.ListDetail(fake.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 88 pages++ 89 90 actual, err := flavors.ExtractFlavors(page) 91 if err != nil { 92 return false, err 93 } 94 95 expected := []flavors.Flavor{ 96 {ID: "1", Name: "m1.tiny", VCPUs: 1, Disk: 1, RAM: 9216000, Swap: 0, IsPublic: true, Ephemeral: 10, Description: "foo", ExtraSpecs: map[string]string{"foo": "bar"}}, 97 {ID: "2", Name: "m1.small", VCPUs: 1, Disk: 20, RAM: 2048, Swap: 1000, IsPublic: true, Ephemeral: 0}, 98 {ID: "3", Name: "m1.medium", VCPUs: 2, Disk: 40, RAM: 4096, Swap: 1000, IsPublic: false, Ephemeral: 0}, 99 } 100 101 if !reflect.DeepEqual(expected, actual) { 102 t.Errorf("Expected %#v, but was %#v", expected, actual) 103 } 104 105 return true, nil 106 }) 107 if err != nil { 108 t.Fatal(err) 109 } 110 if pages != 1 { 111 t.Errorf("Expected one page, got %d", pages) 112 } 113 } 114 115 func TestGetFlavor(t *testing.T) { 116 th.SetupHTTP() 117 defer th.TeardownHTTP() 118 119 th.Mux.HandleFunc("/flavors/12345", func(w http.ResponseWriter, r *http.Request) { 120 th.TestMethod(t, r, "GET") 121 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 122 123 w.Header().Add("Content-Type", "application/json") 124 fmt.Fprint(w, ` 125 { 126 "flavor": { 127 "id": "1", 128 "name": "m1.tiny", 129 "disk": 1, 130 "ram": 512, 131 "vcpus": 1, 132 "rxtx_factor": 1, 133 "swap": "", 134 "description": "foo", 135 "extra_specs": { 136 "foo": "bar" 137 } 138 } 139 } 140 `) 141 }) 142 143 actual, err := flavors.Get(context.TODO(), fake.ServiceClient(), "12345").Extract() 144 if err != nil { 145 t.Fatalf("Unable to get flavor: %v", err) 146 } 147 148 expected := &flavors.Flavor{ 149 ID: "1", 150 Name: "m1.tiny", 151 Disk: 1, 152 RAM: 512, 153 VCPUs: 1, 154 RxTxFactor: 1, 155 Swap: 0, 156 Description: "foo", 157 ExtraSpecs: map[string]string{"foo": "bar"}, 158 } 159 if !reflect.DeepEqual(expected, actual) { 160 t.Errorf("Expected %#v, but was %#v", expected, actual) 161 } 162 } 163 164 func TestCreateFlavor(t *testing.T) { 165 th.SetupHTTP() 166 defer th.TeardownHTTP() 167 168 th.Mux.HandleFunc("/flavors", func(w http.ResponseWriter, r *http.Request) { 169 th.TestMethod(t, r, "POST") 170 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 171 172 w.Header().Add("Content-Type", "application/json") 173 fmt.Fprint(w, ` 174 { 175 "flavor": { 176 "id": "1", 177 "name": "m1.tiny", 178 "disk": 1, 179 "ram": 512, 180 "vcpus": 1, 181 "rxtx_factor": 1, 182 "swap": "", 183 "description": "foo" 184 } 185 } 186 `) 187 }) 188 189 disk := 1 190 opts := &flavors.CreateOpts{ 191 ID: "1", 192 Name: "m1.tiny", 193 Disk: &disk, 194 RAM: 512, 195 VCPUs: 1, 196 RxTxFactor: 1.0, 197 Description: "foo", 198 } 199 actual, err := flavors.Create(context.TODO(), fake.ServiceClient(), opts).Extract() 200 if err != nil { 201 t.Fatalf("Unable to create flavor: %v", err) 202 } 203 204 expected := &flavors.Flavor{ 205 ID: "1", 206 Name: "m1.tiny", 207 Disk: 1, 208 RAM: 512, 209 VCPUs: 1, 210 RxTxFactor: 1, 211 Swap: 0, 212 Description: "foo", 213 } 214 if !reflect.DeepEqual(expected, actual) { 215 t.Errorf("Expected %#v, but was %#v", expected, actual) 216 } 217 } 218 219 func TestUpdateFlavor(t *testing.T) { 220 th.SetupHTTP() 221 defer th.TeardownHTTP() 222 223 th.Mux.HandleFunc("/flavors/12345678", func(w http.ResponseWriter, r *http.Request) { 224 th.TestMethod(t, r, "PUT") 225 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 226 227 w.Header().Add("Content-Type", "application/json") 228 fmt.Fprint(w, ` 229 { 230 "flavor": { 231 "id": "1", 232 "name": "m1.tiny", 233 "disk": 1, 234 "ram": 512, 235 "vcpus": 1, 236 "rxtx_factor": 1, 237 "swap": "", 238 "description": "foo" 239 } 240 } 241 `) 242 }) 243 244 opts := &flavors.UpdateOpts{ 245 Description: "foo", 246 } 247 actual, err := flavors.Update(context.TODO(), fake.ServiceClient(), "12345678", opts).Extract() 248 if err != nil { 249 t.Fatalf("Unable to update flavor: %v", err) 250 } 251 252 expected := &flavors.Flavor{ 253 ID: "1", 254 Name: "m1.tiny", 255 Disk: 1, 256 RAM: 512, 257 VCPUs: 1, 258 RxTxFactor: 1, 259 Swap: 0, 260 Description: "foo", 261 } 262 263 if !reflect.DeepEqual(expected, actual) { 264 t.Errorf("Expected %#v, but was %#v", expected, actual) 265 } 266 } 267 268 func TestDeleteFlavor(t *testing.T) { 269 th.SetupHTTP() 270 defer th.TeardownHTTP() 271 272 th.Mux.HandleFunc("/flavors/12345678", func(w http.ResponseWriter, r *http.Request) { 273 th.TestMethod(t, r, "DELETE") 274 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 275 276 w.WriteHeader(http.StatusAccepted) 277 }) 278 279 res := flavors.Delete(context.TODO(), fake.ServiceClient(), "12345678") 280 th.AssertNoErr(t, res.Err) 281 } 282 283 func TestFlavorAccessesList(t *testing.T) { 284 th.SetupHTTP() 285 defer th.TeardownHTTP() 286 287 th.Mux.HandleFunc("/flavors/12345678/os-flavor-access", func(w http.ResponseWriter, r *http.Request) { 288 th.TestMethod(t, r, "GET") 289 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 290 w.Header().Add("Content-Type", "application/json") 291 fmt.Fprint(w, ` 292 { 293 "flavor_access": [ 294 { 295 "flavor_id": "12345678", 296 "tenant_id": "2f954bcf047c4ee9b09a37d49ae6db54" 297 } 298 ] 299 } 300 `) 301 }) 302 303 expected := []flavors.FlavorAccess{ 304 { 305 FlavorID: "12345678", 306 TenantID: "2f954bcf047c4ee9b09a37d49ae6db54", 307 }, 308 } 309 310 allPages, err := flavors.ListAccesses(fake.ServiceClient(), "12345678").AllPages(context.TODO()) 311 th.AssertNoErr(t, err) 312 313 actual, err := flavors.ExtractAccesses(allPages) 314 th.AssertNoErr(t, err) 315 316 if !reflect.DeepEqual(expected, actual) { 317 t.Errorf("Expected %#v, but was %#v", expected, actual) 318 } 319 } 320 321 func TestFlavorAccessAdd(t *testing.T) { 322 th.SetupHTTP() 323 defer th.TeardownHTTP() 324 325 th.Mux.HandleFunc("/flavors/12345678/action", func(w http.ResponseWriter, r *http.Request) { 326 th.TestMethod(t, r, "POST") 327 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 328 th.TestHeader(t, r, "accept", "application/json") 329 th.TestJSONRequest(t, r, ` 330 { 331 "addTenantAccess": { 332 "tenant": "2f954bcf047c4ee9b09a37d49ae6db54" 333 } 334 } 335 `) 336 337 w.Header().Add("Content-Type", "application/json") 338 w.WriteHeader(http.StatusOK) 339 fmt.Fprint(w, ` 340 { 341 "flavor_access": [ 342 { 343 "flavor_id": "12345678", 344 "tenant_id": "2f954bcf047c4ee9b09a37d49ae6db54" 345 } 346 ] 347 } 348 `) 349 }) 350 351 expected := []flavors.FlavorAccess{ 352 { 353 FlavorID: "12345678", 354 TenantID: "2f954bcf047c4ee9b09a37d49ae6db54", 355 }, 356 } 357 358 addAccessOpts := flavors.AddAccessOpts{ 359 Tenant: "2f954bcf047c4ee9b09a37d49ae6db54", 360 } 361 362 actual, err := flavors.AddAccess(context.TODO(), fake.ServiceClient(), "12345678", addAccessOpts).Extract() 363 th.AssertNoErr(t, err) 364 365 if !reflect.DeepEqual(expected, actual) { 366 t.Errorf("Expected %#v, but was %#v", expected, actual) 367 } 368 } 369 370 func TestFlavorAccessRemove(t *testing.T) { 371 th.SetupHTTP() 372 defer th.TeardownHTTP() 373 374 th.Mux.HandleFunc("/flavors/12345678/action", func(w http.ResponseWriter, r *http.Request) { 375 th.TestMethod(t, r, "POST") 376 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 377 th.TestHeader(t, r, "accept", "application/json") 378 th.TestJSONRequest(t, r, ` 379 { 380 "removeTenantAccess": { 381 "tenant": "2f954bcf047c4ee9b09a37d49ae6db54" 382 } 383 } 384 `) 385 386 w.Header().Add("Content-Type", "application/json") 387 w.WriteHeader(http.StatusOK) 388 fmt.Fprint(w, ` 389 { 390 "flavor_access": [] 391 } 392 `) 393 }) 394 395 expected := []flavors.FlavorAccess{} 396 removeAccessOpts := flavors.RemoveAccessOpts{ 397 Tenant: "2f954bcf047c4ee9b09a37d49ae6db54", 398 } 399 400 actual, err := flavors.RemoveAccess(context.TODO(), fake.ServiceClient(), "12345678", removeAccessOpts).Extract() 401 th.AssertNoErr(t, err) 402 403 if !reflect.DeepEqual(expected, actual) { 404 t.Errorf("Expected %#v, but was %#v", expected, actual) 405 } 406 } 407 408 func TestFlavorExtraSpecsList(t *testing.T) { 409 th.SetupHTTP() 410 defer th.TeardownHTTP() 411 HandleExtraSpecsListSuccessfully(t) 412 413 expected := ExtraSpecs 414 actual, err := flavors.ListExtraSpecs(context.TODO(), fake.ServiceClient(), "1").Extract() 415 th.AssertNoErr(t, err) 416 th.CheckDeepEquals(t, expected, actual) 417 } 418 419 func TestFlavorExtraSpecGet(t *testing.T) { 420 th.SetupHTTP() 421 defer th.TeardownHTTP() 422 HandleExtraSpecGetSuccessfully(t) 423 424 expected := ExtraSpec 425 actual, err := flavors.GetExtraSpec(context.TODO(), fake.ServiceClient(), "1", "hw:cpu_policy").Extract() 426 th.AssertNoErr(t, err) 427 th.CheckDeepEquals(t, expected, actual) 428 } 429 430 func TestFlavorExtraSpecsCreate(t *testing.T) { 431 th.SetupHTTP() 432 defer th.TeardownHTTP() 433 HandleExtraSpecsCreateSuccessfully(t) 434 435 createOpts := flavors.ExtraSpecsOpts{ 436 "hw:cpu_policy": "CPU-POLICY", 437 "hw:cpu_thread_policy": "CPU-THREAD-POLICY", 438 } 439 expected := ExtraSpecs 440 actual, err := flavors.CreateExtraSpecs(context.TODO(), fake.ServiceClient(), "1", createOpts).Extract() 441 th.AssertNoErr(t, err) 442 th.CheckDeepEquals(t, expected, actual) 443 } 444 445 func TestFlavorExtraSpecUpdate(t *testing.T) { 446 th.SetupHTTP() 447 defer th.TeardownHTTP() 448 HandleExtraSpecUpdateSuccessfully(t) 449 450 updateOpts := flavors.ExtraSpecsOpts{ 451 "hw:cpu_policy": "CPU-POLICY-2", 452 } 453 expected := UpdatedExtraSpec 454 actual, err := flavors.UpdateExtraSpec(context.TODO(), fake.ServiceClient(), "1", updateOpts).Extract() 455 th.AssertNoErr(t, err) 456 th.CheckDeepEquals(t, expected, actual) 457 } 458 459 func TestFlavorExtraSpecDelete(t *testing.T) { 460 th.SetupHTTP() 461 defer th.TeardownHTTP() 462 HandleExtraSpecDeleteSuccessfully(t) 463 464 res := flavors.DeleteExtraSpec(context.TODO(), fake.ServiceClient(), "1", "hw:cpu_policy") 465 th.AssertNoErr(t, res.Err) 466 }