blob: 7a478e02cd83ce7cc9f71fad83bfb3ba6c7314c2 [file] [log] [blame]
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08001package openstack
2
3import (
4 "testing"
5 "fmt"
6 "github.com/rackspace/gophercloud/openstack/compute/servers"
7 "github.com/rackspace/gophercloud/openstack/compute/images"
Samuel A. Falvo II10decf92014-02-13 17:05:35 -08008 "github.com/rackspace/gophercloud/openstack/compute/flavors"
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -08009 "github.com/rackspace/gophercloud/openstack/identity"
10 "github.com/rackspace/gophercloud/openstack/utils"
11 "os"
12 "text/tabwriter"
13)
14
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080015type testState struct {
16 o identity.AuthOptions
17 a identity.AuthResults
18 sc *identity.ServiceCatalog
19 eps []identity.Endpoint
20 w *tabwriter.Writer
21}
22
23func prepForTest() (*testState, error) {
24 var err error
25
26 ts := new(testState)
27
28 ts.o, err = utils.AuthOptions()
29 if err != nil {
30 return ts, err
31 }
32
33 ts.a, err = identity.Authenticate(ts.o)
34 if err != nil {
35 return ts, err
36 }
37
38 ts.sc, err = identity.GetServiceCatalog(ts.a)
39 if err != nil {
40 return ts, err
41 }
42
43 ts.eps, err = findAllComputeEndpoints(ts.sc)
44 if err != nil {
45 return ts, err
46 }
47
48 ts.w = new(tabwriter.Writer)
49 ts.w.Init(os.Stdout, 2, 8, 2, ' ', 0)
50
51 return ts, nil
52}
53
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080054func TestListServers(t *testing.T) {
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080055 ts, err := prepForTest()
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\tIPv4\tIPv6\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 := servers.NewClient(ep.PublicURL, ts.a, ts.o)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080071
72 listResults, err := servers.List(client)
73 if err != nil {
74 t.Error(err)
75 return
76 }
77
78 svrs, err := servers.GetServers(listResults)
79 if err != nil {
80 t.Error(err)
81 return
82 }
83
84 n = n + len(svrs)
85
86 for _, s := range svrs {
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", s.Id, s.Name, ep.Region, s.AccessIPv4, s.AccessIPv6)
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 servers listed.\n", n)
92}
93
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080094func TestListImages(t *testing.T) {
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -080095 ts, err := prepForTest()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -080096 if err != nil {
97 t.Error(err)
98 return
99 }
100
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800101 fmt.Fprintln(ts.w, "ID\tRegion\tName\tStatus\tCreated\t")
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -0800102
103 region := os.Getenv("OS_REGION_NAME")
104 n := 0
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800105 for _, ep := range ts.eps {
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -0800106 if (region != "") && (region != ep.Region) {
107 continue
108 }
109
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800110 client := images.NewClient(ep.PublicURL, ts.a, ts.o)
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -0800111
112 listResults, err := images.List(client)
113 if err != nil {
114 t.Error(err)
115 return
116 }
117
118 imgs, err := images.GetImages(listResults)
119 if err != nil {
120 t.Error(err)
121 return
122 }
123
124 n = n + len(imgs)
125
126 for _, i := range imgs {
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800127 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 -0800128 }
129 }
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800130 ts.w.Flush()
Samuel A. Falvo IIf370dc72014-02-13 15:05:34 -0800131 fmt.Printf("--------\n%d images listed.\n", n)
132}
133
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800134func TestListFlavors(t *testing.T) {
135 ts, err := prepForTest()
136 if err != nil {
137 t.Error(err)
138 return
139 }
140
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800141 fmt.Fprintln(ts.w, "ID\tRegion\tName\tRAM\tDisk\tVCPUs\t")
Samuel A. Falvo IIdb020882014-02-13 15:37:57 -0800142
143 region := os.Getenv("OS_REGION_NAME")
144 n := 0
145 for _, ep := range ts.eps {
146 if (region != "") && (region != ep.Region) {
147 continue
148 }
149
150 client := flavors.NewClient(ep.PublicURL, ts.a, ts.o)
151
152 listResults, err := flavors.List(client)
153 if err != nil {
154 t.Error(err)
155 return
156 }
157
158 flavs, err := flavors.GetFlavors(listResults)
159 if err != nil {
160 t.Error(err)
161 return
162 }
163
164 n = n + len(flavs)
165
166 for _, f := range flavs {
Samuel A. Falvo II10decf92014-02-13 17:05:35 -0800167 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 -0800168 }
169 }
170 ts.w.Flush()
171 fmt.Printf("--------\n%d images listed.\n", n)
172}
173
174func findAllComputeEndpoints(sc *identity.ServiceCatalog) ([]identity.Endpoint, error) {
175 ces, err := sc.CatalogEntries()
176 if err != nil {
177 return nil, err
178 }
179
180 for _, ce := range ces {
181 if ce.Type == "compute" {
182 return ce.Endpoints, nil
183 }
184 }
185
186 return nil, fmt.Errorf("Compute endpoint not found.")
187}
188