blob: d29cc0d0cc9537b04986f16c5a2d4f6f0e2eb798 [file] [log] [blame]
Jon Perritte747a0f2014-09-29 19:54:55 -05001package snapshots
Jon Perritt6d5561b2014-10-01 21:42:15 -05002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10 th "github.com/rackspace/gophercloud/testhelper"
11)
12
13const TokenID = "123"
14
15func ServiceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{
18 TokenID: TokenID,
19 },
20 Endpoint: th.Endpoint(),
21 }
22}
23
24func TestList(t *testing.T) {
25 th.SetupHTTP()
26 defer th.TeardownHTTP()
27
28 th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) {
29 th.TestMethod(t, r, "GET")
30 th.TestHeader(t, r, "X-Auth-Token", TokenID)
31
32 w.Header().Add("Content-Type", "application/json")
33 w.WriteHeader(http.StatusOK)
34
35 fmt.Fprintf(w, `
36 {
37 "snapshots": [
38 {
39 "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
40 "display_name": "snapshot-001"
41 },
42 {
43 "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
44 "display_name": "snapshot-002"
45 }
46 ]
47 }
48 `)
49 })
50
51 client := ServiceClient()
52 count := 0
53
54 List(client, &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
55 count++
56 actual, err := ExtractSnapshots(page)
57 if err != nil {
58 t.Errorf("Failed to extract snapshots: %v", err)
59 return false, err
60 }
61
62 expected := []Snapshot{
63 Snapshot{
64 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
65 Name: "snapshot-001",
66 },
67 Snapshot{
68 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
69 Name: "snapshot-002",
70 },
71 }
72
73 th.CheckDeepEquals(t, expected, actual)
74
75 return true, nil
76 })
77
78 if count != 1 {
79 t.Errorf("Expected 1 page, got %d", count)
80 }
81}
82
83func TestGet(t *testing.T) {
84 th.SetupHTTP()
85 defer th.TeardownHTTP()
86
87 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
88 th.TestMethod(t, r, "GET")
89 th.TestHeader(t, r, "X-Auth-Token", TokenID)
90
91 w.Header().Add("Content-Type", "application/json")
92 w.WriteHeader(http.StatusOK)
93 fmt.Fprintf(w, `
94{
95 "snapshot": {
96 "display_name": "snapshot-001",
97 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
98 }
99}
100 `)
101 })
102
103 v, err := Get(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract()
104 th.AssertNoErr(t, err)
105
106 th.AssertEquals(t, v.Name, "snapshot-001")
107 th.AssertEquals(t, v.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
108}
109
110func TestCreate(t *testing.T) {
111 th.SetupHTTP()
112 defer th.TeardownHTTP()
113
114 th.Mux.HandleFunc("/snapshots", func(w http.ResponseWriter, r *http.Request) {
115 th.TestMethod(t, r, "POST")
116 th.TestHeader(t, r, "X-Auth-Token", TokenID)
117 th.TestHeader(t, r, "Content-Type", "application/json")
118 th.TestHeader(t, r, "Accept", "application/json")
119 th.TestJSONRequest(t, r, `
120{
121 "snapshot": {
122 "display_name": "snapshot-001"
123 }
124}
125 `)
126
127 w.Header().Add("Content-Type", "application/json")
128 w.WriteHeader(http.StatusCreated)
129
130 fmt.Fprintf(w, `
131{
132 "snapshot": {
133 "display_name": "snapshot-001",
134 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
135 }
136}
137 `)
138 })
139
140 options := &CreateOpts{Name: "snapshot-001"}
141 n, err := Create(ServiceClient(), options).Extract()
142 th.AssertNoErr(t, err)
143
144 th.AssertEquals(t, n.Name, "snapshot-001")
145 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
146}
147
Jon Perritte357e3d2014-10-03 01:53:57 -0500148func TestUpdateMetadata(t *testing.T) {
149 th.SetupHTTP()
150 defer th.TeardownHTTP()
151
152 th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) {
153 th.TestMethod(t, r, "PUT")
154 th.TestHeader(t, r, "X-Auth-Token", TokenID)
155 th.TestHeader(t, r, "Content-Type", "application/json")
156 th.TestJSONRequest(t, r, `
157 {
158 "metadata": {
159 "key": "v1"
160 }
161 }
162 `)
163
164 fmt.Fprintf(w, `
165 {
166 "metadata": {
167 "key": "v1"
168 }
169 }
170 `)
171 })
172
173 expected := map[string]interface{}{"key": "v1"}
174
175 options := &UpdateMetadataOpts{
176 Metadata: map[string]interface{}{
177 "key": "v1",
178 },
179 }
180 actual, err := UpdateMetadata(ServiceClient(), "123", options).ExtractMetadata()
181
182 th.AssertNoErr(t, err)
183 th.AssertDeepEquals(t, actual, expected)
184}
185
Jon Perritt6d5561b2014-10-01 21:42:15 -0500186func TestDelete(t *testing.T) {
187 th.SetupHTTP()
188 defer th.TeardownHTTP()
189
190 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
191 th.TestMethod(t, r, "DELETE")
192 th.TestHeader(t, r, "X-Auth-Token", TokenID)
193 w.WriteHeader(http.StatusNoContent)
194 })
195
Jon Perritt57ba7632014-10-02 20:32:22 -0500196 err := Delete(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
197 th.AssertNoErr(t, err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500198}