blob: 466bbf794e286600fba3725144075fae36b16672 [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
Jon Perrittd27a9c72015-02-18 11:33:28 -070042func TestListAllServers(t *testing.T) {
43 th.SetupHTTP()
44 defer th.TeardownHTTP()
45 HandleServerListSuccessfully(t)
46
47 allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages()
48 th.AssertNoErr(t, err)
49 actual, err := ExtractServers(allPages)
50 th.AssertNoErr(t, err)
51 th.CheckDeepEquals(t, ServerHerp, actual[0])
52 th.CheckDeepEquals(t, ServerDerp, actual[1])
53}
54
Ash Wilsonad21c712014-09-25 10:15:22 -040055func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040056 th.SetupHTTP()
57 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040058 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040059
Ash Wilson664fe332014-10-21 17:47:49 -040060 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040061 Name: "derp",
62 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
63 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040064 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040065 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040066
Ash Wilsond3532cd2014-10-21 14:37:47 -040067 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040068}
69
70func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040071 th.SetupHTTP()
72 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040073 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040074
Jamie Hannaford34732fe2014-10-27 11:29:36 +010075 res := Delete(client.ServiceClient(), "asdfasdfasdf")
76 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040077}
78
79func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040080 th.SetupHTTP()
81 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040082 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040083
Ash Wilsone77ffb02014-10-20 13:10:26 -040084 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040085 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040086 if err != nil {
87 t.Fatalf("Unexpected Get error: %v", err)
88 }
89
Ash Wilsond3532cd2014-10-21 14:37:47 -040090 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040091}
92
93func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040094 th.SetupHTTP()
95 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040096 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -040097
Ash Wilsone77ffb02014-10-20 13:10:26 -040098 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -040099 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400100 if err != nil {
101 t.Fatalf("Unexpected Update error: %v", err)
102 }
103
Ash Wilsond3532cd2014-10-21 14:37:47 -0400104 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400105}
106
107func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400108 th.SetupHTTP()
109 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400110 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400111
Ash Wilsone77ffb02014-10-20 13:10:26 -0400112 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
113 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400114}
115
116func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400117 th.SetupHTTP()
118 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400119 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400120
Ash Wilsone77ffb02014-10-20 13:10:26 -0400121 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
122 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400123}
124
125func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400126 th.SetupHTTP()
127 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400128 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400129
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200130 opts := RebuildOpts{
131 Name: "new-name",
132 AdminPass: "swordfish",
133 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
134 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400135 }
136
Ash Wilsone77ffb02014-10-20 13:10:26 -0400137 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
138 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200139
Ash Wilsond3532cd2014-10-21 14:37:47 -0400140 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400141}
142
143func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400144 th.SetupHTTP()
145 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400146
Ash Wilsone77ffb02014-10-20 13:10:26 -0400147 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
148 th.TestMethod(t, r, "POST")
149 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
150 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400151
152 w.WriteHeader(http.StatusAccepted)
153 })
154
Ash Wilson5f7cf182014-10-23 08:35:24 -0400155 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400156 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400157}
158
159func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400160 th.SetupHTTP()
161 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400162
Ash Wilsone77ffb02014-10-20 13:10:26 -0400163 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
164 th.TestMethod(t, r, "POST")
165 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
166 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400167
168 w.WriteHeader(http.StatusNoContent)
169 })
170
Ash Wilsone77ffb02014-10-20 13:10:26 -0400171 res := ConfirmResize(client.ServiceClient(), "1234asdf")
172 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400173}
174
175func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400176 th.SetupHTTP()
177 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400178
Ash Wilsone77ffb02014-10-20 13:10:26 -0400179 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
180 th.TestMethod(t, r, "POST")
181 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
182 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400183
184 w.WriteHeader(http.StatusAccepted)
185 })
186
Ash Wilsone77ffb02014-10-20 13:10:26 -0400187 res := RevertResize(client.ServiceClient(), "1234asdf")
188 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400189}
Alex Gaynor810d4892014-11-12 15:43:36 -0800190
191func TestRescue(t *testing.T) {
192 th.SetupHTTP()
193 defer th.TeardownHTTP()
194
Alex Gaynor6c003d22014-11-13 13:52:05 -0800195 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800196
Alex Gaynor40449ed2014-11-12 16:28:06 -0800197 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800198 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800199 })
200 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800201 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800202 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800203}
Jon Perrittcc77da62014-11-16 13:14:21 -0700204
Jon Perritt78c57ce2014-11-20 11:07:18 -0700205func TestGetMetadatum(t *testing.T) {
206 th.SetupHTTP()
207 defer th.TeardownHTTP()
208
209 HandleMetadatumGetSuccessfully(t)
210
211 expected := map[string]string{"foo": "bar"}
212 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
213 th.AssertNoErr(t, err)
214 th.AssertDeepEquals(t, expected, actual)
215}
216
217func TestCreateMetadatum(t *testing.T) {
218 th.SetupHTTP()
219 defer th.TeardownHTTP()
220
221 HandleMetadatumCreateSuccessfully(t)
222
223 expected := map[string]string{"foo": "bar"}
224 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
225 th.AssertNoErr(t, err)
226 th.AssertDeepEquals(t, expected, actual)
227}
228
229func TestDeleteMetadatum(t *testing.T) {
230 th.SetupHTTP()
231 defer th.TeardownHTTP()
232
233 HandleMetadatumDeleteSuccessfully(t)
234
235 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
236 th.AssertNoErr(t, err)
237}
238
Jon Perrittcc77da62014-11-16 13:14:21 -0700239func TestGetMetadata(t *testing.T) {
240 th.SetupHTTP()
241 defer th.TeardownHTTP()
242
243 HandleMetadataGetSuccessfully(t)
244
Jon Perritt78c57ce2014-11-20 11:07:18 -0700245 expected := map[string]string{"foo": "bar", "this": "that"}
246 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700247 th.AssertNoErr(t, err)
248 th.AssertDeepEquals(t, expected, actual)
249}
250
Jon Perritt789f8322014-11-21 08:20:04 -0700251func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700252 th.SetupHTTP()
253 defer th.TeardownHTTP()
254
Jon Perritt789f8322014-11-21 08:20:04 -0700255 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700256
Jon Perrittcc77da62014-11-16 13:14:21 -0700257 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700258 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700259 "foo": "bar",
260 "this": "that",
261 }).Extract()
262 th.AssertNoErr(t, err)
263 th.AssertDeepEquals(t, expected, actual)
264}
265
Jon Perritt78c57ce2014-11-20 11:07:18 -0700266func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700267 th.SetupHTTP()
268 defer th.TeardownHTTP()
269
Jon Perritt78c57ce2014-11-20 11:07:18 -0700270 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700271
272 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700273 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700274 "foo": "baz",
275 "this": "those",
276 }).Extract()
277 th.AssertNoErr(t, err)
278 th.AssertDeepEquals(t, expected, actual)
279}
Jon Perritt5cb49482015-02-19 12:19:58 -0700280
281func TestListAddresses(t *testing.T) {
282 th.SetupHTTP()
283 defer th.TeardownHTTP()
284 HandleAddressListSuccessfully(t)
285
286 expected := ListAddressesExpected
287 pages := 0
288 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
289 pages++
290
291 actual, err := ExtractAddresses(page)
292 th.AssertNoErr(t, err)
293
294 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700295 t.Fatalf("Expected 2 networks, got %d", len(actual))
296 }
297 th.CheckDeepEquals(t, expected, actual)
298
299 return true, nil
300 })
301 th.AssertNoErr(t, err)
302 th.CheckEquals(t, 1, pages)
303}
304
305func TestListAddressesByNetwork(t *testing.T) {
306 th.SetupHTTP()
307 defer th.TeardownHTTP()
308 HandleNetworkAddressListSuccessfully(t)
309
310 expected := ListNetworkAddressesExpected
311 pages := 0
312 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
313 pages++
314
315 actual, err := ExtractNetworkAddresses(page)
316 th.AssertNoErr(t, err)
317
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700318 if len(actual) != 2 {
319 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700320 }
321 th.CheckDeepEquals(t, expected, actual)
322
323 return true, nil
324 })
325 th.AssertNoErr(t, err)
326 th.CheckEquals(t, 1, pages)
327}
einarf2fc665e2015-04-16 20:16:21 +0000328
329func TestCreateServerImage(t *testing.T) {
330 th.SetupHTTP()
331 defer th.TeardownHTTP()
332 HandleCreateServerImageSuccessfully(t)
333
334 _, err := CreateServerImage(client.ServiceClient(), "serverimage", CreateServerImageOpts{Name: "test"}).ExtractImageID()
335 th.AssertNoErr(t, err)
336
337}