blob: 22b6580403141ee04a4597c4e8387a57daf312bd [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 Topjian66a046c2017-01-19 22:07:26 -070033 tools.PrintResource(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 Topjian66a046c2017-01-19 22:07:26 -070048 server, err := CreateServer(t, client)
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 Topjian66a046c2017-01-19 22:07:26 -070059 tools.PrintResource(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
Joe Topjian66a046c2017-01-19 22:07:26 -070097 server, err := CreateServerWithoutImageRef(t, client)
Joe Topjian50cdddf2016-09-16 10:56:09 -060098 if err != nil {
99 if err400, ok := err.(*gophercloud.ErrUnexpectedResponseCode); ok {
100 if !strings.Contains("Missing imageRef attribute", string(err400.Body)) {
101 defer DeleteServer(t, client, server)
102 }
103 }
104 }
105}
106
Joe Topjian34368bd2016-07-24 06:00:07 +0000107func TestServersUpdate(t *testing.T) {
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600108 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400109 if err != nil {
110 t.Fatalf("Unable to create a compute client: %v", err)
111 }
112
Joe Topjian66a046c2017-01-19 22:07:26 -0700113 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -0400114 if err != nil {
115 t.Fatal(err)
116 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600117 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400118
119 alternateName := tools.RandomString("ACPTTEST", 16)
120 for alternateName == server.Name {
121 alternateName = tools.RandomString("ACPTTEST", 16)
122 }
123
124 t.Logf("Attempting to rename the server to %s.", alternateName)
125
Joe Topjian34368bd2016-07-24 06:00:07 +0000126 updateOpts := servers.UpdateOpts{
127 Name: alternateName,
128 }
129
130 updated, err := servers.Update(client, server.ID, updateOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400131 if err != nil {
132 t.Fatalf("Unable to rename server: %v", err)
133 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400134
135 if updated.ID != server.ID {
136 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
137 }
138
139 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400140 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400141 if err != nil {
142 return false, err
143 }
144
145 return latest.Name == alternateName, nil
146 })
147}
148
Joe Topjian34368bd2016-07-24 06:00:07 +0000149func TestServersMetadata(t *testing.T) {
150 t.Parallel()
151
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600152 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000153 if err != nil {
154 t.Fatalf("Unable to create a compute client: %v", err)
155 }
156
Joe Topjian66a046c2017-01-19 22:07:26 -0700157 server, err := CreateServer(t, client)
Joe Topjian34368bd2016-07-24 06:00:07 +0000158 if err != nil {
159 t.Fatal(err)
160 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600161 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000162
163 metadata, err := servers.UpdateMetadata(client, server.ID, servers.MetadataOpts{
164 "foo": "bar",
165 "this": "that",
166 }).Extract()
167 if err != nil {
168 t.Fatalf("Unable to update metadata: %v", err)
169 }
170 t.Logf("UpdateMetadata result: %+v\n", metadata)
171
172 err = servers.DeleteMetadatum(client, server.ID, "foo").ExtractErr()
173 if err != nil {
174 t.Fatalf("Unable to delete metadatum: %v", err)
175 }
176
177 metadata, err = servers.CreateMetadatum(client, server.ID, servers.MetadatumOpts{
178 "foo": "baz",
179 }).Extract()
180 if err != nil {
181 t.Fatalf("Unable to create metadatum: %v", err)
182 }
183 t.Logf("CreateMetadatum result: %+v\n", metadata)
184
185 metadata, err = servers.Metadatum(client, server.ID, "foo").Extract()
186 if err != nil {
187 t.Fatalf("Unable to get metadatum: %v", err)
188 }
189 t.Logf("Metadatum result: %+v\n", metadata)
190 th.AssertEquals(t, "baz", metadata["foo"])
191
192 metadata, err = servers.Metadata(client, server.ID).Extract()
193 if err != nil {
194 t.Fatalf("Unable to get metadata: %v", err)
195 }
196 t.Logf("Metadata result: %+v\n", metadata)
197
198 metadata, err = servers.ResetMetadata(client, server.ID, servers.MetadataOpts{}).Extract()
199 if err != nil {
200 t.Fatalf("Unable to reset metadata: %v", err)
201 }
202 t.Logf("ResetMetadata result: %+v\n", metadata)
203 th.AssertDeepEquals(t, map[string]string{}, metadata)
204}
205
206func TestServersActionChangeAdminPassword(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400207 t.Parallel()
208
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600209 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400210 if err != nil {
211 t.Fatalf("Unable to create a compute client: %v", err)
212 }
213
Joe Topjian66a046c2017-01-19 22:07:26 -0700214 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -0400215 if err != nil {
216 t.Fatal(err)
217 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600218 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400219
220 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500221 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200222 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000223 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400224 }
225
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600226 if err = WaitForComputeStatus(client, server, "PASSWORD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400227 t.Fatal(err)
228 }
229
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600230 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400231 t.Fatal(err)
232 }
233}
234
Joe Topjian34368bd2016-07-24 06:00:07 +0000235func TestServersActionReboot(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400236 t.Parallel()
237
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600238 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400239 if err != nil {
240 t.Fatalf("Unable to create a compute client: %v", err)
241 }
242
Joe Topjian66a046c2017-01-19 22:07:26 -0700243 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -0400244 if err != nil {
245 t.Fatal(err)
246 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600247 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400248
Joe Topjian34368bd2016-07-24 06:00:07 +0000249 rebootOpts := &servers.RebootOpts{
250 Type: servers.SoftReboot,
Ash Wilsonfd566482014-09-23 15:47:35 -0400251 }
252
253 t.Logf("Attempting reboot of server %s", server.ID)
Joe Topjian34368bd2016-07-24 06:00:07 +0000254 res := servers.Reboot(client, server.ID, rebootOpts)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200255 if res.Err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000256 t.Fatalf("Unable to reboot server: %v", res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400257 }
258
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600259 if err = WaitForComputeStatus(client, server, "REBOOT"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400260 t.Fatal(err)
261 }
262
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600263 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400264 t.Fatal(err)
265 }
266}
267
Joe Topjian34368bd2016-07-24 06:00:07 +0000268func TestServersActionRebuild(t *testing.T) {
Ash Wilsonfd566482014-09-23 15:47:35 -0400269 t.Parallel()
270
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600271 client, err := clients.NewComputeV2Client()
Ash Wilsonfd566482014-09-23 15:47:35 -0400272 if err != nil {
273 t.Fatalf("Unable to create a compute client: %v", err)
274 }
275
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600276 choices, err := clients.AcceptanceTestChoicesFromEnv()
Ash Wilsonfd566482014-09-23 15:47:35 -0400277 if err != nil {
278 t.Fatal(err)
279 }
280
Joe Topjian66a046c2017-01-19 22:07:26 -0700281 server, err := CreateServer(t, client)
Ash Wilsonfd566482014-09-23 15:47:35 -0400282 if err != nil {
283 t.Fatal(err)
284 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600285 defer DeleteServer(t, client, server)
Ash Wilsonfd566482014-09-23 15:47:35 -0400286
287 t.Logf("Attempting to rebuild server %s", server.ID)
288
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200289 rebuildOpts := servers.RebuildOpts{
290 Name: tools.RandomString("ACPTTEST", 16),
291 AdminPass: tools.MakeNewPassword(server.AdminPass),
292 ImageID: choices.ImageID,
293 }
294
295 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400296 if err != nil {
297 t.Fatal(err)
298 }
299
Ash Wilsonfd566482014-09-23 15:47:35 -0400300 if rebuilt.ID != server.ID {
301 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
302 }
303
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600304 if err = WaitForComputeStatus(client, rebuilt, "REBUILD"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400305 t.Fatal(err)
306 }
307
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600308 if err = WaitForComputeStatus(client, rebuilt, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400309 t.Fatal(err)
310 }
311}
312
Joe Topjian34368bd2016-07-24 06:00:07 +0000313func TestServersActionResizeConfirm(t *testing.T) {
314 t.Parallel()
315
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600316 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000317 if err != nil {
318 t.Fatalf("Unable to create a compute client: %v", err)
319 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400320
Joe Topjian66a046c2017-01-19 22:07:26 -0700321 server, err := CreateServer(t, client)
Joe Topjian34368bd2016-07-24 06:00:07 +0000322 if err != nil {
323 t.Fatal(err)
324 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600325 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000326
327 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian66a046c2017-01-19 22:07:26 -0700328 ResizeServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000329
330 t.Logf("Attempting to confirm resize for server %s", server.ID)
331 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
332 t.Fatal(res.Err)
333 }
334
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600335 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000336 t.Fatal(err)
337 }
338}
339
340func TestServersActionResizeRevert(t *testing.T) {
341 t.Parallel()
342
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600343 client, err := clients.NewComputeV2Client()
Joe Topjian34368bd2016-07-24 06:00:07 +0000344 if err != nil {
345 t.Fatalf("Unable to create a compute client: %v", err)
346 }
347
Joe Topjian66a046c2017-01-19 22:07:26 -0700348 server, err := CreateServer(t, client)
Joe Topjian34368bd2016-07-24 06:00:07 +0000349 if err != nil {
350 t.Fatal(err)
351 }
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600352 defer DeleteServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000353
354 t.Logf("Attempting to resize server %s", server.ID)
Joe Topjian66a046c2017-01-19 22:07:26 -0700355 ResizeServer(t, client, server)
Joe Topjian34368bd2016-07-24 06:00:07 +0000356
357 t.Logf("Attempting to revert resize for server %s", server.ID)
358 if res := servers.RevertResize(client, server.ID); res.Err != nil {
359 t.Fatal(res.Err)
360 }
361
Joe Topjian1c15e3f2016-08-08 10:48:38 -0600362 if err = WaitForComputeStatus(client, server, "ACTIVE"); err != nil {
Joe Topjian34368bd2016-07-24 06:00:07 +0000363 t.Fatal(err)
364 }
365}