blob: 73d13753c16d48084d7b2d44443946365a32a5a0 [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
Kevin Pike92e10b52015-04-10 15:16:57 -07004 "encoding/base64"
Kevin Pikea2bfaea2015-04-21 11:45:59 -07005 "encoding/json"
Ash Wilsonad21c712014-09-25 10:15:22 -04006 "net/http"
7 "testing"
8
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/pagination"
10 th "github.com/gophercloud/gophercloud/testhelper"
11 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040012)
13
Ash Wilsonad21c712014-09-25 10:15:22 -040014func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040015 th.SetupHTTP()
16 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040017 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040018
Ash Wilsonad21c712014-09-25 10:15:22 -040019 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040020 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040021 pages++
22
23 actual, err := ExtractServers(page)
24 if err != nil {
25 return false, err
26 }
27
28 if len(actual) != 2 {
29 t.Fatalf("Expected 2 servers, got %d", len(actual))
30 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040031 th.CheckDeepEquals(t, ServerHerp, actual[0])
32 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040033
34 return true, nil
35 })
36
Ash Wilsone77ffb02014-10-20 13:10:26 -040037 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020038
Ash Wilsonad21c712014-09-25 10:15:22 -040039 if pages != 1 {
40 t.Errorf("Expected 1 page, saw %d", pages)
41 }
42}
43
Jon Perrittd27a9c72015-02-18 11:33:28 -070044func TestListAllServers(t *testing.T) {
45 th.SetupHTTP()
46 defer th.TeardownHTTP()
47 HandleServerListSuccessfully(t)
48
49 allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages()
50 th.AssertNoErr(t, err)
51 actual, err := ExtractServers(allPages)
52 th.AssertNoErr(t, err)
53 th.CheckDeepEquals(t, ServerHerp, actual[0])
54 th.CheckDeepEquals(t, ServerDerp, actual[1])
55}
56
Ash Wilsonad21c712014-09-25 10:15:22 -040057func TestCreateServer(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 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040061
Ash Wilson664fe332014-10-21 17:47:49 -040062 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040063 Name: "derp",
64 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
65 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040066 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040067 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040068
Ash Wilsond3532cd2014-10-21 14:37:47 -040069 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040070}
71
72func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040073 th.SetupHTTP()
74 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040075 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040076
Jamie Hannaford34732fe2014-10-27 11:29:36 +010077 res := Delete(client.ServiceClient(), "asdfasdfasdf")
78 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040079}
80
Ian Duffy370c4302016-01-21 10:44:56 +000081func TestForceDeleteServer(t *testing.T) {
82 th.SetupHTTP()
83 defer th.TeardownHTTP()
84 HandleServerForceDeletionSuccessfully(t)
85
86 res := ForceDelete(client.ServiceClient(), "asdfasdfasdf")
87 th.AssertNoErr(t, res.Err)
88}
89
Ash Wilsonad21c712014-09-25 10:15:22 -040090func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040091 th.SetupHTTP()
92 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040093 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040094
Ash Wilsone77ffb02014-10-20 13:10:26 -040095 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040096 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040097 if err != nil {
98 t.Fatalf("Unexpected Get error: %v", err)
99 }
100
Ash Wilsond3532cd2014-10-21 14:37:47 -0400101 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400102}
103
104func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400105 th.SetupHTTP()
106 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400107 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400108
Ash Wilsone77ffb02014-10-20 13:10:26 -0400109 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400110 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400111 if err != nil {
112 t.Fatalf("Unexpected Update error: %v", err)
113 }
114
Ash Wilsond3532cd2014-10-21 14:37:47 -0400115 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400116}
117
118func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400119 th.SetupHTTP()
120 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400121 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400122
Ash Wilsone77ffb02014-10-20 13:10:26 -0400123 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
124 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400125}
126
127func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400128 th.SetupHTTP()
129 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400130 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400131
Jon Perrittf094fef2016-03-07 01:41:59 -0600132 res := Reboot(client.ServiceClient(), "1234asdf", &RebootOpts{
133 Type: SoftReboot,
134 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400135 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400136}
137
138func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400139 th.SetupHTTP()
140 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400141 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400142
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200143 opts := RebuildOpts{
144 Name: "new-name",
145 AdminPass: "swordfish",
146 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
147 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400148 }
149
Ash Wilsone77ffb02014-10-20 13:10:26 -0400150 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
151 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200152
Ash Wilsond3532cd2014-10-21 14:37:47 -0400153 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400154}
155
156func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400157 th.SetupHTTP()
158 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400159
Ash Wilsone77ffb02014-10-20 13:10:26 -0400160 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
161 th.TestMethod(t, r, "POST")
162 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
163 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400164
165 w.WriteHeader(http.StatusAccepted)
166 })
167
Ash Wilson5f7cf182014-10-23 08:35:24 -0400168 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400169 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400170}
171
172func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400173 th.SetupHTTP()
174 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400175
Ash Wilsone77ffb02014-10-20 13:10:26 -0400176 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
177 th.TestMethod(t, r, "POST")
178 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
179 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400180
181 w.WriteHeader(http.StatusNoContent)
182 })
183
Ash Wilsone77ffb02014-10-20 13:10:26 -0400184 res := ConfirmResize(client.ServiceClient(), "1234asdf")
185 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400186}
187
188func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400189 th.SetupHTTP()
190 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400191
Ash Wilsone77ffb02014-10-20 13:10:26 -0400192 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
193 th.TestMethod(t, r, "POST")
194 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
195 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400196
197 w.WriteHeader(http.StatusAccepted)
198 })
199
Ash Wilsone77ffb02014-10-20 13:10:26 -0400200 res := RevertResize(client.ServiceClient(), "1234asdf")
201 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400202}
Alex Gaynor810d4892014-11-12 15:43:36 -0800203
204func TestRescue(t *testing.T) {
205 th.SetupHTTP()
206 defer th.TeardownHTTP()
207
Alex Gaynor6c003d22014-11-13 13:52:05 -0800208 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800209
Alex Gaynor40449ed2014-11-12 16:28:06 -0800210 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800211 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800212 })
213 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800214 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800215 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800216}
Jon Perrittcc77da62014-11-16 13:14:21 -0700217
Jon Perritt78c57ce2014-11-20 11:07:18 -0700218func TestGetMetadatum(t *testing.T) {
219 th.SetupHTTP()
220 defer th.TeardownHTTP()
221
222 HandleMetadatumGetSuccessfully(t)
223
224 expected := map[string]string{"foo": "bar"}
225 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
226 th.AssertNoErr(t, err)
227 th.AssertDeepEquals(t, expected, actual)
228}
229
230func TestCreateMetadatum(t *testing.T) {
231 th.SetupHTTP()
232 defer th.TeardownHTTP()
233
234 HandleMetadatumCreateSuccessfully(t)
235
236 expected := map[string]string{"foo": "bar"}
237 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
238 th.AssertNoErr(t, err)
239 th.AssertDeepEquals(t, expected, actual)
240}
241
242func TestDeleteMetadatum(t *testing.T) {
243 th.SetupHTTP()
244 defer th.TeardownHTTP()
245
246 HandleMetadatumDeleteSuccessfully(t)
247
248 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
249 th.AssertNoErr(t, err)
250}
251
Jon Perrittcc77da62014-11-16 13:14:21 -0700252func TestGetMetadata(t *testing.T) {
253 th.SetupHTTP()
254 defer th.TeardownHTTP()
255
256 HandleMetadataGetSuccessfully(t)
257
Jon Perritt78c57ce2014-11-20 11:07:18 -0700258 expected := map[string]string{"foo": "bar", "this": "that"}
259 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700260 th.AssertNoErr(t, err)
261 th.AssertDeepEquals(t, expected, actual)
262}
263
Jon Perritt789f8322014-11-21 08:20:04 -0700264func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700265 th.SetupHTTP()
266 defer th.TeardownHTTP()
267
Jon Perritt789f8322014-11-21 08:20:04 -0700268 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700269
Jon Perrittcc77da62014-11-16 13:14:21 -0700270 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700271 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700272 "foo": "bar",
273 "this": "that",
274 }).Extract()
275 th.AssertNoErr(t, err)
276 th.AssertDeepEquals(t, expected, actual)
277}
278
Jon Perritt78c57ce2014-11-20 11:07:18 -0700279func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700280 th.SetupHTTP()
281 defer th.TeardownHTTP()
282
Jon Perritt78c57ce2014-11-20 11:07:18 -0700283 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700284
285 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700286 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700287 "foo": "baz",
288 "this": "those",
289 }).Extract()
290 th.AssertNoErr(t, err)
291 th.AssertDeepEquals(t, expected, actual)
292}
Jon Perritt5cb49482015-02-19 12:19:58 -0700293
294func TestListAddresses(t *testing.T) {
295 th.SetupHTTP()
296 defer th.TeardownHTTP()
297 HandleAddressListSuccessfully(t)
298
299 expected := ListAddressesExpected
300 pages := 0
301 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
302 pages++
303
304 actual, err := ExtractAddresses(page)
305 th.AssertNoErr(t, err)
306
307 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700308 t.Fatalf("Expected 2 networks, got %d", len(actual))
309 }
310 th.CheckDeepEquals(t, expected, actual)
311
312 return true, nil
313 })
314 th.AssertNoErr(t, err)
315 th.CheckEquals(t, 1, pages)
316}
317
318func TestListAddressesByNetwork(t *testing.T) {
319 th.SetupHTTP()
320 defer th.TeardownHTTP()
321 HandleNetworkAddressListSuccessfully(t)
322
323 expected := ListNetworkAddressesExpected
324 pages := 0
325 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
326 pages++
327
328 actual, err := ExtractNetworkAddresses(page)
329 th.AssertNoErr(t, err)
330
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700331 if len(actual) != 2 {
332 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700333 }
334 th.CheckDeepEquals(t, expected, actual)
335
336 return true, nil
337 })
338 th.AssertNoErr(t, err)
339 th.CheckEquals(t, 1, pages)
340}
Kevin Pike92e10b52015-04-10 15:16:57 -0700341
einarf2fc665e2015-04-16 20:16:21 +0000342func TestCreateServerImage(t *testing.T) {
343 th.SetupHTTP()
344 defer th.TeardownHTTP()
345 HandleCreateServerImageSuccessfully(t)
346
einarf4e5fdaf2015-04-16 23:14:59 +0000347 _, err := CreateImage(client.ServiceClient(), "serverimage", CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000348 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000349}
Kevin Pike25d45692015-04-23 17:20:09 -0700350
Kevin Pike92e10b52015-04-10 15:16:57 -0700351func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700352 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700353 contents := []byte("asdfasdf")
354
355 personality := Personality{
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700356 &File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700357 Path: name,
358 Contents: contents,
359 },
360 }
361
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700362 data, err := json.Marshal(personality)
363 if err != nil {
364 t.Fatal(err)
365 }
366
367 var actual []map[string]string
368 err = json.Unmarshal(data, &actual)
369 if err != nil {
370 t.Fatal(err)
371 }
372
373 if len(actual) != 1 {
374 t.Fatal("expected personality length 1")
375 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700376
377 if actual[0]["path"] != name {
378 t.Fatal("file path incorrect")
379 }
380
381 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
382 t.Fatal("file contents incorrect")
383 }
384}