Jon Perritt | e747a0f | 2014-09-29 19:54:55 -0500 | [diff] [blame] | 1 | package volumes |
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("/volumes", 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 | "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 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 := 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 | |
| 70 | func 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 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 | "volume": { |
| 83 | "display_name": "vol-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, "vol-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("/volumes", 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 | "volume": { |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 109 | "size": 4 |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 110 | } |
| 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 Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 120 | "size": 4, |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 121 | "id": "d32019d3-bc6e-4319-9c1d-6722fc136a22" |
| 122 | } |
| 123 | } |
| 124 | `) |
| 125 | }) |
| 126 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 127 | options := &CreateOpts{Size: 4} |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame^] | 128 | n, err := Create(client.ServiceClient(), options).Extract() |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 129 | th.AssertNoErr(t, err) |
| 130 | |
Jon Perritt | 1c2356b | 2014-10-13 19:56:43 -0500 | [diff] [blame] | 131 | th.AssertEquals(t, n.Size, 4) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 132 | th.AssertEquals(t, n.ID, "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
| 133 | } |
| 134 | |
| 135 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame^] | 141 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 142 | w.WriteHeader(http.StatusNoContent) |
| 143 | }) |
| 144 | |
Ash Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame^] | 145 | err := Delete(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22") |
Jon Perritt | 57ba763 | 2014-10-02 20:32:22 -0500 | [diff] [blame] | 146 | th.AssertNoErr(t, err) |
Jon Perritt | 6d5561b | 2014-10-01 21:42:15 -0500 | [diff] [blame] | 147 | } |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 148 | |
| 149 | func 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame^] | 155 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 156 | 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 Wilson | 407cfa3 | 2014-10-22 09:21:37 -0400 | [diff] [blame^] | 168 | v, err := Update(client.ServiceClient(), "d32019d3-bc6e-4319-9c1d-6722fc136a22", options).Extract() |
Jon Perritt | 8485713 | 2014-10-07 11:59:14 -0500 | [diff] [blame] | 169 | th.AssertNoErr(t, err) |
| 170 | th.CheckEquals(t, "vol-002", v.Name) |
| 171 | } |