blob: 1cc154ab62496f74dba97f5534e94553af701ff0 [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
188func TestServerAction(t *testing.T) {
189 ts, err := setupForCRUD()
190 if err != nil {
191 t.Fatal(err)
192 }
193
194 err = createServer(ts)
195 if err != nil {
196 t.Fatal(err)
197 }
198
199 defer func(){
200 servers.Delete(ts.client, ts.createdServer.Id)
201 }()
202
203 err = waitForStatus(ts, "ACTIVE")
204 if err != nil {
205 t.Fatal(err)
206 }
207
208 err = changeAdminPassword(ts)
209 if err != nil {
210 t.Fatal(err)
211 }
Samuel A. Falvo II41c9f612014-03-11 19:00:10 -0700212
213 err = servers.Reboot(ts.client, ts.createdServer.Id, "aldhjflaskhjf")
214 if err == nil {
215 t.Fatal("Expected the SDK to provide an ArgumentError here")
216 }
217
218 err = rebootServer(ts)
219 if err != nil {
220 t.Fatal(err)
221 }
Samuel A. Falvo IIca5f9a32014-03-11 17:52:58 -0700222}
223