blob: cb413c13ddb542524ce9e6206d73dc442ddf5463 [file] [log] [blame]
Ash Wilsonfd566482014-09-23 15:47:35 -04001// +build acceptance
2
3package v2
4
5import (
6 "fmt"
Jon Perritt2a7797d2014-10-21 15:08:43 -05007 "os"
Ash Wilsonfd566482014-09-23 15:47:35 -04008 "testing"
9
10 "github.com/rackspace/gophercloud"
11 "github.com/rackspace/gophercloud/acceptance/tools"
Jon Perritt2a7797d2014-10-21 15:08:43 -050012 "github.com/rackspace/gophercloud/openstack"
Ash Wilsonfd566482014-09-23 15:47:35 -040013 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Jon Perritt2a7797d2014-10-21 15:08:43 -050014 "github.com/rackspace/gophercloud/openstack/networking/v2/networks"
15 "github.com/rackspace/gophercloud/openstack/utils"
Ash Wilsonfd566482014-09-23 15:47:35 -040016 "github.com/rackspace/gophercloud/pagination"
17)
18
19func TestListServers(t *testing.T) {
20 client, err := newClient()
21 if err != nil {
22 t.Fatalf("Unable to create a compute client: %v", err)
23 }
24
25 t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6")
26
Jamie Hannaford1e7841b2014-10-16 12:45:49 +020027 pager := servers.List(client, servers.ListOpts{})
Ash Wilsonfd566482014-09-23 15:47:35 -040028 count, pages := 0, 0
29 pager.EachPage(func(page pagination.Page) (bool, error) {
30 pages++
31 t.Logf("---")
32
33 servers, err := servers.ExtractServers(page)
34 if err != nil {
35 return false, err
36 }
37
38 for _, s := range servers {
39 t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6)
40 count++
41 }
42
43 return true, nil
44 })
45
46 fmt.Printf("--------\n%d servers listed on %d pages.\n", count, pages)
47}
48
Jon Perritt2a7797d2014-10-21 15:08:43 -050049func networkingClient() (*gophercloud.ServiceClient, error) {
50 opts, err := utils.AuthOptions()
51 if err != nil {
52 return nil, err
53 }
54
55 provider, err := openstack.AuthenticatedClient(opts)
56 if err != nil {
57 return nil, err
58 }
59
60 return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
61 Name: "neutron",
62 Region: os.Getenv("OS_REGION_NAME"),
63 })
64}
65
Ash Wilsonfd566482014-09-23 15:47:35 -040066func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
Jon Perritt2a7797d2014-10-21 15:08:43 -050067 var network networks.Network
68
69 networkingClient, err := networkingClient()
70 if err != nil {
71 t.Fatalf("Unable to create a networking client: %v", err)
72 }
73
74 pager := networks.List(networkingClient, networks.ListOpts{Name: "public", Limit: 1})
75 pager.EachPage(func(page pagination.Page) (bool, error) {
76 networks, err := networks.ExtractNetworks(page)
77 if err != nil {
78 t.Errorf("Failed to extract networks: %v", err)
79 return false, err
80 }
81
82 if len(networks) == 0 {
Jon Perritt3525c642014-10-21 17:12:06 -050083 t.Fatalf("No networks to attach to server")
Jon Perritt2a7797d2014-10-21 15:08:43 -050084 return false, err
85 }
86
87 network = networks[0]
88
89 return false, nil
90 })
91
Ash Wilsonfd566482014-09-23 15:47:35 -040092 name := tools.RandomString("ACPTTEST", 16)
93 t.Logf("Attempting to create server: %s\n", name)
94
Ash Wilson488e7302014-10-02 11:04:25 -040095 server, err := servers.Create(client, servers.CreateOpts{
Ash Wilsone8458512014-09-29 08:50:09 -040096 Name: name,
97 FlavorRef: choices.FlavorID,
98 ImageRef: choices.ImageID,
Jon Perritt2a7797d2014-10-21 15:08:43 -050099 Networks: []servers.Network{
100 servers.Network{UUID: network.ID},
101 },
Ash Wilson89734d02014-09-25 13:50:08 -0400102 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400103 if err != nil {
104 t.Fatalf("Unable to create server: %v", err)
105 }
106
Ash Wilson89734d02014-09-25 13:50:08 -0400107 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -0400108}
109
110func TestCreateDestroyServer(t *testing.T) {
111 choices, err := ComputeChoicesFromEnv()
112 if err != nil {
113 t.Fatal(err)
114 }
115
116 client, err := newClient()
117 if err != nil {
118 t.Fatalf("Unable to create a compute client: %v", err)
119 }
120
Ash Wilsonfd566482014-09-23 15:47:35 -0400121 server, err := createServer(t, client, choices)
122 if err != nil {
123 t.Fatalf("Unable to create server: %v", err)
124 }
125 defer func() {
126 servers.Delete(client, server.ID)
127 t.Logf("Server deleted.")
128 }()
129
130 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
131 t.Fatalf("Unable to wait for server: %v", err)
132 }
133}
134
135func TestUpdateServer(t *testing.T) {
136 client, err := newClient()
137 if err != nil {
138 t.Fatalf("Unable to create a compute client: %v", err)
139 }
140
141 choices, err := ComputeChoicesFromEnv()
142 if err != nil {
143 t.Fatal(err)
144 }
145
146 server, err := createServer(t, client, choices)
147 if err != nil {
148 t.Fatal(err)
149 }
150 defer servers.Delete(client, server.ID)
151
152 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
153 t.Fatal(err)
154 }
155
156 alternateName := tools.RandomString("ACPTTEST", 16)
157 for alternateName == server.Name {
158 alternateName = tools.RandomString("ACPTTEST", 16)
159 }
160
161 t.Logf("Attempting to rename the server to %s.", alternateName)
162
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400163 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400164 if err != nil {
165 t.Fatalf("Unable to rename server: %v", err)
166 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400167
168 if updated.ID != server.ID {
169 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
170 }
171
172 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400173 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400174 if err != nil {
175 return false, err
176 }
177
178 return latest.Name == alternateName, nil
179 })
180}
181
182func TestActionChangeAdminPassword(t *testing.T) {
183 t.Parallel()
184
185 client, err := newClient()
186 if err != nil {
187 t.Fatalf("Unable to create a compute client: %v", err)
188 }
189
190 choices, err := ComputeChoicesFromEnv()
191 if err != nil {
192 t.Fatal(err)
193 }
194
195 server, err := createServer(t, client, choices)
196 if err != nil {
197 t.Fatal(err)
198 }
199 defer servers.Delete(client, server.ID)
200
201 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
202 t.Fatal(err)
203 }
204
205 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500206 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200207 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400208 t.Fatal(err)
209 }
210
211 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
212 t.Fatal(err)
213 }
214
215 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
216 t.Fatal(err)
217 }
218}
219
220func TestActionReboot(t *testing.T) {
221 t.Parallel()
222
223 client, err := newClient()
224 if err != nil {
225 t.Fatalf("Unable to create a compute client: %v", err)
226 }
227
228 choices, err := ComputeChoicesFromEnv()
229 if err != nil {
230 t.Fatal(err)
231 }
232
233 server, err := createServer(t, client, choices)
234 if err != nil {
235 t.Fatal(err)
236 }
237 defer servers.Delete(client, server.ID)
238
239 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
240 t.Fatal(err)
241 }
242
Jon Perritt2a7797d2014-10-21 15:08:43 -0500243 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200244 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400245 t.Fatal("Expected the SDK to provide an ArgumentError here")
246 }
247
248 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200249 res = servers.Reboot(client, server.ID, servers.OSReboot)
250 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400251 t.Fatalf("Unable to reboot server: %v", err)
252 }
253
254 if err = waitForStatus(client, server, "REBOOT"); err != nil {
255 t.Fatal(err)
256 }
257
258 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
259 t.Fatal(err)
260 }
261}
262
263func TestActionRebuild(t *testing.T) {
264 t.Parallel()
265
266 client, err := newClient()
267 if err != nil {
268 t.Fatalf("Unable to create a compute client: %v", err)
269 }
270
271 choices, err := ComputeChoicesFromEnv()
272 if err != nil {
273 t.Fatal(err)
274 }
275
276 server, err := createServer(t, client, choices)
277 if err != nil {
278 t.Fatal(err)
279 }
280 defer servers.Delete(client, server.ID)
281
282 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
283 t.Fatal(err)
284 }
285
286 t.Logf("Attempting to rebuild server %s", server.ID)
287
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200288 rebuildOpts := servers.RebuildOpts{
289 Name: tools.RandomString("ACPTTEST", 16),
290 AdminPass: tools.MakeNewPassword(server.AdminPass),
291 ImageID: choices.ImageID,
292 }
293
294 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400295 if err != nil {
296 t.Fatal(err)
297 }
298
Ash Wilsonfd566482014-09-23 15:47:35 -0400299 if rebuilt.ID != server.ID {
300 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
301 }
302
303 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
304 t.Fatal(err)
305 }
306
307 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
308 t.Fatal(err)
309 }
310}
311
Ash Wilsondc7daa82014-09-23 16:34:42 -0400312func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
313 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400314 t.Fatal(err)
315 }
316
317 t.Logf("Attempting to resize server [%s]", server.ID)
318
Jamie Hannaforda2621742014-10-16 14:35:49 +0200319 if res := servers.Resize(client, server.ID, choices.FlavorIDResize); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500320 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400321 }
322
Ash Wilsondc7daa82014-09-23 16:34:42 -0400323 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400324 t.Fatal(err)
325 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400326}
327
328func TestActionResizeConfirm(t *testing.T) {
329 t.Parallel()
330
Ash Wilsondc7daa82014-09-23 16:34:42 -0400331 choices, err := ComputeChoicesFromEnv()
332 if err != nil {
333 t.Fatal(err)
334 }
335
Ash Wilsonfd566482014-09-23 15:47:35 -0400336 client, err := newClient()
337 if err != nil {
338 t.Fatalf("Unable to create a compute client: %v", err)
339 }
340
Ash Wilsondc7daa82014-09-23 16:34:42 -0400341 server, err := createServer(t, client, choices)
342 if err != nil {
343 t.Fatal(err)
344 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400345 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400346 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400347
348 t.Logf("Attempting to confirm resize for server %s", server.ID)
349
Jamie Hannaforda2621742014-10-16 14:35:49 +0200350 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400351 t.Fatal(err)
352 }
353
354 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
355 t.Fatal(err)
356 }
357}
358
359func TestActionResizeRevert(t *testing.T) {
360 t.Parallel()
361
Ash Wilsondc7daa82014-09-23 16:34:42 -0400362 choices, err := ComputeChoicesFromEnv()
363 if err != nil {
364 t.Fatal(err)
365 }
366
Ash Wilsonfd566482014-09-23 15:47:35 -0400367 client, err := newClient()
368 if err != nil {
369 t.Fatalf("Unable to create a compute client: %v", err)
370 }
371
Ash Wilsondc7daa82014-09-23 16:34:42 -0400372 server, err := createServer(t, client, choices)
373 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400374 t.Fatal(err)
375 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400376 defer servers.Delete(client, server.ID)
377 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400378
379 t.Logf("Attempting to revert resize for server %s", server.ID)
380
Jamie Hannaforda2621742014-10-16 14:35:49 +0200381 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400382 t.Fatal(err)
383 }
384
Ash Wilsonfd566482014-09-23 15:47:35 -0400385 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
386 t.Fatal(err)
387 }
388}