blob: bc9b82e79cecf161f8858f1ebcebf504252d7bbc [file] [log] [blame]
Ash Wilson32be7e12014-09-24 14:47:47 -04001package flavors
2
3import (
4 "fmt"
5 "net/http"
6 "reflect"
7 "testing"
8
Ash Wilson32be7e12014-09-24 14:47:47 -04009 "github.com/rackspace/gophercloud/pagination"
Jon Perritt2082ce32014-10-14 02:27:50 -050010 th "github.com/rackspace/gophercloud/testhelper"
Jon Perritt9af03852014-10-07 13:41:35 -050011 fake "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilson32be7e12014-09-24 14:47:47 -040012)
13
14const tokenID = "blerb"
15
Ash Wilson32be7e12014-09-24 14:47:47 -040016func TestListFlavors(t *testing.T) {
Jon Perritt2082ce32014-10-14 02:27:50 -050017 th.SetupHTTP()
18 defer th.TeardownHTTP()
Ash Wilson32be7e12014-09-24 14:47:47 -040019
Jon Perritt2082ce32014-10-14 02:27:50 -050020 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 Wilson32be7e12014-09-24 14:47:47 -040023
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 Wilsonae609612014-09-24 14:48:30 -040046 ],
47 "flavors_links": [
48 {
Ash Wilson7aca3cb2014-09-25 13:31:20 -040049 "href": "%s/flavors/detail?marker=2",
50 "rel": "next"
Ash Wilsonae609612014-09-24 14:48:30 -040051 }
Ash Wilson32be7e12014-09-24 14:47:47 -040052 ]
53 }
Jon Perritt2082ce32014-10-14 02:27:50 -050054 `, th.Server.URL)
Ash Wilson32be7e12014-09-24 14:47:47 -040055 case "2":
56 fmt.Fprintf(w, `{ "flavors": [] }`)
57 default:
58 t.Fatalf("Unexpected marker: [%s]", marker)
59 }
60 })
61
Ash Wilson32be7e12014-09-24 14:47:47 -040062 pages := 0
Jon Perritt9af03852014-10-07 13:41:35 -050063 err := List(fake.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilson32be7e12014-09-24 14:47:47 -040064 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 Wilsonb60b38c2014-09-24 15:07:35 -040085 if pages != 1 {
86 t.Errorf("Expected one page, got %d", pages)
87 }
88}
89
90func TestGetFlavor(t *testing.T) {
Jon Perritt2082ce32014-10-14 02:27:50 -050091 th.SetupHTTP()
92 defer th.TeardownHTTP()
Ash Wilsonb60b38c2014-09-24 15:07:35 -040093
Jon Perritt2082ce32014-10-14 02:27:50 -050094 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 Wilsonb60b38c2014-09-24 15:07:35 -040097
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 Perritt9af03852014-10-07 13:41:35 -0500113 actual, err := Get(fake.ServiceClient(), "12345").Extract()
Ash Wilsonb60b38c2014-09-24 15:07:35 -0400114 if err != nil {
115 t.Fatalf("Unable to get flavor: %v", err)
116 }
Ash Wilsonb60b38c2014-09-24 15:07:35 -0400117
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 Wilson32be7e12014-09-24 14:47:47 -0400129}