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