blob: f5e800fd513a68f9a363f684a19202d15dad419c [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
49 server, err := servers.Create(client, map[string]interface{}{
50 "flavorRef": choices.FlavorID,
51 "imageRef": choices.ImageID,
52 "name": name,
53 })
54 if err != nil {
55 t.Fatalf("Unable to create server: %v", err)
56 }
57
58 return servers.ExtractServer(server)
59}
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
117 result, err := servers.Update(client, server.ID, map[string]interface{}{
118 "name": alternateName,
119 })
120 if err != nil {
121 t.Fatalf("Unable to rename server: %v", err)
122 }
123 updated, err := servers.ExtractServer(result)
124 if err != nil {
125 t.Fatalf("Unable to extract server: %v", err)
126 }
127
128 if updated.ID != server.ID {
129 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
130 }
131
132 err = tools.WaitFor(func() (bool, error) {
133 result, err := servers.Get(client, updated.ID)
134 if err != nil {
135 return false, err
136 }
137 latest, err := servers.ExtractServer(result)
138 if err != nil {
139 return false, err
140 }
141
142 return latest.Name == alternateName, nil
143 })
144}
145
146func TestActionChangeAdminPassword(t *testing.T) {
147 t.Parallel()
148
149 client, err := newClient()
150 if err != nil {
151 t.Fatalf("Unable to create a compute client: %v", err)
152 }
153
154 choices, err := ComputeChoicesFromEnv()
155 if err != nil {
156 t.Fatal(err)
157 }
158
159 server, err := createServer(t, client, choices)
160 if err != nil {
161 t.Fatal(err)
162 }
163 defer servers.Delete(client, server.ID)
164
165 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
166 t.Fatal(err)
167 }
168
169 randomPassword := tools.MakeNewPassword(server.AdminPass)
170 err = servers.ChangeAdminPassword(client, server.ID, randomPassword)
171 if err != nil {
172 t.Fatal(err)
173 }
174
175 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
176 t.Fatal(err)
177 }
178
179 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
180 t.Fatal(err)
181 }
182}
183
184func TestActionReboot(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 err = servers.Reboot(client, server.ID, "aldhjflaskhjf")
208 if err == nil {
209 t.Fatal("Expected the SDK to provide an ArgumentError here")
210 }
211
212 t.Logf("Attempting reboot of server %s", server.ID)
213 err = servers.Reboot(client, server.ID, servers.OSReboot)
214 if err != nil {
215 t.Fatalf("Unable to reboot server: %v", err)
216 }
217
218 if err = waitForStatus(client, server, "REBOOT"); err != nil {
219 t.Fatal(err)
220 }
221
222 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
223 t.Fatal(err)
224 }
225}
226
227func TestActionRebuild(t *testing.T) {
228 t.Parallel()
229
230 client, err := newClient()
231 if err != nil {
232 t.Fatalf("Unable to create a compute client: %v", err)
233 }
234
235 choices, err := ComputeChoicesFromEnv()
236 if err != nil {
237 t.Fatal(err)
238 }
239
240 server, err := createServer(t, client, choices)
241 if err != nil {
242 t.Fatal(err)
243 }
244 defer servers.Delete(client, server.ID)
245
246 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
247 t.Fatal(err)
248 }
249
250 t.Logf("Attempting to rebuild server %s", server.ID)
251
252 newPassword := tools.MakeNewPassword(server.AdminPass)
253 newName := tools.RandomString("ACPTTEST", 16)
254 result, err := servers.Rebuild(client, server.ID, newName, newPassword, choices.ImageID, nil)
255 if err != nil {
256 t.Fatal(err)
257 }
258
259 rebuilt, err := servers.ExtractServer(result)
260 if err != nil {
261 t.Fatal(err)
262 }
263 if rebuilt.ID != server.ID {
264 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
265 }
266
267 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
268 t.Fatal(err)
269 }
270
271 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
272 t.Fatal(err)
273 }
274}
275
276func resizeServer(t *testing.T, client *gophercloud.ServiceClient) *servers.Server {
277 choices, err := ComputeChoicesFromEnv()
278 if err != nil {
279 t.Fatal(err)
280 }
281
282 server, err := createServer(t, client, choices)
283 if err != nil {
284 t.Fatal(err)
285 }
286
287 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
288 t.Fatal(err)
289 }
290
291 t.Logf("Attempting to resize server [%s]", server.ID)
292
293 if err = servers.Resize(client, server.ID, choices.FlavorIDResize); err != nil {
294 t.Fatal(err)
295 }
296
297 if err = waitForStatus(client, server, "RESIZE"); err != nil {
298 t.Fatal(err)
299 }
300
301 if err = waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
302 t.Fatal(err)
303 }
304
305 return server
306}
307
308func TestActionResizeConfirm(t *testing.T) {
309 t.Parallel()
310
311 client, err := newClient()
312 if err != nil {
313 t.Fatalf("Unable to create a compute client: %v", err)
314 }
315
316 server := resizeServer(t, client)
317 defer servers.Delete(client, server.ID)
318
319 t.Logf("Attempting to confirm resize for server %s", server.ID)
320
321 if err = servers.ConfirmResize(client, server.ID); err != nil {
322 t.Fatal(err)
323 }
324
325 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
326 t.Fatal(err)
327 }
328}
329
330func TestActionResizeRevert(t *testing.T) {
331 t.Parallel()
332
333 client, err := newClient()
334 if err != nil {
335 t.Fatalf("Unable to create a compute client: %v", err)
336 }
337
338 server := resizeServer(t, client)
339 defer servers.Delete(client, server.ID)
340
341 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
342 t.Fatal(err)
343 }
344
345 t.Logf("Attempting to revert resize for server %s", server.ID)
346
347 if err := servers.RevertResize(client, server.ID); err != nil {
348 t.Fatal(err)
349 }
350
351 if err = waitForStatus(client, server, "REVERT_RESIZE"); err != nil {
352 t.Fatal(err)
353 }
354
355 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
356 t.Fatal(err)
357 }
358}