blob: 86fe1e2e1af4f2f5e67823025b5f44fb1ac6885a [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Ash Wilsonad21c712014-09-25 10:15:22 -04008 "github.com/rackspace/gophercloud/pagination"
9 "github.com/rackspace/gophercloud/testhelper"
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +020010 fake "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040011)
12
Ash Wilsonad21c712014-09-25 10:15:22 -040013func TestListServers(t *testing.T) {
14 testhelper.SetupHTTP()
15 defer testhelper.TeardownHTTP()
16
17 testhelper.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
18 testhelper.TestMethod(t, r, "GET")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +020019 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilsonad21c712014-09-25 10:15:22 -040020
21 w.Header().Add("Content-Type", "application/json")
22 r.ParseForm()
23 marker := r.Form.Get("marker")
24 switch marker {
25 case "":
26 fmt.Fprintf(w, serverListBody)
27 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
28 fmt.Fprintf(w, `{ "servers": [] }`)
29 default:
30 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
31 }
32 })
33
Ash Wilsonad21c712014-09-25 10:15:22 -040034 pages := 0
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +020035 err := List(fake.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040036 pages++
37
38 actual, err := ExtractServers(page)
39 if err != nil {
40 return false, err
41 }
42
43 if len(actual) != 2 {
44 t.Fatalf("Expected 2 servers, got %d", len(actual))
45 }
46 equalServers(t, serverHerp, actual[0])
47 equalServers(t, serverDerp, actual[1])
48
49 return true, nil
50 })
51
Jamie Hannafordcf001722014-10-16 12:54:07 +020052 testhelper.AssertNoErr(t, err)
53
Ash Wilsonad21c712014-09-25 10:15:22 -040054 if pages != 1 {
55 t.Errorf("Expected 1 page, saw %d", pages)
56 }
57}
58
59func TestCreateServer(t *testing.T) {
60 testhelper.SetupHTTP()
61 defer testhelper.TeardownHTTP()
Ash Wilson3204d0d2014-09-25 10:37:44 -040062
63 testhelper.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
64 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +020065 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson3204d0d2014-09-25 10:37:44 -040066 testhelper.TestJSONRequest(t, r, `{
67 "server": {
68 "name": "derp",
69 "imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
70 "flavorRef": "1"
71 }
72 }`)
73
74 w.WriteHeader(http.StatusAccepted)
75 w.Header().Add("Content-Type", "application/json")
76 fmt.Fprintf(w, singleServerBody)
77 })
78
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +020079 client := fake.ServiceClient()
Ash Wilson3a0e3b42014-10-02 10:58:09 -040080 actual, err := Create(client, CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040081 Name: "derp",
82 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
83 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040084 }).Extract()
Ash Wilson3204d0d2014-09-25 10:37:44 -040085 if err != nil {
86 t.Fatalf("Unexpected Create error: %v", err)
87 }
88
Ash Wilson3204d0d2014-09-25 10:37:44 -040089 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040090}
91
92func TestDeleteServer(t *testing.T) {
93 testhelper.SetupHTTP()
94 defer testhelper.TeardownHTTP()
Ash Wilsonaff36272014-09-25 10:40:05 -040095
96 testhelper.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
97 testhelper.TestMethod(t, r, "DELETE")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +020098 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilsonaff36272014-09-25 10:40:05 -040099
100 w.WriteHeader(http.StatusNoContent)
101 })
102
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200103 client := fake.ServiceClient()
Ash Wilsonaff36272014-09-25 10:40:05 -0400104 err := Delete(client, "asdfasdfasdf")
105 if err != nil {
106 t.Fatalf("Unexpected Delete error: %v", err)
107 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400108}
109
110func TestGetServer(t *testing.T) {
111 testhelper.SetupHTTP()
112 defer testhelper.TeardownHTTP()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400113
114 testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
115 testhelper.TestMethod(t, r, "GET")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200116 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400117 testhelper.TestHeader(t, r, "Accept", "application/json")
118
119 fmt.Fprintf(w, singleServerBody)
120 })
121
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200122 client := fake.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400123 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400124 if err != nil {
125 t.Fatalf("Unexpected Get error: %v", err)
126 }
127
Ash Wilsona612f1f2014-09-25 10:42:40 -0400128 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400129}
130
131func TestUpdateServer(t *testing.T) {
132 testhelper.SetupHTTP()
133 defer testhelper.TeardownHTTP()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400134
135 testhelper.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
136 testhelper.TestMethod(t, r, "PUT")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200137 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400138 testhelper.TestHeader(t, r, "Accept", "application/json")
139 testhelper.TestHeader(t, r, "Content-Type", "application/json")
140 testhelper.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
141
142 fmt.Fprintf(w, singleServerBody)
143 })
144
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200145 client := fake.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400146 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400147 if err != nil {
148 t.Fatalf("Unexpected Update error: %v", err)
149 }
150
Ash Wilson0aac3a82014-09-25 10:45:03 -0400151 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400152}
153
154func TestChangeServerAdminPassword(t *testing.T) {
155 testhelper.SetupHTTP()
156 defer testhelper.TeardownHTTP()
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400157
158 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
159 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200160 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400161 testhelper.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
162
163 w.WriteHeader(http.StatusAccepted)
164 })
165
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200166 client := fake.ServiceClient()
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400167 err := ChangeAdminPassword(client, "1234asdf", "new-password")
168 if err != nil {
169 t.Errorf("Unexpected ChangeAdminPassword error: %v", err)
170 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400171}
172
173func TestRebootServer(t *testing.T) {
174 testhelper.SetupHTTP()
175 defer testhelper.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400176
177 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
178 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200179 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson8d368e92014-09-25 10:49:07 -0400180 testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
181
182 w.WriteHeader(http.StatusAccepted)
183 })
184
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200185 client := fake.ServiceClient()
Ash Wilson8d368e92014-09-25 10:49:07 -0400186 err := Reboot(client, "1234asdf", SoftReboot)
187 if err != nil {
188 t.Errorf("Unexpected Reboot error: %v", err)
189 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400190}
191
192func TestRebuildServer(t *testing.T) {
193 testhelper.SetupHTTP()
194 defer testhelper.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400195
196 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
197 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200198 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson077f8772014-09-25 10:57:13 -0400199 testhelper.TestJSONRequest(t, r, `
200 {
201 "rebuild": {
202 "name": "new-name",
203 "adminPass": "swordfish",
204 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
205 "accessIPv4": "1.2.3.4"
206 }
207 }
208 `)
209
210 w.WriteHeader(http.StatusAccepted)
211 w.Header().Add("Content-Type", "application/json")
212 fmt.Fprintf(w, singleServerBody)
213 })
214
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200215 opts := RebuildOpts{
216 Name: "new-name",
217 AdminPass: "swordfish",
218 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
219 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400220 }
221
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200222 actual, err := Rebuild(serviceClient(), "1234asdf", opts).Extract()
223 testhelper.AssertNoErr(t, err)
224
Ash Wilson077f8772014-09-25 10:57:13 -0400225 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400226}
227
228func TestResizeServer(t *testing.T) {
229 testhelper.SetupHTTP()
230 defer testhelper.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400231
232 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
233 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200234 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson45181f42014-09-25 11:00:16 -0400235 testhelper.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
236
237 w.WriteHeader(http.StatusAccepted)
238 })
239
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200240 client := fake.ServiceClient()
Ash Wilson45181f42014-09-25 11:00:16 -0400241 err := Resize(client, "1234asdf", "2")
242 if err != nil {
243 t.Errorf("Unexpected Reboot error: %v", err)
244 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400245}
246
247func TestConfirmResize(t *testing.T) {
248 testhelper.SetupHTTP()
249 defer testhelper.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400250
251 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
252 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200253 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400254 testhelper.TestJSONRequest(t, r, `{ "confirmResize": null }`)
255
256 w.WriteHeader(http.StatusNoContent)
257 })
258
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200259 client := fake.ServiceClient()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400260 err := ConfirmResize(client, "1234asdf")
261 if err != nil {
262 t.Errorf("Unexpected ConfirmResize error: %v", err)
263 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400264}
265
266func TestRevertResize(t *testing.T) {
267 testhelper.SetupHTTP()
268 defer testhelper.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400269
270 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
271 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200272 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400273 testhelper.TestJSONRequest(t, r, `{ "revertResize": null }`)
274
275 w.WriteHeader(http.StatusAccepted)
276 })
277
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200278 client := fake.ServiceClient()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400279 err := RevertResize(client, "1234asdf")
280 if err != nil {
281 t.Errorf("Unexpected RevertResize error: %v", err)
282 }
Ash Wilsonad21c712014-09-25 10:15:22 -0400283}