blob: e1b6b4fd48f9dd4ae98391badcc5219728376b29 [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 {
Ash Wilson7aca3cb2014-09-25 13:31:20 -040056 "href": "%s/flavors/detail?marker=2",
57 "rel": "next"
Ash Wilsonae609612014-09-24 14:48:30 -040058 }
Ash Wilson32be7e12014-09-24 14:47:47 -040059 ]
60 }
Ash Wilsonae609612014-09-24 14:48:30 -040061 `, testhelper.Server.URL)
Ash Wilson32be7e12014-09-24 14:47:47 -040062 case "2":
63 fmt.Fprintf(w, `{ "flavors": [] }`)
64 default:
65 t.Fatalf("Unexpected marker: [%s]", marker)
66 }
67 })
68
69 client := serviceClient()
70 pages := 0
71 err := List(client, ListFilterOptions{}).EachPage(func(page pagination.Page) (bool, error) {
72 pages++
73
74 actual, err := ExtractFlavors(page)
75 if err != nil {
76 return false, err
77 }
78
79 expected := []Flavor{
80 Flavor{ID: "1", Name: "m1.tiny", Disk: 1, RAM: 512, VCPUs: 1},
81 Flavor{ID: "2", Name: "m2.small", Disk: 10, RAM: 1024, VCPUs: 2},
82 }
83
84 if !reflect.DeepEqual(expected, actual) {
85 t.Errorf("Expected %#v, but was %#v", expected, actual)
86 }
87
88 return true, nil
89 })
90 if err != nil {
91 t.Fatal(err)
92 }
Ash Wilsonb60b38c2014-09-24 15:07:35 -040093 if pages != 1 {
94 t.Errorf("Expected one page, got %d", pages)
95 }
96}
97
98func TestGetFlavor(t *testing.T) {
99 testhelper.SetupHTTP()
100 defer testhelper.TeardownHTTP()
101
102 testhelper.Mux.HandleFunc("/flavors/12345", func(w http.ResponseWriter, r *http.Request) {
103 testhelper.TestMethod(t, r, "GET")
104 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
105
106 w.Header().Add("Content-Type", "application/json")
107 fmt.Fprintf(w, `
108 {
109 "flavor": {
110 "id": "1",
111 "name": "m1.tiny",
112 "disk": 1,
113 "ram": 512,
114 "vcpus": 1,
115 "rxtx_factor": 1
116 }
117 }
118 `)
119 })
120
121 client := serviceClient()
Ash Wilson8a8b86f2014-09-25 11:26:51 -0400122 actual, err := Get(client, "12345").Extract()
Ash Wilsonb60b38c2014-09-24 15:07:35 -0400123 if err != nil {
124 t.Fatalf("Unable to get flavor: %v", err)
125 }
Ash Wilsonb60b38c2014-09-24 15:07:35 -0400126
127 expected := &Flavor{
128 ID: "1",
129 Name: "m1.tiny",
130 Disk: 1,
131 RAM: 512,
132 VCPUs: 1,
133 RxTxFactor: 1,
134 }
135 if !reflect.DeepEqual(expected, actual) {
136 t.Errorf("Expected %#v, but was %#v", expected, actual)
137 }
Ash Wilson32be7e12014-09-24 14:47:47 -0400138}