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 | |
Jon Perritt | 723a24b | 2015-02-02 10:06:42 -0700 | [diff] [blame] | 80 | func TestUpdateServer(t *testing.T) { |
| 81 | th.SetupHTTP() |
| 82 | defer th.TeardownHTTP() |
| 83 | |
| 84 | th.Mux.HandleFunc("/servers/8c65cb68-0681-4c30-bc88-6b83a8a26aee", func(w http.ResponseWriter, r *http.Request) { |
| 85 | th.TestMethod(t, r, "PUT") |
| 86 | th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 87 | th.TestJSONRequest(t, r, `{ "server": { "name": "test-server-updated" } }`) |
| 88 | |
| 89 | w.Header().Add("Content-Type", "application/json") |
| 90 | |
| 91 | fmt.Fprintf(w, UpdateOutput) |
| 92 | }) |
| 93 | |
| 94 | opts := os.UpdateOpts{ |
| 95 | Name: "test-server-updated", |
| 96 | } |
| 97 | actual, err := Update(client.ServiceClient(), "8c65cb68-0681-4c30-bc88-6b83a8a26aee", opts).Extract() |
| 98 | th.AssertNoErr(t, err) |
| 99 | th.CheckDeepEquals(t, &GophercloudUpdatedServer, actual) |
| 100 | } |
| 101 | |
Ash Wilson | e846e17 | 2014-10-21 14:37:24 -0400 | [diff] [blame] | 102 | func TestChangeAdminPassword(t *testing.T) { |
| 103 | th.SetupHTTP() |
| 104 | defer th.TeardownHTTP() |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 105 | os.HandleAdminPasswordChangeSuccessfully(t) |
Ash Wilson | e846e17 | 2014-10-21 14:37:24 -0400 | [diff] [blame] | 106 | |
| 107 | res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password") |
| 108 | th.AssertNoErr(t, res.Err) |
| 109 | } |
Ash Wilson | 2295ba5 | 2014-10-21 18:19:28 -0400 | [diff] [blame] | 110 | |
| 111 | func TestReboot(t *testing.T) { |
| 112 | th.SetupHTTP() |
| 113 | defer th.TeardownHTTP() |
| 114 | os.HandleRebootSuccessfully(t) |
| 115 | |
| 116 | res := Reboot(client.ServiceClient(), "1234asdf", os.SoftReboot) |
| 117 | th.AssertNoErr(t, res.Err) |
| 118 | } |
Ash Wilson | acf49c6 | 2014-10-21 18:25:11 -0400 | [diff] [blame] | 119 | |
| 120 | func TestRebuildServer(t *testing.T) { |
| 121 | th.SetupHTTP() |
| 122 | defer th.TeardownHTTP() |
| 123 | os.HandleRebuildSuccessfully(t, GetOutput) |
| 124 | |
| 125 | opts := os.RebuildOpts{ |
| 126 | Name: "new-name", |
| 127 | AdminPass: "swordfish", |
| 128 | ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb", |
| 129 | AccessIPv4: "1.2.3.4", |
| 130 | } |
| 131 | actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract() |
| 132 | th.AssertNoErr(t, err) |
| 133 | th.CheckDeepEquals(t, &GophercloudServer, actual) |
| 134 | } |