blob: f3016f4bd8ae8c9c836cc460b5362737b4204bbd [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
Ash Wilsonad21c712014-09-25 10:15:22 -04004 "net/http"
5 "testing"
6
Ash Wilsonad21c712014-09-25 10:15:22 -04007 "github.com/rackspace/gophercloud/pagination"
Ash Wilsone77ffb02014-10-20 13:10:26 -04008 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040010)
11
Ash Wilsonad21c712014-09-25 10:15:22 -040012func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040013 th.SetupHTTP()
14 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040015 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040016
Ash Wilsonad21c712014-09-25 10:15:22 -040017 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040018 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040019 pages++
20
21 actual, err := ExtractServers(page)
22 if err != nil {
23 return false, err
24 }
25
26 if len(actual) != 2 {
27 t.Fatalf("Expected 2 servers, got %d", len(actual))
28 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040029 th.CheckDeepEquals(t, ServerHerp, actual[0])
30 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040031
32 return true, nil
33 })
34
Ash Wilsone77ffb02014-10-20 13:10:26 -040035 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020036
Ash Wilsonad21c712014-09-25 10:15:22 -040037 if pages != 1 {
38 t.Errorf("Expected 1 page, saw %d", pages)
39 }
40}
41
42func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040043 th.SetupHTTP()
44 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040045 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040046
Ash Wilson664fe332014-10-21 17:47:49 -040047 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040048 Name: "derp",
49 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
50 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040051 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040052 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040053
Ash Wilsond3532cd2014-10-21 14:37:47 -040054 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040055}
56
57func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040058 th.SetupHTTP()
59 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040060 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040061
Jamie Hannaford34732fe2014-10-27 11:29:36 +010062 res := Delete(client.ServiceClient(), "asdfasdfasdf")
63 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040064}
65
66func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040067 th.SetupHTTP()
68 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040069 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040070
Ash Wilsone77ffb02014-10-20 13:10:26 -040071 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040072 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040073 if err != nil {
74 t.Fatalf("Unexpected Get error: %v", err)
75 }
76
Ash Wilsond3532cd2014-10-21 14:37:47 -040077 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040078}
79
80func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040081 th.SetupHTTP()
82 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040083 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -040084
Ash Wilsone77ffb02014-10-20 13:10:26 -040085 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -040086 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -040087 if err != nil {
88 t.Fatalf("Unexpected Update error: %v", err)
89 }
90
Ash Wilsond3532cd2014-10-21 14:37:47 -040091 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040092}
93
94func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040095 th.SetupHTTP()
96 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -040097 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -040098
Ash Wilsone77ffb02014-10-20 13:10:26 -040099 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
100 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400101}
102
103func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400104 th.SetupHTTP()
105 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400106 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400107
Ash Wilsone77ffb02014-10-20 13:10:26 -0400108 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
109 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400110}
111
112func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400113 th.SetupHTTP()
114 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400115 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400116
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200117 opts := RebuildOpts{
118 Name: "new-name",
119 AdminPass: "swordfish",
120 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
121 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400122 }
123
Ash Wilsone77ffb02014-10-20 13:10:26 -0400124 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
125 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200126
Ash Wilsond3532cd2014-10-21 14:37:47 -0400127 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400128}
129
130func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400131 th.SetupHTTP()
132 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400133
Ash Wilsone77ffb02014-10-20 13:10:26 -0400134 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
135 th.TestMethod(t, r, "POST")
136 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
137 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400138
139 w.WriteHeader(http.StatusAccepted)
140 })
141
Ash Wilson5f7cf182014-10-23 08:35:24 -0400142 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400143 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400144}
145
146func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400147 th.SetupHTTP()
148 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400149
Ash Wilsone77ffb02014-10-20 13:10:26 -0400150 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
151 th.TestMethod(t, r, "POST")
152 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
153 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400154
155 w.WriteHeader(http.StatusNoContent)
156 })
157
Ash Wilsone77ffb02014-10-20 13:10:26 -0400158 res := ConfirmResize(client.ServiceClient(), "1234asdf")
159 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400160}
161
162func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400163 th.SetupHTTP()
164 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400165
Ash Wilsone77ffb02014-10-20 13:10:26 -0400166 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
167 th.TestMethod(t, r, "POST")
168 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
169 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400170
171 w.WriteHeader(http.StatusAccepted)
172 })
173
Ash Wilsone77ffb02014-10-20 13:10:26 -0400174 res := RevertResize(client.ServiceClient(), "1234asdf")
175 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400176}
Alex Gaynor810d4892014-11-12 15:43:36 -0800177
178func TestRescue(t *testing.T) {
179 th.SetupHTTP()
180 defer th.TeardownHTTP()
181
182 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
183 th.TestMethod(t, r, "POST")
184 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
185 th.TestJSONRequest(t, r, `{ "rescue": { "adminPass": "1234567890" } }`)
186
Alex Gaynor02c86a32014-11-12 22:57:32 -0800187 w.WriteHeader(http.StatusOK)
Alex Gaynora72749e2014-11-12 22:43:56 -0800188 w.Write([]byte(`{ "adminPass": "1234567890" }`))
Alex Gaynor810d4892014-11-12 15:43:36 -0800189 })
190
Alex Gaynor40449ed2014-11-12 16:28:06 -0800191 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800192 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800193 })
194 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800195 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800196 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800197}