blob: f43c94da1d034a84506b984ac5ab4b3d11f8573b [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"
Joe Topjian0f64da02017-03-09 18:59:53 -070012 "github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones"
Jon Perritt27249f42016-02-18 10:35:59 -060013 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt27249f42016-02-18 10:35:59 -060014 th "github.com/gophercloud/gophercloud/testhelper"
Ash Wilsonfd566482014-09-23 15:47:35 -040015)
16
Joe Topjian34368bd2016-07-24 06:00:07 +000017func TestServersList(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060018 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -040019 if err != nil {
20 t.Fatalf("Unable to create a compute client: %v", err)
21 }
22
Joe Topjian34368bd2016-07-24 06:00:07 +000023 allPages, err := servers.List(client, servers.ListOpts{}).AllPages()
24 if err != nil {
25 t.Fatalf("Unable to retrieve servers: %v", err)
26 }
Ash Wilsonfd566482014-09-23 15:47:35 -040027
Joe Topjian34368bd2016-07-24 06:00:07 +000028 allServers, err := servers.ExtractServers(allPages)
29 if err != nil {
30 t.Fatalf("Unable to extract servers: %v", err)
31 }
Ash Wilsonfd566482014-09-23 15:47:35 -040032
Joe Topjian34368bd2016-07-24 06:00:07 +000033 for _, server := range allServers {
Joe Topjian66a046c2017-01-19 22:07:26 -070034 tools.PrintResource(t, server)
Joe Topjian34368bd2016-07-24 06:00:07 +000035 }
Ash Wilsonfd566482014-09-23 15:47:35 -040036}
37
Joe Topjian34368bd2016-07-24 06:00:07 +000038func TestServersCreateDestroy(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060039 client, err := clients.NewComputeV2Client()
Jon Perritt2a7797d2014-10-21 15:08:43 -050040 if err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +000041 t.Fatalf("Unable to create a compute client: %v", err)
Jon Perritt2a7797d2014-10-21 15:08:43 -050042 }
43
Joe Topjian1c15e3f2016-08-08 10:48:38 -060044 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -040045 if err != nil {
46 t.Fatal(err)
47 }
48
Joe Topjian66a046c2017-01-19 22:07:26 -070049 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -040050 if err != nil {
51 t.Fatalf("Unable to create server: %v", err)
52 }
Ash Wilsonfd566482014-09-23 15:47:35 -040053
Joe Topjian1c15e3f2016-08-08 10:48:38 -060054 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 }
Joe Topjian66a046c2017-01-19 22:07:26 -070060 tools.PrintResource(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 Topjian0f64da02017-03-09 18:59:53 -070092func TestServersCreateDestroyWithExtensions(t *testing.T) {
93 var extendedServer struct {
94 servers.Server
95 availabilityzones.ServerExt
96 }
97
98 client, err := clients.NewComputeV2Client()
99 if err != nil {
100 t.Fatalf("Unable to create a compute client: %v", err)
101 }
102
103 server, err := CreateServer(t, client)
104 if err != nil {
105 t.Fatalf("Unable to create server: %v", err)
106 }
107 defer DeleteServer(t, client, server)
108
109 err = servers.Get(client, server.ID).ExtractInto(&extendedServer)
110 if err != nil {
111 t.Errorf("Unable to retrieve server: %v", err)
112 }
113 tools.PrintResource(t, extendedServer)
114}
115
Joe Topjian50cdddf2016-09-16 10:56:09 -0600116func TestServersWithoutImageRef(t *testing.T) {
117 client, err := clients.NewComputeV2Client()
118 if err != nil {
119 t.Fatalf("Unable to create a compute client: %v", err)
120 }
121
Joe Topjian66a046c2017-01-19 22:07:26 -0700122 server, err := CreateServerWithoutImageRef(t, client)
Joe Topjian50cdddf2016-09-16 10:56:09 -0600123 if err != nil {
124 if err400, ok := err.(*gophercloud.ErrUnexpectedResponseCode); ok {
125 if !strings.Contains("Missing imageRef attribute", string(err400.Body)) {
126 defer DeleteServer(t, client, server)
127 }
128 }
129 }
130}
131
Joe Topjian34368bd2016-07-24 06:00:07 +0000132func TestServersUpdate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600133 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400134 if err != nil {
135 t.Fatalf("Unable to create a compute client: %v", err)
136 }
137
Joe Topjian66a046c2017-01-19 22:07:26 -0700138 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -0400139 if err != nil {
140 t.Fatal(err)
141 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600142 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400143
144 alternateName := tools.RandomString("ACPTTEST", 16)
145 for alternateName == server.Name {
146 alternateName = tools.RandomString("ACPTTEST", 16)
147 }
148
149 t.Logf("Attempting to rename the server to %s.", alternateName)
150
Joe Topjian34368bd2016-07-24 06:00:07 +0000151 updateOpts := servers.UpdateOpts{
152 Name: alternateName,
153 }
154
155 updated, err := servers.Update(client, server.ID, updateOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400156 if err != nil {
157 t.Fatalf("Unable to rename server: %v", err)
158 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400159
160 if updated.ID != server.ID {
161 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
162 }
163
164 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400165 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400166 if err != nil {
167 return false, err
168 }
169
170 return latest.Name == alternateName, nil
171 })
172}
173
Joe Topjian34368bd2016-07-24 06:00:07 +0000174func TestServersMetadata(t *testing.T) {
175 t.Parallel()
176
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600177 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000178 if err != nil {
179 t.Fatalf("Unable to create a compute client: %v", err)
180 }
181
Joe Topjian66a046c2017-01-19 22:07:26 -0700182 server, err := CreateServer(t, client)
Joe Topjian34368bd2016-07-24 06:00:07 +0000183 if err != nil {
184 t.Fatal(err)
185 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600186 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000187
188 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
189 "foo": "bar",
190 "this": "that",
191 }).Extract()
192 if err != nil {
193 t.Fatalf("Unable to update metadata: %v", err)
194 }
195 t.Logf("UpdateMetadata result: %+v\n", metadata)
196
197 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
198 if err != nil {
199 t.Fatalf("Unable to delete metadatum: %v", err)
200 }
201
202 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
203 "foo": "baz",
204 }).Extract()
205 if err != nil {
206 t.Fatalf("Unable to create metadatum: %v", err)
207 }
208 t.Logf("CreateMetadatum result: %+v\n", metadata)
209
210 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
211 if err != nil {
212 t.Fatalf("Unable to get metadatum: %v", err)
213 }
214 t.Logf("Metadatum result: %+v\n", metadata)
215 th.AssertEquals(t, "baz", metadata["foo"])
216
217 metadata, err = servers.Metadata(client, server.ID).Extract()
218 if err != nil {
219 t.Fatalf("Unable to get metadata: %v", err)
220 }
221 t.Logf("Metadata result: %+v\n", metadata)
222
223 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
224 if err != nil {
225 t.Fatalf("Unable to reset metadata: %v", err)
226 }
227 t.Logf("ResetMetadata result: %+v\n", metadata)
228 th.AssertDeepEquals(t, map[string]string{}, metadata)
229}
230
231func TestServersActionChangeAdminPassword(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400232 t.Parallel()
233
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600234 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400235 if err != nil {
236 t.Fatalf("Unable to create a compute client: %v", err)
237 }
238
Joe Topjian66a046c2017-01-19 22:07:26 -0700239 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -0400240 if err != nil {
241 t.Fatal(err)
242 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600243 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400244
245 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500246 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200247 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000248 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400249 }
250
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600251 if err = WaitForComputeStatus(client, server, "PASSWORD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400252 t.Fatal(err)
253 }
254
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600255 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400256 t.Fatal(err)
257 }
258}
259
Joe Topjian34368bd2016-07-24 06:00:07 +0000260func TestServersActionReboot(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400261 t.Parallel()
262
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600263 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400264 if err != nil {
265 t.Fatalf("Unable to create a compute client: %v", err)
266 }
267
Joe Topjian66a046c2017-01-19 22:07:26 -0700268 server, err := CreateServer(t, client)
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 Topjian66a046c2017-01-19 22:07:26 -0700306 server, err := CreateServer(t, client)
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 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000342 if err != nil {
343 t.Fatalf("Unable to create a compute client: %v", err)
344 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400345
Joe Topjian66a046c2017-01-19 22:07:26 -0700346 server, err := CreateServer(t, client)
Joe Topjian34368bd2016-07-24 06:00:07 +0000347 if err != nil {
348 t.Fatal(err)
349 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600350 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000351
352 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian66a046c2017-01-19 22:07:26 -0700353 ResizeServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000354
355 t.Logf("Attempting to confirm resize for server %s", server.ID)
356 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
357 t.Fatal(res.Err)
358 }
359
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600360 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000361 t.Fatal(err)
362 }
363}
364
365func TestServersActionResizeRevert(t *testing.T) {
366 t.Parallel()
367
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600368 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000369 if err != nil {
370 t.Fatalf("Unable to create a compute client: %v", err)
371 }
372
Joe Topjian66a046c2017-01-19 22:07:26 -0700373 server, err := CreateServer(t, client)
Joe Topjian34368bd2016-07-24 06:00:07 +0000374 if err != nil {
375 t.Fatal(err)
376 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600377 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000378
379 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian66a046c2017-01-19 22:07:26 -0700380 ResizeServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000381
382 t.Logf("Attempting to revert resize for server %s", server.ID)
383 if res := servers.RevertResize(client, server.ID); res.Err != nil {
384 t.Fatal(res.Err)
385 }
386
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600387 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000388 t.Fatal(err)
389 }
390}