blob: 047d763fcc7249dcd50d53112854794f3c9d629a [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
Jamie Hannaford5bfa2e82014-10-16 16:32:42 +0200242 rebuildOpts := servers.RebuildOpts{
243 Name: tools.RandomString("ACPTTEST", 16),
244 AdminPass: tools.MakeNewPassword(server.AdminPass),
245 ImageID: choices.ImageID,
246 }
247
248 rebuilt, err := servers.Rebuild(client, server.ID, rebuildOpts).Extract()
Ash Wilsonfd566482014-09-23 15:47:35 -0400249 if err != nil {
250 t.Fatal(err)
251 }
252
Ash Wilsonfd566482014-09-23 15:47:35 -0400253 if rebuilt.ID != server.ID {
254 t.Errorf("Expected rebuilt server ID of [%s]; got [%s]", server.ID, rebuilt.ID)
255 }
256
257 if err = waitForStatus(client, rebuilt, "REBUILD"); err != nil {
258 t.Fatal(err)
259 }
260
261 if err = waitForStatus(client, rebuilt, "ACTIVE"); err != nil {
262 t.Fatal(err)
263 }
264}
265
Ash Wilsondc7daa82014-09-23 16:34:42 -0400266func resizeServer(t *testing.T, client *gophercloud.ServiceClient, server *servers.Server, choices *ComputeChoices) {
267 if err := waitForStatus(client, server, "ACTIVE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400268 t.Fatal(err)
269 }
270
271 t.Logf("Attempting to resize server [%s]", server.ID)
272
Ash Wilsondc7daa82014-09-23 16:34:42 -0400273 if err := servers.Resize(client, server.ID, choices.FlavorIDResize); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400274 t.Fatal(err)
275 }
276
Ash Wilsondc7daa82014-09-23 16:34:42 -0400277 if err := waitForStatus(client, server, "VERIFY_RESIZE"); err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400278 t.Fatal(err)
279 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400280}
281
282func TestActionResizeConfirm(t *testing.T) {
283 t.Parallel()
284
Ash Wilsondc7daa82014-09-23 16:34:42 -0400285 choices, err := ComputeChoicesFromEnv()
286 if err != nil {
287 t.Fatal(err)
288 }
289
Ash Wilsonfd566482014-09-23 15:47:35 -0400290 client, err := newClient()
291 if err != nil {
292 t.Fatalf("Unable to create a compute client: %v", err)
293 }
294
Ash Wilsondc7daa82014-09-23 16:34:42 -0400295 server, err := createServer(t, client, choices)
296 if err != nil {
297 t.Fatal(err)
298 }
Ash Wilsonfd566482014-09-23 15:47:35 -0400299 defer servers.Delete(client, server.ID)
Ash Wilsondc7daa82014-09-23 16:34:42 -0400300 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400301
302 t.Logf("Attempting to confirm resize for server %s", server.ID)
303
304 if err = servers.ConfirmResize(client, server.ID); err != nil {
305 t.Fatal(err)
306 }
307
308 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
309 t.Fatal(err)
310 }
311}
312
313func TestActionResizeRevert(t *testing.T) {
314 t.Parallel()
315
Ash Wilsondc7daa82014-09-23 16:34:42 -0400316 choices, err := ComputeChoicesFromEnv()
317 if err != nil {
318 t.Fatal(err)
319 }
320
Ash Wilsonfd566482014-09-23 15:47:35 -0400321 client, err := newClient()
322 if err != nil {
323 t.Fatalf("Unable to create a compute client: %v", err)
324 }
325
Ash Wilsondc7daa82014-09-23 16:34:42 -0400326 server, err := createServer(t, client, choices)
327 if err != nil {
Ash Wilsonfd566482014-09-23 15:47:35 -0400328 t.Fatal(err)
329 }
Ash Wilsondc7daa82014-09-23 16:34:42 -0400330 defer servers.Delete(client, server.ID)
331 resizeServer(t, client, server, choices)
Ash Wilsonfd566482014-09-23 15:47:35 -0400332
333 t.Logf("Attempting to revert resize for server %s", server.ID)
334
335 if err := servers.RevertResize(client, server.ID); err != nil {
336 t.Fatal(err)
337 }
338
Ash Wilsonfd566482014-09-23 15:47:35 -0400339 if err = waitForStatus(client, server, "ACTIVE"); err != nil {
340 t.Fatal(err)
341 }
342}