blob: a3e58791b128b8fb69e9e82c7bf4f96173b23a53 [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 }
141}
142
143func TestUpdateServer(t *testing.T) {
144 client, err := newClient()
145 if err != nil {
146 t.Fatalf("Unable to create a compute client: %v", err)
147 }
148
149 choices, err := ComputeChoicesFromEnv()
150 if err != nil {
151 t.Fatal(err)
152 }
153
154 server, err := createServer(t, client, choices)
155 if err != nil {
156 t.Fatal(err)
157 }
158 defer servers.Delete(client, server.ID)
159
160 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
161 t.Fatal(err)
162 }
163
164 alternateName := tools.RandomString("ACPTTEST", 16)
165 for alternateName == server.Name {
166 alternateName = tools.RandomString("ACPTTEST", 16)
167 }
168
169 t.Logf("Attempting to rename the server to %s.", alternateName)
170
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400171 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400172 if err != nil {
173 t.Fatalf("Unable to rename server: %v", err)
174 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400175
176 if updated.ID != server.ID {
177 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
178 }
179
180 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400181 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400182 if err != nil {
183 return false, err
184 }
185
186 return latest.Name == alternateName, nil
187 })
188}
189
190func TestActionChangeAdminPassword(t *testing.T) {
191 t.Parallel()
192
193 client, err := newClient()
194 if err != nil {
195 t.Fatalf("Unable to create a compute client: %v", err)
196 }
197
198 choices, err := ComputeChoicesFromEnv()
199 if err != nil {
200 t.Fatal(err)
201 }
202
203 server, err := createServer(t, client, choices)
204 if err != nil {
205 t.Fatal(err)
206 }
207 defer servers.Delete(client, server.ID)
208
209 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
210 t.Fatal(err)
211 }
212
213 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500214 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200215 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400216 t.Fatal(err)
217 }
218
219 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
220 t.Fatal(err)
221 }
222
223 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
224 t.Fatal(err)
225 }
226}
227
228func TestActionReboot(t *testing.T) {
229 t.Parallel()
230
231 client, err := newClient()
232 if err != nil {
233 t.Fatalf("Unable to create a compute client: %v", err)
234 }
235
236 choices, err := ComputeChoicesFromEnv()
237 if err != nil {
238 t.Fatal(err)
239 }
240
241 server, err := createServer(t, client, choices)
242 if err != nil {
243 t.Fatal(err)
244 }
245 defer servers.Delete(client, server.ID)
246
247 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
248 t.Fatal(err)
249 }
250
Jon Perritt2a7797d2014-10-21 15:08:43 -0500251 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200252 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400253 t.Fatal("Expected the SDK to provide an ArgumentError here")
254 }
255
256 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200257 res = servers.Reboot(client, server.ID, servers.OSReboot)
258 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400259 t.Fatalf("Unable to reboot server: %v", err)
260 }
261
262 if err = waitForStatus(client, server, "REBOOT"); err != nil {
263 t.Fatal(err)
264 }
265
266 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
267 t.Fatal(err)
268 }
269}
270
271func TestActionRebuild(t *testing.T) {
272 t.Parallel()
273
274 client, err := newClient()
275 if err != nil {
276 t.Fatalf("Unable to create a compute client: %v", err)
277 }
278
279 choices, err := ComputeChoicesFromEnv()
280 if err != nil {
281 t.Fatal(err)
282 }
283
284 server, err := createServer(t, client, choices)
285 if err != nil {
286 t.Fatal(err)
287 }
288 defer servers.Delete(client, server.ID)
289
290 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
291 t.Fatal(err)
292 }
293
294 t.Logf("Attempting to rebuild server %s", server.ID)
295
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200296 rebuildOpts := servers.RebuildOpts{
297 Name: tools.RandomString("ACPTTEST", 16),
298 AdminPass: tools.MakeNewPassword(server.AdminPass),
299 ImageID: choices.ImageID,
300 }
301
302 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400303 if err != nil {
304 t.Fatal(err)
305 }
306
Ash Wilsonfd566482014-09-23 15:47:35 -0400307 if rebuilt.ID != server.ID {
308 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
309 }
310
311 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
312 t.Fatal(err)
313 }
314
315 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
316 t.Fatal(err)
317 }
318}
319
Ash Wilsondc7daa82014-09-23 16:34:42 -0400320func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
321 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400322 t.Fatal(err)
323 }
324
325 t.Logf("Attempting to resize server [%s]", server.ID)
326
Ash Wilson14c37412014-10-24 14:45:27 -0400327 opts := &servers.ResizeOpts{
328 FlavorRef: choices.FlavorIDResize,
329 }
330 if res := servers.Resize(client, server.ID, opts); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500331 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400332 }
333
Ash Wilsondc7daa82014-09-23 16:34:42 -0400334 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400335 t.Fatal(err)
336 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400337}
338
339func TestActionResizeConfirm(t *testing.T) {
340 t.Parallel()
341
Ash Wilsondc7daa82014-09-23 16:34:42 -0400342 choices, err := ComputeChoicesFromEnv()
343 if err != nil {
344 t.Fatal(err)
345 }
346
Ash Wilsonfd566482014-09-23 15:47:35 -0400347 client, err := newClient()
348 if err != nil {
349 t.Fatalf("Unable to create a compute client: %v", err)
350 }
351
Ash Wilsondc7daa82014-09-23 16:34:42 -0400352 server, err := createServer(t, client, choices)
353 if err != nil {
354 t.Fatal(err)
355 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400356 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400357 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400358
359 t.Logf("Attempting to confirm resize for server %s", server.ID)
360
Jamie Hannaforda2621742014-10-16 14:35:49 +0200361 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400362 t.Fatal(err)
363 }
364
365 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
366 t.Fatal(err)
367 }
368}
369
370func TestActionResizeRevert(t *testing.T) {
371 t.Parallel()
372
Ash Wilsondc7daa82014-09-23 16:34:42 -0400373 choices, err := ComputeChoicesFromEnv()
374 if err != nil {
375 t.Fatal(err)
376 }
377
Ash Wilsonfd566482014-09-23 15:47:35 -0400378 client, err := newClient()
379 if err != nil {
380 t.Fatalf("Unable to create a compute client: %v", err)
381 }
382
Ash Wilsondc7daa82014-09-23 16:34:42 -0400383 server, err := createServer(t, client, choices)
384 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400385 t.Fatal(err)
386 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400387 defer servers.Delete(client, server.ID)
388 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400389
390 t.Logf("Attempting to revert resize for server %s", server.ID)
391
Jamie Hannaforda2621742014-10-16 14:35:49 +0200392 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400393 t.Fatal(err)
394 }
395
Ash Wilsonfd566482014-09-23 15:47:35 -0400396 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
397 t.Fatal(err)
398 }
399}
Jon Perrittcc77da62014-11-16 13:14:21 -0700400
401func TestServerMetadata(t *testing.T) {
402 t.Parallel()
403
404 choices, err := ComputeChoicesFromEnv()
405 if err != nil {
406 t.Fatal(err)
407 }
408
409 client, err := newClient()
410 if err != nil {
411 t.Fatalf("Unable to create a compute client: %v", err)
412 }
413
414 server, err := createServer(t, client, choices)
415 if err != nil {
416 t.Fatal(err)
417 }
418 defer servers.Delete(client, server.ID)
419 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
420 t.Fatal(err)
421 }
422
423 metadata, err := servers.UpdateMetadatas(client, server.ID, servers.MetadatasOpts{
424 "foo": "bar",
425 "this": "that",
426 }).Extract()
427 th.AssertNoErr(t, err)
428 t.Logf("UpdateMetadatas result: %+v\n", metadata)
429
430 err = servers.DeleteMetadata(client, server.ID, "foo").ExtractErr()
431 th.AssertNoErr(t, err)
432
433 metadata, err = servers.CreateMetadata(client, server.ID, servers.MetadataOpts{
434 "foo": "baz",
435 }).Extract()
436 th.AssertNoErr(t, err)
437 t.Logf("CreateMetadata result: %+v\n", metadata)
438
439 metadata, err = servers.Metadata(client, server.ID, "foo").Extract()
440 th.AssertNoErr(t, err)
441 t.Logf("Metadata result: %+v\n", metadata)
442 th.AssertEquals(t, "baz", metadata["foo"])
443
444 metadata, err = servers.Metadatas(client, server.ID).Extract()
445 th.AssertNoErr(t, err)
446 t.Logf("Metadatas result: %+v\n", metadata)
447
448 metadata, err = servers.CreateMetadatas(client, server.ID, servers.MetadatasOpts{}).Extract()
449 th.AssertNoErr(t, err)
450 t.Logf("CreateMetadatas result: %+v\n", metadata)
451 th.AssertDeepEquals(t, map[string]string{}, metadata)
452}