blob: 5e384339663bf81014512721d0c7470da3358f07 [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"
Ash Wilsone77ffb02014-10-20 13:10:26 -04009 th "github.com/rackspace/gophercloud/testhelper"
10 "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) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Ash Wilsonad21c712014-09-25 10:15:22 -040016
Ash Wilsone77ffb02014-10-20 13:10:26 -040017 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
19 th.TestHeader(t, r, "X-Auth-Token", client.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 "":
Ash Wilsone77ffb02014-10-20 13:10:26 -040026 fmt.Fprintf(w, ServerListBody)
Ash Wilsonad21c712014-09-25 10:15:22 -040027 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
Ash Wilsone77ffb02014-10-20 13:10:26 -040035 err := List(client.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 }
Ash Wilsone77ffb02014-10-20 13:10:26 -040046 CheckServerEquals(t, ServerHerp, actual[0])
47 CheckServerEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040048
49 return true, nil
50 })
51
Ash Wilsone77ffb02014-10-20 13:10:26 -040052 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020053
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) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040060 th.SetupHTTP()
61 defer th.TeardownHTTP()
Ash Wilson3204d0d2014-09-25 10:37:44 -040062
Ash Wilsone77ffb02014-10-20 13:10:26 -040063 th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
64 th.TestMethod(t, r, "POST")
65 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
66 th.TestJSONRequest(t, r, `{
Ash Wilson3204d0d2014-09-25 10:37:44 -040067 "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")
Ash Wilsone77ffb02014-10-20 13:10:26 -040076 fmt.Fprintf(w, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040077 })
78
Ash Wilsone77ffb02014-10-20 13:10:26 -040079 client := client.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 Wilsone77ffb02014-10-20 13:10:26 -040089 CheckServerEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040090}
91
92func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040093 th.SetupHTTP()
94 defer th.TeardownHTTP()
Ash Wilsonaff36272014-09-25 10:40:05 -040095
Ash Wilsone77ffb02014-10-20 13:10:26 -040096 th.Mux.HandleFunc("/servers/asdfasdfasdf", func(w http.ResponseWriter, r *http.Request) {
97 th.TestMethod(t, r, "DELETE")
98 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonaff36272014-09-25 10:40:05 -040099
100 w.WriteHeader(http.StatusNoContent)
101 })
102
Ash Wilsone77ffb02014-10-20 13:10:26 -0400103 client := client.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) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400111 th.SetupHTTP()
112 defer th.TeardownHTTP()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400113
Ash Wilsone77ffb02014-10-20 13:10:26 -0400114 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
115 th.TestMethod(t, r, "GET")
116 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
117 th.TestHeader(t, r, "Accept", "application/json")
Ash Wilsona612f1f2014-09-25 10:42:40 -0400118
Ash Wilsone77ffb02014-10-20 13:10:26 -0400119 fmt.Fprintf(w, SingleServerBody)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400120 })
121
Ash Wilsone77ffb02014-10-20 13:10:26 -0400122 client := client.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 Wilsone77ffb02014-10-20 13:10:26 -0400128 CheckServerEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400129}
130
131func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400132 th.SetupHTTP()
133 defer th.TeardownHTTP()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400134
Ash Wilsone77ffb02014-10-20 13:10:26 -0400135 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
136 th.TestMethod(t, r, "PUT")
137 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
138 th.TestHeader(t, r, "Accept", "application/json")
139 th.TestHeader(t, r, "Content-Type", "application/json")
140 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400141
Ash Wilsone77ffb02014-10-20 13:10:26 -0400142 fmt.Fprintf(w, SingleServerBody)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400143 })
144
Ash Wilsone77ffb02014-10-20 13:10:26 -0400145 client := client.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 Wilsone77ffb02014-10-20 13:10:26 -0400151 CheckServerEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400152}
153
154func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400155 th.SetupHTTP()
156 defer th.TeardownHTTP()
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400157
Ash Wilsone77ffb02014-10-20 13:10:26 -0400158 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
159 th.TestMethod(t, r, "POST")
160 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
161 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400162
163 w.WriteHeader(http.StatusAccepted)
164 })
165
Ash Wilsone77ffb02014-10-20 13:10:26 -0400166 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
167 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400168}
169
170func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400171 th.SetupHTTP()
172 defer th.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400173
Ash Wilsone77ffb02014-10-20 13:10:26 -0400174 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
175 th.TestMethod(t, r, "POST")
176 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
177 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
Ash Wilson8d368e92014-09-25 10:49:07 -0400178
179 w.WriteHeader(http.StatusAccepted)
180 })
181
Ash Wilsone77ffb02014-10-20 13:10:26 -0400182 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
183 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400184}
185
186func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400187 th.SetupHTTP()
188 defer th.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400189
Ash Wilsone77ffb02014-10-20 13:10:26 -0400190 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
191 th.TestMethod(t, r, "POST")
192 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
193 th.TestJSONRequest(t, r, `
Ash Wilson077f8772014-09-25 10:57:13 -0400194 {
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")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400206 fmt.Fprintf(w, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400207 })
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
Ash Wilsone77ffb02014-10-20 13:10:26 -0400216 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
217 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200218
Ash Wilsone77ffb02014-10-20 13:10:26 -0400219 CheckServerEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400220}
221
222func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400223 th.SetupHTTP()
224 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400225
Ash Wilsone77ffb02014-10-20 13:10:26 -0400226 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
227 th.TestMethod(t, r, "POST")
228 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
229 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400230
231 w.WriteHeader(http.StatusAccepted)
232 })
233
Ash Wilsone77ffb02014-10-20 13:10:26 -0400234 res := Resize(client.ServiceClient(), "1234asdf", "2")
235 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400236}
237
238func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400239 th.SetupHTTP()
240 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400241
Ash Wilsone77ffb02014-10-20 13:10:26 -0400242 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
243 th.TestMethod(t, r, "POST")
244 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
245 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400246
247 w.WriteHeader(http.StatusNoContent)
248 })
249
Ash Wilsone77ffb02014-10-20 13:10:26 -0400250 res := ConfirmResize(client.ServiceClient(), "1234asdf")
251 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400252}
253
254func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400255 th.SetupHTTP()
256 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400257
Ash Wilsone77ffb02014-10-20 13:10:26 -0400258 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
259 th.TestMethod(t, r, "POST")
260 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
261 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400262
263 w.WriteHeader(http.StatusAccepted)
264 })
265
Ash Wilsone77ffb02014-10-20 13:10:26 -0400266 res := RevertResize(client.ServiceClient(), "1234asdf")
267 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400268}