blob: fa6603da523bfbc0960b9df213594e6e75cc8e2b [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 (
Joe Topjian50cdddf2016-09-16 10:56:09 -06006 "strings"
Ash Wilsonfd566482014-09-23 15:47:35 -04007 "testing"
8
Joe Topjian50cdddf2016-09-16 10:56:09 -06009 "github.com/gophercloud/gophercloud"
Joe Topjian1c15e3f2016-08-08 10:48:38 -060010 "github.com/gophercloud/gophercloud/acceptance/clients"
Jon Perritt27249f42016-02-18 10:35:59 -060011 "github.com/gophercloud/gophercloud/acceptance/tools"
Jon Perritt27249f42016-02-18 10:35:59 -060012 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt27249f42016-02-18 10:35:59 -060013 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonfd566482014-09-23 15:47:35 -040014)
15
Joe Topjian34368bd2016-07-24 06:00:07 +000016func TestServersList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060017 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -040018 if err != nil {
19 t.Fatalf("Unable to create a compute client: %v", err)
20 }
21
Joe Topjian34368bd2016-07-24 06:00:07 +000022 allPages, err := servers.List(client, servers.ListOpts{}).AllPages()
23 if err != nil {
24 t.Fatalf("Unable to retrieve servers: %v", err)
25 }
Ash Wilsonfd566482014-09-23 15:47:35 -040026
Joe Topjian34368bd2016-07-24 06:00:07 +000027 allServers, err := servers.ExtractServers(allPages)
28 if err != nil {
29 t.Fatalf("Unable to extract servers: %v", err)
30 }
Ash Wilsonfd566482014-09-23 15:47:35 -040031
Joe Topjian34368bd2016-07-24 06:00:07 +000032 for _, server := range allServers {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060033 PrintServer(t, &server)
Joe Topjian34368bd2016-07-24 06:00:07 +000034 }
Ash Wilsonfd566482014-09-23 15:47:35 -040035}
36
Joe Topjian34368bd2016-07-24 06:00:07 +000037func TestServersCreateDestroy(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060038 client, err := clients.NewComputeV2Client()
Jon Perritt2a7797d2014-10-21 15:08:43 -050039 if err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +000040 t.Fatalf("Unable to create a compute client: %v", err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050041 }
42
Joe Topjian1c15e3f2016-08-08 10:48:38 -060043 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -040044 if err != nil {
45 t.Fatal(err)
46 }
47
Joe Topjian1c15e3f2016-08-08 10:48:38 -060048 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -040049 if err != nil {
50 t.Fatalf("Unable to create server: %v", err)
51 }
Ash Wilsonfd566482014-09-23 15:47:35 -040052
Joe Topjian1c15e3f2016-08-08 10:48:38 -060053 defer DeleteServer(t, client, server)
Jon Perritt38cc0522015-02-19 22:16:40 -070054
Joe Topjian34368bd2016-07-24 06:00:07 +000055 newServer, err := servers.Get(client, server.ID).Extract()
56 if err != nil {
57 t.Errorf("Unable to retrieve server: %v", err)
58 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060059 PrintServer(t, newServer)
Jon Perritt38cc0522015-02-19 22:16:40 -070060
Joe Topjian34368bd2016-07-24 06:00:07 +000061 allAddressPages, err := servers.ListAddresses(client, server.ID).AllPages()
62 if err != nil {
63 t.Errorf("Unable to list server addresses: %v", err)
64 }
Jon Perritt38cc0522015-02-19 22:16:40 -070065
Joe Topjian34368bd2016-07-24 06:00:07 +000066 allAddresses, err := servers.ExtractAddresses(allAddressPages)
67 if err != nil {
68 t.Errorf("Unable to extract server addresses: %v", err)
69 }
Jon Perritt38cc0522015-02-19 22:16:40 -070070
Joe Topjian34368bd2016-07-24 06:00:07 +000071 for network, address := range allAddresses {
72 t.Logf("Addresses on %s: %+v", network, address)
73 }
74
75 allNetworkAddressPages, err := servers.ListAddressesByNetwork(client, server.ID, choices.NetworkName).AllPages()
76 if err != nil {
77 t.Errorf("Unable to list server addresses: %v", err)
78 }
79
80 allNetworkAddresses, err := servers.ExtractNetworkAddresses(allNetworkAddressPages)
81 if err != nil {
82 t.Errorf("Unable to extract server addresses: %v", err)
83 }
84
85 t.Logf("Addresses on %s:", choices.NetworkName)
86 for _, address := range allNetworkAddresses {
87 t.Logf("%+v", address)
88 }
Ash Wilsonfd566482014-09-23 15:47:35 -040089}
90
Joe Topjian50cdddf2016-09-16 10:56:09 -060091func TestServersWithoutImageRef(t *testing.T) {
92 client, err := clients.NewComputeV2Client()
93 if err != nil {
94 t.Fatalf("Unable to create a compute client: %v", err)
95 }
96
97 choices, err := clients.AcceptanceTestChoicesFromEnv()
98 if err != nil {
99 t.Fatal(err)
100 }
101
102 server, err := CreateServerWithoutImageRef(t, client, choices)
103 if err != nil {
104 if err400, ok := err.(*gophercloud.ErrUnexpectedResponseCode); ok {
105 if !strings.Contains("Missing imageRef attribute", string(err400.Body)) {
106 defer DeleteServer(t, client, server)
107 }
108 }
109 }
110}
111
Joe Topjian34368bd2016-07-24 06:00:07 +0000112func TestServersUpdate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600113 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400114 if err != nil {
115 t.Fatalf("Unable to create a compute client: %v", err)
116 }
117
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600118 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400119 if err != nil {
120 t.Fatal(err)
121 }
122
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600123 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400124 if err != nil {
125 t.Fatal(err)
126 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600127 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400128
129 alternateName := tools.RandomString("ACPTTEST", 16)
130 for alternateName == server.Name {
131 alternateName = tools.RandomString("ACPTTEST", 16)
132 }
133
134 t.Logf("Attempting to rename the server to %s.", alternateName)
135
Joe Topjian34368bd2016-07-24 06:00:07 +0000136 updateOpts := servers.UpdateOpts{
137 Name: alternateName,
138 }
139
140 updated, err := servers.Update(client, server.ID, updateOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400141 if err != nil {
142 t.Fatalf("Unable to rename server: %v", err)
143 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400144
145 if updated.ID != server.ID {
146 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
147 }
148
149 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400150 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400151 if err != nil {
152 return false, err
153 }
154
155 return latest.Name == alternateName, nil
156 })
157}
158
Joe Topjian34368bd2016-07-24 06:00:07 +0000159func TestServersMetadata(t *testing.T) {
160 t.Parallel()
161
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600162 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian34368bd2016-07-24 06:00:07 +0000163 if err != nil {
164 t.Fatal(err)
165 }
166
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600167 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000168 if err != nil {
169 t.Fatalf("Unable to create a compute client: %v", err)
170 }
171
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600172 server, err := CreateServer(t, client, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000173 if err != nil {
174 t.Fatal(err)
175 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600176 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000177
178 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
179 "foo": "bar",
180 "this": "that",
181 }).Extract()
182 if err != nil {
183 t.Fatalf("Unable to update metadata: %v", err)
184 }
185 t.Logf("UpdateMetadata result: %+v\n", metadata)
186
187 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
188 if err != nil {
189 t.Fatalf("Unable to delete metadatum: %v", err)
190 }
191
192 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
193 "foo": "baz",
194 }).Extract()
195 if err != nil {
196 t.Fatalf("Unable to create metadatum: %v", err)
197 }
198 t.Logf("CreateMetadatum result: %+v\n", metadata)
199
200 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
201 if err != nil {
202 t.Fatalf("Unable to get metadatum: %v", err)
203 }
204 t.Logf("Metadatum result: %+v\n", metadata)
205 th.AssertEquals(t, "baz", metadata["foo"])
206
207 metadata, err = servers.Metadata(client, server.ID).Extract()
208 if err != nil {
209 t.Fatalf("Unable to get metadata: %v", err)
210 }
211 t.Logf("Metadata result: %+v\n", metadata)
212
213 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
214 if err != nil {
215 t.Fatalf("Unable to reset metadata: %v", err)
216 }
217 t.Logf("ResetMetadata result: %+v\n", metadata)
218 th.AssertDeepEquals(t, map[string]string{}, metadata)
219}
220
221func TestServersActionChangeAdminPassword(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400222 t.Parallel()
223
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600224 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400225 if err != nil {
226 t.Fatalf("Unable to create a compute client: %v", err)
227 }
228
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600229 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400230 if err != nil {
231 t.Fatal(err)
232 }
233
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600234 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400235 if err != nil {
236 t.Fatal(err)
237 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600238 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400239
240 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500241 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200242 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000243 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400244 }
245
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600246 if err = WaitForComputeStatus(client, server, "PASSWORD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400247 t.Fatal(err)
248 }
249
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600250 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400251 t.Fatal(err)
252 }
253}
254
Joe Topjian34368bd2016-07-24 06:00:07 +0000255func TestServersActionReboot(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400256 t.Parallel()
257
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600258 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400259 if err != nil {
260 t.Fatalf("Unable to create a compute client: %v", err)
261 }
262
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600263 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400264 if err != nil {
265 t.Fatal(err)
266 }
267
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600268 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400269 if err != nil {
270 t.Fatal(err)
271 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600272 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400273
Joe Topjian34368bd2016-07-24 06:00:07 +0000274 rebootOpts := &servers.RebootOpts{
275 Type: servers.SoftReboot,
Ash Wilsonfd566482014-09-23 15:47:35 -0400276 }
277
278 t.Logf("Attempting reboot of server %s", server.ID)
Joe Topjian34368bd2016-07-24 06:00:07 +0000279 res := servers.Reboot(client, server.ID, rebootOpts)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200280 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000281 t.Fatalf("Unable to reboot server: %v", res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400282 }
283
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600284 if err = WaitForComputeStatus(client, server, "REBOOT"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400285 t.Fatal(err)
286 }
287
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600288 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400289 t.Fatal(err)
290 }
291}
292
Joe Topjian34368bd2016-07-24 06:00:07 +0000293func TestServersActionRebuild(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400294 t.Parallel()
295
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600296 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400297 if err != nil {
298 t.Fatalf("Unable to create a compute client: %v", err)
299 }
300
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600301 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400302 if err != nil {
303 t.Fatal(err)
304 }
305
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600306 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400307 if err != nil {
308 t.Fatal(err)
309 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600310 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
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600329 if err = WaitForComputeStatus(client, rebuilt, "REBUILD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400330 t.Fatal(err)
331 }
332
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600333 if err = WaitForComputeStatus(client, rebuilt, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400334 t.Fatal(err)
335 }
336}
337
Joe Topjian34368bd2016-07-24 06:00:07 +0000338func TestServersActionResizeConfirm(t *testing.T) {
339 t.Parallel()
340
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600341 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian34368bd2016-07-24 06:00:07 +0000342 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400343 t.Fatal(err)
344 }
345
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600346 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000347 if err != nil {
348 t.Fatalf("Unable to create a compute client: %v", err)
349 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400350
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600351 server, err := CreateServer(t, client, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000352 if err != nil {
353 t.Fatal(err)
354 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600355 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000356
357 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600358 ResizeServer(t, client, server, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000359
360 t.Logf("Attempting to confirm resize for server %s", server.ID)
361 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
362 t.Fatal(res.Err)
363 }
364
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600365 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000366 t.Fatal(err)
367 }
368}
369
370func TestServersActionResizeRevert(t *testing.T) {
371 t.Parallel()
372
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600373 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian34368bd2016-07-24 06:00:07 +0000374 if err != nil {
375 t.Fatal(err)
376 }
377
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600378 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000379 if err != nil {
380 t.Fatalf("Unable to create a compute client: %v", err)
381 }
382
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600383 server, err := CreateServer(t, client, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000384 if err != nil {
385 t.Fatal(err)
386 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600387 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000388
389 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600390 ResizeServer(t, client, server, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000391
392 t.Logf("Attempting to revert resize for server %s", server.ID)
393 if res := servers.RevertResize(client, server.ID); res.Err != nil {
394 t.Fatal(res.Err)
395 }
396
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600397 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000398 t.Fatal(err)
399 }
400}