Jon Perritt | e747a0f | 2014-09-29 19:54:55 -0500 | [diff] [blame] | 1 | package snapshots |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/testhelper/client" |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 11 | ) |
| 12 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 13 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 19 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 20 | |
| 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 Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 40 | count := 0 |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 41 | List(client.ServiceClient(), &ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 42 | 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 | |
| 70 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 76 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 77 | |
| 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 90 | v, err := Get(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22").Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 91 | 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 | |
| 97 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 103 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 104 | 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 Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 109 | "volume_id": "1234", |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 110 | "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 Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 121 | "volume_id": "1234", |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 122 | "display_name": "snapshot-001", |
| 123 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 124 | } |
| 125 | } |
| 126 | `) |
| 127 | }) |
| 128 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 129 | options := &CreateOpts{VolumeID: "1234", Name: "snapshot-001"} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 130 | n, err := Create(client.ServiceClient(), options).Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 131 | th.AssertNoErr(t, err) |
| 132 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 133 | th.AssertEquals(t, n.VolumeID, "1234") |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 134 | th.AssertEquals(t, n.Name, "snapshot-001") |
| 135 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 136 | } |
| 137 | |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 138 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 144 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 145 | 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 170 | actual, err := UpdateMetadata(client.ServiceClient(), "123", options).ExtractMetadata() |
Jon Perritt | e357e3d | 2014-10-03 01:53:57 -0500 | [diff] [blame] | 171 | |
| 172 | th.AssertNoErr(t, err) |
| 173 | th.AssertDeepEquals(t, actual, expected) |
| 174 | } |
| 175 | |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 176 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 182 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 183 | w.WriteHeader(http.StatusNoContent) |
| 184 | }) |
| 185 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame] | 186 | err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 187 | th.AssertNoErr(t, err) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 188 | } |