blob: 2310858b3fd2dab14e46a3b9cd07ba2550249f4a [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 IIf370dc72014-02-13 15:05:34 -08007 "os"
Samuel A. Falvo IIe246ac02014-02-13 23:20:09 -08008 "testing"
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +02009
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070010 "github.com/rackspace/gophercloud/acceptance/tools"
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +020011 "github.com/rackspace/gophercloud/openstack/compute/v2/flavors"
12 "github.com/rackspace/gophercloud/openstack/compute/v2/images"
13 "github.com/rackspace/gophercloud/openstack/compute/v2/servers"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080014)
15
Jon Perritt816d2a02014-03-11 20:49:46 -050016var service = "compute"
17
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080018func TestListServers(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070019 ts, err := tools.SetupForList(service)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080020 if err != nil {
21 t.Error(err)
22 return
23 }
24
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070025 fmt.Fprintln(ts.W, "ID\tRegion\tName\tStatus\tIPv4\tIPv6\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080026
27 region := os.Getenv("OS_REGION_NAME")
28 n := 0
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070029 for _, ep := range ts.EPs {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080030 if (region != "") && (region != ep.Region) {
31 continue
32 }
33
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070034 client := servers.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080035
36 listResults, err := servers.List(client)
37 if err != nil {
38 t.Error(err)
39 return
40 }
41
42 svrs, err := servers.GetServers(listResults)
43 if err != nil {
44 t.Error(err)
45 return
46 }
47
48 n = n + len(svrs)
49
50 for _, s := range svrs {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070051 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 -080052 }
53 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070054 ts.W.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080055 fmt.Printf("--------\n%d servers listed.\n", n)
56}
57
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080058func TestListImages(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070059 ts, err := tools.SetupForList(service)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080060 if err != nil {
61 t.Error(err)
62 return
63 }
64
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070065 fmt.Fprintln(ts.W, "ID\tRegion\tName\tStatus\tCreated\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080066
67 region := os.Getenv("OS_REGION_NAME")
68 n := 0
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070069 for _, ep := range ts.EPs {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080070 if (region != "") && (region != ep.Region) {
71 continue
72 }
73
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070074 client := images.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080075
76 listResults, err := images.List(client)
77 if err != nil {
78 t.Error(err)
79 return
80 }
81
82 imgs, err := images.GetImages(listResults)
83 if err != nil {
84 t.Error(err)
85 return
86 }
87
88 n = n + len(imgs)
89
90 for _, i := range imgs {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070091 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 -080092 }
93 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070094 ts.W.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080095 fmt.Printf("--------\n%d images listed.\n", n)
96}
97
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080098func TestListFlavors(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -070099 ts, err := tools.SetupForList(service)
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800100 if err != nil {
101 t.Error(err)
102 return
103 }
104
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700105 fmt.Fprintln(ts.W, "ID\tRegion\tName\tRAM\tDisk\tVCPUs\t")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800106
107 region := os.Getenv("OS_REGION_NAME")
108 n := 0
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700109 for _, ep := range ts.EPs {
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800110 if (region != "") && (region != ep.Region) {
111 continue
112 }
113
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700114 client := flavors.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800115
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700116 listResults, err := flavors.List(client, flavors.ListFilterOptions{})
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800117 if err != nil {
118 t.Error(err)
119 return
120 }
121
122 flavs, err := flavors.GetFlavors(listResults)
123 if err != nil {
124 t.Error(err)
125 return
126 }
127
128 n = n + len(flavs)
129
130 for _, f := range flavs {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700131 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 -0800132 }
133 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700134 ts.W.Flush()
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700135 fmt.Printf("--------\n%d flavors listed.\n", n)
136}
137
138func TestGetFlavor(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700139 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700140 if err != nil {
141 t.Fatal(err)
142 }
143
144 region := os.Getenv("OS_REGION_NAME")
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700145 for _, ep := range ts.EPs {
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700146 if (region != "") && (region != ep.Region) {
147 continue
148 }
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700149 client := flavors.NewClient(ep.PublicURL, ts.A, ts.O)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700150
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700151 getResults, err := flavors.Get(client, ts.FlavorId)
Samuel A. Falvo II38c6ad02014-05-06 18:09:46 -0700152 if err != nil {
153 t.Fatal(err)
154 }
155 flav, err := flavors.GetFlavor(getResults)
156 if err != nil {
157 t.Fatal(err)
158 }
159 fmt.Printf("%#v\n", flav)
160 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800161}
162
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800163func TestCreateDestroyServer(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700164 ts, err := tools.SetupForCRUD()
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800165 if err != nil {
166 t.Error(err)
167 return
168 }
169
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700170 err = tools.CreateServer(ts)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800171 if err != nil {
172 t.Error(err)
173 return
174 }
175
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800176 // 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 -0800177 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700178 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800179 }()
180
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700181 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800182 if err != nil {
183 t.Error(err)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800184 }
185}
186
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800187func TestUpdateServer(t *testing.T) {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700188 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800189 if err != nil {
190 t.Error(err)
191 return
192 }
193
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700194 err = tools.CreateServer(ts)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800195 if err != nil {
196 t.Error(err)
197 return
198 }
199
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800200 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700201 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800202 }()
203
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700204 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800205 if err != nil {
206 t.Error(err)
207 return
208 }
209
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700210 err = tools.ChangeServerName(ts)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800211 if err != nil {
212 t.Error(err)
213 return
214 }
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800215}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700216
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700217func TestActionChangeAdminPassword(t *testing.T) {
218 t.Parallel()
219
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700220 ts, err := tools.SetupForCRUD()
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700221 if err != nil {
222 t.Fatal(err)
223 }
224
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700225 err = tools.CreateServer(ts)
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700226 if err != nil {
227 t.Fatal(err)
228 }
229
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200230 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700231 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700232 }()
233
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700234 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700235 if err != nil {
236 t.Fatal(err)
237 }
238
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700239 err = tools.ChangeAdminPassword(ts)
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700240 if err != nil {
241 t.Fatal(err)
242 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700243}
244
245func TestActionReboot(t *testing.T) {
246 t.Parallel()
247
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700248 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700249 if err != nil {
250 t.Fatal(err)
251 }
252
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700253 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700254 if err != nil {
255 t.Fatal(err)
256 }
257
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200258 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700259 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700260 }()
261
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700262 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700263 if err != nil {
264 t.Fatal(err)
265 }
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700266
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700267 err = servers.Reboot(ts.Client, ts.CreatedServer.Id, "aldhjflaskhjf")
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700268 if err == nil {
269 t.Fatal("Expected the SDK to provide an ArgumentError here")
270 }
271
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700272 err = tools.RebootServer(ts)
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700273 if err != nil {
274 t.Fatal(err)
275 }
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700276}
277
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700278func TestActionRebuild(t *testing.T) {
279 t.Parallel()
280
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700281 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700282 if err != nil {
283 t.Fatal(err)
284 }
285
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700286 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700287 if err != nil {
288 t.Fatal(err)
289 }
290
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200291 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700292 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700293 }()
294
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700295 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700296 if err != nil {
297 t.Fatal(err)
298 }
299
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700300 err = tools.RebuildServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700301 if err != nil {
302 t.Fatal(err)
303 }
304}
305
306func TestActionResizeConfirm(t *testing.T) {
307 t.Parallel()
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200308
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700309 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700310 if err != nil {
311 t.Fatal(err)
312 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200313
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700314 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700315 if err != nil {
316 t.Fatal(err)
317 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200318
319 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700320 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700321 }()
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200322
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700323 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700324 if err != nil {
325 t.Fatal(err)
326 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200327
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700328 err = tools.ResizeServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700329 if err != nil {
330 t.Fatal(err)
331 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200332
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700333 err = tools.ConfirmResize(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700334 if err != nil {
335 t.Fatal(err)
336 }
337}
338
339func TestActionResizeRevert(t *testing.T) {
340 t.Parallel()
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200341
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700342 ts, err := tools.SetupForCRUD()
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700343 if err != nil {
344 t.Fatal(err)
345 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200346
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700347 err = tools.CreateServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700348 if err != nil {
349 t.Fatal(err)
350 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200351
352 defer func() {
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700353 servers.Delete(ts.Client, ts.CreatedServer.Id)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700354 }()
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200355
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700356 err = tools.WaitForStatus(ts, "ACTIVE")
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700357 if err != nil {
358 t.Fatal(err)
359 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200360
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700361 err = tools.ResizeServer(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700362 if err != nil {
363 t.Fatal(err)
364 }
Jamie Hannafordc8fc6ea2014-09-10 13:59:58 +0200365
Samuel A. Falvo II43d83532014-07-31 14:34:48 -0700366 err = tools.RevertResize(ts)
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700367 if err != nil {
368 t.Fatal(err)
369 }
370}