blob: 67b1b4670b335fab0e79fb8f0113845e37d9d93b [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 (
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"
Ash Wilsonfd566482014-09-23 15:47:35 -040015 "github.com/rackspace/gophercloud/pagination"
16)
17
18func TestListServers(t *testing.T) {
19 client, err := newClient()
20 if err != nil {
21 t.Fatalf("Unable to create a compute client: %v", err)
22 }
23
24 t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6")
25
Jamie Hannaford1e7841b2014-10-16 12:45:49 +020026 pager := servers.List(client, servers.ListOpts{})
Ash Wilsonfd566482014-09-23 15:47:35 -040027 count, pages := 0, 0
28 pager.EachPage(func(page pagination.Page) (bool, error) {
29 pages++
30 t.Logf("---")
31
32 servers, err := servers.ExtractServers(page)
33 if err != nil {
34 return false, err
35 }
36
37 for _, s := range servers {
38 t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6)
39 count++
40 }
41
42 return true, nil
43 })
44
45 fmt.Printf("--------\n%d servers listed on %d pages.\n", count, pages)
46}
47
Jon Perritt2a7797d2014-10-21 15:08:43 -050048func networkingClient() (*gophercloud.ServiceClient, error) {
Ash Wilson1904fce2014-10-22 15:50:48 -040049 opts, err := openstack.AuthOptionsFromEnv()
Jon Perritt2a7797d2014-10-21 15:08:43 -050050 if err != nil {
51 return nil, err
52 }
53
54 provider, err := openstack.AuthenticatedClient(opts)
55 if err != nil {
56 return nil, err
57 }
58
59 return openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{
60 Name: "neutron",
61 Region: os.Getenv("OS_REGION_NAME"),
62 })
63}
64
Ash Wilsonfd566482014-09-23 15:47:35 -040065func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
Jon Perritt2a7797d2014-10-21 15:08:43 -050066 var network networks.Network
67
68 networkingClient, err := networkingClient()
69 if err != nil {
70 t.Fatalf("Unable to create a networking client: %v", err)
71 }
72
73 pager := networks.List(networkingClient, networks.ListOpts{Name: "public", Limit: 1})
74 pager.EachPage(func(page pagination.Page) (bool, error) {
75 networks, err := networks.ExtractNetworks(page)
76 if err != nil {
77 t.Errorf("Failed to extract networks: %v", err)
78 return false, err
79 }
80
81 if len(networks) == 0 {
Jon Perritt3525c642014-10-21 17:12:06 -050082 t.Fatalf("No networks to attach to server")
Jon Perritt2a7797d2014-10-21 15:08:43 -050083 return false, err
84 }
85
86 network = networks[0]
87
88 return false, nil
89 })
90
Ash Wilsonfd566482014-09-23 15:47:35 -040091 name := tools.RandomString("ACPTTEST", 16)
92 t.Logf("Attempting to create server: %s\n", name)
93
Ash Wilson488e7302014-10-02 11:04:25 -040094 server, err := servers.Create(client, servers.CreateOpts{
Ash Wilsone8458512014-09-29 08:50:09 -040095 Name: name,
96 FlavorRef: choices.FlavorID,
97 ImageRef: choices.ImageID,
Jon Perritt2a7797d2014-10-21 15:08:43 -050098 Networks: []servers.Network{
99 servers.Network{UUID: network.ID},
100 },
Ash Wilson89734d02014-09-25 13:50:08 -0400101 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400102 if err != nil {
103 t.Fatalf("Unable to create server: %v", err)
104 }
105
Ash Wilson89734d02014-09-25 13:50:08 -0400106 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -0400107}
108
109func TestCreateDestroyServer(t *testing.T) {
110 choices, err := ComputeChoicesFromEnv()
111 if err != nil {
112 t.Fatal(err)
113 }
114
115 client, err := newClient()
116 if err != nil {
117 t.Fatalf("Unable to create a compute client: %v", err)
118 }
119
Ash Wilsonfd566482014-09-23 15:47:35 -0400120 server, err := createServer(t, client, choices)
121 if err != nil {
122 t.Fatalf("Unable to create server: %v", err)
123 }
124 defer func() {
125 servers.Delete(client, server.ID)
126 t.Logf("Server deleted.")
127 }()
128
129 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
130 t.Fatalf("Unable to wait for server: %v", err)
131 }
132}
133
134func TestUpdateServer(t *testing.T) {
135 client, err := newClient()
136 if err != nil {
137 t.Fatalf("Unable to create a compute client: %v", err)
138 }
139
140 choices, err := ComputeChoicesFromEnv()
141 if err != nil {
142 t.Fatal(err)
143 }
144
145 server, err := createServer(t, client, choices)
146 if err != nil {
147 t.Fatal(err)
148 }
149 defer servers.Delete(client, server.ID)
150
151 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
152 t.Fatal(err)
153 }
154
155 alternateName := tools.RandomString("ACPTTEST", 16)
156 for alternateName == server.Name {
157 alternateName = tools.RandomString("ACPTTEST", 16)
158 }
159
160 t.Logf("Attempting to rename the server to %s.", alternateName)
161
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400162 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400163 if err != nil {
164 t.Fatalf("Unable to rename server: %v", err)
165 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400166
167 if updated.ID != server.ID {
168 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
169 }
170
171 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400172 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400173 if err != nil {
174 return false, err
175 }
176
177 return latest.Name == alternateName, nil
178 })
179}
180
181func TestActionChangeAdminPassword(t *testing.T) {
182 t.Parallel()
183
184 client, err := newClient()
185 if err != nil {
186 t.Fatalf("Unable to create a compute client: %v", err)
187 }
188
189 choices, err := ComputeChoicesFromEnv()
190 if err != nil {
191 t.Fatal(err)
192 }
193
194 server, err := createServer(t, client, choices)
195 if err != nil {
196 t.Fatal(err)
197 }
198 defer servers.Delete(client, server.ID)
199
200 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
201 t.Fatal(err)
202 }
203
204 randomPassword := tools.MakeNewPassword(server.AdminPass)
Jon Perritt2a7797d2014-10-21 15:08:43 -0500205 res := servers.ChangeAdminPassword(client, server.ID, randomPassword)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200206 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400207 t.Fatal(err)
208 }
209
210 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
211 t.Fatal(err)
212 }
213
214 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
215 t.Fatal(err)
216 }
217}
218
219func TestActionReboot(t *testing.T) {
220 t.Parallel()
221
222 client, err := newClient()
223 if err != nil {
224 t.Fatalf("Unable to create a compute client: %v", err)
225 }
226
227 choices, err := ComputeChoicesFromEnv()
228 if err != nil {
229 t.Fatal(err)
230 }
231
232 server, err := createServer(t, client, choices)
233 if err != nil {
234 t.Fatal(err)
235 }
236 defer servers.Delete(client, server.ID)
237
238 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
239 t.Fatal(err)
240 }
241
Jon Perritt2a7797d2014-10-21 15:08:43 -0500242 res := servers.Reboot(client, server.ID, "aldhjflaskhjf")
Jamie Hannaforda2621742014-10-16 14:35:49 +0200243 if res.Err == nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400244 t.Fatal("Expected the SDK to provide an ArgumentError here")
245 }
246
247 t.Logf("Attempting reboot of server %s", server.ID)
Jamie Hannaforda2621742014-10-16 14:35:49 +0200248 res = servers.Reboot(client, server.ID, servers.OSReboot)
249 if res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400250 t.Fatalf("Unable to reboot server: %v", err)
251 }
252
253 if err = waitForStatus(client, server, "REBOOT"); err != nil {
254 t.Fatal(err)
255 }
256
257 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
258 t.Fatal(err)
259 }
260}
261
262func TestActionRebuild(t *testing.T) {
263 t.Parallel()
264
265 client, err := newClient()
266 if err != nil {
267 t.Fatalf("Unable to create a compute client: %v", err)
268 }
269
270 choices, err := ComputeChoicesFromEnv()
271 if err != nil {
272 t.Fatal(err)
273 }
274
275 server, err := createServer(t, client, choices)
276 if err != nil {
277 t.Fatal(err)
278 }
279 defer servers.Delete(client, server.ID)
280
281 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
282 t.Fatal(err)
283 }
284
285 t.Logf("Attempting to rebuild server %s", server.ID)
286
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200287 rebuildOpts := servers.RebuildOpts{
288 Name: tools.RandomString("ACPTTEST", 16),
289 AdminPass: tools.MakeNewPassword(server.AdminPass),
290 ImageID: choices.ImageID,
291 }
292
293 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400294 if err != nil {
295 t.Fatal(err)
296 }
297
Ash Wilsonfd566482014-09-23 15:47:35 -0400298 if rebuilt.ID != server.ID {
299 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
300 }
301
302 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
303 t.Fatal(err)
304 }
305
306 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
307 t.Fatal(err)
308 }
309}
310
Ash Wilsondc7daa82014-09-23 16:34:42 -0400311func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
312 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400313 t.Fatal(err)
314 }
315
316 t.Logf("Attempting to resize server [%s]", server.ID)
317
Jamie Hannaforda2621742014-10-16 14:35:49 +0200318 if res := servers.Resize(client, server.ID, choices.FlavorIDResize); res.Err != nil {
Jon Perritt2a7797d2014-10-21 15:08:43 -0500319 t.Fatal(res.Err)
Ash Wilsonfd566482014-09-23 15:47:35 -0400320 }
321
Ash Wilsondc7daa82014-09-23 16:34:42 -0400322 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400323 t.Fatal(err)
324 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400325}
326
327func TestActionResizeConfirm(t *testing.T) {
328 t.Parallel()
329
Ash Wilsondc7daa82014-09-23 16:34:42 -0400330 choices, err := ComputeChoicesFromEnv()
331 if err != nil {
332 t.Fatal(err)
333 }
334
Ash Wilsonfd566482014-09-23 15:47:35 -0400335 client, err := newClient()
336 if err != nil {
337 t.Fatalf("Unable to create a compute client: %v", err)
338 }
339
Ash Wilsondc7daa82014-09-23 16:34:42 -0400340 server, err := createServer(t, client, choices)
341 if err != nil {
342 t.Fatal(err)
343 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400344 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400345 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400346
347 t.Logf("Attempting to confirm resize for server %s", server.ID)
348
Jamie Hannaforda2621742014-10-16 14:35:49 +0200349 if res := servers.ConfirmResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400350 t.Fatal(err)
351 }
352
353 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
354 t.Fatal(err)
355 }
356}
357
358func TestActionResizeRevert(t *testing.T) {
359 t.Parallel()
360
Ash Wilsondc7daa82014-09-23 16:34:42 -0400361 choices, err := ComputeChoicesFromEnv()
362 if err != nil {
363 t.Fatal(err)
364 }
365
Ash Wilsonfd566482014-09-23 15:47:35 -0400366 client, err := newClient()
367 if err != nil {
368 t.Fatalf("Unable to create a compute client: %v", err)
369 }
370
Ash Wilsondc7daa82014-09-23 16:34:42 -0400371 server, err := createServer(t, client, choices)
372 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400373 t.Fatal(err)
374 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400375 defer servers.Delete(client, server.ID)
376 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400377
378 t.Logf("Attempting to revert resize for server %s", server.ID)
379
Jamie Hannaforda2621742014-10-16 14:35:49 +0200380 if res := servers.RevertResize(client, server.ID); res.Err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400381 t.Fatal(err)
382 }
383
Ash Wilsonfd566482014-09-23 15:47:35 -0400384 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
385 t.Fatal(err)
386 }
387}