blob: 34a3fd28e2df5d417709cfc3d9dde9a6efe035b7 [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 (
Ash Wilsonfd566482014-09-23 15:47:35 -04006 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 "github.com/gophercloud/gophercloud"
9 "github.com/gophercloud/gophercloud/acceptance/tools"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt27249f42016-02-18 10:35:59 -060011 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonfd566482014-09-23 15:47:35 -040012)
13
Joe Topjian34368bd2016-07-24 06:00:07 +000014func TestServersList(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -040015 client, err := newClient()
16 if err != nil {
17 t.Fatalf("Unable to create a compute client: %v", err)
18 }
19
Joe Topjian34368bd2016-07-24 06:00:07 +000020 allPages, err := servers.List(client, servers.ListOpts{}).AllPages()
21 if err != nil {
22 t.Fatalf("Unable to retrieve servers: %v", err)
23 }
Ash Wilsonfd566482014-09-23 15:47:35 -040024
Joe Topjian34368bd2016-07-24 06:00:07 +000025 allServers, err := servers.ExtractServers(allPages)
26 if err != nil {
27 t.Fatalf("Unable to extract servers: %v", err)
28 }
Ash Wilsonfd566482014-09-23 15:47:35 -040029
Joe Topjian34368bd2016-07-24 06:00:07 +000030 for _, server := range allServers {
31 printServer(t, &server)
32 }
Ash Wilsonfd566482014-09-23 15:47:35 -040033}
34
Joe Topjian34368bd2016-07-24 06:00:07 +000035func TestServersCreateDestroy(t *testing.T) {
36 client, err := newClient()
Jon Perritt2a7797d2014-10-21 15:08:43 -050037 if err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +000038 t.Fatalf("Unable to create a compute client: %v", err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050039 }
40
Ash Wilsonfd566482014-09-23 15:47:35 -040041 choices, err := ComputeChoicesFromEnv()
42 if err != nil {
43 t.Fatal(err)
44 }
45
Ash Wilsonfd566482014-09-23 15:47:35 -040046 server, err := createServer(t, client, choices)
47 if err != nil {
48 t.Fatalf("Unable to create server: %v", err)
49 }
Ash Wilsonfd566482014-09-23 15:47:35 -040050
51 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
52 t.Fatalf("Unable to wait for server: %v", err)
53 }
Joe Topjian34368bd2016-07-24 06:00:07 +000054 defer deleteServer(t, client, server)
Jon Perritt38cc0522015-02-19 22:16:40 -070055
Joe Topjian34368bd2016-07-24 06:00:07 +000056 newServer, err := servers.Get(client, server.ID).Extract()
57 if err != nil {
58 t.Errorf("Unable to retrieve server: %v", err)
59 }
60 printServer(t, newServer)
Jon Perritt38cc0522015-02-19 22:16:40 -070061
Joe Topjian34368bd2016-07-24 06:00:07 +000062 allAddressPages, err := servers.ListAddresses(client, server.ID).AllPages()
63 if err != nil {
64 t.Errorf("Unable to list server addresses: %v", err)
65 }
Jon Perritt38cc0522015-02-19 22:16:40 -070066
Joe Topjian34368bd2016-07-24 06:00:07 +000067 allAddresses, err := servers.ExtractAddresses(allAddressPages)
68 if err != nil {
69 t.Errorf("Unable to extract server addresses: %v", err)
70 }
Jon Perritt38cc0522015-02-19 22:16:40 -070071
Joe Topjian34368bd2016-07-24 06:00:07 +000072 for network, address := range allAddresses {
73 t.Logf("Addresses on %s: %+v", network, address)
74 }
75
76 allNetworkAddressPages, err := servers.ListAddressesByNetwork(client, server.ID, choices.NetworkName).AllPages()
77 if err != nil {
78 t.Errorf("Unable to list server addresses: %v", err)
79 }
80
81 allNetworkAddresses, err := servers.ExtractNetworkAddresses(allNetworkAddressPages)
82 if err != nil {
83 t.Errorf("Unable to extract server addresses: %v", err)
84 }
85
86 t.Logf("Addresses on %s:", choices.NetworkName)
87 for _, address := range allNetworkAddresses {
88 t.Logf("%+v", address)
89 }
Ash Wilsonfd566482014-09-23 15:47:35 -040090}
91
Joe Topjian34368bd2016-07-24 06:00:07 +000092func TestServersUpdate(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -040093 client, err := newClient()
94 if err != nil {
95 t.Fatalf("Unable to create a compute client: %v", err)
96 }
97
98 choices, err := ComputeChoicesFromEnv()
99 if err != nil {
100 t.Fatal(err)
101 }
102
103 server, err := createServer(t, client, choices)
104 if err != nil {
105 t.Fatal(err)
106 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400107
108 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
109 t.Fatal(err)
110 }
Joe Topjian34368bd2016-07-24 06:00:07 +0000111 defer deleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400112
113 alternateName := tools.RandomString("ACPTTEST", 16)
114 for alternateName == server.Name {
115 alternateName = tools.RandomString("ACPTTEST", 16)
116 }
117
118 t.Logf("Attempting to rename the server to %s.", alternateName)
119
Joe Topjian34368bd2016-07-24 06:00:07 +0000120 updateOpts := servers.UpdateOpts{
121 Name: alternateName,
122 }
123
124 updated, err := servers.Update(client, server.ID, updateOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400125 if err != nil {
126 t.Fatalf("Unable to rename server: %v", err)
127 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400128
129 if updated.ID != server.ID {
130 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
131 }
132
133 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400134 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400135 if err != nil {
136 return false, err
137 }
138
139 return latest.Name == alternateName, nil
140 })
141}
142
Joe Topjian34368bd2016-07-24 06:00:07 +0000143func TestServersMetadata(t *testing.T) {
144 t.Parallel()
145
146 choices, err := ComputeChoicesFromEnv()
147 if err != nil {
148 t.Fatal(err)
149 }
150
151 client, err := newClient()
152 if err != nil {
153 t.Fatalf("Unable to create a compute client: %v", err)
154 }
155
156 server, err := createServer(t, client, choices)
157 if err != nil {
158 t.Fatal(err)
159 }
160
161 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
162 t.Fatal(err)
163 }
164 defer deleteServer(t, client, server)
165
166 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
167 "foo": "bar",
168 "this": "that",
169 }).Extract()
170 if err != nil {
171 t.Fatalf("Unable to update metadata: %v", err)
172 }
173 t.Logf("UpdateMetadata result: %+v\n", metadata)
174
175 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
176 if err != nil {
177 t.Fatalf("Unable to delete metadatum: %v", err)
178 }
179
180 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
181 "foo": "baz",
182 }).Extract()
183 if err != nil {
184 t.Fatalf("Unable to create metadatum: %v", err)
185 }
186 t.Logf("CreateMetadatum result: %+v\n", metadata)
187
188 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
189 if err != nil {
190 t.Fatalf("Unable to get metadatum: %v", err)
191 }
192 t.Logf("Metadatum result: %+v\n", metadata)
193 th.AssertEquals(t, "baz", metadata["foo"])
194
195 metadata, err = servers.Metadata(client, server.ID).Extract()
196 if err != nil {
197 t.Fatalf("Unable to get metadata: %v", err)
198 }
199 t.Logf("Metadata result: %+v\n", metadata)
200
201 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
202 if err != nil {
203 t.Fatalf("Unable to reset metadata: %v", err)
204 }
205 t.Logf("ResetMetadata result: %+v\n", metadata)
206 th.AssertDeepEquals(t, map[string]string{}, metadata)
207}
208
209func TestServersActionChangeAdminPassword(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400210 t.Parallel()
211
212 client, err := newClient()
213 if err != nil {
214 t.Fatalf("Unable to create a compute client: %v", err)
215 }
216
217 choices, err := ComputeChoicesFromEnv()
218 if err != nil {
219 t.Fatal(err)
220 }
221
222 server, err := createServer(t, client, choices)
223 if err != nil {
224 t.Fatal(err)
225 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400226
227 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
228 t.Fatal(err)
229 }
Joe Topjian34368bd2016-07-24 06:00:07 +0000230 defer deleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400231
232 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500233 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200234 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000235 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400236 }
237
238 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
239 t.Fatal(err)
240 }
241
242 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
243 t.Fatal(err)
244 }
245}
246
Joe Topjian34368bd2016-07-24 06:00:07 +0000247func TestServersActionReboot(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400248 t.Parallel()
249
250 client, err := newClient()
251 if err != nil {
252 t.Fatalf("Unable to create a compute client: %v", err)
253 }
254
255 choices, err := ComputeChoicesFromEnv()
256 if err != nil {
257 t.Fatal(err)
258 }
259
260 server, err := createServer(t, client, choices)
261 if err != nil {
262 t.Fatal(err)
263 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400264
265 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
266 t.Fatal(err)
267 }
Joe Topjian34368bd2016-07-24 06:00:07 +0000268 defer deleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400269
Joe Topjian34368bd2016-07-24 06:00:07 +0000270 rebootOpts := &servers.RebootOpts{
271 Type: servers.SoftReboot,
Ash Wilsonfd566482014-09-23 15:47:35 -0400272 }
273
274 t.Logf("Attempting reboot of server %s", server.ID)
Joe Topjian34368bd2016-07-24 06:00:07 +0000275 res := servers.Reboot(client, server.ID, rebootOpts)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200276 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000277 t.Fatalf("Unable to reboot server: %v", res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400278 }
279
280 if err = waitForStatus(client, server, "REBOOT"); err != nil {
281 t.Fatal(err)
282 }
283
284 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
285 t.Fatal(err)
286 }
287}
288
Joe Topjian34368bd2016-07-24 06:00:07 +0000289func TestServersActionRebuild(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400290 t.Parallel()
291
292 client, err := newClient()
293 if err != nil {
294 t.Fatalf("Unable to create a compute client: %v", err)
295 }
296
297 choices, err := ComputeChoicesFromEnv()
298 if err != nil {
299 t.Fatal(err)
300 }
301
302 server, err := createServer(t, client, choices)
303 if err != nil {
304 t.Fatal(err)
305 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400306
307 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
308 t.Fatal(err)
309 }
Joe Topjian34368bd2016-07-24 06:00:07 +0000310 defer deleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400311
312 t.Logf("Attempting to rebuild server %s", server.ID)
313
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200314 rebuildOpts := servers.RebuildOpts{
315 Name: tools.RandomString("ACPTTEST", 16),
316 AdminPass: tools.MakeNewPassword(server.AdminPass),
317 ImageID: choices.ImageID,
318 }
319
320 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400321 if err != nil {
322 t.Fatal(err)
323 }
324
Ash Wilsonfd566482014-09-23 15:47:35 -0400325 if rebuilt.ID != server.ID {
326 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
327 }
328
329 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
330 t.Fatal(err)
331 }
332
333 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
334 t.Fatal(err)
335 }
336}
337
Joe Topjian34368bd2016-07-24 06:00:07 +0000338func TestServersActionResizeConfirm(t *testing.T) {
339 t.Parallel()
340
341 choices, err := ComputeChoicesFromEnv()
342 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400343 t.Fatal(err)
344 }
345
Joe Topjian34368bd2016-07-24 06:00:07 +0000346 client, err := newClient()
347 if err != nil {
348 t.Fatalf("Unable to create a compute client: %v", err)
349 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400350
Joe Topjian34368bd2016-07-24 06:00:07 +0000351 server, err := createServer(t, client, choices)
352 if err != nil {
353 t.Fatal(err)
354 }
355
356 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
357 t.Fatal(err)
358 }
359 defer deleteServer(t, client, server)
360
361 t.Logf("Attempting to resize server %s", server.ID)
362 resizeServer(t, client, server, choices)
363
364 t.Logf("Attempting to confirm resize for server %s", server.ID)
365 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
366 t.Fatal(res.Err)
367 }
368
369 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
370 t.Fatal(err)
371 }
372}
373
374func TestServersActionResizeRevert(t *testing.T) {
375 t.Parallel()
376
377 choices, err := ComputeChoicesFromEnv()
378 if err != nil {
379 t.Fatal(err)
380 }
381
382 client, err := newClient()
383 if err != nil {
384 t.Fatalf("Unable to create a compute client: %v", err)
385 }
386
387 server, err := createServer(t, client, choices)
388 if err != nil {
389 t.Fatal(err)
390 }
391
392 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
393 t.Fatal(err)
394 }
395 defer deleteServer(t, client, server)
396
397 t.Logf("Attempting to resize server %s", server.ID)
398 resizeServer(t, client, server, choices)
399
400 t.Logf("Attempting to revert resize for server %s", server.ID)
401 if res := servers.RevertResize(client, server.ID); res.Err != nil {
402 t.Fatal(res.Err)
403 }
404
405 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
406 t.Fatal(err)
407 }
408}
409
410func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
411 if testing.Short() {
412 t.Skip("Skipping test that requires server creation in short mode.")
413 }
414
415 networkID, err := getNetworkIDFromTenantNetworks(t, client, choices.NetworkName)
416 if err != nil {
417 t.Fatalf("Failed to obtain network ID: %v", err)
418 }
419
420 name := tools.RandomString("ACPTTEST", 16)
421 t.Logf("Attempting to create server: %s", name)
422
423 pwd := tools.MakeNewPassword("")
424
425 server, err := servers.Create(client, servers.CreateOpts{
426 Name: name,
427 FlavorRef: choices.FlavorID,
428 ImageRef: choices.ImageID,
429 AdminPass: pwd,
430 Networks: []servers.Network{
431 servers.Network{UUID: networkID},
432 },
433 Personality: servers.Personality{
434 &servers.File{
435 Path: "/etc/test",
436 Contents: []byte("hello world"),
437 },
438 },
439 }).Extract()
440 if err != nil {
441 t.Fatalf("Unable to create server: %v", err)
442 }
443
444 th.AssertEquals(t, pwd, server.AdminPass)
445
446 return server, err
447}
448
449func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
Ash Wilson14c37412014-10-24 14:45:27 -0400450 opts := &servers.ResizeOpts{
451 FlavorRef: choices.FlavorIDResize,
452 }
453 if res := servers.Resize(client, server.ID, opts); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500454 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400455 }
456
Ash Wilsondc7daa82014-09-23 16:34:42 -0400457 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400458 t.Fatal(err)
459 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400460}
461
Joe Topjian34368bd2016-07-24 06:00:07 +0000462func deleteServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server) {
463 err := servers.Delete(client, server.ID).ExtractErr()
Ash Wilsondc7daa82014-09-23 16:34:42 -0400464 if err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000465 t.Fatalf("Unable to delete server %s: %s", server.ID, err)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400466 }
467
Joe Topjian34368bd2016-07-24 06:00:07 +0000468 t.Logf("Deleted server: %s", server.ID)
Ash Wilsonfd566482014-09-23 15:47:35 -0400469}
470
Joe Topjian34368bd2016-07-24 06:00:07 +0000471func printServer(t *testing.T, server *servers.Server) {
472 t.Logf("ID: %s", server.ID)
473 t.Logf("TenantID: %s", server.TenantID)
474 t.Logf("UserID: %s", server.UserID)
475 t.Logf("Name: %s", server.Name)
476 t.Logf("Updated: %s", server.Updated)
477 t.Logf("Created: %s", server.Created)
478 t.Logf("HostID: %s", server.HostID)
479 t.Logf("Status: %s", server.Status)
480 t.Logf("Progress: %d", server.Progress)
481 t.Logf("AccessIPv4: %s", server.AccessIPv4)
482 t.Logf("AccessIPv6: %s", server.AccessIPv6)
483 t.Logf("Image: %s", server.Image)
484 t.Logf("Flavor: %s", server.Flavor)
485 t.Logf("Addresses: %#v", server.Addresses)
486 t.Logf("Metadata: %#v", server.Metadata)
487 t.Logf("Links: %#v", server.Links)
488 t.Logf("KeyName: %s", server.KeyName)
489 t.Logf("AdminPass: %s", server.AdminPass)
490 t.Logf("SecurityGroups: %#v", server.SecurityGroups)
Jon Perrittcc77da62014-11-16 13:14:21 -0700491}