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 | |
Ash Wilson | 7a20cc9 | 2014-10-21 15:53:03 -0400 | [diff] [blame^] | 8 | os "github.com/rackspace/gophercloud/openstack/compute/v2/servers" |
Ash Wilson | 69c0874 | 2014-10-21 14:04:40 -0400 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/pagination" |
| 10 | th "github.com/rackspace/gophercloud/testhelper" |
| 11 | "github.com/rackspace/gophercloud/testhelper/client" |
| 12 | ) |
| 13 | |
| 14 | func TestListServers(t *testing.T) { |
| 15 | th.SetupHTTP() |
| 16 | defer th.TeardownHTTP() |
| 17 | |
| 18 | th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) { |
| 19 | th.TestMethod(t, r, "GET") |
| 20 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 21 | |
| 22 | w.Header().Add("Content-Type", "application/json") |
| 23 | fmt.Fprintf(w, ListOutput) |
| 24 | }) |
| 25 | |
| 26 | count := 0 |
| 27 | err := List(client.ServiceClient(), nil).EachPage(func(page pagination.Page) (bool, error) { |
| 28 | count++ |
| 29 | actual, err := ExtractServers(page) |
| 30 | th.AssertNoErr(t, err) |
| 31 | th.CheckDeepEquals(t, ExpectedServerSlice, actual) |
| 32 | |
| 33 | return true, nil |
| 34 | }) |
| 35 | th.AssertNoErr(t, err) |
| 36 | th.CheckEquals(t, 1, count) |
| 37 | } |
| 38 | |
Ash Wilson | 7a20cc9 | 2014-10-21 15:53:03 -0400 | [diff] [blame^] | 39 | func TestCreateServer(t *testing.T) { |
| 40 | th.SetupHTTP() |
| 41 | defer th.TeardownHTTP() |
| 42 | |
| 43 | th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) { |
| 44 | th.TestMethod(t, r, "POST") |
| 45 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 46 | th.TestJSONRequest(t, r, `{ |
| 47 | "server": { |
| 48 | "name": "derp", |
| 49 | "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 50 | "flavorRef": "1" |
| 51 | } |
| 52 | }`) |
| 53 | |
| 54 | w.WriteHeader(http.StatusAccepted) |
| 55 | w.Header().Add("Content-Type", "application/json") |
| 56 | fmt.Fprintf(w, CreateOutput) |
| 57 | }) |
| 58 | |
| 59 | client := client.ServiceClient() |
| 60 | actual, err := Create(client, os.CreateOpts{ |
| 61 | Name: "derp", |
| 62 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 63 | FlavorRef: "1", |
| 64 | }).Extract() |
| 65 | th.AssertNoErr(t, err) |
| 66 | |
| 67 | th.CheckDeepEquals(t, &CreatedServer, actual) |
| 68 | } |
| 69 | |
Ash Wilson | 69c0874 | 2014-10-21 14:04:40 -0400 | [diff] [blame] | 70 | func TestGetServer(t *testing.T) { |
| 71 | th.SetupHTTP() |
| 72 | defer th.TeardownHTTP() |
| 73 | |
| 74 | th.Mux.HandleFunc("/servers/8c65cb68-0681-4c30-bc88-6b83a8a26aee", func(w http.ResponseWriter, r *http.Request) { |
| 75 | th.TestMethod(t, r, "GET") |
| 76 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 77 | |
| 78 | w.Header().Add("Content-Type", "application/json") |
| 79 | fmt.Fprintf(w, GetOutput) |
| 80 | }) |
| 81 | |
| 82 | actual, err := Get(client.ServiceClient(), "8c65cb68-0681-4c30-bc88-6b83a8a26aee").Extract() |
| 83 | th.AssertNoErr(t, err) |
| 84 | th.CheckDeepEquals(t, &GophercloudServer, actual) |
| 85 | } |
Ash Wilson | e846e17 | 2014-10-21 14:37:24 -0400 | [diff] [blame] | 86 | |
| 87 | func TestChangeAdminPassword(t *testing.T) { |
| 88 | th.SetupHTTP() |
| 89 | defer th.TeardownHTTP() |
| 90 | |
| 91 | th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) { |
| 92 | th.TestMethod(t, r, "POST") |
| 93 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 94 | th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`) |
| 95 | |
| 96 | w.WriteHeader(http.StatusAccepted) |
| 97 | }) |
| 98 | |
| 99 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 100 | th.AssertNoErr(t, res.Err) |
| 101 | } |