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() |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 42 | os.HandleServerCreationSuccessfully(t, CreateOutput) |
Ash Wilson | 7a20cc9 | 2014-10-21 15:53:03 -0400 | [diff] [blame] | 43 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 44 | actual, err := Create(client.ServiceClient(), os.CreateOpts{ |
Ash Wilson | 7a20cc9 | 2014-10-21 15:53:03 -0400 | [diff] [blame] | 45 | Name: "derp", |
| 46 | ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb", |
| 47 | FlavorRef: "1", |
| 48 | }).Extract() |
| 49 | th.AssertNoErr(t, err) |
| 50 | |
| 51 | th.CheckDeepEquals(t, &CreatedServer, actual) |
| 52 | } |
| 53 | |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 54 | func TestDeleteServer(t *testing.T) { |
| 55 | th.SetupHTTP() |
| 56 | defer th.TeardownHTTP() |
| 57 | os.HandleServerDeletionSuccessfully(t) |
| 58 | |
Jamie Hannaford | 7bc317d | 2014-10-27 11:41:52 +0100 | [diff] [blame^] | 59 | res := Delete(client.ServiceClient(), "asdfasdfasdf") |
| 60 | th.AssertNoErr(t, res.Err) |
Ash Wilson | 664fe33 | 2014-10-21 17:47:49 -0400 | [diff] [blame] | 61 | } |
| 62 | |
Ash Wilson | 69c0874 | 2014-10-21 14:04:40 -0400 | [diff] [blame] | 63 | func TestGetServer(t *testing.T) { |
| 64 | th.SetupHTTP() |
| 65 | defer th.TeardownHTTP() |
| 66 | |
| 67 | th.Mux.HandleFunc("/servers/8c65cb68-0681-4c30-bc88-6b83a8a26aee", func(w http.ResponseWriter, r *http.Request) { |
| 68 | th.TestMethod(t, r, "GET") |
| 69 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 70 | |
| 71 | w.Header().Add("Content-Type", "application/json") |
| 72 | fmt.Fprintf(w, GetOutput) |
| 73 | }) |
| 74 | |
| 75 | actual, err := Get(client.ServiceClient(), "8c65cb68-0681-4c30-bc88-6b83a8a26aee").Extract() |
| 76 | th.AssertNoErr(t, err) |
| 77 | th.CheckDeepEquals(t, &GophercloudServer, actual) |
| 78 | } |
Ash Wilson | e846e17 | 2014-10-21 14:37:24 -0400 | [diff] [blame] | 79 | |
| 80 | func TestChangeAdminPassword(t *testing.T) { |
| 81 | th.SetupHTTP() |
| 82 | defer th.TeardownHTTP() |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 83 | os.HandleAdminPasswordChangeSuccessfully(t) |
Ash Wilson | e846e17 | 2014-10-21 14:37:24 -0400 | [diff] [blame] | 84 | |
| 85 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 86 | th.AssertNoErr(t, res.Err) |
| 87 | } |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 88 | |
| 89 | func TestReboot(t *testing.T) { |
| 90 | th.SetupHTTP() |
| 91 | defer th.TeardownHTTP() |
| 92 | os.HandleRebootSuccessfully(t) |
| 93 | |
| 94 | res := Reboot(client.ServiceClient(), "1234asdf", os.SoftReboot) |
| 95 | th.AssertNoErr(t, res.Err) |
| 96 | } |
Ash Wilson | acf49c6 | 2014-10-21 18:25:11 -0400 | [diff] [blame] | 97 | |
| 98 | func TestRebuildServer(t *testing.T) { |
| 99 | th.SetupHTTP() |
| 100 | defer th.TeardownHTTP() |
| 101 | os.HandleRebuildSuccessfully(t, GetOutput) |
| 102 | |
| 103 | opts := os.RebuildOpts{ |
| 104 | Name: "new-name", |
| 105 | AdminPass: "swordfish", |
| 106 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 107 | AccessIPv4: "1.2.3.4", |
| 108 | } |
| 109 | actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract() |
| 110 | th.AssertNoErr(t, err) |
| 111 | th.CheckDeepEquals(t, &GophercloudServer, actual) |
| 112 | } |