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