blob: f6c7c052458a251d4ce5cfe4aaa09b5286c72114 [file] [log] [blame]
Jamie Hannaford2d480372014-10-22 10:47:09 +02001// +build acceptance compute servers
Ash Wilsonfd566482014-09-23 15:47:35 -04002
3package v2
4
5import (
Jon Perritt2a7797d2014-10-21 15:08:43 -05006 "os"
Ash Wilsonfd566482014-09-23 15:47:35 -04007 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/acceptance/tools"
Jon Perritt2a7797d2014-10-21 15:08:43 -050011 "github.com/rackspace/gophercloud/openstack"
Ash Wilsonfd566482014-09-23 15:47:35 -040012 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Jon Perritt2a7797d2014-10-21 15:08:43 -050013 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
Ash Wilsonfd566482014-09-23 15:47:35 -040014 "github.com/rackspace/gophercloud/pagination"
Jon Perrittf3b2e142014-11-04 16:00:19 -060015 th "github.com/rackspace/gophercloud/testhelper"
Ash Wilsonfd566482014-09-23 15:47:35 -040016)
17
18func TestListServers(t *testing.T) {
19 client, err := newClient()
20 if err != nil {
21 t.Fatalf("Unable to create a compute client: %v", err)
22 }
23
24 t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6")
25
Jamie Hannaford1e7841b2014-10-16 12:45:49 +020026 pager := servers.List(client, servers.ListOpts{})
Ash Wilsonfd566482014-09-23 15:47:35 -040027 count, pages := 0, 0
28 pager.EachPage(func(page pagination.Page) (bool, error) {
29 pages++
30 t.Logf("---")
31
32 servers, err := servers.ExtractServers(page)
33 if err != nil {
34 return false, err
35 }
36
37 for _, s := range servers {
38 t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6)
39 count++
40 }
41
42 return true, nil
43 })
44
Ash Wilson9a9f5bc2014-10-24 14:55:40 -040045 t.Logf("--------\n%d servers listed on %d pages.\n", count, pages)
Ash Wilsonfd566482014-09-23 15:47:35 -040046}
47
Jon Perritt2a7797d2014-10-21 15:08:43 -050048func networkingClient() (*gophercloud.ServiceClient, error) {
Ash Wilson1904fce2014-10-22 15:50:48 -040049 opts, err := openstack.AuthOptionsFromEnv()
Jon Perritt2a7797d2014-10-21 15:08:43 -050050 if err != nil {
51 return nil, err
52 }
53
54 provider, err := openstack.AuthenticatedClient(opts)
55 if err != nil {
56 return nil, err
57 }
58
59 return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
Jon Perritt2a7797d2014-10-21 15:08:43 -050060 Region: os.Getenv("OS_REGION_NAME"),
61 })
62}
63
Ash Wilsonfd566482014-09-23 15:47:35 -040064func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
Ash Wilson14c37412014-10-24 14:45:27 -040065 if testing.Short() {
Jon Perritt8135c6f2014-10-23 12:29:14 -050066 t.Skip("Skipping test that requires server creation in short mode.")
67 }
68
Jon Perritt2a7797d2014-10-21 15:08:43 -050069 var network networks.Network
70
71 networkingClient, err := networkingClient()
72 if err != nil {
73 t.Fatalf("Unable to create a networking client: %v", err)
74 }
75
Joe Topjianb4395c72015-02-24 02:47:23 +000076 pager := networks.List(networkingClient, networks.ListOpts{
77 Name: choices.NetworkName,
78 Limit: 1,
79 })
Jon Perritt2a7797d2014-10-21 15:08:43 -050080 pager.EachPage(func(page pagination.Page) (bool, error) {
81 networks, err := networks.ExtractNetworks(page)
82 if err != nil {
83 t.Errorf("Failed to extract networks: %v", err)
84 return false, err
85 }
86
87 if len(networks) == 0 {
Jon Perritt3525c642014-10-21 17:12:06 -050088 t.Fatalf("No networks to attach to server")
Jon Perritt2a7797d2014-10-21 15:08:43 -050089 return false, err
90 }
91
92 network = networks[0]
93
94 return false, nil
95 })
96
Ash Wilsonfd566482014-09-23 15:47:35 -040097 name := tools.RandomString("ACPTTEST", 16)
98 t.Logf("Attempting to create server: %s\n", name)
99
Jon Perrittf3b2e142014-11-04 16:00:19 -0600100 pwd := tools.MakeNewPassword("")
101
Ash Wilson488e7302014-10-02 11:04:25 -0400102 server, err := servers.Create(client, servers.CreateOpts{
Ash Wilsone8458512014-09-29 08:50:09 -0400103 Name: name,
104 FlavorRef: choices.FlavorID,
105 ImageRef: choices.ImageID,
Jon Perritt2a7797d2014-10-21 15:08:43 -0500106 Networks: []servers.Network{
107 servers.Network{UUID: network.ID},
108 },
Jon Perrittf3b2e142014-11-04 16:00:19 -0600109 AdminPass: pwd,
Kevin Pike60c1e892015-05-05 07:35:02 -0700110 Personality: servers.Personality{
111 &servers.File{
112 Path: "/etc/test",
113 Contents: []byte("hello world"),
114 },
115 },
Ash Wilson89734d02014-09-25 13:50:08 -0400116 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400117 if err != nil {
118 t.Fatalf("Unable to create server: %v", err)
119 }
120
Jon Perrittf3b2e142014-11-04 16:00:19 -0600121 th.AssertEquals(t, pwd, server.AdminPass)
122
Ash Wilson89734d02014-09-25 13:50:08 -0400123 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -0400124}
125
126func TestCreateDestroyServer(t *testing.T) {
127 choices, err := ComputeChoicesFromEnv()
128 if err != nil {
129 t.Fatal(err)
130 }
131
132 client, err := newClient()
133 if err != nil {
134 t.Fatalf("Unable to create a compute client: %v", err)
135 }
136
Ash Wilsonfd566482014-09-23 15:47:35 -0400137 server, err := createServer(t, client, choices)
138 if err != nil {
139 t.Fatalf("Unable to create server: %v", err)
140 }
141 defer func() {
142 servers.Delete(client, server.ID)
143 t.Logf("Server deleted.")
144 }()
145
146 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
147 t.Fatalf("Unable to wait for server: %v", err)
148 }
Jon Perritt38cc0522015-02-19 22:16:40 -0700149
150 pager := servers.ListAddresses(client, server.ID)
151 pager.EachPage(func(page pagination.Page) (bool, error) {
152 networks, err := servers.ExtractAddresses(page)
153 if err != nil {
154 return false, err
155 }
156
157 for n, a := range networks {
158 t.Logf("%s: %+v\n", n, a)
159 }
160 return true, nil
161 })
162
Joe Topjianb4395c72015-02-24 02:47:23 +0000163 pager = servers.ListAddressesByNetwork(client, server.ID, choices.NetworkName)
Jon Perritt38cc0522015-02-19 22:16:40 -0700164 pager.EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700165 addresses, err := servers.ExtractNetworkAddresses(page)
Jon Perritt38cc0522015-02-19 22:16:40 -0700166 if err != nil {
167 return false, err
168 }
169
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700170 for _, a := range addresses {
171 t.Logf("%+v\n", a)
Jon Perritt38cc0522015-02-19 22:16:40 -0700172 }
173 return true, nil
174 })
Ash Wilsonfd566482014-09-23 15:47:35 -0400175}
176
177func TestUpdateServer(t *testing.T) {
178 client, err := newClient()
179 if err != nil {
180 t.Fatalf("Unable to create a compute client: %v", err)
181 }
182
183 choices, err := ComputeChoicesFromEnv()
184 if err != nil {
185 t.Fatal(err)
186 }
187
188 server, err := createServer(t, client, choices)
189 if err != nil {
190 t.Fatal(err)
191 }
192 defer servers.Delete(client, server.ID)
193
194 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
195 t.Fatal(err)
196 }
197
198 alternateName := tools.RandomString("ACPTTEST", 16)
199 for alternateName == server.Name {
200 alternateName = tools.RandomString("ACPTTEST", 16)
201 }
202
203 t.Logf("Attempting to rename the server to %s.", alternateName)
204
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400205 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400206 if err != nil {
207 t.Fatalf("Unable to rename server: %v", err)
208 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400209
210 if updated.ID != server.ID {
211 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
212 }
213
214 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400215 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400216 if err != nil {
217 return false, err
218 }
219
220 return latest.Name == alternateName, nil
221 })
222}
223
224func TestActionChangeAdminPassword(t *testing.T) {
225 t.Parallel()
226
227 client, err := newClient()
228 if err != nil {
229 t.Fatalf("Unable to create a compute client: %v", err)
230 }
231
232 choices, err := ComputeChoicesFromEnv()
233 if err != nil {
234 t.Fatal(err)
235 }
236
237 server, err := createServer(t, client, choices)
238 if err != nil {
239 t.Fatal(err)
240 }
241 defer servers.Delete(client, server.ID)
242
243 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
244 t.Fatal(err)
245 }
246
247 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500248 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200249 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400250 t.Fatal(err)
251 }
252
253 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
254 t.Fatal(err)
255 }
256
257 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
258 t.Fatal(err)
259 }
260}
261
262func TestActionReboot(t *testing.T) {
263 t.Parallel()
264
265 client, err := newClient()
266 if err != nil {
267 t.Fatalf("Unable to create a compute client: %v", err)
268 }
269
270 choices, err := ComputeChoicesFromEnv()
271 if err != nil {
272 t.Fatal(err)
273 }
274
275 server, err := createServer(t, client, choices)
276 if err != nil {
277 t.Fatal(err)
278 }
279 defer servers.Delete(client, server.ID)
280
281 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
282 t.Fatal(err)
283 }
284
Jon Perritt2a7797d2014-10-21 15:08:43 -0500285 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200286 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400287 t.Fatal("Expected the SDK to provide an ArgumentError here")
288 }
289
290 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200291 res = servers.Reboot(client, server.ID, servers.OSReboot)
292 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400293 t.Fatalf("Unable to reboot server: %v", err)
294 }
295
296 if err = waitForStatus(client, server, "REBOOT"); err != nil {
297 t.Fatal(err)
298 }
299
300 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
301 t.Fatal(err)
302 }
303}
304
305func TestActionRebuild(t *testing.T) {
306 t.Parallel()
307
308 client, err := newClient()
309 if err != nil {
310 t.Fatalf("Unable to create a compute client: %v", err)
311 }
312
313 choices, err := ComputeChoicesFromEnv()
314 if err != nil {
315 t.Fatal(err)
316 }
317
318 server, err := createServer(t, client, choices)
319 if err != nil {
320 t.Fatal(err)
321 }
322 defer servers.Delete(client, server.ID)
323
324 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
325 t.Fatal(err)
326 }
327
328 t.Logf("Attempting to rebuild server %s", server.ID)
329
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200330 rebuildOpts := servers.RebuildOpts{
331 Name: tools.RandomString("ACPTTEST", 16),
332 AdminPass: tools.MakeNewPassword(server.AdminPass),
333 ImageID: choices.ImageID,
334 }
335
336 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400337 if err != nil {
338 t.Fatal(err)
339 }
340
Ash Wilsonfd566482014-09-23 15:47:35 -0400341 if rebuilt.ID != server.ID {
342 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
343 }
344
345 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
346 t.Fatal(err)
347 }
348
349 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
350 t.Fatal(err)
351 }
352}
353
Ash Wilsondc7daa82014-09-23 16:34:42 -0400354func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
355 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400356 t.Fatal(err)
357 }
358
359 t.Logf("Attempting to resize server [%s]", server.ID)
360
Ash Wilson14c37412014-10-24 14:45:27 -0400361 opts := &servers.ResizeOpts{
362 FlavorRef: choices.FlavorIDResize,
363 }
364 if res := servers.Resize(client, server.ID, opts); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500365 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400366 }
367
Ash Wilsondc7daa82014-09-23 16:34:42 -0400368 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400369 t.Fatal(err)
370 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400371}
372
373func TestActionResizeConfirm(t *testing.T) {
374 t.Parallel()
375
Ash Wilsondc7daa82014-09-23 16:34:42 -0400376 choices, err := ComputeChoicesFromEnv()
377 if err != nil {
378 t.Fatal(err)
379 }
380
Ash Wilsonfd566482014-09-23 15:47:35 -0400381 client, err := newClient()
382 if err != nil {
383 t.Fatalf("Unable to create a compute client: %v", err)
384 }
385
Ash Wilsondc7daa82014-09-23 16:34:42 -0400386 server, err := createServer(t, client, choices)
387 if err != nil {
388 t.Fatal(err)
389 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400390 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400391 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400392
393 t.Logf("Attempting to confirm resize for server %s", server.ID)
394
Jamie Hannaforda2621742014-10-16 14:35:49 +0200395 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400396 t.Fatal(err)
397 }
398
399 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
400 t.Fatal(err)
401 }
402}
403
404func TestActionResizeRevert(t *testing.T) {
405 t.Parallel()
406
Ash Wilsondc7daa82014-09-23 16:34:42 -0400407 choices, err := ComputeChoicesFromEnv()
408 if err != nil {
409 t.Fatal(err)
410 }
411
Ash Wilsonfd566482014-09-23 15:47:35 -0400412 client, err := newClient()
413 if err != nil {
414 t.Fatalf("Unable to create a compute client: %v", err)
415 }
416
Ash Wilsondc7daa82014-09-23 16:34:42 -0400417 server, err := createServer(t, client, choices)
418 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400419 t.Fatal(err)
420 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400421 defer servers.Delete(client, server.ID)
422 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400423
424 t.Logf("Attempting to revert resize for server %s", server.ID)
425
Jamie Hannaforda2621742014-10-16 14:35:49 +0200426 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400427 t.Fatal(err)
428 }
429
Ash Wilsonfd566482014-09-23 15:47:35 -0400430 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
431 t.Fatal(err)
432 }
433}
Jon Perrittcc77da62014-11-16 13:14:21 -0700434
435func TestServerMetadata(t *testing.T) {
436 t.Parallel()
437
438 choices, err := ComputeChoicesFromEnv()
Jon Perrittd29c4af2014-11-20 08:47:56 -0700439 th.AssertNoErr(t, err)
Jon Perrittcc77da62014-11-16 13:14:21 -0700440
441 client, err := newClient()
442 if err != nil {
443 t.Fatalf("Unable to create a compute client: %v", err)
444 }
445
446 server, err := createServer(t, client, choices)
447 if err != nil {
448 t.Fatal(err)
449 }
450 defer servers.Delete(client, server.ID)
451 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
452 t.Fatal(err)
453 }
454
Jon Perritt78c57ce2014-11-20 11:07:18 -0700455 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700456 "foo": "bar",
457 "this": "that",
458 }).Extract()
459 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700460 t.Logf("UpdateMetadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700461
Jon Perritt78c57ce2014-11-20 11:07:18 -0700462 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
Jon Perrittcc77da62014-11-16 13:14:21 -0700463 th.AssertNoErr(t, err)
464
Jon Perritt78c57ce2014-11-20 11:07:18 -0700465 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700466 "foo": "baz",
467 }).Extract()
468 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700469 t.Logf("CreateMetadatum result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700470
Jon Perritt78c57ce2014-11-20 11:07:18 -0700471 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700472 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700473 t.Logf("Metadatum result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700474 th.AssertEquals(t, "baz", metadata["foo"])
475
Jon Perritt78c57ce2014-11-20 11:07:18 -0700476 metadata, err = servers.Metadata(client, server.ID).Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700477 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700478 t.Logf("Metadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700479
Jon Perritt789f8322014-11-21 08:20:04 -0700480 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700481 th.AssertNoErr(t, err)
Jon Perritt789f8322014-11-21 08:20:04 -0700482 t.Logf("ResetMetadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700483 th.AssertDeepEquals(t, map[string]string{}, metadata)
484}