blob: e223c18d16a7105ff38a02c207e28c6d3f0a3e7d [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 (
Jon Perritt2a7797d2014-10-21 15:08:43 -05006 "os"
Ash Wilsonfd566482014-09-23 15:47:35 -04007 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/acceptance/tools"
Jon Perritt2a7797d2014-10-21 15:08:43 -050011 "github.com/rackspace/gophercloud/openstack"
Ash Wilsonfd566482014-09-23 15:47:35 -040012 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Jon Perritt2a7797d2014-10-21 15:08:43 -050013 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
Ash Wilsonfd566482014-09-23 15:47:35 -040014 "github.com/rackspace/gophercloud/pagination"
15)
16
17func TestListServers(t *testing.T) {
18 client, err := newClient()
19 if err != nil {
20 t.Fatalf("Unable to create a compute client: %v", err)
21 }
22
23 t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6")
24
Jamie Hannaford1e7841b2014-10-16 12:45:49 +020025 pager := servers.List(client, servers.ListOpts{})
Ash Wilsonfd566482014-09-23 15:47:35 -040026 count, pages := 0, 0
27 pager.EachPage(func(page pagination.Page) (bool, error) {
28 pages++
29 t.Logf("---")
30
31 servers, err := servers.ExtractServers(page)
32 if err != nil {
33 return false, err
34 }
35
36 for _, s := range servers {
37 t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6)
38 count++
39 }
40
41 return true, nil
42 })
43
Ash Wilson9a9f5bc2014-10-24 14:55:40 -040044 t.Logf("--------\n%d servers listed on %d pages.\n", count, pages)
Ash Wilsonfd566482014-09-23 15:47:35 -040045}
46
Jon Perritt2a7797d2014-10-21 15:08:43 -050047func networkingClient() (*gophercloud.ServiceClient, error) {
Ash Wilson1904fce2014-10-22 15:50:48 -040048 opts, err := openstack.AuthOptionsFromEnv()
Jon Perritt2a7797d2014-10-21 15:08:43 -050049 if err != nil {
50 return nil, err
51 }
52
53 provider, err := openstack.AuthenticatedClient(opts)
54 if err != nil {
55 return nil, err
56 }
57
58 return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
59 Name: "neutron",
60 Region: os.Getenv("OS_REGION_NAME"),
61 })
62}
63
Ash Wilsonfd566482014-09-23 15:47:35 -040064func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
Ash Wilson14c37412014-10-24 14:45:27 -040065 if testing.Short() {
Jon Perritt8135c6f2014-10-23 12:29:14 -050066 t.Skip("Skipping test that requires server creation in short mode.")
67 }
68
Jon Perritt2a7797d2014-10-21 15:08:43 -050069 var network networks.Network
70
71 networkingClient, err := networkingClient()
72 if err != nil {
73 t.Fatalf("Unable to create a networking client: %v", err)
74 }
75
76 pager := networks.List(networkingClient, networks.ListOpts{Name: "public", Limit: 1})
77 pager.EachPage(func(page pagination.Page) (bool, error) {
78 networks, err := networks.ExtractNetworks(page)
79 if err != nil {
80 t.Errorf("Failed to extract networks: %v", err)
81 return false, err
82 }
83
84 if len(networks) == 0 {
Jon Perritt3525c642014-10-21 17:12:06 -050085 t.Fatalf("No networks to attach to server")
Jon Perritt2a7797d2014-10-21 15:08:43 -050086 return false, err
87 }
88
89 network = networks[0]
90
91 return false, nil
92 })
93
Ash Wilsonfd566482014-09-23 15:47:35 -040094 name := tools.RandomString("ACPTTEST", 16)
95 t.Logf("Attempting to create server: %s\n", name)
96
Ash Wilson488e7302014-10-02 11:04:25 -040097 server, err := servers.Create(client, servers.CreateOpts{
Ash Wilsone8458512014-09-29 08:50:09 -040098 Name: name,
99 FlavorRef: choices.FlavorID,
100 ImageRef: choices.ImageID,
Jon Perritt2a7797d2014-10-21 15:08:43 -0500101 Networks: []servers.Network{
102 servers.Network{UUID: network.ID},
103 },
Ash Wilson89734d02014-09-25 13:50:08 -0400104 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400105 if err != nil {
106 t.Fatalf("Unable to create server: %v", err)
107 }
108
Ash Wilson89734d02014-09-25 13:50:08 -0400109 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -0400110}
111
112func TestCreateDestroyServer(t *testing.T) {
113 choices, err := ComputeChoicesFromEnv()
114 if err != nil {
115 t.Fatal(err)
116 }
117
118 client, err := newClient()
119 if err != nil {
120 t.Fatalf("Unable to create a compute client: %v", err)
121 }
122
Ash Wilsonfd566482014-09-23 15:47:35 -0400123 server, err := createServer(t, client, choices)
124 if err != nil {
125 t.Fatalf("Unable to create server: %v", err)
126 }
127 defer func() {
128 servers.Delete(client, server.ID)
129 t.Logf("Server deleted.")
130 }()
131
132 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
133 t.Fatalf("Unable to wait for server: %v", err)
134 }
135}
136
137func TestUpdateServer(t *testing.T) {
138 client, err := newClient()
139 if err != nil {
140 t.Fatalf("Unable to create a compute client: %v", err)
141 }
142
143 choices, err := ComputeChoicesFromEnv()
144 if err != nil {
145 t.Fatal(err)
146 }
147
148 server, err := createServer(t, client, choices)
149 if err != nil {
150 t.Fatal(err)
151 }
152 defer servers.Delete(client, server.ID)
153
154 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
155 t.Fatal(err)
156 }
157
158 alternateName := tools.RandomString("ACPTTEST", 16)
159 for alternateName == server.Name {
160 alternateName = tools.RandomString("ACPTTEST", 16)
161 }
162
163 t.Logf("Attempting to rename the server to %s.", alternateName)
164
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400165 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400166 if err != nil {
167 t.Fatalf("Unable to rename server: %v", err)
168 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400169
170 if updated.ID != server.ID {
171 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
172 }
173
174 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400175 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400176 if err != nil {
177 return false, err
178 }
179
180 return latest.Name == alternateName, nil
181 })
182}
183
184func TestActionChangeAdminPassword(t *testing.T) {
185 t.Parallel()
186
187 client, err := newClient()
188 if err != nil {
189 t.Fatalf("Unable to create a compute client: %v", err)
190 }
191
192 choices, err := ComputeChoicesFromEnv()
193 if err != nil {
194 t.Fatal(err)
195 }
196
197 server, err := createServer(t, client, choices)
198 if err != nil {
199 t.Fatal(err)
200 }
201 defer servers.Delete(client, server.ID)
202
203 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
204 t.Fatal(err)
205 }
206
207 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500208 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200209 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400210 t.Fatal(err)
211 }
212
213 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
214 t.Fatal(err)
215 }
216
217 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
218 t.Fatal(err)
219 }
220}
221
222func TestActionReboot(t *testing.T) {
223 t.Parallel()
224
225 client, err := newClient()
226 if err != nil {
227 t.Fatalf("Unable to create a compute client: %v", err)
228 }
229
230 choices, err := ComputeChoicesFromEnv()
231 if err != nil {
232 t.Fatal(err)
233 }
234
235 server, err := createServer(t, client, choices)
236 if err != nil {
237 t.Fatal(err)
238 }
239 defer servers.Delete(client, server.ID)
240
241 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
242 t.Fatal(err)
243 }
244
Jon Perritt2a7797d2014-10-21 15:08:43 -0500245 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200246 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400247 t.Fatal("Expected the SDK to provide an ArgumentError here")
248 }
249
250 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200251 res = servers.Reboot(client, server.ID, servers.OSReboot)
252 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400253 t.Fatalf("Unable to reboot server: %v", err)
254 }
255
256 if err = waitForStatus(client, server, "REBOOT"); err != nil {
257 t.Fatal(err)
258 }
259
260 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
261 t.Fatal(err)
262 }
263}
264
265func TestActionRebuild(t *testing.T) {
266 t.Parallel()
267
268 client, err := newClient()
269 if err != nil {
270 t.Fatalf("Unable to create a compute client: %v", err)
271 }
272
273 choices, err := ComputeChoicesFromEnv()
274 if err != nil {
275 t.Fatal(err)
276 }
277
278 server, err := createServer(t, client, choices)
279 if err != nil {
280 t.Fatal(err)
281 }
282 defer servers.Delete(client, server.ID)
283
284 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
285 t.Fatal(err)
286 }
287
288 t.Logf("Attempting to rebuild server %s", server.ID)
289
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200290 rebuildOpts := servers.RebuildOpts{
291 Name: tools.RandomString("ACPTTEST", 16),
292 AdminPass: tools.MakeNewPassword(server.AdminPass),
293 ImageID: choices.ImageID,
294 }
295
296 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400297 if err != nil {
298 t.Fatal(err)
299 }
300
Ash Wilsonfd566482014-09-23 15:47:35 -0400301 if rebuilt.ID != server.ID {
302 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
303 }
304
305 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
306 t.Fatal(err)
307 }
308
309 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
310 t.Fatal(err)
311 }
312}
313
Ash Wilsondc7daa82014-09-23 16:34:42 -0400314func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
315 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400316 t.Fatal(err)
317 }
318
319 t.Logf("Attempting to resize server [%s]", server.ID)
320
Ash Wilson14c37412014-10-24 14:45:27 -0400321 opts := &servers.ResizeOpts{
322 FlavorRef: choices.FlavorIDResize,
323 }
324 if res := servers.Resize(client, server.ID, opts); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500325 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400326 }
327
Ash Wilsondc7daa82014-09-23 16:34:42 -0400328 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400329 t.Fatal(err)
330 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400331}
332
333func TestActionResizeConfirm(t *testing.T) {
334 t.Parallel()
335
Ash Wilsondc7daa82014-09-23 16:34:42 -0400336 choices, err := ComputeChoicesFromEnv()
337 if err != nil {
338 t.Fatal(err)
339 }
340
Ash Wilsonfd566482014-09-23 15:47:35 -0400341 client, err := newClient()
342 if err != nil {
343 t.Fatalf("Unable to create a compute client: %v", err)
344 }
345
Ash Wilsondc7daa82014-09-23 16:34:42 -0400346 server, err := createServer(t, client, choices)
347 if err != nil {
348 t.Fatal(err)
349 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400350 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400351 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400352
353 t.Logf("Attempting to confirm resize for server %s", server.ID)
354
Jamie Hannaforda2621742014-10-16 14:35:49 +0200355 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400356 t.Fatal(err)
357 }
358
359 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
360 t.Fatal(err)
361 }
362}
363
364func TestActionResizeRevert(t *testing.T) {
365 t.Parallel()
366
Ash Wilsondc7daa82014-09-23 16:34:42 -0400367 choices, err := ComputeChoicesFromEnv()
368 if err != nil {
369 t.Fatal(err)
370 }
371
Ash Wilsonfd566482014-09-23 15:47:35 -0400372 client, err := newClient()
373 if err != nil {
374 t.Fatalf("Unable to create a compute client: %v", err)
375 }
376
Ash Wilsondc7daa82014-09-23 16:34:42 -0400377 server, err := createServer(t, client, choices)
378 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400379 t.Fatal(err)
380 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400381 defer servers.Delete(client, server.ID)
382 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400383
384 t.Logf("Attempting to revert resize for server %s", server.ID)
385
Jamie Hannaforda2621742014-10-16 14:35:49 +0200386 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400387 t.Fatal(err)
388 }
389
Ash Wilsonfd566482014-09-23 15:47:35 -0400390 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
391 t.Fatal(err)
392 }
393}