blob: 7413a32bbf2dfc53184f7719bbc4a9daf61af674 [file] [log] [blame]
Ash Wilson32be7e12014-09-24 14:47:47 -04001package flavors
2
3import (
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
14const tokenID = "blerb"
15
16func serviceClient() *gophercloud.ServiceClient {
17 return &gophercloud.ServiceClient{
18 Provider: &gophercloud.ProviderClient{TokenID: tokenID},
19 Endpoint: testhelper.Endpoint(),
20 }
21}
22
23func 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 }
53 ]
54 }
55 `)
56 case "2":
57 fmt.Fprintf(w, `{ "flavors": [] }`)
58 default:
59 t.Fatalf("Unexpected marker: [%s]", marker)
60 }
61 })
62
63 client := serviceClient()
64 pages := 0
65 err := List(client, ListFilterOptions{}).EachPage(func(page pagination.Page) (bool, error) {
66 pages++
67
68 actual, err := ExtractFlavors(page)
69 if err != nil {
70 return false, err
71 }
72
73 expected := []Flavor{
74 Flavor{ID: "1", Name: "m1.tiny", Disk: 1, RAM: 512, VCPUs: 1},
75 Flavor{ID: "2", Name: "m2.small", Disk: 10, RAM: 1024, VCPUs: 2},
76 }
77
78 if !reflect.DeepEqual(expected, actual) {
79 t.Errorf("Expected %#v, but was %#v", expected, actual)
80 }
81
82 return true, nil
83 })
84 if err != nil {
85 t.Fatal(err)
86 }
87}