blob: b0de950b53cf6e513c43c24421248b15de01c9bf [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 "github.com/rackspace/gophercloud"
9 "github.com/rackspace/gophercloud/pagination"
10 "github.com/rackspace/gophercloud/testhelper"
11)
12
13const tokenID = "bzbzbzbzbz"
14
15func serviceClient() *gophercloud.ServiceClient {
16 return &gophercloud.ServiceClient{
17 Provider: &gophercloud.ProviderClient{TokenID: tokenID},
18 Endpoint: testhelper.Endpoint(),
19 }
20}
21
22func TestListServers(t *testing.T) {
23 testhelper.SetupHTTP()
24 defer testhelper.TeardownHTTP()
25
26 testhelper.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
27 testhelper.TestMethod(t, r, "GET")
28 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
29
30 w.Header().Add("Content-Type", "application/json")
31 r.ParseForm()
32 marker := r.Form.Get("marker")
33 switch marker {
34 case "":
35 fmt.Fprintf(w, serverListBody)
36 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
37 fmt.Fprintf(w, `{ "servers": [] }`)
38 default:
39 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
40 }
41 })
42
43 client := serviceClient()
44 pages := 0
Jamie Hannafordcf001722014-10-16 12:54:07 +020045 err := List(client, ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040046 pages++
47
48 actual, err := ExtractServers(page)
49 if err != nil {
50 return false, err
51 }
52
53 if len(actual) != 2 {
54 t.Fatalf("Expected 2 servers, got %d", len(actual))
55 }
56 equalServers(t, serverHerp, actual[0])
57 equalServers(t, serverDerp, actual[1])
58
59 return true, nil
60 })
61
Jamie Hannafordcf001722014-10-16 12:54:07 +020062 testhelper.AssertNoErr(t, err)
63
Ash Wilsonad21c712014-09-25 10:15:22 -040064 if pages != 1 {
65 t.Errorf("Expected 1 page, saw %d", pages)
66 }
67}
68
69func TestCreateServer(t *testing.T) {
70 testhelper.SetupHTTP()
71 defer testhelper.TeardownHTTP()
Ash Wilson3204d0d2014-09-25 10:37:44 -040072
73 testhelper.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
74 testhelper.TestMethod(t, r, "POST")
75 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
76 testhelper.TestJSONRequest(t, r, `{
77 "server": {
78 "name": "derp",
79 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
80 "flavorRef": "1"
81 }
82 }`)
83
84 w.WriteHeader(http.StatusAccepted)
85 w.Header().Add("Content-Type", "application/json")
86 fmt.Fprintf(w, singleServerBody)
87 })
88
89 client := serviceClient()
Ash Wilson3a0e3b42014-10-02 10:58:09 -040090 actual, err := Create(client, CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040091 Name: "derp",
92 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
93 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040094 }).Extract()
Ash Wilson3204d0d2014-09-25 10:37:44 -040095 if err != nil {
96 t.Fatalf("Unexpected Create error: %v", err)
97 }
98
Ash Wilson3204d0d2014-09-25 10:37:44 -040099 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400100}
101
102func TestDeleteServer(t *testing.T) {
103 testhelper.SetupHTTP()
104 defer testhelper.TeardownHTTP()
Ash Wilsonaff36272014-09-25 10:40:05 -0400105
106 testhelper.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
107 testhelper.TestMethod(t, r, "DELETE")
108 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
109
110 w.WriteHeader(http.StatusNoContent)
111 })
112
113 client := serviceClient()
114 err := Delete(client, "asdfasdfasdf")
115 if err != nil {
116 t.Fatalf("Unexpected Delete error: %v", err)
117 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400118}
119
120func TestGetServer(t *testing.T) {
121 testhelper.SetupHTTP()
122 defer testhelper.TeardownHTTP()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400123
124 testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
125 testhelper.TestMethod(t, r, "GET")
126 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
127 testhelper.TestHeader(t, r, "Accept", "application/json")
128
129 fmt.Fprintf(w, singleServerBody)
130 })
131
132 client := serviceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400133 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400134 if err != nil {
135 t.Fatalf("Unexpected Get error: %v", err)
136 }
137
Ash Wilsona612f1f2014-09-25 10:42:40 -0400138 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400139}
140
141func TestUpdateServer(t *testing.T) {
142 testhelper.SetupHTTP()
143 defer testhelper.TeardownHTTP()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400144
145 testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
146 testhelper.TestMethod(t, r, "PUT")
147 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
148 testhelper.TestHeader(t, r, "Accept", "application/json")
149 testhelper.TestHeader(t, r, "Content-Type", "application/json")
150 testhelper.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
151
152 fmt.Fprintf(w, singleServerBody)
153 })
154
155 client := serviceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400156 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400157 if err != nil {
158 t.Fatalf("Unexpected Update error: %v", err)
159 }
160
Ash Wilson0aac3a82014-09-25 10:45:03 -0400161 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400162}
163
164func TestChangeServerAdminPassword(t *testing.T) {
165 testhelper.SetupHTTP()
166 defer testhelper.TeardownHTTP()
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400167
168 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
169 testhelper.TestMethod(t, r, "POST")
170 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
171 testhelper.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
172
173 w.WriteHeader(http.StatusAccepted)
174 })
175
176 client := serviceClient()
177 err := ChangeAdminPassword(client, "1234asdf", "new-password")
178 if err != nil {
179 t.Errorf("Unexpected ChangeAdminPassword error: %v", err)
180 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400181}
182
183func TestRebootServer(t *testing.T) {
184 testhelper.SetupHTTP()
185 defer testhelper.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400186
187 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
188 testhelper.TestMethod(t, r, "POST")
189 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
190 testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
191
192 w.WriteHeader(http.StatusAccepted)
193 })
194
195 client := serviceClient()
196 err := Reboot(client, "1234asdf", SoftReboot)
197 if err != nil {
198 t.Errorf("Unexpected Reboot error: %v", err)
199 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400200}
201
202func TestRebuildServer(t *testing.T) {
203 testhelper.SetupHTTP()
204 defer testhelper.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400205
206 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
207 testhelper.TestMethod(t, r, "POST")
208 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
209 testhelper.TestJSONRequest(t, r, `
210 {
211 "rebuild": {
212 "name": "new-name",
213 "adminPass": "swordfish",
214 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
215 "accessIPv4": "1.2.3.4"
216 }
217 }
218 `)
219
220 w.WriteHeader(http.StatusAccepted)
221 w.Header().Add("Content-Type", "application/json")
222 fmt.Fprintf(w, singleServerBody)
223 })
224
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200225 opts := RebuildOpts{
226 Name: "new-name",
227 AdminPass: "swordfish",
228 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
229 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400230 }
231
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200232 actual, err := Rebuild(serviceClient(), "1234asdf", opts).Extract()
233 testhelper.AssertNoErr(t, err)
234
Ash Wilson077f8772014-09-25 10:57:13 -0400235 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400236}
237
238func TestResizeServer(t *testing.T) {
239 testhelper.SetupHTTP()
240 defer testhelper.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400241
242 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
243 testhelper.TestMethod(t, r, "POST")
244 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
245 testhelper.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
246
247 w.WriteHeader(http.StatusAccepted)
248 })
249
250 client := serviceClient()
251 err := Resize(client, "1234asdf", "2")
252 if err != nil {
253 t.Errorf("Unexpected Reboot error: %v", err)
254 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400255}
256
257func TestConfirmResize(t *testing.T) {
258 testhelper.SetupHTTP()
259 defer testhelper.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400260
261 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
262 testhelper.TestMethod(t, r, "POST")
263 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
264 testhelper.TestJSONRequest(t, r, `{ "confirmResize": null }`)
265
266 w.WriteHeader(http.StatusNoContent)
267 })
268
269 client := serviceClient()
270 err := ConfirmResize(client, "1234asdf")
271 if err != nil {
272 t.Errorf("Unexpected ConfirmResize error: %v", err)
273 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400274}
275
276func TestRevertResize(t *testing.T) {
277 testhelper.SetupHTTP()
278 defer testhelper.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400279
280 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
281 testhelper.TestMethod(t, r, "POST")
282 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
283 testhelper.TestJSONRequest(t, r, `{ "revertResize": null }`)
284
285 w.WriteHeader(http.StatusAccepted)
286 })
287
288 client := serviceClient()
289 err := RevertResize(client, "1234asdf")
290 if err != nil {
291 t.Errorf("Unexpected RevertResize error: %v", err)
292 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400293}