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