blob: ddfa81b1cbe3ab746e5aea28c3e769fee315b419 [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": {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500122 "volume_id": "1234",
Jon Perritt6d5561b2014-10-01 21:42:15 -0500123 "display_name": "snapshot-001"
124 }
125}
126 `)
127
128 w.Header().Add("Content-Type", "application/json")
129 w.WriteHeader(http.StatusCreated)
130
131 fmt.Fprintf(w, `
132{
133 "snapshot": {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500134 "volume_id": "1234",
Jon Perritt6d5561b2014-10-01 21:42:15 -0500135 "display_name": "snapshot-001",
136 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
137 }
138}
139 `)
140 })
141
Jon Perritt1c2356b2014-10-13 19:56:43 -0500142 options := &CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
Jon Perritt6d5561b2014-10-01 21:42:15 -0500143 n, err := Create(ServiceClient(), options).Extract()
144 th.AssertNoErr(t, err)
145
Jon Perritt1c2356b2014-10-13 19:56:43 -0500146 th.AssertEquals(t, n.VolumeID, "1234")
Jon Perritt6d5561b2014-10-01 21:42:15 -0500147 th.AssertEquals(t, n.Name, "snapshot-001")
148 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
149}
150
Jon Perritte357e3d2014-10-03 01:53:57 -0500151func TestUpdateMetadata(t *testing.T) {
152 th.SetupHTTP()
153 defer th.TeardownHTTP()
154
155 th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) {
156 th.TestMethod(t, r, "PUT")
157 th.TestHeader(t, r, "X-Auth-Token", TokenID)
158 th.TestHeader(t, r, "Content-Type", "application/json")
159 th.TestJSONRequest(t, r, `
160 {
161 "metadata": {
162 "key": "v1"
163 }
164 }
165 `)
166
167 fmt.Fprintf(w, `
168 {
169 "metadata": {
170 "key": "v1"
171 }
172 }
173 `)
174 })
175
176 expected := map[string]interface{}{"key": "v1"}
177
178 options := &UpdateMetadataOpts{
179 Metadata: map[string]interface{}{
180 "key": "v1",
181 },
182 }
183 actual, err := UpdateMetadata(ServiceClient(), "123", options).ExtractMetadata()
184
185 th.AssertNoErr(t, err)
186 th.AssertDeepEquals(t, actual, expected)
187}
188
Jon Perritt6d5561b2014-10-01 21:42:15 -0500189func TestDelete(t *testing.T) {
190 th.SetupHTTP()
191 defer th.TeardownHTTP()
192
193 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
194 th.TestMethod(t, r, "DELETE")
195 th.TestHeader(t, r, "X-Auth-Token", TokenID)
196 w.WriteHeader(http.StatusNoContent)
197 })
198
Jon Perritt57ba7632014-10-02 20:32:22 -0500199 err := Delete(ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
200 th.AssertNoErr(t, err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500201}