Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 1 | package flavors |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "reflect" |
| 7 | "testing" |
| 8 | |
| 9 | "github.com/rackspace/gophercloud" |
| 10 | "github.com/rackspace/gophercloud/pagination" |
| 11 | "github.com/rackspace/gophercloud/testhelper" |
| 12 | ) |
| 13 | |
| 14 | const tokenID = "blerb" |
| 15 | |
| 16 | func serviceClient() *gophercloud.ServiceClient { |
| 17 | return &gophercloud.ServiceClient{ |
| 18 | Provider: &gophercloud.ProviderClient{TokenID: tokenID}, |
| 19 | Endpoint: testhelper.Endpoint(), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | func TestListFlavors(t *testing.T) { |
| 24 | testhelper.SetupHTTP() |
| 25 | defer testhelper.TeardownHTTP() |
| 26 | |
| 27 | testhelper.Mux.HandleFunc("/flavors/detail", func(w http.ResponseWriter, r *http.Request) { |
| 28 | testhelper.TestMethod(t, r, "GET") |
| 29 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 30 | |
| 31 | w.Header().Add("Content-Type", "application/json") |
| 32 | r.ParseForm() |
| 33 | marker := r.Form.Get("marker") |
| 34 | switch marker { |
| 35 | case "": |
| 36 | fmt.Fprintf(w, ` |
| 37 | { |
| 38 | "flavors": [ |
| 39 | { |
| 40 | "id": "1", |
| 41 | "name": "m1.tiny", |
| 42 | "disk": 1, |
| 43 | "ram": 512, |
| 44 | "vcpus": 1 |
| 45 | }, |
| 46 | { |
| 47 | "id": "2", |
| 48 | "name": "m2.small", |
| 49 | "disk": 10, |
| 50 | "ram": 1024, |
| 51 | "vcpus": 2 |
| 52 | } |
Ash Wilson | ae60961 | 2014-09-24 14:48:30 -0400 | [diff] [blame] | 53 | ], |
| 54 | "flavors_links": [ |
| 55 | { |
| 56 | "href": "%s/flavors?marker=2" |
| 57 | } |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 58 | ] |
| 59 | } |
Ash Wilson | ae60961 | 2014-09-24 14:48:30 -0400 | [diff] [blame] | 60 | `, testhelper.Server.URL) |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 61 | case "2": |
| 62 | fmt.Fprintf(w, `{ "flavors": [] }`) |
| 63 | default: |
| 64 | t.Fatalf("Unexpected marker: [%s]", marker) |
| 65 | } |
| 66 | }) |
| 67 | |
| 68 | client := serviceClient() |
| 69 | pages := 0 |
| 70 | err := List(client, ListFilterOptions{}).EachPage(func(page pagination.Page) (bool, error) { |
| 71 | pages++ |
| 72 | |
| 73 | actual, err := ExtractFlavors(page) |
| 74 | if err != nil { |
| 75 | return false, err |
| 76 | } |
| 77 | |
| 78 | expected := []Flavor{ |
| 79 | Flavor{ID: "1", Name: "m1.tiny", Disk: 1, RAM: 512, VCPUs: 1}, |
| 80 | Flavor{ID: "2", Name: "m2.small", Disk: 10, RAM: 1024, VCPUs: 2}, |
| 81 | } |
| 82 | |
| 83 | if !reflect.DeepEqual(expected, actual) { |
| 84 | t.Errorf("Expected %#v, but was %#v", expected, actual) |
| 85 | } |
| 86 | |
| 87 | return true, nil |
| 88 | }) |
| 89 | if err != nil { |
| 90 | t.Fatal(err) |
| 91 | } |
Ash Wilson | b60b38c | 2014-09-24 15:07:35 -0400 | [diff] [blame] | 92 | if pages != 1 { |
| 93 | t.Errorf("Expected one page, got %d", pages) |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | func TestGetFlavor(t *testing.T) { |
| 98 | testhelper.SetupHTTP() |
| 99 | defer testhelper.TeardownHTTP() |
| 100 | |
| 101 | testhelper.Mux.HandleFunc("/flavors/12345", func(w http.ResponseWriter, r *http.Request) { |
| 102 | testhelper.TestMethod(t, r, "GET") |
| 103 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 104 | |
| 105 | w.Header().Add("Content-Type", "application/json") |
| 106 | fmt.Fprintf(w, ` |
| 107 | { |
| 108 | "flavor": { |
| 109 | "id": "1", |
| 110 | "name": "m1.tiny", |
| 111 | "disk": 1, |
| 112 | "ram": 512, |
| 113 | "vcpus": 1, |
| 114 | "rxtx_factor": 1 |
| 115 | } |
| 116 | } |
| 117 | `) |
| 118 | }) |
| 119 | |
| 120 | client := serviceClient() |
Ash Wilson | 8a8b86f | 2014-09-25 11:26:51 -0400 | [diff] [blame^] | 121 | actual, err := Get(client, "12345").Extract() |
Ash Wilson | b60b38c | 2014-09-24 15:07:35 -0400 | [diff] [blame] | 122 | if err != nil { |
| 123 | t.Fatalf("Unable to get flavor: %v", err) |
| 124 | } |
Ash Wilson | b60b38c | 2014-09-24 15:07:35 -0400 | [diff] [blame] | 125 | |
| 126 | expected := &Flavor{ |
| 127 | ID: "1", |
| 128 | Name: "m1.tiny", |
| 129 | Disk: 1, |
| 130 | RAM: 512, |
| 131 | VCPUs: 1, |
| 132 | RxTxFactor: 1, |
| 133 | } |
| 134 | if !reflect.DeepEqual(expected, actual) { |
| 135 | t.Errorf("Expected %#v, but was %#v", expected, actual) |
| 136 | } |
Ash Wilson | 32be7e1 | 2014-09-24 14:47:47 -0400 | [diff] [blame] | 137 | } |