blob: ec63c120154d6a6f06c97528c1cd06c09bec7b5d [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{
60 Name: "neutron",
61 Region: os.Getenv("OS_REGION_NAME"),
62 })
63}
64
Ash Wilsonfd566482014-09-23 15:47:35 -040065func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
Ash Wilson14c37412014-10-24 14:45:27 -040066 if testing.Short() {
Jon Perritt8135c6f2014-10-23 12:29:14 -050067 t.Skip("Skipping test that requires server creation in short mode.")
68 }
69
Jon Perritt2a7797d2014-10-21 15:08:43 -050070 var network networks.Network
71
72 networkingClient, err := networkingClient()
73 if err != nil {
74 t.Fatalf("Unable to create a networking client: %v", err)
75 }
76
77 pager := networks.List(networkingClient, networks.ListOpts{Name: "public", Limit: 1})
78 pager.EachPage(func(page pagination.Page) (bool, error) {
79 networks, err := networks.ExtractNetworks(page)
80 if err != nil {
81 t.Errorf("Failed to extract networks: %v", err)
82 return false, err
83 }
84
85 if len(networks) == 0 {
Jon Perritt3525c642014-10-21 17:12:06 -050086 t.Fatalf("No networks to attach to server")
Jon Perritt2a7797d2014-10-21 15:08:43 -050087 return false, err
88 }
89
90 network = networks[0]
91
92 return false, nil
93 })
94
Ash Wilsonfd566482014-09-23 15:47:35 -040095 name := tools.RandomString("ACPTTEST", 16)
96 t.Logf("Attempting to create server: %s\n", name)
97
Jon Perrittf3b2e142014-11-04 16:00:19 -060098 pwd := tools.MakeNewPassword("")
99
Ash Wilson488e7302014-10-02 11:04:25 -0400100 server, err := servers.Create(client, servers.CreateOpts{
Ash Wilsone8458512014-09-29 08:50:09 -0400101 Name: name,
102 FlavorRef: choices.FlavorID,
103 ImageRef: choices.ImageID,
Jon Perritt2a7797d2014-10-21 15:08:43 -0500104 Networks: []servers.Network{
105 servers.Network{UUID: network.ID},
106 },
Jon Perrittf3b2e142014-11-04 16:00:19 -0600107 AdminPass: pwd,
Ash Wilson89734d02014-09-25 13:50:08 -0400108 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400109 if err != nil {
110 t.Fatalf("Unable to create server: %v", err)
111 }
112
Jon Perrittf3b2e142014-11-04 16:00:19 -0600113 th.AssertEquals(t, pwd, server.AdminPass)
114
Ash Wilson89734d02014-09-25 13:50:08 -0400115 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -0400116}
117
118func TestCreateDestroyServer(t *testing.T) {
119 choices, err := ComputeChoicesFromEnv()
120 if err != nil {
121 t.Fatal(err)
122 }
123
124 client, err := newClient()
125 if err != nil {
126 t.Fatalf("Unable to create a compute client: %v", err)
127 }
128
Ash Wilsonfd566482014-09-23 15:47:35 -0400129 server, err := createServer(t, client, choices)
130 if err != nil {
131 t.Fatalf("Unable to create server: %v", err)
132 }
133 defer func() {
134 servers.Delete(client, server.ID)
135 t.Logf("Server deleted.")
136 }()
137
138 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
139 t.Fatalf("Unable to wait for server: %v", err)
140 }
Jon Perritt38cc0522015-02-19 22:16:40 -0700141
142 pager := servers.ListAddresses(client, server.ID)
143 pager.EachPage(func(page pagination.Page) (bool, error) {
144 networks, err := servers.ExtractAddresses(page)
145 if err != nil {
146 return false, err
147 }
148
149 for n, a := range networks {
150 t.Logf("%s: %+v\n", n, a)
151 }
152 return true, nil
153 })
154
155 pager = servers.ListAddressesByNetwork(client, server.ID, "public")
156 pager.EachPage(func(page pagination.Page) (bool, error) {
157 network, err := servers.ExtractNetworkAddresses(page)
158 if err != nil {
159 return false, err
160 }
161
162 for n, a := range network {
163 t.Logf("%s: %+v\n", n, a)
164 }
165 return true, nil
166 })
Ash Wilsonfd566482014-09-23 15:47:35 -0400167}
168
169func TestUpdateServer(t *testing.T) {
170 client, err := newClient()
171 if err != nil {
172 t.Fatalf("Unable to create a compute client: %v", err)
173 }
174
175 choices, err := ComputeChoicesFromEnv()
176 if err != nil {
177 t.Fatal(err)
178 }
179
180 server, err := createServer(t, client, choices)
181 if err != nil {
182 t.Fatal(err)
183 }
184 defer servers.Delete(client, server.ID)
185
186 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
187 t.Fatal(err)
188 }
189
190 alternateName := tools.RandomString("ACPTTEST", 16)
191 for alternateName == server.Name {
192 alternateName = tools.RandomString("ACPTTEST", 16)
193 }
194
195 t.Logf("Attempting to rename the server to %s.", alternateName)
196
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400197 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400198 if err != nil {
199 t.Fatalf("Unable to rename server: %v", err)
200 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400201
202 if updated.ID != server.ID {
203 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
204 }
205
206 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400207 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400208 if err != nil {
209 return false, err
210 }
211
212 return latest.Name == alternateName, nil
213 })
214}
215
216func TestActionChangeAdminPassword(t *testing.T) {
217 t.Parallel()
218
219 client, err := newClient()
220 if err != nil {
221 t.Fatalf("Unable to create a compute client: %v", err)
222 }
223
224 choices, err := ComputeChoicesFromEnv()
225 if err != nil {
226 t.Fatal(err)
227 }
228
229 server, err := createServer(t, client, choices)
230 if err != nil {
231 t.Fatal(err)
232 }
233 defer servers.Delete(client, server.ID)
234
235 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
236 t.Fatal(err)
237 }
238
239 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500240 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200241 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400242 t.Fatal(err)
243 }
244
245 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
246 t.Fatal(err)
247 }
248
249 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
250 t.Fatal(err)
251 }
252}
253
254func TestActionReboot(t *testing.T) {
255 t.Parallel()
256
257 client, err := newClient()
258 if err != nil {
259 t.Fatalf("Unable to create a compute client: %v", err)
260 }
261
262 choices, err := ComputeChoicesFromEnv()
263 if err != nil {
264 t.Fatal(err)
265 }
266
267 server, err := createServer(t, client, choices)
268 if err != nil {
269 t.Fatal(err)
270 }
271 defer servers.Delete(client, server.ID)
272
273 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
274 t.Fatal(err)
275 }
276
Jon Perritt2a7797d2014-10-21 15:08:43 -0500277 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200278 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400279 t.Fatal("Expected the SDK to provide an ArgumentError here")
280 }
281
282 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200283 res = servers.Reboot(client, server.ID, servers.OSReboot)
284 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400285 t.Fatalf("Unable to reboot server: %v", err)
286 }
287
288 if err = waitForStatus(client, server, "REBOOT"); err != nil {
289 t.Fatal(err)
290 }
291
292 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
293 t.Fatal(err)
294 }
295}
296
297func TestActionRebuild(t *testing.T) {
298 t.Parallel()
299
300 client, err := newClient()
301 if err != nil {
302 t.Fatalf("Unable to create a compute client: %v", err)
303 }
304
305 choices, err := ComputeChoicesFromEnv()
306 if err != nil {
307 t.Fatal(err)
308 }
309
310 server, err := createServer(t, client, choices)
311 if err != nil {
312 t.Fatal(err)
313 }
314 defer servers.Delete(client, server.ID)
315
316 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
317 t.Fatal(err)
318 }
319
320 t.Logf("Attempting to rebuild server %s", server.ID)
321
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200322 rebuildOpts := servers.RebuildOpts{
323 Name: tools.RandomString("ACPTTEST", 16),
324 AdminPass: tools.MakeNewPassword(server.AdminPass),
325 ImageID: choices.ImageID,
326 }
327
328 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400329 if err != nil {
330 t.Fatal(err)
331 }
332
Ash Wilsonfd566482014-09-23 15:47:35 -0400333 if rebuilt.ID != server.ID {
334 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
335 }
336
337 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
338 t.Fatal(err)
339 }
340
341 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
342 t.Fatal(err)
343 }
344}
345
Ash Wilsondc7daa82014-09-23 16:34:42 -0400346func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
347 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400348 t.Fatal(err)
349 }
350
351 t.Logf("Attempting to resize server [%s]", server.ID)
352
Ash Wilson14c37412014-10-24 14:45:27 -0400353 opts := &servers.ResizeOpts{
354 FlavorRef: choices.FlavorIDResize,
355 }
356 if res := servers.Resize(client, server.ID, opts); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500357 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400358 }
359
Ash Wilsondc7daa82014-09-23 16:34:42 -0400360 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400361 t.Fatal(err)
362 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400363}
364
365func TestActionResizeConfirm(t *testing.T) {
366 t.Parallel()
367
Ash Wilsondc7daa82014-09-23 16:34:42 -0400368 choices, err := ComputeChoicesFromEnv()
369 if err != nil {
370 t.Fatal(err)
371 }
372
Ash Wilsonfd566482014-09-23 15:47:35 -0400373 client, err := newClient()
374 if err != nil {
375 t.Fatalf("Unable to create a compute client: %v", err)
376 }
377
Ash Wilsondc7daa82014-09-23 16:34:42 -0400378 server, err := createServer(t, client, choices)
379 if err != nil {
380 t.Fatal(err)
381 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400382 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400383 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400384
385 t.Logf("Attempting to confirm resize for server %s", server.ID)
386
Jamie Hannaforda2621742014-10-16 14:35:49 +0200387 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400388 t.Fatal(err)
389 }
390
391 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
392 t.Fatal(err)
393 }
394}
395
396func TestActionResizeRevert(t *testing.T) {
397 t.Parallel()
398
Ash Wilsondc7daa82014-09-23 16:34:42 -0400399 choices, err := ComputeChoicesFromEnv()
400 if err != nil {
401 t.Fatal(err)
402 }
403
Ash Wilsonfd566482014-09-23 15:47:35 -0400404 client, err := newClient()
405 if err != nil {
406 t.Fatalf("Unable to create a compute client: %v", err)
407 }
408
Ash Wilsondc7daa82014-09-23 16:34:42 -0400409 server, err := createServer(t, client, choices)
410 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400411 t.Fatal(err)
412 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400413 defer servers.Delete(client, server.ID)
414 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400415
416 t.Logf("Attempting to revert resize for server %s", server.ID)
417
Jamie Hannaforda2621742014-10-16 14:35:49 +0200418 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400419 t.Fatal(err)
420 }
421
Ash Wilsonfd566482014-09-23 15:47:35 -0400422 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
423 t.Fatal(err)
424 }
425}
Jon Perrittcc77da62014-11-16 13:14:21 -0700426
427func TestServerMetadata(t *testing.T) {
428 t.Parallel()
429
430 choices, err := ComputeChoicesFromEnv()
Jon Perrittd29c4af2014-11-20 08:47:56 -0700431 th.AssertNoErr(t, err)
Jon Perrittcc77da62014-11-16 13:14:21 -0700432
433 client, err := newClient()
434 if err != nil {
435 t.Fatalf("Unable to create a compute client: %v", err)
436 }
437
438 server, err := createServer(t, client, choices)
439 if err != nil {
440 t.Fatal(err)
441 }
442 defer servers.Delete(client, server.ID)
443 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
444 t.Fatal(err)
445 }
446
Jon Perritt78c57ce2014-11-20 11:07:18 -0700447 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700448 "foo": "bar",
449 "this": "that",
450 }).Extract()
451 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700452 t.Logf("UpdateMetadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700453
Jon Perritt78c57ce2014-11-20 11:07:18 -0700454 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
Jon Perrittcc77da62014-11-16 13:14:21 -0700455 th.AssertNoErr(t, err)
456
Jon Perritt78c57ce2014-11-20 11:07:18 -0700457 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700458 "foo": "baz",
459 }).Extract()
460 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700461 t.Logf("CreateMetadatum result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700462
Jon Perritt78c57ce2014-11-20 11:07:18 -0700463 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700464 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700465 t.Logf("Metadatum result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700466 th.AssertEquals(t, "baz", metadata["foo"])
467
Jon Perritt78c57ce2014-11-20 11:07:18 -0700468 metadata, err = servers.Metadata(client, server.ID).Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700469 th.AssertNoErr(t, err)
Jon Perritt78c57ce2014-11-20 11:07:18 -0700470 t.Logf("Metadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700471
Jon Perritt789f8322014-11-21 08:20:04 -0700472 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700473 th.AssertNoErr(t, err)
Jon Perritt789f8322014-11-21 08:20:04 -0700474 t.Logf("ResetMetadata result: %+v\n", metadata)
Jon Perrittcc77da62014-11-16 13:14:21 -0700475 th.AssertDeepEquals(t, map[string]string{}, metadata)
476}