blob: 45e3e20ff56d05434004924122bff35657591965 [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 }
Ash Wilsonae609612014-09-24 14:48:30 -040053 ],
54 "flavors_links": [
55 {
56 "href": "%s/flavors?marker=2"
57 }
Ash Wilson32be7e12014-09-24 14:47:47 -040058 ]
59 }
Ash Wilsonae609612014-09-24 14:48:30 -040060 `, testhelper.Server.URL)
Ash Wilson32be7e12014-09-24 14:47:47 -040061 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 }
92}