blob: fa1a2f584bcef0421ac4e598baccb393aebc7560 [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
45 err := List(client).EachPage(func(page pagination.Page) (bool, error) {
46 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
62 if err != nil {
63 t.Fatalf("Unexpected error from EachPage: %v", err)
64 }
65 if pages != 1 {
66 t.Errorf("Expected 1 page, saw %d", pages)
67 }
68}
69
70func TestCreateServer(t *testing.T) {
71 testhelper.SetupHTTP()
72 defer testhelper.TeardownHTTP()
Ash Wilson3204d0d2014-09-25 10:37:44 -040073
74 testhelper.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
75 testhelper.TestMethod(t, r, "POST")
76 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
77 testhelper.TestJSONRequest(t, r, `{
78 "server": {
79 "name": "derp",
80 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
81 "flavorRef": "1"
82 }
83 }`)
84
85 w.WriteHeader(http.StatusAccepted)
86 w.Header().Add("Content-Type", "application/json")
87 fmt.Fprintf(w, singleServerBody)
88 })
89
90 client := serviceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040091 actual, err := Create(client, map[string]interface{}{
Ash Wilson3204d0d2014-09-25 10:37:44 -040092 "name": "derp",
93 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
94 "flavorRef": "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040095 }).Extract()
Ash Wilson3204d0d2014-09-25 10:37:44 -040096 if err != nil {
97 t.Fatalf("Unexpected Create error: %v", err)
98 }
99
Ash Wilson3204d0d2014-09-25 10:37:44 -0400100 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400101}
102
103func TestDeleteServer(t *testing.T) {
104 testhelper.SetupHTTP()
105 defer testhelper.TeardownHTTP()
Ash Wilsonaff36272014-09-25 10:40:05 -0400106
107 testhelper.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
108 testhelper.TestMethod(t, r, "DELETE")
109 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
110
111 w.WriteHeader(http.StatusNoContent)
112 })
113
114 client := serviceClient()
115 err := Delete(client, "asdfasdfasdf")
116 if err != nil {
117 t.Fatalf("Unexpected Delete error: %v", err)
118 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400119}
120
121func TestGetServer(t *testing.T) {
122 testhelper.SetupHTTP()
123 defer testhelper.TeardownHTTP()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400124
125 testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
126 testhelper.TestMethod(t, r, "GET")
127 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
128 testhelper.TestHeader(t, r, "Accept", "application/json")
129
130 fmt.Fprintf(w, singleServerBody)
131 })
132
133 client := serviceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400134 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400135 if err != nil {
136 t.Fatalf("Unexpected Get error: %v", err)
137 }
138
Ash Wilsona612f1f2014-09-25 10:42:40 -0400139 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400140}
141
142func TestUpdateServer(t *testing.T) {
143 testhelper.SetupHTTP()
144 defer testhelper.TeardownHTTP()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400145
146 testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
147 testhelper.TestMethod(t, r, "PUT")
148 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
149 testhelper.TestHeader(t, r, "Accept", "application/json")
150 testhelper.TestHeader(t, r, "Content-Type", "application/json")
151 testhelper.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
152
153 fmt.Fprintf(w, singleServerBody)
154 })
155
156 client := serviceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400157 actual, err := Update(client, "1234asdf", map[string]interface{}{
Ash Wilson0aac3a82014-09-25 10:45:03 -0400158 "name": "new-name",
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400159 }).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400160 if err != nil {
161 t.Fatalf("Unexpected Update error: %v", err)
162 }
163
Ash Wilson0aac3a82014-09-25 10:45:03 -0400164 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400165}
166
167func TestChangeServerAdminPassword(t *testing.T) {
168 testhelper.SetupHTTP()
169 defer testhelper.TeardownHTTP()
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400170
171 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
172 testhelper.TestMethod(t, r, "POST")
173 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
174 testhelper.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
175
176 w.WriteHeader(http.StatusAccepted)
177 })
178
179 client := serviceClient()
180 err := ChangeAdminPassword(client, "1234asdf", "new-password")
181 if err != nil {
182 t.Errorf("Unexpected ChangeAdminPassword error: %v", err)
183 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400184}
185
186func TestRebootServer(t *testing.T) {
187 testhelper.SetupHTTP()
188 defer testhelper.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400189
190 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
191 testhelper.TestMethod(t, r, "POST")
192 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
193 testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
194
195 w.WriteHeader(http.StatusAccepted)
196 })
197
198 client := serviceClient()
199 err := Reboot(client, "1234asdf", SoftReboot)
200 if err != nil {
201 t.Errorf("Unexpected Reboot error: %v", err)
202 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400203}
204
205func TestRebuildServer(t *testing.T) {
206 testhelper.SetupHTTP()
207 defer testhelper.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400208
209 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
210 testhelper.TestMethod(t, r, "POST")
211 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
212 testhelper.TestJSONRequest(t, r, `
213 {
214 "rebuild": {
215 "name": "new-name",
216 "adminPass": "swordfish",
217 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
218 "accessIPv4": "1.2.3.4"
219 }
220 }
221 `)
222
223 w.WriteHeader(http.StatusAccepted)
224 w.Header().Add("Content-Type", "application/json")
225 fmt.Fprintf(w, singleServerBody)
226 })
227
228 client := serviceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400229 actual, err := Rebuild(client,
Ash Wilson077f8772014-09-25 10:57:13 -0400230 "1234asdf", "new-name", "swordfish",
231 "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400232 map[string]interface{}{"accessIPv4": "1.2.3.4"},
233 ).Extract()
Ash Wilson077f8772014-09-25 10:57:13 -0400234 if err != nil {
235 t.Fatalf("Unexpected Rebuild error: %v", err)
236 }
237
Ash Wilson077f8772014-09-25 10:57:13 -0400238 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400239}
240
241func TestResizeServer(t *testing.T) {
242 testhelper.SetupHTTP()
243 defer testhelper.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400244
245 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
246 testhelper.TestMethod(t, r, "POST")
247 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
248 testhelper.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
249
250 w.WriteHeader(http.StatusAccepted)
251 })
252
253 client := serviceClient()
254 err := Resize(client, "1234asdf", "2")
255 if err != nil {
256 t.Errorf("Unexpected Reboot error: %v", err)
257 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400258}
259
260func TestConfirmResize(t *testing.T) {
261 testhelper.SetupHTTP()
262 defer testhelper.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400263
264 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
265 testhelper.TestMethod(t, r, "POST")
266 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
267 testhelper.TestJSONRequest(t, r, `{ "confirmResize": null }`)
268
269 w.WriteHeader(http.StatusNoContent)
270 })
271
272 client := serviceClient()
273 err := ConfirmResize(client, "1234asdf")
274 if err != nil {
275 t.Errorf("Unexpected ConfirmResize error: %v", err)
276 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400277}
278
279func TestRevertResize(t *testing.T) {
280 testhelper.SetupHTTP()
281 defer testhelper.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400282
283 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
284 testhelper.TestMethod(t, r, "POST")
285 testhelper.TestHeader(t, r, "X-Auth-Token", tokenID)
286 testhelper.TestJSONRequest(t, r, `{ "revertResize": null }`)
287
288 w.WriteHeader(http.StatusAccepted)
289 })
290
291 client := serviceClient()
292 err := RevertResize(client, "1234asdf")
293 if err != nil {
294 t.Errorf("Unexpected RevertResize error: %v", err)
295 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400296}