blob: 131b089bc2ae1636b379144d23f0f9b0bb502edc [file] [log] [blame]
Ash Wilsonfd566482014-09-23 15:47:35 -04001// +build acceptance
2
3package v2
4
5import (
6 "fmt"
7 "testing"
8
9 "github.com/rackspace/gophercloud"
10 "github.com/rackspace/gophercloud/acceptance/tools"
11 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
12 "github.com/rackspace/gophercloud/pagination"
13)
14
15func TestListServers(t *testing.T) {
16 client, err := newClient()
17 if err != nil {
18 t.Fatalf("Unable to create a compute client: %v", err)
19 }
20
21 t.Logf("ID\tRegion\tName\tStatus\tIPv4\tIPv6")
22
23 pager := servers.List(client)
24 count, pages := 0, 0
25 pager.EachPage(func(page pagination.Page) (bool, error) {
26 pages++
27 t.Logf("---")
28
29 servers, err := servers.ExtractServers(page)
30 if err != nil {
31 return false, err
32 }
33
34 for _, s := range servers {
35 t.Logf("%s\t%s\t%s\t%s\t%s\t\n", s.ID, s.Name, s.Status, s.AccessIPv4, s.AccessIPv6)
36 count++
37 }
38
39 return true, nil
40 })
41
42 fmt.Printf("--------\n%d servers listed on %d pages.\n", count, pages)
43}
44
45func createServer(t *testing.T, client *gophercloud.ServiceClient, choices *ComputeChoices) (*servers.Server, error) {
46 name := tools.RandomString("ACPTTEST", 16)
47 t.Logf("Attempting to create server: %s\n", name)
48
Ash Wilson488e7302014-10-02 11:04:25 -040049 server, err := servers.Create(client, servers.CreateOpts{
Ash Wilsone8458512014-09-29 08:50:09 -040050 Name: name,
51 FlavorRef: choices.FlavorID,
52 ImageRef: choices.ImageID,
Ash Wilson89734d02014-09-25 13:50:08 -040053 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -040054 if err != nil {
55 t.Fatalf("Unable to create server: %v", err)
56 }
57
Ash Wilson89734d02014-09-25 13:50:08 -040058 return server, err
Ash Wilsonfd566482014-09-23 15:47:35 -040059}
60
61func TestCreateDestroyServer(t *testing.T) {
62 choices, err := ComputeChoicesFromEnv()
63 if err != nil {
64 t.Fatal(err)
65 }
66
67 client, err := newClient()
68 if err != nil {
69 t.Fatalf("Unable to create a compute client: %v", err)
70 }
71
72 name := tools.RandomString("ACPTTEST", 16)
73 t.Logf("Attempting to create server: %s\n", name)
74
75 server, err := createServer(t, client, choices)
76 if err != nil {
77 t.Fatalf("Unable to create server: %v", err)
78 }
79 defer func() {
80 servers.Delete(client, server.ID)
81 t.Logf("Server deleted.")
82 }()
83
84 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
85 t.Fatalf("Unable to wait for server: %v", err)
86 }
87}
88
89func TestUpdateServer(t *testing.T) {
90 client, err := newClient()
91 if err != nil {
92 t.Fatalf("Unable to create a compute client: %v", err)
93 }
94
95 choices, err := ComputeChoicesFromEnv()
96 if err != nil {
97 t.Fatal(err)
98 }
99
100 server, err := createServer(t, client, choices)
101 if err != nil {
102 t.Fatal(err)
103 }
104 defer servers.Delete(client, server.ID)
105
106 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
107 t.Fatal(err)
108 }
109
110 alternateName := tools.RandomString("ACPTTEST", 16)
111 for alternateName == server.Name {
112 alternateName = tools.RandomString("ACPTTEST", 16)
113 }
114
115 t.Logf("Attempting to rename the server to %s.", alternateName)
116
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400117 updated, err := servers.Update(client, server.ID, servers.UpdateOpts{Name: alternateName}).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
136func TestActionChangeAdminPassword(t *testing.T) {
137 t.Parallel()
138
139 client, err := newClient()
140 if err != nil {
141 t.Fatalf("Unable to create a compute client: %v", err)
142 }
143
144 choices, err := ComputeChoicesFromEnv()
145 if err != nil {
146 t.Fatal(err)
147 }
148
149 server, err := createServer(t, client, choices)
150 if err != nil {
151 t.Fatal(err)
152 }
153 defer servers.Delete(client, server.ID)
154
155 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
156 t.Fatal(err)
157 }
158
159 randomPassword := tools.MakeNewPassword(server.AdminPass)
160 err = servers.ChangeAdminPassword(client, server.ID, randomPassword)
161 if err != nil {
162 t.Fatal(err)
163 }
164
165 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
166 t.Fatal(err)
167 }
168
169 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
170 t.Fatal(err)
171 }
172}
173
174func TestActionReboot(t *testing.T) {
175 t.Parallel()
176
177 client, err := newClient()
178 if err != nil {
179 t.Fatalf("Unable to create a compute client: %v", err)
180 }
181
182 choices, err := ComputeChoicesFromEnv()
183 if err != nil {
184 t.Fatal(err)
185 }
186
187 server, err := createServer(t, client, choices)
188 if err != nil {
189 t.Fatal(err)
190 }
191 defer servers.Delete(client, server.ID)
192
193 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
194 t.Fatal(err)
195 }
196
197 err = servers.Reboot(client, server.ID, "aldhjflaskhjf")
198 if err == nil {
199 t.Fatal("Expected the SDK to provide an ArgumentError here")
200 }
201
202 t.Logf("Attempting reboot of server %s", server.ID)
203 err = servers.Reboot(client, server.ID, servers.OSReboot)
204 if err != nil {
205 t.Fatalf("Unable to reboot server: %v", err)
206 }
207
208 if err = waitForStatus(client, server, "REBOOT"); err != nil {
209 t.Fatal(err)
210 }
211
212 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
213 t.Fatal(err)
214 }
215}
216
217func TestActionRebuild(t *testing.T) {
218 t.Parallel()
219
220 client, err := newClient()
221 if err != nil {
222 t.Fatalf("Unable to create a compute client: %v", err)
223 }
224
225 choices, err := ComputeChoicesFromEnv()
226 if err != nil {
227 t.Fatal(err)
228 }
229
230 server, err := createServer(t, client, choices)
231 if err != nil {
232 t.Fatal(err)
233 }
234 defer servers.Delete(client, server.ID)
235
236 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
237 t.Fatal(err)
238 }
239
240 t.Logf("Attempting to rebuild server %s", server.ID)
241
242 newPassword := tools.MakeNewPassword(server.AdminPass)
243 newName := tools.RandomString("ACPTTEST", 16)
Ash Wilson89734d02014-09-25 13:50:08 -0400244 rebuilt, err := servers.Rebuild(client, server.ID, newName, newPassword, choices.ImageID, nil).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400245 if err != nil {
246 t.Fatal(err)
247 }
248
Ash Wilsonfd566482014-09-23 15:47:35 -0400249 if rebuilt.ID != server.ID {
250 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
251 }
252
253 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
254 t.Fatal(err)
255 }
256
257 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
258 t.Fatal(err)
259 }
260}
261
Ash Wilsondc7daa82014-09-23 16:34:42 -0400262func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
263 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400264 t.Fatal(err)
265 }
266
267 t.Logf("Attempting to resize server [%s]", server.ID)
268
Ash Wilsondc7daa82014-09-23 16:34:42 -0400269 if err := servers.Resize(client, server.ID, choices.FlavorIDResize); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400270 t.Fatal(err)
271 }
272
Ash Wilsondc7daa82014-09-23 16:34:42 -0400273 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400274 t.Fatal(err)
275 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400276}
277
278func TestActionResizeConfirm(t *testing.T) {
279 t.Parallel()
280
Ash Wilsondc7daa82014-09-23 16:34:42 -0400281 choices, err := ComputeChoicesFromEnv()
282 if err != nil {
283 t.Fatal(err)
284 }
285
Ash Wilsonfd566482014-09-23 15:47:35 -0400286 client, err := newClient()
287 if err != nil {
288 t.Fatalf("Unable to create a compute client: %v", err)
289 }
290
Ash Wilsondc7daa82014-09-23 16:34:42 -0400291 server, err := createServer(t, client, choices)
292 if err != nil {
293 t.Fatal(err)
294 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400295 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400296 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400297
298 t.Logf("Attempting to confirm resize for server %s", server.ID)
299
300 if err = servers.ConfirmResize(client, server.ID); err != nil {
301 t.Fatal(err)
302 }
303
304 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
305 t.Fatal(err)
306 }
307}
308
309func TestActionResizeRevert(t *testing.T) {
310 t.Parallel()
311
Ash Wilsondc7daa82014-09-23 16:34:42 -0400312 choices, err := ComputeChoicesFromEnv()
313 if err != nil {
314 t.Fatal(err)
315 }
316
Ash Wilsonfd566482014-09-23 15:47:35 -0400317 client, err := newClient()
318 if err != nil {
319 t.Fatalf("Unable to create a compute client: %v", err)
320 }
321
Ash Wilsondc7daa82014-09-23 16:34:42 -0400322 server, err := createServer(t, client, choices)
323 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400324 t.Fatal(err)
325 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400326 defer servers.Delete(client, server.ID)
327 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400328
329 t.Logf("Attempting to revert resize for server %s", server.ID)
330
331 if err := servers.RevertResize(client, server.ID); err != nil {
332 t.Fatal(err)
333 }
334
Ash Wilsonfd566482014-09-23 15:47:35 -0400335 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
336 t.Fatal(err)
337 }
338}