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" |
| 9 | "github.com/rackspace/gophercloud/testhelper" |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 10 | fake "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) { |
| 14 | testhelper.SetupHTTP() |
| 15 | defer testhelper.TeardownHTTP() |
| 16 | |
| 17 | testhelper.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { |
| 18 | testhelper.TestMethod(t, r, "GET") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 19 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.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 "": |
| 26 | fmt.Fprintf(w, serverListBody) |
| 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 |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 35 | err := List(fake.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 | } |
| 46 | equalServers(t, serverHerp, actual[0]) |
| 47 | equalServers(t, serverDerp, actual[1]) |
| 48 | |
| 49 | return true, nil |
| 50 | }) |
| 51 | |
Jamie Hannaford | cf00172 | 2014-10-16 12:54:07 +0200 | [diff] [blame] | 52 | testhelper.AssertNoErr(t, err) |
| 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) { |
| 60 | testhelper.SetupHTTP() |
| 61 | defer testhelper.TeardownHTTP() |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 62 | |
| 63 | testhelper.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) { |
| 64 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 65 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 66 | testhelper.TestJSONRequest(t, r, `{ |
| 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") |
| 76 | fmt.Fprintf(w, singleServerBody) |
| 77 | }) |
| 78 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 79 | client := fake.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 | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 89 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func TestDeleteServer(t *testing.T) { |
| 93 | testhelper.SetupHTTP() |
| 94 | defer testhelper.TeardownHTTP() |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 95 | |
| 96 | testhelper.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) { |
| 97 | testhelper.TestMethod(t, r, "DELETE") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 98 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 99 | |
| 100 | w.WriteHeader(http.StatusNoContent) |
| 101 | }) |
| 102 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 103 | client := fake.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) { |
| 111 | testhelper.SetupHTTP() |
| 112 | defer testhelper.TeardownHTTP() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 113 | |
| 114 | testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 115 | testhelper.TestMethod(t, r, "GET") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 116 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 117 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 118 | |
| 119 | fmt.Fprintf(w, singleServerBody) |
| 120 | }) |
| 121 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 122 | client := fake.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 | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 128 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | func TestUpdateServer(t *testing.T) { |
| 132 | testhelper.SetupHTTP() |
| 133 | defer testhelper.TeardownHTTP() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 134 | |
| 135 | testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 136 | testhelper.TestMethod(t, r, "PUT") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 137 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 138 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 139 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 140 | testhelper.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`) |
| 141 | |
| 142 | fmt.Fprintf(w, singleServerBody) |
| 143 | }) |
| 144 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 145 | client := fake.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 | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 151 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | func TestChangeServerAdminPassword(t *testing.T) { |
| 155 | testhelper.SetupHTTP() |
| 156 | defer testhelper.TeardownHTTP() |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 157 | |
| 158 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 159 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 160 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 161 | testhelper.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
| 162 | |
| 163 | w.WriteHeader(http.StatusAccepted) |
| 164 | }) |
| 165 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 166 | client := fake.ServiceClient() |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 167 | err := ChangeAdminPassword(client, "1234asdf", "new-password") |
| 168 | if err != nil { |
| 169 | t.Errorf("Unexpected ChangeAdminPassword error: %v", err) |
| 170 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | func TestRebootServer(t *testing.T) { |
| 174 | testhelper.SetupHTTP() |
| 175 | defer testhelper.TeardownHTTP() |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 176 | |
| 177 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 178 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 179 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 180 | testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`) |
| 181 | |
| 182 | w.WriteHeader(http.StatusAccepted) |
| 183 | }) |
| 184 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 185 | client := fake.ServiceClient() |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 186 | err := Reboot(client, "1234asdf", SoftReboot) |
| 187 | if err != nil { |
| 188 | t.Errorf("Unexpected Reboot error: %v", err) |
| 189 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | func TestRebuildServer(t *testing.T) { |
| 193 | testhelper.SetupHTTP() |
| 194 | defer testhelper.TeardownHTTP() |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 195 | |
| 196 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 197 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 198 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 199 | testhelper.TestJSONRequest(t, r, ` |
| 200 | { |
| 201 | "rebuild": { |
| 202 | "name": "new-name", |
| 203 | "adminPass": "swordfish", |
| 204 | "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 205 | "accessIPv4": "1.2.3.4" |
| 206 | } |
| 207 | } |
| 208 | `) |
| 209 | |
| 210 | w.WriteHeader(http.StatusAccepted) |
| 211 | w.Header().Add("Content-Type", "application/json") |
| 212 | fmt.Fprintf(w, singleServerBody) |
| 213 | }) |
| 214 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 215 | opts := RebuildOpts{ |
| 216 | Name: "new-name", |
| 217 | AdminPass: "swordfish", |
| 218 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 219 | AccessIPv4: "1.2.3.4", |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 222 | actual, err := Rebuild(serviceClient(), "1234asdf", opts).Extract() |
| 223 | testhelper.AssertNoErr(t, err) |
| 224 | |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 225 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | func TestResizeServer(t *testing.T) { |
| 229 | testhelper.SetupHTTP() |
| 230 | defer testhelper.TeardownHTTP() |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 231 | |
| 232 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 233 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 234 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 235 | testhelper.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`) |
| 236 | |
| 237 | w.WriteHeader(http.StatusAccepted) |
| 238 | }) |
| 239 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 240 | client := fake.ServiceClient() |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 241 | err := Resize(client, "1234asdf", "2") |
| 242 | if err != nil { |
| 243 | t.Errorf("Unexpected Reboot error: %v", err) |
| 244 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | func TestConfirmResize(t *testing.T) { |
| 248 | testhelper.SetupHTTP() |
| 249 | defer testhelper.TeardownHTTP() |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 250 | |
| 251 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 252 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 253 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 254 | testhelper.TestJSONRequest(t, r, `{ "confirmResize": null }`) |
| 255 | |
| 256 | w.WriteHeader(http.StatusNoContent) |
| 257 | }) |
| 258 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 259 | client := fake.ServiceClient() |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 260 | err := ConfirmResize(client, "1234asdf") |
| 261 | if err != nil { |
| 262 | t.Errorf("Unexpected ConfirmResize error: %v", err) |
| 263 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | func TestRevertResize(t *testing.T) { |
| 267 | testhelper.SetupHTTP() |
| 268 | defer testhelper.TeardownHTTP() |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 269 | |
| 270 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 271 | testhelper.TestMethod(t, r, "POST") |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 272 | testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 273 | testhelper.TestJSONRequest(t, r, `{ "revertResize": null }`) |
| 274 | |
| 275 | w.WriteHeader(http.StatusAccepted) |
| 276 | }) |
| 277 | |
Jamie Hannaford | 2aaf1a6 | 2014-10-16 12:55:50 +0200 | [diff] [blame^] | 278 | client := fake.ServiceClient() |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 279 | err := RevertResize(client, "1234asdf") |
| 280 | if err != nil { |
| 281 | t.Errorf("Unexpected RevertResize error: %v", err) |
| 282 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 283 | } |