| package flavors |
| |
| import ( |
| "fmt" |
| "net/http" |
| "reflect" |
| "testing" |
| |
| "github.com/rackspace/gophercloud/pagination" |
| th "github.com/rackspace/gophercloud/testhelper" |
| fake "github.com/rackspace/gophercloud/testhelper/client" |
| ) |
| |
| const tokenID = "blerb" |
| |
| func TestListFlavors(t *testing.T) { |
| th.SetupHTTP() |
| defer th.TeardownHTTP() |
| |
| th.Mux.HandleFunc("/flavors/detail", func(w http.ResponseWriter, r *http.Request) { |
| th.TestMethod(t, r, "GET") |
| th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| |
| w.Header().Add("Content-Type", "application/json") |
| r.ParseForm() |
| marker := r.Form.Get("marker") |
| switch marker { |
| case "": |
| fmt.Fprintf(w, ` |
| { |
| "flavors": [ |
| { |
| "id": "1", |
| "name": "m1.tiny", |
| "disk": 1, |
| "ram": 512, |
| "vcpus": 1 |
| }, |
| { |
| "id": "2", |
| "name": "m2.small", |
| "disk": 10, |
| "ram": 1024, |
| "vcpus": 2 |
| } |
| ], |
| "flavors_links": [ |
| { |
| "href": "%s/flavors/detail?marker=2", |
| "rel": "next" |
| } |
| ] |
| } |
| `, th.Server.URL) |
| case "2": |
| fmt.Fprintf(w, `{ "flavors": [] }`) |
| default: |
| t.Fatalf("Unexpected marker: [%s]", marker) |
| } |
| }) |
| |
| pages := 0 |
| err := List(fake.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| pages++ |
| |
| actual, err := ExtractFlavors(page) |
| if err != nil { |
| return false, err |
| } |
| |
| expected := []Flavor{ |
| Flavor{ID: "1", Name: "m1.tiny", Disk: 1, RAM: 512, VCPUs: 1}, |
| Flavor{ID: "2", Name: "m2.small", Disk: 10, RAM: 1024, VCPUs: 2}, |
| } |
| |
| if !reflect.DeepEqual(expected, actual) { |
| t.Errorf("Expected %#v, but was %#v", expected, actual) |
| } |
| |
| return true, nil |
| }) |
| if err != nil { |
| t.Fatal(err) |
| } |
| if pages != 1 { |
| t.Errorf("Expected one page, got %d", pages) |
| } |
| } |
| |
| func TestGetFlavor(t *testing.T) { |
| th.SetupHTTP() |
| defer th.TeardownHTTP() |
| |
| th.Mux.HandleFunc("/flavors/12345", func(w http.ResponseWriter, r *http.Request) { |
| th.TestMethod(t, r, "GET") |
| th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| |
| w.Header().Add("Content-Type", "application/json") |
| fmt.Fprintf(w, ` |
| { |
| "flavor": { |
| "id": "1", |
| "name": "m1.tiny", |
| "disk": 1, |
| "ram": 512, |
| "vcpus": 1, |
| "rxtx_factor": 1 |
| } |
| } |
| `) |
| }) |
| |
| actual, err := Get(fake.ServiceClient(), "12345").Extract() |
| if err != nil { |
| t.Fatalf("Unable to get flavor: %v", err) |
| } |
| |
| expected := &Flavor{ |
| ID: "1", |
| Name: "m1.tiny", |
| Disk: 1, |
| RAM: 512, |
| VCPUs: 1, |
| RxTxFactor: 1, |
| } |
| if !reflect.DeepEqual(expected, actual) { |
| t.Errorf("Expected %#v, but was %#v", expected, actual) |
| } |
| } |