blob: e13a34831a395bff5d42a660a63309800e5697d3 [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
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("/snapshots", 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 "snapshots": [
27 {
28 "id": "289da7f8-6440-407c-9fb4-7db01ec49164",
29 "display_name": "snapshot-001"
30 },
31 {
32 "id": "96c3bda7-c82a-4f50-be73-ca7621794835",
33 "display_name": "snapshot-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 := ExtractSnapshots(page)
44 if err != nil {
45 t.Errorf("Failed to extract snapshots: %v", err)
46 return false, err
47 }
48
49 expected := []Snapshot{
50 Snapshot{
51 ID: "289da7f8-6440-407c-9fb4-7db01ec49164",
52 Name: "snapshot-001",
53 },
54 Snapshot{
55 ID: "96c3bda7-c82a-4f50-be73-ca7621794835",
56 Name: "snapshot-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("/snapshots/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 "snapshot": {
83 "display_name": "snapshot-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, "snapshot-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("/snapshots", 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 "snapshot": {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500109 "volume_id": "1234",
Jon Perritt6d5561b2014-10-01 21:42:15 -0500110 "display_name": "snapshot-001"
111 }
112}
113 `)
114
115 w.Header().Add("Content-Type", "application/json")
116 w.WriteHeader(http.StatusCreated)
117
118 fmt.Fprintf(w, `
119{
120 "snapshot": {
Jon Perritt1c2356b2014-10-13 19:56:43 -0500121 "volume_id": "1234",
Jon Perritt6d5561b2014-10-01 21:42:15 -0500122 "display_name": "snapshot-001",
123 "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22"
124 }
125}
126 `)
127 })
128
Jon Perritt1c2356b2014-10-13 19:56:43 -0500129 options := &CreateOpts{VolumeID: "1234", Name: "snapshot-001"}
Ash Wilson407cfa32014-10-22 09:21:37 -0400130 n, err := Create(client.ServiceClient(), options).Extract()
Jon Perritt6d5561b2014-10-01 21:42:15 -0500131 th.AssertNoErr(t, err)
132
Jon Perritt1c2356b2014-10-13 19:56:43 -0500133 th.AssertEquals(t, n.VolumeID, "1234")
Jon Perritt6d5561b2014-10-01 21:42:15 -0500134 th.AssertEquals(t, n.Name, "snapshot-001")
135 th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22")
136}
137
Jon Perritte357e3d2014-10-03 01:53:57 -0500138func TestUpdateMetadata(t *testing.T) {
139 th.SetupHTTP()
140 defer th.TeardownHTTP()
141
142 th.Mux.HandleFunc("/snapshots/123/metadata", func(w http.ResponseWriter, r *http.Request) {
143 th.TestMethod(t, r, "PUT")
Ash Wilson407cfa32014-10-22 09:21:37 -0400144 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritte357e3d2014-10-03 01:53:57 -0500145 th.TestHeader(t, r, "Content-Type", "application/json")
146 th.TestJSONRequest(t, r, `
147 {
148 "metadata": {
149 "key": "v1"
150 }
151 }
152 `)
153
154 fmt.Fprintf(w, `
155 {
156 "metadata": {
157 "key": "v1"
158 }
159 }
160 `)
161 })
162
163 expected := map[string]interface{}{"key": "v1"}
164
165 options := &UpdateMetadataOpts{
166 Metadata: map[string]interface{}{
167 "key": "v1",
168 },
169 }
Ash Wilson407cfa32014-10-22 09:21:37 -0400170 actual, err := UpdateMetadata(client.ServiceClient(), "123", options).ExtractMetadata()
Jon Perritte357e3d2014-10-03 01:53:57 -0500171
172 th.AssertNoErr(t, err)
173 th.AssertDeepEquals(t, actual, expected)
174}
175
Jon Perritt6d5561b2014-10-01 21:42:15 -0500176func TestDelete(t *testing.T) {
177 th.SetupHTTP()
178 defer th.TeardownHTTP()
179
180 th.Mux.HandleFunc("/snapshots/d32019d3-bc6e-4319-9c1d-6722fc136a22", func(w http.ResponseWriter, r *http.Request) {
181 th.TestMethod(t, r, "DELETE")
Ash Wilson407cfa32014-10-22 09:21:37 -0400182 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500183 w.WriteHeader(http.StatusNoContent)
184 })
185
Ash Wilson407cfa32014-10-22 09:21:37 -0400186 err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22")
Jon Perritt57ba7632014-10-02 20:32:22 -0500187 th.AssertNoErr(t, err)
Jon Perritt6d5561b2014-10-01 21:42:15 -0500188}