blob: 7473f1398c54a3bfd71a2f8a2b6c7a0b7c7ae04f [file] [log] [blame]
Samuel A. Falvo IIb5d93f22014-02-21 15:00:20 -08001// +build acceptance
2
Samuel A. Falvo II43d83532014-07-31 14:34:48 -07003package compute
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08004
5import (
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08006 "fmt"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08007 "github.com/rackspace/gophercloud/openstack/compute/flavors"
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -08008 "github.com/rackspace/gophercloud/openstack/compute/images"
9 "github.com/rackspace/gophercloud/openstack/compute/servers"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080010 "os"
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -080011 "testing"
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070012 "github.com/rackspace/gophercloud/acceptance/tools"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080013)
14
Jon Perritt816d2a02014-03-11 20:49:46 -050015var service = "compute"
16
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080017func TestListServers(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070018 ts, err := tools.SetupForList(service)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080019 if err != nil {
20 t.Error(err)
21 return
22 }
23
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070024 fmt.Fprintln(ts.W, "ID\tRegion\tName\tStatus\tIPv4\tIPv6\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080025
26 region := os.Getenv("OS_REGION_NAME")
27 n := 0
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070028 for _, ep := range ts.EPs {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080029 if (region != "") && (region != ep.Region) {
30 continue
31 }
32
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070033 client := servers.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080034
35 listResults, err := servers.List(client)
36 if err != nil {
37 t.Error(err)
38 return
39 }
40
41 svrs, err := servers.GetServers(listResults)
42 if err != nil {
43 t.Error(err)
44 return
45 }
46
47 n = n + len(svrs)
48
49 for _, s := range svrs {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070050 fmt.Fprintf(ts.W, "%s\t%s\t%s\t%s\t%s\t%s\t\n", s.Id, s.Name, ep.Region, s.Status, s.AccessIPv4, s.AccessIPv6)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080051 }
52 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070053 ts.W.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080054 fmt.Printf("--------\n%d servers listed.\n", n)
55}
56
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080057func TestListImages(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070058 ts, err := tools.SetupForList(service)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080059 if err != nil {
60 t.Error(err)
61 return
62 }
63
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070064 fmt.Fprintln(ts.W, "ID\tRegion\tName\tStatus\tCreated\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080065
66 region := os.Getenv("OS_REGION_NAME")
67 n := 0
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070068 for _, ep := range ts.EPs {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080069 if (region != "") && (region != ep.Region) {
70 continue
71 }
72
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070073 client := images.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080074
75 listResults, err := images.List(client)
76 if err != nil {
77 t.Error(err)
78 return
79 }
80
81 imgs, err := images.GetImages(listResults)
82 if err != nil {
83 t.Error(err)
84 return
85 }
86
87 n = n + len(imgs)
88
89 for _, i := range imgs {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070090 fmt.Fprintf(ts.W, "%s\t%s\t%s\t%s\t%s\t\n", i.Id, ep.Region, i.Name, i.Status, i.Created)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080091 }
92 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070093 ts.W.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080094 fmt.Printf("--------\n%d images listed.\n", n)
95}
96
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080097func TestListFlavors(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070098 ts, err := tools.SetupForList(service)
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080099 if err != nil {
100 t.Error(err)
101 return
102 }
103
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700104 fmt.Fprintln(ts.W, "ID\tRegion\tName\tRAM\tDisk\tVCPUs\t")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800105
106 region := os.Getenv("OS_REGION_NAME")
107 n := 0
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700108 for _, ep := range ts.EPs {
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800109 if (region != "") && (region != ep.Region) {
110 continue
111 }
112
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700113 client := flavors.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800114
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700115 listResults, err := flavors.List(client, flavors.ListFilterOptions{})
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800116 if err != nil {
117 t.Error(err)
118 return
119 }
120
121 flavs, err := flavors.GetFlavors(listResults)
122 if err != nil {
123 t.Error(err)
124 return
125 }
126
127 n = n + len(flavs)
128
129 for _, f := range flavs {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700130 fmt.Fprintf(ts.W, "%s\t%s\t%s\t%d\t%d\t%d\t\n", f.Id, ep.Region, f.Name, f.Ram, f.Disk, f.VCpus)
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800131 }
132 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700133 ts.W.Flush()
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700134 fmt.Printf("--------\n%d flavors listed.\n", n)
135}
136
137func TestGetFlavor(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700138 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700139 if err != nil {
140 t.Fatal(err)
141 }
142
143 region := os.Getenv("OS_REGION_NAME")
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700144 for _, ep := range ts.EPs {
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700145 if (region != "") && (region != ep.Region) {
146 continue
147 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700148 client := flavors.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700149
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700150 getResults, err := flavors.Get(client, ts.FlavorId)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700151 if err != nil {
152 t.Fatal(err)
153 }
154 flav, err := flavors.GetFlavor(getResults)
155 if err != nil {
156 t.Fatal(err)
157 }
158 fmt.Printf("%#v\n", flav)
159 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800160}
161
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800162func TestCreateDestroyServer(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700163 ts, err := tools.SetupForCRUD()
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800164 if err != nil {
165 t.Error(err)
166 return
167 }
168
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700169 err = tools.CreateServer(ts)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800170 if err != nil {
171 t.Error(err)
172 return
173 }
174
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800175 // We put this in a defer so that it gets executed even in the face of errors or panics.
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800176 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700177 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800178 }()
179
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700180 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800181 if err != nil {
182 t.Error(err)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800183 }
184}
185
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800186func TestUpdateServer(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700187 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800188 if err != nil {
189 t.Error(err)
190 return
191 }
192
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700193 err = tools.CreateServer(ts)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800194 if err != nil {
195 t.Error(err)
196 return
197 }
198
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800199 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700200 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800201 }()
202
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700203 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800204 if err != nil {
205 t.Error(err)
206 return
207 }
208
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700209 err = tools.ChangeServerName(ts)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800210 if err != nil {
211 t.Error(err)
212 return
213 }
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800214}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700215
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700216func TestActionChangeAdminPassword(t *testing.T) {
217 t.Parallel()
218
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700219 ts, err := tools.SetupForCRUD()
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700220 if err != nil {
221 t.Fatal(err)
222 }
223
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700224 err = tools.CreateServer(ts)
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700225 if err != nil {
226 t.Fatal(err)
227 }
228
229 defer func(){
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700230 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700231 }()
232
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700233 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700234 if err != nil {
235 t.Fatal(err)
236 }
237
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700238 err = tools.ChangeAdminPassword(ts)
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700239 if err != nil {
240 t.Fatal(err)
241 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700242}
243
244func TestActionReboot(t *testing.T) {
245 t.Parallel()
246
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700247 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700248 if err != nil {
249 t.Fatal(err)
250 }
251
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700252 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700253 if err != nil {
254 t.Fatal(err)
255 }
256
257 defer func(){
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700258 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700259 }()
260
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700261 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700262 if err != nil {
263 t.Fatal(err)
264 }
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700265
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700266 err = servers.Reboot(ts.Client, ts.CreatedServer.Id, "aldhjflaskhjf")
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700267 if err == nil {
268 t.Fatal("Expected the SDK to provide an ArgumentError here")
269 }
270
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700271 err = tools.RebootServer(ts)
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700272 if err != nil {
273 t.Fatal(err)
274 }
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700275}
276
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700277func TestActionRebuild(t *testing.T) {
278 t.Parallel()
279
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700280 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700281 if err != nil {
282 t.Fatal(err)
283 }
284
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700285 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700286 if err != nil {
287 t.Fatal(err)
288 }
289
290 defer func(){
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700291 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700292 }()
293
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700294 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700295 if err != nil {
296 t.Fatal(err)
297 }
298
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700299 err = tools.RebuildServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700300 if err != nil {
301 t.Fatal(err)
302 }
303}
304
305func TestActionResizeConfirm(t *testing.T) {
306 t.Parallel()
307
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700308 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700309 if err != nil {
310 t.Fatal(err)
311 }
312
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700313 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700314 if err != nil {
315 t.Fatal(err)
316 }
317
318 defer func(){
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700319 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700320 }()
321
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700322 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700323 if err != nil {
324 t.Fatal(err)
325 }
326
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700327 err = tools.ResizeServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700328 if err != nil {
329 t.Fatal(err)
330 }
331
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700332 err = tools.ConfirmResize(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700333 if err != nil {
334 t.Fatal(err)
335 }
336}
337
338func TestActionResizeRevert(t *testing.T) {
339 t.Parallel()
340
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700341 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700342 if err != nil {
343 t.Fatal(err)
344 }
345
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700346 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700347 if err != nil {
348 t.Fatal(err)
349 }
350
351 defer func(){
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700352 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700353 }()
354
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700355 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700356 if err != nil {
357 t.Fatal(err)
358 }
359
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700360 err = tools.ResizeServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700361 if err != nil {
362 t.Fatal(err)
363 }
364
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700365 err = tools.RevertResize(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700366 if err != nil {
367 t.Fatal(err)
368 }
369}