Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/pagination" |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | "github.com/rackspace/gophercloud/testhelper/client" |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 11 | ) |
| 12 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 13 | func TestListServers(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 16 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 17 | th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { |
| 18 | th.TestMethod(t, r, "GET") |
| 19 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 20 | |
| 21 | w.Header().Add("Content-Type", "application/json") |
| 22 | r.ParseForm() |
| 23 | marker := r.Form.Get("marker") |
| 24 | switch marker { |
| 25 | case "": |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 26 | fmt.Fprintf(w, ServerListBody) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 27 | case "9e5476bd-a4ec-4653-93d6-72c93aa682ba": |
| 28 | fmt.Fprintf(w, `{ "servers": [] }`) |
| 29 | default: |
| 30 | t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker) |
| 31 | } |
| 32 | }) |
| 33 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 34 | pages := 0 |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 35 | err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 36 | pages++ |
| 37 | |
| 38 | actual, err := ExtractServers(page) |
| 39 | if err != nil { |
| 40 | return false, err |
| 41 | } |
| 42 | |
| 43 | if len(actual) != 2 { |
| 44 | t.Fatalf("Expected 2 servers, got %d", len(actual)) |
| 45 | } |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame^] | 46 | th.CheckDeepEquals(t, ServerHerp, actual[0]) |
| 47 | th.CheckDeepEquals(t, ServerDerp, actual[1]) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 48 | |
| 49 | return true, nil |
| 50 | }) |
| 51 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 52 | th.AssertNoErr(t, err) |
Jamie Hannaford | cf00172 | 2014-10-16 12:54:07 +0200 | [diff] [blame] | 53 | |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 54 | if pages != 1 { |
| 55 | t.Errorf("Expected 1 page, saw %d", pages) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestCreateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 60 | th.SetupHTTP() |
| 61 | defer th.TeardownHTTP() |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 62 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 63 | th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) { |
| 64 | th.TestMethod(t, r, "POST") |
| 65 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 66 | th.TestJSONRequest(t, r, `{ |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 67 | "server": { |
| 68 | "name": "derp", |
| 69 | "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 70 | "flavorRef": "1" |
| 71 | } |
| 72 | }`) |
| 73 | |
| 74 | w.WriteHeader(http.StatusAccepted) |
| 75 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 76 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 77 | }) |
| 78 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 79 | client := client.ServiceClient() |
Ash Wilson | 3a0e3b4 | 2014-10-02 10:58:09 -0400 | [diff] [blame] | 80 | actual, err := Create(client, CreateOpts{ |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 81 | Name: "derp", |
| 82 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 83 | FlavorRef: "1", |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 84 | }).Extract() |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 85 | if err != nil { |
| 86 | t.Fatalf("Unexpected Create error: %v", err) |
| 87 | } |
| 88 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame^] | 89 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func TestDeleteServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 93 | th.SetupHTTP() |
| 94 | defer th.TeardownHTTP() |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 95 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 96 | th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) { |
| 97 | th.TestMethod(t, r, "DELETE") |
| 98 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 99 | |
| 100 | w.WriteHeader(http.StatusNoContent) |
| 101 | }) |
| 102 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 103 | client := client.ServiceClient() |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 104 | err := Delete(client, "asdfasdfasdf") |
| 105 | if err != nil { |
| 106 | t.Fatalf("Unexpected Delete error: %v", err) |
| 107 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | func TestGetServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 111 | th.SetupHTTP() |
| 112 | defer th.TeardownHTTP() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 113 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 114 | th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 115 | th.TestMethod(t, r, "GET") |
| 116 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 117 | th.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 118 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 119 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 120 | }) |
| 121 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 122 | client := client.ServiceClient() |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 123 | actual, err := Get(client, "1234asdf").Extract() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 124 | if err != nil { |
| 125 | t.Fatalf("Unexpected Get error: %v", err) |
| 126 | } |
| 127 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame^] | 128 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | func TestUpdateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 132 | th.SetupHTTP() |
| 133 | defer th.TeardownHTTP() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 134 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 135 | th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 136 | th.TestMethod(t, r, "PUT") |
| 137 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 138 | th.TestHeader(t, r, "Accept", "application/json") |
| 139 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 140 | th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 141 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 142 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 143 | }) |
| 144 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 145 | client := client.ServiceClient() |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 146 | actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 147 | if err != nil { |
| 148 | t.Fatalf("Unexpected Update error: %v", err) |
| 149 | } |
| 150 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame^] | 151 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | func TestChangeServerAdminPassword(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 155 | th.SetupHTTP() |
| 156 | defer th.TeardownHTTP() |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 157 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 158 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 159 | th.TestMethod(t, r, "POST") |
| 160 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 161 | th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 162 | |
| 163 | w.WriteHeader(http.StatusAccepted) |
| 164 | }) |
| 165 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 166 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 167 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | func TestRebootServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 171 | th.SetupHTTP() |
| 172 | defer th.TeardownHTTP() |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 173 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 174 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 175 | th.TestMethod(t, r, "POST") |
| 176 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 177 | th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`) |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 178 | |
| 179 | w.WriteHeader(http.StatusAccepted) |
| 180 | }) |
| 181 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 182 | res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot) |
| 183 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | func TestRebuildServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 187 | th.SetupHTTP() |
| 188 | defer th.TeardownHTTP() |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 189 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 190 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 191 | th.TestMethod(t, r, "POST") |
| 192 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 193 | th.TestJSONRequest(t, r, ` |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 194 | { |
| 195 | "rebuild": { |
| 196 | "name": "new-name", |
| 197 | "adminPass": "swordfish", |
| 198 | "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 199 | "accessIPv4": "1.2.3.4" |
| 200 | } |
| 201 | } |
| 202 | `) |
| 203 | |
| 204 | w.WriteHeader(http.StatusAccepted) |
| 205 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 206 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 207 | }) |
| 208 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 209 | opts := RebuildOpts{ |
| 210 | Name: "new-name", |
| 211 | AdminPass: "swordfish", |
| 212 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 213 | AccessIPv4: "1.2.3.4", |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 216 | actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract() |
| 217 | th.AssertNoErr(t, err) |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 218 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame^] | 219 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | func TestResizeServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 223 | th.SetupHTTP() |
| 224 | defer th.TeardownHTTP() |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 225 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 226 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 227 | th.TestMethod(t, r, "POST") |
| 228 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 229 | th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`) |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 230 | |
| 231 | w.WriteHeader(http.StatusAccepted) |
| 232 | }) |
| 233 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 234 | res := Resize(client.ServiceClient(), "1234asdf", "2") |
| 235 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | func TestConfirmResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 239 | th.SetupHTTP() |
| 240 | defer th.TeardownHTTP() |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 241 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 242 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 243 | th.TestMethod(t, r, "POST") |
| 244 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 245 | th.TestJSONRequest(t, r, `{ "confirmResize": null }`) |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 246 | |
| 247 | w.WriteHeader(http.StatusNoContent) |
| 248 | }) |
| 249 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 250 | res := ConfirmResize(client.ServiceClient(), "1234asdf") |
| 251 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | func TestRevertResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 255 | th.SetupHTTP() |
| 256 | defer th.TeardownHTTP() |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 257 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 258 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 259 | th.TestMethod(t, r, "POST") |
| 260 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 261 | th.TestJSONRequest(t, r, `{ "revertResize": null }`) |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 262 | |
| 263 | w.WriteHeader(http.StatusAccepted) |
| 264 | }) |
| 265 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 266 | res := RevertResize(client.ServiceClient(), "1234asdf") |
| 267 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 268 | } |