blob: fc8ceae25db7c9e23d3cb0782a3f56fd5b3fcc0b [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 Wilsone8458512014-09-29 08:50:09 -040049 server, err := servers.Create(client, &servers.CreateOpts{
50 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 Wilson89734d02014-09-25 13:50:08 -0400117 updated, err := servers.Update(client, server.ID, map[string]interface{}{
Ash Wilsonfd566482014-09-23 15:47:35 -0400118 "name": alternateName,
Ash Wilson89734d02014-09-25 13:50:08 -0400119 }).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400120 if err != nil {
121 t.Fatalf("Unable to rename server: %v", err)
122 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400123
124 if updated.ID != server.ID {
125 t.Errorf("Updated server ID [%s] didn't match original server ID [%s]!", updated.ID, server.ID)
126 }
127
128 err = tools.WaitFor(func() (bool, error) {
Ash Wilson89734d02014-09-25 13:50:08 -0400129 latest, err := servers.Get(client, updated.ID).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400130 if err != nil {
131 return false, err
132 }
133
134 return latest.Name == alternateName, nil
135 })
136}
137
138func TestActionChangeAdminPassword(t *testing.T) {
139 t.Parallel()
140
141 client, err := newClient()
142 if err != nil {
143 t.Fatalf("Unable to create a compute client: %v", err)
144 }
145
146 choices, err := ComputeChoicesFromEnv()
147 if err != nil {
148 t.Fatal(err)
149 }
150
151 server, err := createServer(t, client, choices)
152 if err != nil {
153 t.Fatal(err)
154 }
155 defer servers.Delete(client, server.ID)
156
157 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
158 t.Fatal(err)
159 }
160
161 randomPassword := tools.MakeNewPassword(server.AdminPass)
162 err = servers.ChangeAdminPassword(client, server.ID, randomPassword)
163 if err != nil {
164 t.Fatal(err)
165 }
166
167 if err = waitForStatus(client, server, "PASSWORD"); err != nil {
168 t.Fatal(err)
169 }
170
171 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
172 t.Fatal(err)
173 }
174}
175
176func TestActionReboot(t *testing.T) {
177 t.Parallel()
178
179 client, err := newClient()
180 if err != nil {
181 t.Fatalf("Unable to create a compute client: %v", err)
182 }
183
184 choices, err := ComputeChoicesFromEnv()
185 if err != nil {
186 t.Fatal(err)
187 }
188
189 server, err := createServer(t, client, choices)
190 if err != nil {
191 t.Fatal(err)
192 }
193 defer servers.Delete(client, server.ID)
194
195 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
196 t.Fatal(err)
197 }
198
199 err = servers.Reboot(client, server.ID, "aldhjflaskhjf")
200 if err == nil {
201 t.Fatal("Expected the SDK to provide an ArgumentError here")
202 }
203
204 t.Logf("Attempting reboot of server %s", server.ID)
205 err = servers.Reboot(client, server.ID, servers.OSReboot)
206 if err != nil {
207 t.Fatalf("Unable to reboot server: %v", err)
208 }
209
210 if err = waitForStatus(client, server, "REBOOT"); 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 TestActionRebuild(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
242 t.Logf("Attempting to rebuild server %s", server.ID)
243
244 newPassword := tools.MakeNewPassword(server.AdminPass)
245 newName := tools.RandomString("ACPTTEST", 16)
Ash Wilson89734d02014-09-25 13:50:08 -0400246 rebuilt, err := servers.Rebuild(client, server.ID, newName, newPassword, choices.ImageID, nil).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400247 if err != nil {
248 t.Fatal(err)
249 }
250
Ash Wilsonfd566482014-09-23 15:47:35 -0400251 if rebuilt.ID != server.ID {
252 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
253 }
254
255 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
256 t.Fatal(err)
257 }
258
259 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
260 t.Fatal(err)
261 }
262}
263
Ash Wilsondc7daa82014-09-23 16:34:42 -0400264func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
265 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400266 t.Fatal(err)
267 }
268
269 t.Logf("Attempting to resize server [%s]", server.ID)
270
Ash Wilsondc7daa82014-09-23 16:34:42 -0400271 if err := servers.Resize(client, server.ID, choices.FlavorIDResize); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400272 t.Fatal(err)
273 }
274
Ash Wilsondc7daa82014-09-23 16:34:42 -0400275 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400276 t.Fatal(err)
277 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400278}
279
280func TestActionResizeConfirm(t *testing.T) {
281 t.Parallel()
282
Ash Wilsondc7daa82014-09-23 16:34:42 -0400283 choices, err := ComputeChoicesFromEnv()
284 if err != nil {
285 t.Fatal(err)
286 }
287
Ash Wilsonfd566482014-09-23 15:47:35 -0400288 client, err := newClient()
289 if err != nil {
290 t.Fatalf("Unable to create a compute client: %v", err)
291 }
292
Ash Wilsondc7daa82014-09-23 16:34:42 -0400293 server, err := createServer(t, client, choices)
294 if err != nil {
295 t.Fatal(err)
296 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400297 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400298 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400299
300 t.Logf("Attempting to confirm resize for server %s", server.ID)
301
302 if err = servers.ConfirmResize(client, server.ID); err != nil {
303 t.Fatal(err)
304 }
305
306 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
307 t.Fatal(err)
308 }
309}
310
311func TestActionResizeRevert(t *testing.T) {
312 t.Parallel()
313
Ash Wilsondc7daa82014-09-23 16:34:42 -0400314 choices, err := ComputeChoicesFromEnv()
315 if err != nil {
316 t.Fatal(err)
317 }
318
Ash Wilsonfd566482014-09-23 15:47:35 -0400319 client, err := newClient()
320 if err != nil {
321 t.Fatalf("Unable to create a compute client: %v", err)
322 }
323
Ash Wilsondc7daa82014-09-23 16:34:42 -0400324 server, err := createServer(t, client, choices)
325 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400326 t.Fatal(err)
327 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400328 defer servers.Delete(client, server.ID)
329 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400330
331 t.Logf("Attempting to revert resize for server %s", server.ID)
332
333 if err := servers.RevertResize(client, server.ID); err != nil {
334 t.Fatal(err)
335 }
336
Ash Wilsonfd566482014-09-23 15:47:35 -0400337 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
338 t.Fatal(err)
339 }
340}