Ash Wilson | 69c0874 | 2014-10-21 14:04:40 -0400 | [diff] [blame] | 1 | package servers |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "net/http" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/rackspace/gophercloud/pagination" |
| 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
| 13 | func TestListServers(t *testing.T) { |
| 14 | th.SetupHTTP() |
| 15 | defer th.TeardownHTTP() |
| 16 | |
| 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) |
| 20 | |
| 21 | w.Header().Add("Content-Type", "application/json") |
| 22 | fmt.Fprintf(w, ListOutput) |
| 23 | }) |
| 24 | |
| 25 | count := 0 |
| 26 | err := List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { |
| 27 | count++ |
| 28 | actual, err := ExtractServers(page) |
| 29 | th.AssertNoErr(t, err) |
| 30 | th.CheckDeepEquals(t, ExpectedServerSlice, actual) |
| 31 | |
| 32 | return true, nil |
| 33 | }) |
| 34 | th.AssertNoErr(t, err) |
| 35 | th.CheckEquals(t, 1, count) |
| 36 | } |
| 37 | |
| 38 | func TestGetServer(t *testing.T) { |
| 39 | th.SetupHTTP() |
| 40 | defer th.TeardownHTTP() |
| 41 | |
| 42 | th.Mux.HandleFunc("/servers/8c65cb68-0681-4c30-bc88-6b83a8a26aee", func(w http.ResponseWriter, r *http.Request) { |
| 43 | th.TestMethod(t, r, "GET") |
| 44 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 45 | |
| 46 | w.Header().Add("Content-Type", "application/json") |
| 47 | fmt.Fprintf(w, GetOutput) |
| 48 | }) |
| 49 | |
| 50 | actual, err := Get(client.ServiceClient(), "8c65cb68-0681-4c30-bc88-6b83a8a26aee").Extract() |
| 51 | th.AssertNoErr(t, err) |
| 52 | th.CheckDeepEquals(t, &GophercloudServer, actual) |
| 53 | } |
Ash Wilson | e846e17 | 2014-10-21 14:37:24 -0400 | [diff] [blame^] | 54 | |
| 55 | func TestChangeAdminPassword(t *testing.T) { |
| 56 | th.SetupHTTP() |
| 57 | defer th.TeardownHTTP() |
| 58 | |
| 59 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 60 | th.TestMethod(t, r, "POST") |
| 61 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 62 | th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
| 63 | |
| 64 | w.WriteHeader(http.StatusAccepted) |
| 65 | }) |
| 66 | |
| 67 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 68 | th.AssertNoErr(t, res.Err) |
| 69 | } |