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 | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame^] | 62 | HandleServerCreationSuccessfully(t, SingleServerBody) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 63 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame^] | 64 | actual, err := Create(client.ServiceClient(), CreateOpts{ |
Ash Wilson | 6a310e0 | 2014-09-29 08:24:02 -0400 | [diff] [blame] | 65 | Name: "derp", |
| 66 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 67 | FlavorRef: "1", |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 68 | }).Extract() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame^] | 69 | th.AssertNoErr(t, err) |
Ash Wilson | 3204d0d | 2014-09-25 10:37:44 -0400 | [diff] [blame] | 70 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 71 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | func TestDeleteServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 75 | th.SetupHTTP() |
| 76 | defer th.TeardownHTTP() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame^] | 77 | HandleServerDeletionSuccessfully(t) |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 78 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame^] | 79 | err := Delete(client.ServiceClient(), "asdfasdfasdf") |
| 80 | th.AssertNoErr(t, err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | func TestGetServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 84 | th.SetupHTTP() |
| 85 | defer th.TeardownHTTP() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 86 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 87 | th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 88 | th.TestMethod(t, r, "GET") |
| 89 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 90 | th.TestHeader(t, r, "Accept", "application/json") |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 91 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 92 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 93 | }) |
| 94 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 95 | client := client.ServiceClient() |
Ash Wilson | d27e0ff | 2014-09-25 11:50:31 -0400 | [diff] [blame] | 96 | actual, err := Get(client, "1234asdf").Extract() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 97 | if err != nil { |
| 98 | t.Fatalf("Unexpected Get error: %v", err) |
| 99 | } |
| 100 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 101 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func TestUpdateServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 105 | th.SetupHTTP() |
| 106 | defer th.TeardownHTTP() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 107 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 108 | th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 109 | th.TestMethod(t, r, "PUT") |
| 110 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 111 | th.TestHeader(t, r, "Accept", "application/json") |
| 112 | th.TestHeader(t, r, "Content-Type", "application/json") |
| 113 | th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 114 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 115 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 116 | }) |
| 117 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 118 | client := client.ServiceClient() |
Ash Wilson | dcbc8fb | 2014-09-29 09:05:44 -0400 | [diff] [blame] | 119 | actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 120 | if err != nil { |
| 121 | t.Fatalf("Unexpected Update error: %v", err) |
| 122 | } |
| 123 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 124 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | func TestChangeServerAdminPassword(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 128 | th.SetupHTTP() |
| 129 | defer th.TeardownHTTP() |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 130 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 131 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 132 | th.TestMethod(t, r, "POST") |
| 133 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 134 | th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 135 | |
| 136 | w.WriteHeader(http.StatusAccepted) |
| 137 | }) |
| 138 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 139 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 140 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | func TestRebootServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 144 | th.SetupHTTP() |
| 145 | defer th.TeardownHTTP() |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 146 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 147 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 148 | th.TestMethod(t, r, "POST") |
| 149 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 150 | th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`) |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame] | 151 | |
| 152 | w.WriteHeader(http.StatusAccepted) |
| 153 | }) |
| 154 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 155 | res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot) |
| 156 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | func TestRebuildServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 160 | th.SetupHTTP() |
| 161 | defer th.TeardownHTTP() |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 162 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 163 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 164 | th.TestMethod(t, r, "POST") |
| 165 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 166 | th.TestJSONRequest(t, r, ` |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 167 | { |
| 168 | "rebuild": { |
| 169 | "name": "new-name", |
| 170 | "adminPass": "swordfish", |
| 171 | "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 172 | "accessIPv4": "1.2.3.4" |
| 173 | } |
| 174 | } |
| 175 | `) |
| 176 | |
| 177 | w.WriteHeader(http.StatusAccepted) |
| 178 | w.Header().Add("Content-Type", "application/json") |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 179 | fmt.Fprintf(w, SingleServerBody) |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 180 | }) |
| 181 | |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 182 | opts := RebuildOpts{ |
| 183 | Name: "new-name", |
| 184 | AdminPass: "swordfish", |
| 185 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 186 | AccessIPv4: "1.2.3.4", |
Ash Wilson | 077f877 | 2014-09-25 10:57:13 -0400 | [diff] [blame] | 187 | } |
| 188 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 189 | actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract() |
| 190 | th.AssertNoErr(t, err) |
Jamie Hannaford | 6c9eb60 | 2014-10-16 16:28:07 +0200 | [diff] [blame] | 191 | |
Ash Wilson | d3532cd | 2014-10-21 14:37:47 -0400 | [diff] [blame] | 192 | th.CheckDeepEquals(t, ServerDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | func TestResizeServer(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 196 | th.SetupHTTP() |
| 197 | defer th.TeardownHTTP() |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 198 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 199 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 200 | th.TestMethod(t, r, "POST") |
| 201 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 202 | th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`) |
Ash Wilson | 45181f4 | 2014-09-25 11:00:16 -0400 | [diff] [blame] | 203 | |
| 204 | w.WriteHeader(http.StatusAccepted) |
| 205 | }) |
| 206 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 207 | res := Resize(client.ServiceClient(), "1234asdf", "2") |
| 208 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | func TestConfirmResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 212 | th.SetupHTTP() |
| 213 | defer th.TeardownHTTP() |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 214 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 215 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 216 | th.TestMethod(t, r, "POST") |
| 217 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 218 | th.TestJSONRequest(t, r, `{ "confirmResize": null }`) |
Ash Wilson | e2bffd5 | 2014-09-25 11:11:43 -0400 | [diff] [blame] | 219 | |
| 220 | w.WriteHeader(http.StatusNoContent) |
| 221 | }) |
| 222 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 223 | res := ConfirmResize(client.ServiceClient(), "1234asdf") |
| 224 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | func TestRevertResize(t *testing.T) { |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 228 | th.SetupHTTP() |
| 229 | defer th.TeardownHTTP() |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 230 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 231 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 232 | th.TestMethod(t, r, "POST") |
| 233 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 234 | th.TestJSONRequest(t, r, `{ "revertResize": null }`) |
Ash Wilson | 8deb38c | 2014-09-25 11:11:53 -0400 | [diff] [blame] | 235 | |
| 236 | w.WriteHeader(http.StatusAccepted) |
| 237 | }) |
| 238 | |
Ash Wilson | e77ffb0 | 2014-10-20 13:10:26 -0400 | [diff] [blame] | 239 | res := RevertResize(client.ServiceClient(), "1234asdf") |
| 240 | th.AssertNoErr(t, res.Err) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 241 | } |