blob: f5b60c52ab9a3b891b037ef2ed321edfbe8f1b69 [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
Joe Topjian1c15e3f2016-08-08 10:48:38 -06008 "github.com/gophercloud/gophercloud/acceptance/clients"
Jon Perritt27249f42016-02-18 10:35:59 -06009 "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) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060015 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -040016 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 {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060031 PrintServer(t, &server)
Joe Topjian34368bd2016-07-24 06:00:07 +000032 }
Ash Wilsonfd566482014-09-23 15:47:35 -040033}
34
Joe Topjian34368bd2016-07-24 06:00:07 +000035func TestServersCreateDestroy(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060036 client, err := clients.NewComputeV2Client()
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
Joe Topjian1c15e3f2016-08-08 10:48:38 -060041 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -040042 if err != nil {
43 t.Fatal(err)
44 }
45
Joe Topjian1c15e3f2016-08-08 10:48:38 -060046 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -040047 if err != nil {
48 t.Fatalf("Unable to create server: %v", err)
49 }
Ash Wilsonfd566482014-09-23 15:47:35 -040050
Joe Topjian1c15e3f2016-08-08 10:48:38 -060051 defer DeleteServer(t, client, server)
Jon Perritt38cc0522015-02-19 22:16:40 -070052
Joe Topjian34368bd2016-07-24 06:00:07 +000053 newServer, err := servers.Get(client, server.ID).Extract()
54 if err != nil {
55 t.Errorf("Unable to retrieve server: %v", err)
56 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -060057 PrintServer(t, newServer)
Jon Perritt38cc0522015-02-19 22:16:40 -070058
Joe Topjian34368bd2016-07-24 06:00:07 +000059 allAddressPages, err := servers.ListAddresses(client, server.ID).AllPages()
60 if err != nil {
61 t.Errorf("Unable to list server addresses: %v", err)
62 }
Jon Perritt38cc0522015-02-19 22:16:40 -070063
Joe Topjian34368bd2016-07-24 06:00:07 +000064 allAddresses, err := servers.ExtractAddresses(allAddressPages)
65 if err != nil {
66 t.Errorf("Unable to extract server addresses: %v", err)
67 }
Jon Perritt38cc0522015-02-19 22:16:40 -070068
Joe Topjian34368bd2016-07-24 06:00:07 +000069 for network, address := range allAddresses {
70 t.Logf("Addresses on %s: %+v", network, address)
71 }
72
73 allNetworkAddressPages, err := servers.ListAddressesByNetwork(client, server.ID, choices.NetworkName).AllPages()
74 if err != nil {
75 t.Errorf("Unable to list server addresses: %v", err)
76 }
77
78 allNetworkAddresses, err := servers.ExtractNetworkAddresses(allNetworkAddressPages)
79 if err != nil {
80 t.Errorf("Unable to extract server addresses: %v", err)
81 }
82
83 t.Logf("Addresses on %s:", choices.NetworkName)
84 for _, address := range allNetworkAddresses {
85 t.Logf("%+v", address)
86 }
Ash Wilsonfd566482014-09-23 15:47:35 -040087}
88
Joe Topjian34368bd2016-07-24 06:00:07 +000089func TestServersUpdate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -060090 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -040091 if err != nil {
92 t.Fatalf("Unable to create a compute client: %v", err)
93 }
94
Joe Topjian1c15e3f2016-08-08 10:48:38 -060095 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -040096 if err != nil {
97 t.Fatal(err)
98 }
99
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600100 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400101 if err != nil {
102 t.Fatal(err)
103 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600104 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400105
106 alternateName := tools.RandomString("ACPTTEST", 16)
107 for alternateName == server.Name {
108 alternateName = tools.RandomString("ACPTTEST", 16)
109 }
110
111 t.Logf("Attempting to rename the server to %s.", alternateName)
112
Joe Topjian34368bd2016-07-24 06:00:07 +0000113 updateOpts := servers.UpdateOpts{
114 Name: alternateName,
115 }
116
117 updated, err := servers.Update(client, server.ID, updateOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400118 if err != nil {
119 t.Fatalf("Unable to rename server: %v", err)
120 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400121
122 if updated.ID != server.ID {
123 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
124 }
125
126 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400127 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400128 if err != nil {
129 return false, err
130 }
131
132 return latest.Name == alternateName, nil
133 })
134}
135
Joe Topjian34368bd2016-07-24 06:00:07 +0000136func TestServersMetadata(t *testing.T) {
137 t.Parallel()
138
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600139 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian34368bd2016-07-24 06:00:07 +0000140 if err != nil {
141 t.Fatal(err)
142 }
143
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600144 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000145 if err != nil {
146 t.Fatalf("Unable to create a compute client: %v", err)
147 }
148
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600149 server, err := CreateServer(t, client, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000150 if err != nil {
151 t.Fatal(err)
152 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600153 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000154
155 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
156 "foo": "bar",
157 "this": "that",
158 }).Extract()
159 if err != nil {
160 t.Fatalf("Unable to update metadata: %v", err)
161 }
162 t.Logf("UpdateMetadata result: %+v\n", metadata)
163
164 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
165 if err != nil {
166 t.Fatalf("Unable to delete metadatum: %v", err)
167 }
168
169 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
170 "foo": "baz",
171 }).Extract()
172 if err != nil {
173 t.Fatalf("Unable to create metadatum: %v", err)
174 }
175 t.Logf("CreateMetadatum result: %+v\n", metadata)
176
177 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
178 if err != nil {
179 t.Fatalf("Unable to get metadatum: %v", err)
180 }
181 t.Logf("Metadatum result: %+v\n", metadata)
182 th.AssertEquals(t, "baz", metadata["foo"])
183
184 metadata, err = servers.Metadata(client, server.ID).Extract()
185 if err != nil {
186 t.Fatalf("Unable to get metadata: %v", err)
187 }
188 t.Logf("Metadata result: %+v\n", metadata)
189
190 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
191 if err != nil {
192 t.Fatalf("Unable to reset metadata: %v", err)
193 }
194 t.Logf("ResetMetadata result: %+v\n", metadata)
195 th.AssertDeepEquals(t, map[string]string{}, metadata)
196}
197
198func TestServersActionChangeAdminPassword(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400199 t.Parallel()
200
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600201 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400202 if err != nil {
203 t.Fatalf("Unable to create a compute client: %v", err)
204 }
205
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600206 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400207 if err != nil {
208 t.Fatal(err)
209 }
210
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600211 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400212 if err != nil {
213 t.Fatal(err)
214 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600215 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400216
217 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500218 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200219 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000220 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400221 }
222
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600223 if err = WaitForComputeStatus(client, server, "PASSWORD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400224 t.Fatal(err)
225 }
226
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600227 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400228 t.Fatal(err)
229 }
230}
231
Joe Topjian34368bd2016-07-24 06:00:07 +0000232func TestServersActionReboot(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400233 t.Parallel()
234
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600235 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400236 if err != nil {
237 t.Fatalf("Unable to create a compute client: %v", err)
238 }
239
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600240 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400241 if err != nil {
242 t.Fatal(err)
243 }
244
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600245 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400246 if err != nil {
247 t.Fatal(err)
248 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600249 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400250
Joe Topjian34368bd2016-07-24 06:00:07 +0000251 rebootOpts := &servers.RebootOpts{
252 Type: servers.SoftReboot,
Ash Wilsonfd566482014-09-23 15:47:35 -0400253 }
254
255 t.Logf("Attempting reboot of server %s", server.ID)
Joe Topjian34368bd2016-07-24 06:00:07 +0000256 res := servers.Reboot(client, server.ID, rebootOpts)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200257 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000258 t.Fatalf("Unable to reboot server: %v", res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400259 }
260
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600261 if err = WaitForComputeStatus(client, server, "REBOOT"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400262 t.Fatal(err)
263 }
264
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600265 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400266 t.Fatal(err)
267 }
268}
269
Joe Topjian34368bd2016-07-24 06:00:07 +0000270func TestServersActionRebuild(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400271 t.Parallel()
272
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600273 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400274 if err != nil {
275 t.Fatalf("Unable to create a compute client: %v", err)
276 }
277
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600278 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400279 if err != nil {
280 t.Fatal(err)
281 }
282
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600283 server, err := CreateServer(t, client, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400284 if err != nil {
285 t.Fatal(err)
286 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600287 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400288
289 t.Logf("Attempting to rebuild server %s", server.ID)
290
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200291 rebuildOpts := servers.RebuildOpts{
292 Name: tools.RandomString("ACPTTEST", 16),
293 AdminPass: tools.MakeNewPassword(server.AdminPass),
294 ImageID: choices.ImageID,
295 }
296
297 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400298 if err != nil {
299 t.Fatal(err)
300 }
301
Ash Wilsonfd566482014-09-23 15:47:35 -0400302 if rebuilt.ID != server.ID {
303 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
304 }
305
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600306 if err = WaitForComputeStatus(client, rebuilt, "REBUILD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400307 t.Fatal(err)
308 }
309
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600310 if err = WaitForComputeStatus(client, rebuilt, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400311 t.Fatal(err)
312 }
313}
314
Joe Topjian34368bd2016-07-24 06:00:07 +0000315func TestServersActionResizeConfirm(t *testing.T) {
316 t.Parallel()
317
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600318 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian34368bd2016-07-24 06:00:07 +0000319 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400320 t.Fatal(err)
321 }
322
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600323 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000324 if err != nil {
325 t.Fatalf("Unable to create a compute client: %v", err)
326 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400327
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600328 server, err := CreateServer(t, client, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000329 if err != nil {
330 t.Fatal(err)
331 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600332 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000333
334 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600335 ResizeServer(t, client, server, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000336
337 t.Logf("Attempting to confirm resize for server %s", server.ID)
338 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
339 t.Fatal(res.Err)
340 }
341
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600342 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000343 t.Fatal(err)
344 }
345}
346
347func TestServersActionResizeRevert(t *testing.T) {
348 t.Parallel()
349
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600350 choices, err := clients.AcceptanceTestChoicesFromEnv()
Joe Topjian34368bd2016-07-24 06:00:07 +0000351 if err != nil {
352 t.Fatal(err)
353 }
354
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600355 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000356 if err != nil {
357 t.Fatalf("Unable to create a compute client: %v", err)
358 }
359
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600360 server, err := CreateServer(t, client, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000361 if err != nil {
362 t.Fatal(err)
363 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600364 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000365
366 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600367 ResizeServer(t, client, server, choices)
Joe Topjian34368bd2016-07-24 06:00:07 +0000368
369 t.Logf("Attempting to revert resize for server %s", server.ID)
370 if res := servers.RevertResize(client, server.ID); res.Err != nil {
371 t.Fatal(res.Err)
372 }
373
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600374 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000375 t.Fatal(err)
376 }
377}