blob: a66b54c2e053b75f1bc1d256f5dc6d984b8c36e0 [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 }
Ash Wilsonb60b38c2014-09-24 15:07:35 -040092 if pages != 1 {
93 t.Errorf("Expected one page, got %d", pages)
94 }
95}
96
97func 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 Wilson8a8b86f2014-09-25 11:26:51 -0400121 actual, err := Get(client, "12345").Extract()
Ash Wilsonb60b38c2014-09-24 15:07:35 -0400122 if err != nil {
123 t.Fatalf("Unable to get flavor: %v", err)
124 }
Ash Wilsonb60b38c2014-09-24 15:07:35 -0400125
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 Wilson32be7e12014-09-24 14:47:47 -0400137}