blob: 0db92f983426fbc637575904f0051e3a6f077f00 [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 Hannaford8c072a32014-10-16 14:33:32 +0200166 res := ChangeAdminPassword(fake.ServiceClient(), "1234asdf", "new-password")
167 testhelper.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400168}
169
170func TestRebootServer(t *testing.T) {
171 testhelper.SetupHTTP()
172 defer testhelper.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400173
174 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
175 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200176 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson8d368e92014-09-25 10:49:07 -0400177 testhelper.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
178
179 w.WriteHeader(http.StatusAccepted)
180 })
181
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200182 res := Reboot(fake.ServiceClient(), "1234asdf", SoftReboot)
183 testhelper.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400184}
185
186func TestRebuildServer(t *testing.T) {
187 testhelper.SetupHTTP()
188 defer testhelper.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400189
190 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
191 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200192 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson077f8772014-09-25 10:57:13 -0400193 testhelper.TestJSONRequest(t, r, `
194 {
195 "rebuild": {
196 "name": "new-name",
197 "adminPass": "swordfish",
198 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
199 "accessIPv4": "1.2.3.4"
200 }
201 }
202 `)
203
204 w.WriteHeader(http.StatusAccepted)
205 w.Header().Add("Content-Type", "application/json")
206 fmt.Fprintf(w, singleServerBody)
207 })
208
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200209 opts := RebuildOpts{
210 Name: "new-name",
211 AdminPass: "swordfish",
212 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
213 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400214 }
215
Jamie Hannaford304ab092014-10-16 18:23:44 +0200216 actual, err := Rebuild(fake.ServiceClient(), "1234asdf", opts).Extract()
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200217 testhelper.AssertNoErr(t, err)
218
Ash Wilson077f8772014-09-25 10:57:13 -0400219 equalServers(t, serverDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400220}
221
222func TestResizeServer(t *testing.T) {
223 testhelper.SetupHTTP()
224 defer testhelper.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400225
226 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
227 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200228 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson45181f42014-09-25 11:00:16 -0400229 testhelper.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
230
231 w.WriteHeader(http.StatusAccepted)
232 })
233
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200234 res := Resize(fake.ServiceClient(), "1234asdf", "2")
235 testhelper.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400236}
237
238func TestConfirmResize(t *testing.T) {
239 testhelper.SetupHTTP()
240 defer testhelper.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400241
242 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
243 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200244 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400245 testhelper.TestJSONRequest(t, r, `{ "confirmResize": null }`)
246
247 w.WriteHeader(http.StatusNoContent)
248 })
249
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200250 res := ConfirmResize(fake.ServiceClient(), "1234asdf")
251 testhelper.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400252}
253
254func TestRevertResize(t *testing.T) {
255 testhelper.SetupHTTP()
256 defer testhelper.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400257
258 testhelper.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
259 testhelper.TestMethod(t, r, "POST")
Jamie Hannaford2aaf1a62014-10-16 12:55:50 +0200260 testhelper.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400261 testhelper.TestJSONRequest(t, r, `{ "revertResize": null }`)
262
263 w.WriteHeader(http.StatusAccepted)
264 })
265
Jamie Hannaford8c072a32014-10-16 14:33:32 +0200266 res := RevertResize(fake.ServiceClient(), "1234asdf")
267 testhelper.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400268}