blob: 473a4e7c2b7d8eac42884f6c1c0a34909fa4e5bc [file] [log] [blame]
Jon Perritte747a0f2014-09-29 19:54:55 -05001package volumes
Jon Perritt6d5561b2014-10-01 21:42:15 -05002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt6d5561b2014-10-01 21:42:15 -05008 "github.com/rackspace/gophercloud/pagination"
9 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilson407cfa32014-10-22 09:21:37 -040010 "github.com/rackspace/gophercloud/testhelper/client"
Jon Perritt6d5561b2014-10-01 21:42:15 -050011)
12
Jon Perritt6d5561b2014-10-01 21:42:15 -050013func TestList(t *testing.T) {
14 th.SetupHTTP()
15 defer th.TeardownHTTP()
16
17 th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
Ash Wilson407cfa32014-10-22 09:21:37 -040019 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritt6d5561b2014-10-01 21:42:15 -050020
21 w.Header().Add("Content-Type", "application/json")
22 w.WriteHeader(http.StatusOK)
23
24 fmt.Fprintf(w, `
25 {
26 "volumes": [
27 {
28 "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
29 "display_name": "vol-001"
30 },
31 {
32 "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
33 "display_name": "vol-002"
34 }
35 ]
36 }
37 `)
38 })
39
Jon Perritt6d5561b2014-10-01 21:42:15 -050040 count := 0
Ash Wilson407cfa32014-10-22 09:21:37 -040041 List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt6d5561b2014-10-01 21:42:15 -050042 count++
43 actual, err := ExtractVolumes(page)
44 if err != nil {
45 t.Errorf("Failed to extract volumes: %v", err)
46 return false, err
47 }
48
49 expected := []Volume{
50 Volume{
51 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
52 Name: "vol-001",
53 },
54 Volume{
55 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
56 Name: "vol-002",
57 },
58 }
59
60 th.CheckDeepEquals(t, expected, actual)
61
62 return true, nil
63 })
64
65 if count != 1 {
66 t.Errorf("Expected 1 page, got %d", count)
67 }
68}
69
70func TestGet(t *testing.T) {
71 th.SetupHTTP()
72 defer th.TeardownHTTP()
73
74 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
75 th.TestMethod(t, r, "GET")
Ash Wilson407cfa32014-10-22 09:21:37 -040076 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritt6d5561b2014-10-01 21:42:15 -050077
78 w.Header().Add("Content-Type", "application/json")
79 w.WriteHeader(http.StatusOK)
80 fmt.Fprintf(w, `
81{
82 "volume": {
83 "display_name": "vol-001",
84 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
85 }
86}
87 `)
88 })
89
Ash Wilson407cfa32014-10-22 09:21:37 -040090 v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -050091 th.AssertNoErr(t, err)
92
93 th.AssertEquals(t, v.Name, "vol-001")
94 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
95}
96
97func TestCreate(t *testing.T) {
98 th.SetupHTTP()
99 defer th.TeardownHTTP()
100
101 th.Mux.HandleFunc("/volumes", func(w http.ResponseWriter, r *http.Request) {
102 th.TestMethod(t, r, "POST")
Ash Wilson407cfa32014-10-22 09:21:37 -0400103 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500104 th.TestHeader(t, r, "Content-Type", "application/json")
105 th.TestHeader(t, r, "Accept", "application/json")
106 th.TestJSONRequest(t, r, `
107{
108 "volume": {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500109 "size": 4
Jon Perritt6d5561b2014-10-01 21:42:15 -0500110 }
111}
112 `)
113
114 w.Header().Add("Content-Type", "application/json")
115 w.WriteHeader(http.StatusCreated)
116
117 fmt.Fprintf(w, `
118{
119 "volume": {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500120 "size": 4,
Jon Perritt6d5561b2014-10-01 21:42:15 -0500121 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
122 }
123}
124 `)
125 })
126
Jon Perritt1c2356b2014-10-13 19:56:43 -0500127 options := &CreateOpts{Size: 4}
Ash Wilson407cfa32014-10-22 09:21:37 -0400128 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500129 th.AssertNoErr(t, err)
130
Jon Perritt1c2356b2014-10-13 19:56:43 -0500131 th.AssertEquals(t, n.Size, 4)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500132 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
133}
134
135func TestDelete(t *testing.T) {
136 th.SetupHTTP()
137 defer th.TeardownHTTP()
138
139 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
140 th.TestMethod(t, r, "DELETE")
Ash Wilson407cfa32014-10-22 09:21:37 -0400141 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500142 w.WriteHeader(http.StatusNoContent)
143 })
144
Ash Wilson407cfa32014-10-22 09:21:37 -0400145 err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
Jon Perritt57ba7632014-10-02 20:32:22 -0500146 th.AssertNoErr(t, err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500147}
Jon Perritt84857132014-10-07 11:59:14 -0500148
149func TestUpdate(t *testing.T) {
150 th.SetupHTTP()
151 defer th.TeardownHTTP()
152
153 th.Mux.HandleFunc("/volumes/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
154 th.TestMethod(t, r, "PUT")
Ash Wilson407cfa32014-10-22 09:21:37 -0400155 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritt84857132014-10-07 11:59:14 -0500156 w.WriteHeader(http.StatusOK)
157 fmt.Fprintf(w, `
158 {
159 "volume": {
160 "display_name": "vol-002",
161 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
162 }
163 }
164 `)
165 })
166
167 options := &UpdateOpts{Name: "vol-002"}
Ash Wilson407cfa32014-10-22 09:21:37 -0400168 v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract()
Jon Perritt84857132014-10-07 11:59:14 -0500169 th.AssertNoErr(t, err)
170 th.CheckEquals(t, "vol-002", v.Name)
171}