blob: 7b928e9ef5099afc2e0c89c6302f52880c677475 [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,
Ash Wilson89734d02014-09-25 13:50:08 -0400110 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400111 if err != nil {
112 t.Fatalf("Unable to create server: %v", err)
113 }
114
Jon Perrittf3b2e142014-11-04 16:00:19 -0600115 th.AssertEquals(t, pwd, server.AdminPass)
116
Ash Wilson89734d02014-09-25 13:50:08 -0400117 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -0400118}
119
120func TestCreateDestroyServer(t *testing.T) {
121 choices, err := ComputeChoicesFromEnv()
122 if err != nil {
123 t.Fatal(err)
124 }
125
126 client, err := newClient()
127 if err != nil {
128 t.Fatalf("Unable to create a compute client: %v", err)
129 }
130
Ash Wilsonfd566482014-09-23 15:47:35 -0400131 server, err := createServer(t, client, choices)
132 if err != nil {
133 t.Fatalf("Unable to create server: %v", err)
134 }
135 defer func() {
136 servers.Delete(client, server.ID)
137 t.Logf("Server deleted.")
138 }()
139
140 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
141 t.Fatalf("Unable to wait for server: %v", err)
142 }
Jon Perritt38cc0522015-02-19 22:16:40 -0700143
144 pager := servers.ListAddresses(client, server.ID)
145 pager.EachPage(func(page pagination.Page) (bool, error) {
146 networks, err := servers.ExtractAddresses(page)
147 if err != nil {
148 return false, err
149 }
150
151 for n, a := range networks {
152 t.Logf("%s: %+v\n", n, a)
153 }
154 return true, nil
155 })
156
Joe Topjianb4395c72015-02-24 02:47:23 +0000157 pager = servers.ListAddressesByNetwork(client, server.ID, choices.NetworkName)
Jon Perritt38cc0522015-02-19 22:16:40 -0700158 pager.EachPage(func(page pagination.Page) (bool, error) {
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700159 addresses, err := servers.ExtractNetworkAddresses(page)
Jon Perritt38cc0522015-02-19 22:16:40 -0700160 if err != nil {
161 return false, err
162 }
163
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700164 for _, a := range addresses {
165 t.Logf("%+v\n", a)
Jon Perritt38cc0522015-02-19 22:16:40 -0700166 }
167 return true, nil
168 })
Ash Wilsonfd566482014-09-23 15:47:35 -0400169}
170
171func TestUpdateServer(t *testing.T) {
172 client, err := newClient()
173 if err != nil {
174 t.Fatalf("Unable to create a compute client: %v", err)
175 }
176
177 choices, err := ComputeChoicesFromEnv()
178 if err != nil {
179 t.Fatal(err)
180 }
181
182 server, err := createServer(t, client, choices)
183 if err != nil {
184 t.Fatal(err)
185 }
186 defer servers.Delete(client, server.ID)
187
188 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
189 t.Fatal(err)
190 }
191
192 alternateName := tools.RandomString("ACPTTEST", 16)
193 for alternateName == server.Name {
194 alternateName = tools.RandomString("ACPTTEST", 16)
195 }
196
197 t.Logf("Attempting to rename the server to %s.", alternateName)
198
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400199 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400200 if err != nil {
201 t.Fatalf("Unable to rename server: %v", err)
202 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400203
204 if updated.ID != server.ID {
205 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
206 }
207
208 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400209 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400210 if err != nil {
211 return false, err
212 }
213
214 return latest.Name == alternateName, nil
215 })
216}
217
218func TestActionChangeAdminPassword(t *testing.T) {
219 t.Parallel()
220
221 client, err := newClient()
222 if err != nil {
223 t.Fatalf("Unable to create a compute client: %v", err)
224 }
225
226 choices, err := ComputeChoicesFromEnv()
227 if err != nil {
228 t.Fatal(err)
229 }
230
231 server, err := createServer(t, client, choices)
232 if err != nil {
233 t.Fatal(err)
234 }
235 defer servers.Delete(client, server.ID)
236
237 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
238 t.Fatal(err)
239 }
240
241 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500242 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200243 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400244 t.Fatal(err)
245 }
246
247 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
248 t.Fatal(err)
249 }
250
251 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
252 t.Fatal(err)
253 }
254}
255
256func TestActionReboot(t *testing.T) {
257 t.Parallel()
258
259 client, err := newClient()
260 if err != nil {
261 t.Fatalf("Unable to create a compute client: %v", err)
262 }
263
264 choices, err := ComputeChoicesFromEnv()
265 if err != nil {
266 t.Fatal(err)
267 }
268
269 server, err := createServer(t, client, choices)
270 if err != nil {
271 t.Fatal(err)
272 }
273 defer servers.Delete(client, server.ID)
274
275 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
276 t.Fatal(err)
277 }
278
Jon Perritt2a7797d2014-10-21 15:08:43 -0500279 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200280 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400281 t.Fatal("Expected the SDK to provide an ArgumentError here")
282 }
283
284 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200285 res = servers.Reboot(client, server.ID, servers.OSReboot)
286 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400287 t.Fatalf("Unable to reboot server: %v", err)
288 }
289
290 if err = waitForStatus(client, server, "REBOOT"); err != nil {
291 t.Fatal(err)
292 }
293
294 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
295 t.Fatal(err)
296 }
297}
298
299func TestActionRebuild(t *testing.T) {
300 t.Parallel()
301
302 client, err := newClient()
303 if err != nil {
304 t.Fatalf("Unable to create a compute client: %v", err)
305 }
306
307 choices, err := ComputeChoicesFromEnv()
308 if err != nil {
309 t.Fatal(err)
310 }
311
312 server, err := createServer(t, client, choices)
313 if err != nil {
314 t.Fatal(err)
315 }
316 defer servers.Delete(client, server.ID)
317
318 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
319 t.Fatal(err)
320 }
321
322 t.Logf("Attempting to rebuild server %s", server.ID)
323
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200324 rebuildOpts := servers.RebuildOpts{
325 Name: tools.RandomString("ACPTTEST", 16),
326 AdminPass: tools.MakeNewPassword(server.AdminPass),
327 ImageID: choices.ImageID,
328 }
329
330 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400331 if err != nil {
332 t.Fatal(err)
333 }
334
Ash Wilsonfd566482014-09-23 15:47:35 -0400335 if rebuilt.ID != server.ID {
336 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
337 }
338
339 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
340 t.Fatal(err)
341 }
342
343 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
344 t.Fatal(err)
345 }
346}
347
Ash Wilsondc7daa82014-09-23 16:34:42 -0400348func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
349 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400350 t.Fatal(err)
351 }
352
353 t.Logf("Attempting to resize server [%s]", server.ID)
354
Ash Wilson14c37412014-10-24 14:45:27 -0400355 opts := &servers.ResizeOpts{
356 FlavorRef: choices.FlavorIDResize,
357 }
358 if res := servers.Resize(client, server.ID, opts); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500359 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400360 }
361
Ash Wilsondc7daa82014-09-23 16:34:42 -0400362 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400363 t.Fatal(err)
364 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400365}
366
367func TestActionResizeConfirm(t *testing.T) {
368 t.Parallel()
369
Ash Wilsondc7daa82014-09-23 16:34:42 -0400370 choices, err := ComputeChoicesFromEnv()
371 if err != nil {
372 t.Fatal(err)
373 }
374
Ash Wilsonfd566482014-09-23 15:47:35 -0400375 client, err := newClient()
376 if err != nil {
377 t.Fatalf("Unable to create a compute client: %v", err)
378 }
379
Ash Wilsondc7daa82014-09-23 16:34:42 -0400380 server, err := createServer(t, client, choices)
381 if err != nil {
382 t.Fatal(err)
383 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400384 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400385 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400386
387 t.Logf("Attempting to confirm resize for server %s", server.ID)
388
Jamie Hannaforda2621742014-10-16 14:35:49 +0200389 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400390 t.Fatal(err)
391 }
392
393 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
394 t.Fatal(err)
395 }
396}
397
398func TestActionResizeRevert(t *testing.T) {
399 t.Parallel()
400
Ash Wilsondc7daa82014-09-23 16:34:42 -0400401 choices, err := ComputeChoicesFromEnv()
402 if err != nil {
403 t.Fatal(err)
404 }
405
Ash Wilsonfd566482014-09-23 15:47:35 -0400406 client, err := newClient()
407 if err != nil {
408 t.Fatalf("Unable to create a compute client: %v", err)
409 }
410
Ash Wilsondc7daa82014-09-23 16:34:42 -0400411 server, err := createServer(t, client, choices)
412 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400413 t.Fatal(err)
414 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400415 defer servers.Delete(client, server.ID)
416 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400417
418 t.Logf("Attempting to revert resize for server %s", server.ID)
419
Jamie Hannaforda2621742014-10-16 14:35:49 +0200420 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400421 t.Fatal(err)
422 }
423
Ash Wilsonfd566482014-09-23 15:47:35 -0400424 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
425 t.Fatal(err)
426 }
427}
Jon Perrittcc77da62014-11-16 13:14:21 -0700428
429func TestServerMetadata(t *testing.T) {
430 t.Parallel()
431
432 choices, err := ComputeChoicesFromEnv()
Jon Perrittd29c4af2014-11-20 08:47:56 -0700433 th.AssertNoErr(t, err)
Jon Perrittcc77da62014-11-16 13:14:21 -0700434
435 client, err := newClient()
436 if err != nil {
437 t.Fatalf("Unable to create a compute client: %v", err)
438 }
439
440 server, err := createServer(t, client, choices)
441 if err != nil {
442 t.Fatal(err)
443 }
444 defer servers.Delete(client, server.ID)
445 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
446 t.Fatal(err)
447 }
448
Jon Perritt78c57ce2014-11-20 11:07:18 -0700449 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700450 "foo": "bar",
451 "this": "that",
452 }).Extract()
453 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700454 t.Logf("UpdateMetadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700455
Jon Perritt78c57ce2014-11-20 11:07:18 -0700456 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
Jon Perrittcc77da62014-11-16 13:14:21 -0700457 th.AssertNoErr(t, err)
458
Jon Perritt78c57ce2014-11-20 11:07:18 -0700459 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700460 "foo": "baz",
461 }).Extract()
462 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700463 t.Logf("CreateMetadatum result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700464
Jon Perritt78c57ce2014-11-20 11:07:18 -0700465 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700466 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700467 t.Logf("Metadatum result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700468 th.AssertEquals(t, "baz", metadata["foo"])
469
Jon Perritt78c57ce2014-11-20 11:07:18 -0700470 metadata, err = servers.Metadata(client, server.ID).Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700471 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700472 t.Logf("Metadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700473
Jon Perritt789f8322014-11-21 08:20:04 -0700474 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700475 th.AssertNoErr(t, err)
Jon Perritt789f8322014-11-21 08:20:04 -0700476 t.Logf("ResetMetadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700477 th.AssertDeepEquals(t, map[string]string{}, metadata)
478}