blob: 0a99d0554d95aabc39d0ca1d98949b2191b212da [file] [log] [blame]
Samuel A. Falvo IIb5d93f22014-02-21 15:00:20 -08001// +build acceptance
2
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08003package openstack
4
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 IIf370dc72014-02-13 15:05:34 -080012)
13
14func TestListServers(t *testing.T) {
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080015 ts, err := setupForList()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080016 if err != nil {
17 t.Error(err)
18 return
19 }
20
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080021 fmt.Fprintln(ts.w, "ID\tRegion\tName\tStatus\tIPv4\tIPv6\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080022
23 region := os.Getenv("OS_REGION_NAME")
24 n := 0
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080025 for _, ep := range ts.eps {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080026 if (region != "") && (region != ep.Region) {
27 continue
28 }
29
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080030 client := servers.NewClient(ep.PublicURL, ts.a, ts.o)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080031
32 listResults, err := servers.List(client)
33 if err != nil {
34 t.Error(err)
35 return
36 }
37
38 svrs, err := servers.GetServers(listResults)
39 if err != nil {
40 t.Error(err)
41 return
42 }
43
44 n = n + len(svrs)
45
46 for _, s := range svrs {
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080047 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 -080048 }
49 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080050 ts.w.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080051 fmt.Printf("--------\n%d servers listed.\n", n)
52}
53
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080054func TestListImages(t *testing.T) {
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080055 ts, err := setupForList()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080056 if err != nil {
57 t.Error(err)
58 return
59 }
60
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080061 fmt.Fprintln(ts.w, "ID\tRegion\tName\tStatus\tCreated\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080062
63 region := os.Getenv("OS_REGION_NAME")
64 n := 0
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080065 for _, ep := range ts.eps {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080066 if (region != "") && (region != ep.Region) {
67 continue
68 }
69
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080070 client := images.NewClient(ep.PublicURL, ts.a, ts.o)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080071
72 listResults, err := images.List(client)
73 if err != nil {
74 t.Error(err)
75 return
76 }
77
78 imgs, err := images.GetImages(listResults)
79 if err != nil {
80 t.Error(err)
81 return
82 }
83
84 n = n + len(imgs)
85
86 for _, i := range imgs {
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080087 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 -080088 }
89 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080090 ts.w.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080091 fmt.Printf("--------\n%d images listed.\n", n)
92}
93
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080094func TestListFlavors(t *testing.T) {
Samuel A. Falvo IIce000732014-02-13 18:53:53 -080095 ts, err := setupForList()
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080096 if err != nil {
97 t.Error(err)
98 return
99 }
100
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800101 fmt.Fprintln(ts.w, "ID\tRegion\tName\tRAM\tDisk\tVCPUs\t")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800102
103 region := os.Getenv("OS_REGION_NAME")
104 n := 0
105 for _, ep := range ts.eps {
106 if (region != "") && (region != ep.Region) {
107 continue
108 }
109
110 client := flavors.NewClient(ep.PublicURL, ts.a, ts.o)
111
112 listResults, err := flavors.List(client)
113 if err != nil {
114 t.Error(err)
115 return
116 }
117
118 flavs, err := flavors.GetFlavors(listResults)
119 if err != nil {
120 t.Error(err)
121 return
122 }
123
124 n = n + len(flavs)
125
126 for _, f := range flavs {
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800127 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 -0800128 }
129 }
130 ts.w.Flush()
131 fmt.Printf("--------\n%d images listed.\n", n)
132}
133
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800134func TestCreateDestroyServer(t *testing.T) {
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800135 ts, err := setupForCRUD()
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800136 if err != nil {
137 t.Error(err)
138 return
139 }
140
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800141 err = createServer(ts)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800142 if err != nil {
143 t.Error(err)
144 return
145 }
146
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800147 // 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 -0800148 defer func() {
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800149 servers.Delete(ts.client, ts.createdServer.Id)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800150 }()
151
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800152 err = waitForStatus(ts, "ACTIVE")
153 if err != nil {
154 t.Error(err)
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800155 }
156}
157
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800158func TestUpdateServer(t *testing.T) {
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800159 ts, err := setupForCRUD()
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800160 if err != nil {
161 t.Error(err)
162 return
163 }
164
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800165 err = createServer(ts)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800166 if err != nil {
167 t.Error(err)
168 return
169 }
170
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800171 defer func() {
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800172 servers.Delete(ts.client, ts.createdServer.Id)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800173 }()
174
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800175 err = waitForStatus(ts, "ACTIVE")
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800176 if err != nil {
177 t.Error(err)
178 return
179 }
180
Samuel A. Falvo II0abdb102014-02-13 23:19:26 -0800181 err = changeServerName(ts)
Samuel A. Falvo II22d3b772014-02-13 19:27:05 -0800182 if err != nil {
183 t.Error(err)
184 return
185 }
Samuel A. Falvo IIce000732014-02-13 18:53:53 -0800186}
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700187
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700188func TestActionChangeAdminPassword(t *testing.T) {
189 t.Parallel()
190
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700191 ts, err := setupForCRUD()
192 if err != nil {
193 t.Fatal(err)
194 }
195
196 err = createServer(ts)
197 if err != nil {
198 t.Fatal(err)
199 }
200
201 defer func(){
202 servers.Delete(ts.client, ts.createdServer.Id)
203 }()
204
205 err = waitForStatus(ts, "ACTIVE")
206 if err != nil {
207 t.Fatal(err)
208 }
209
210 err = changeAdminPassword(ts)
211 if err != nil {
212 t.Fatal(err)
213 }
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700214}
215
216func TestActionReboot(t *testing.T) {
217 t.Parallel()
218
219 ts, err := setupForCRUD()
220 if err != nil {
221 t.Fatal(err)
222 }
223
224 err = createServer(ts)
225 if err != nil {
226 t.Fatal(err)
227 }
228
229 defer func(){
230 servers.Delete(ts.client, ts.createdServer.Id)
231 }()
232
233 err = waitForStatus(ts, "ACTIVE")
234 if err != nil {
235 t.Fatal(err)
236 }
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700237
238 err = servers.Reboot(ts.client, ts.createdServer.Id, "aldhjflaskhjf")
239 if err == nil {
240 t.Fatal("Expected the SDK to provide an ArgumentError here")
241 }
242
243 err = rebootServer(ts)
244 if err != nil {
245 t.Fatal(err)
246 }
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700247}
248
Samuel A. Falvo II808bb632014-03-12 00:07:50 -0700249func TestActionRebuild(t *testing.T) {
250 t.Parallel()
251
252 ts, err := setupForCRUD()
253 if err != nil {
254 t.Fatal(err)
255 }
256
257 err = createServer(ts)
258 if err != nil {
259 t.Fatal(err)
260 }
261
262 defer func(){
263 servers.Delete(ts.client, ts.createdServer.Id)
264 }()
265
266 err = waitForStatus(ts, "ACTIVE")
267 if err != nil {
268 t.Fatal(err)
269 }
270
271 err = rebuildServer(ts)
272 if err != nil {
273 t.Fatal(err)
274 }
275}
276
277func TestActionResizeConfirm(t *testing.T) {
278 t.Parallel()
279
280 ts, err := setupForCRUD()
281 if err != nil {
282 t.Fatal(err)
283 }
284
285 err = createServer(ts)
286 if err != nil {
287 t.Fatal(err)
288 }
289
290 defer func(){
291 servers.Delete(ts.client, ts.createdServer.Id)
292 }()
293
294 err = waitForStatus(ts, "ACTIVE")
295 if err != nil {
296 t.Fatal(err)
297 }
298
299 err = resizeServer(ts)
300 if err != nil {
301 t.Fatal(err)
302 }
303
304 err = confirmResize(ts)
305 if err != nil {
306 t.Fatal(err)
307 }
308}
309
310func TestActionResizeRevert(t *testing.T) {
311 t.Parallel()
312
313 ts, err := setupForCRUD()
314 if err != nil {
315 t.Fatal(err)
316 }
317
318 err = createServer(ts)
319 if err != nil {
320 t.Fatal(err)
321 }
322
323 defer func(){
324 servers.Delete(ts.client, ts.createdServer.Id)
325 }()
326
327 err = waitForStatus(ts, "ACTIVE")
328 if err != nil {
329 t.Fatal(err)
330 }
331
332 err = resizeServer(ts)
333 if err != nil {
334 t.Fatal(err)
335 }
336
337 err = revertResize(ts)
338 if err != nil {
339 t.Fatal(err)
340 }
341}