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() |
| 91 | result, err := Create(client, map[string]interface{}{ |
| 92 | "name": "derp", |
| 93 | "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 94 | "flavorRef": "1", |
| 95 | }) |
| 96 | if err != nil { |
| 97 | t.Fatalf("Unexpected Create error: %v", err) |
| 98 | } |
| 99 | |
| 100 | actual, err := ExtractServer(result) |
| 101 | if err != nil { |
| 102 | t.Fatalf("Unexpected ExtractServer error: %v", err) |
| 103 | } |
| 104 | |
| 105 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func TestDeleteServer(t *testing.T) { |
| 109 | testhelper.SetupHTTP() |
| 110 | defer testhelper.TeardownHTTP() |
Ash Wilson | aff3627 | 2014-09-25 10:40:05 -0400 | [diff] [blame] | 111 | |
| 112 | testhelper.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) { |
| 113 | testhelper.TestMethod(t, r, "DELETE") |
| 114 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 115 | |
| 116 | w.WriteHeader(http.StatusNoContent) |
| 117 | }) |
| 118 | |
| 119 | client := serviceClient() |
| 120 | err := Delete(client, "asdfasdfasdf") |
| 121 | if err != nil { |
| 122 | t.Fatalf("Unexpected Delete error: %v", err) |
| 123 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | func TestGetServer(t *testing.T) { |
| 127 | testhelper.SetupHTTP() |
| 128 | defer testhelper.TeardownHTTP() |
Ash Wilson | a612f1f | 2014-09-25 10:42:40 -0400 | [diff] [blame] | 129 | |
| 130 | testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 131 | testhelper.TestMethod(t, r, "GET") |
| 132 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 133 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 134 | |
| 135 | fmt.Fprintf(w, singleServerBody) |
| 136 | }) |
| 137 | |
| 138 | client := serviceClient() |
| 139 | response, err := Get(client, "1234asdf") |
| 140 | if err != nil { |
| 141 | t.Fatalf("Unexpected Get error: %v", err) |
| 142 | } |
| 143 | |
| 144 | actual, err := ExtractServer(response) |
| 145 | if err != nil { |
| 146 | t.Fatalf("Unexpected ExtractServer error: %v", err) |
| 147 | } |
| 148 | |
| 149 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | func TestUpdateServer(t *testing.T) { |
| 153 | testhelper.SetupHTTP() |
| 154 | defer testhelper.TeardownHTTP() |
Ash Wilson | 0aac3a8 | 2014-09-25 10:45:03 -0400 | [diff] [blame] | 155 | |
| 156 | testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) { |
| 157 | testhelper.TestMethod(t, r, "PUT") |
| 158 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 159 | testhelper.TestHeader(t, r, "Accept", "application/json") |
| 160 | testhelper.TestHeader(t, r, "Content-Type", "application/json") |
| 161 | testhelper.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`) |
| 162 | |
| 163 | fmt.Fprintf(w, singleServerBody) |
| 164 | }) |
| 165 | |
| 166 | client := serviceClient() |
| 167 | response, err := Update(client, "1234asdf", map[string]interface{}{ |
| 168 | "name": "new-name", |
| 169 | }) |
| 170 | if err != nil { |
| 171 | t.Fatalf("Unexpected Update error: %v", err) |
| 172 | } |
| 173 | |
| 174 | actual, err := ExtractServer(response) |
| 175 | if err != nil { |
| 176 | t.Fatalf("Unexpected ExtractServer error: %v", err) |
| 177 | } |
| 178 | |
| 179 | equalServers(t, serverDerp, *actual) |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | func TestChangeServerAdminPassword(t *testing.T) { |
| 183 | testhelper.SetupHTTP() |
| 184 | defer testhelper.TeardownHTTP() |
Ash Wilson | fb99ec7 | 2014-09-25 10:48:51 -0400 | [diff] [blame] | 185 | |
| 186 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 187 | testhelper.TestMethod(t, r, "POST") |
| 188 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 189 | testhelper.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
| 190 | |
| 191 | w.WriteHeader(http.StatusAccepted) |
| 192 | }) |
| 193 | |
| 194 | client := serviceClient() |
| 195 | err := ChangeAdminPassword(client, "1234asdf", "new-password") |
| 196 | if err != nil { |
| 197 | t.Errorf("Unexpected ChangeAdminPassword error: %v", err) |
| 198 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | func TestRebootServer(t *testing.T) { |
| 202 | testhelper.SetupHTTP() |
| 203 | defer testhelper.TeardownHTTP() |
Ash Wilson | 8d368e9 | 2014-09-25 10:49:07 -0400 | [diff] [blame^] | 204 | |
| 205 | testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 206 | testhelper.TestMethod(t, r, "POST") |
| 207 | testhelper.TestHeader(t, r, "X-Auth-Token", tokenID) |
| 208 | testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`) |
| 209 | |
| 210 | w.WriteHeader(http.StatusAccepted) |
| 211 | }) |
| 212 | |
| 213 | client := serviceClient() |
| 214 | err := Reboot(client, "1234asdf", SoftReboot) |
| 215 | if err != nil { |
| 216 | t.Errorf("Unexpected Reboot error: %v", err) |
| 217 | } |
Ash Wilson | ad21c71 | 2014-09-25 10:15:22 -0400 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | func TestRebuildServer(t *testing.T) { |
| 221 | testhelper.SetupHTTP() |
| 222 | defer testhelper.TeardownHTTP() |
| 223 | t.Error("Pending") |
| 224 | } |
| 225 | |
| 226 | func TestResizeServer(t *testing.T) { |
| 227 | testhelper.SetupHTTP() |
| 228 | defer testhelper.TeardownHTTP() |
| 229 | t.Error("Pending") |
| 230 | } |
| 231 | |
| 232 | func TestConfirmResize(t *testing.T) { |
| 233 | testhelper.SetupHTTP() |
| 234 | defer testhelper.TeardownHTTP() |
| 235 | t.Error("Pending") |
| 236 | } |
| 237 | |
| 238 | func TestRevertResize(t *testing.T) { |
| 239 | testhelper.SetupHTTP() |
| 240 | defer testhelper.TeardownHTTP() |
| 241 | t.Error("Pending") |
| 242 | } |