blob: 4df5d144ee3cf0ce1b20e2d8b27cb41f7988c4b1 [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 Wilsond3532cd2014-10-21 14:37:47 -040046 th.CheckDeepEquals(t, ServerHerp, actual[0])
47 th.CheckDeepEquals(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 Wilson664fe332014-10-21 17:47:49 -040062 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040063
Ash Wilson664fe332014-10-21 17:47:49 -040064 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040065 Name: "derp",
66 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
67 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040068 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040069 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040070
Ash Wilsond3532cd2014-10-21 14:37:47 -040071 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040072}
73
74func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040075 th.SetupHTTP()
76 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040077 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040078
Ash Wilson664fe332014-10-21 17:47:49 -040079 err := Delete(client.ServiceClient(), "asdfasdfasdf")
80 th.AssertNoErr(t, err)
Ash Wilsonad21c712014-09-25 10:15:22 -040081}
82
83func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040084 th.SetupHTTP()
85 defer th.TeardownHTTP()
Ash Wilsona612f1f2014-09-25 10:42:40 -040086
Ash Wilsone77ffb02014-10-20 13:10:26 -040087 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
88 th.TestMethod(t, r, "GET")
89 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
90 th.TestHeader(t, r, "Accept", "application/json")
Ash Wilsona612f1f2014-09-25 10:42:40 -040091
Ash Wilsone77ffb02014-10-20 13:10:26 -040092 fmt.Fprintf(w, SingleServerBody)
Ash Wilsona612f1f2014-09-25 10:42:40 -040093 })
94
Ash Wilsone77ffb02014-10-20 13:10:26 -040095 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040096 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040097 if err != nil {
98 t.Fatalf("Unexpected Get error: %v", err)
99 }
100
Ash Wilsond3532cd2014-10-21 14:37:47 -0400101 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400102}
103
104func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400105 th.SetupHTTP()
106 defer th.TeardownHTTP()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400107
Ash Wilsone77ffb02014-10-20 13:10:26 -0400108 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
109 th.TestMethod(t, r, "PUT")
110 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
111 th.TestHeader(t, r, "Accept", "application/json")
112 th.TestHeader(t, r, "Content-Type", "application/json")
113 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400114
Ash Wilsone77ffb02014-10-20 13:10:26 -0400115 fmt.Fprintf(w, SingleServerBody)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400116 })
117
Ash Wilsone77ffb02014-10-20 13:10:26 -0400118 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400119 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400120 if err != nil {
121 t.Fatalf("Unexpected Update error: %v", err)
122 }
123
Ash Wilsond3532cd2014-10-21 14:37:47 -0400124 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400125}
126
127func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400128 th.SetupHTTP()
129 defer th.TeardownHTTP()
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400130
Ash Wilsone77ffb02014-10-20 13:10:26 -0400131 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
132 th.TestMethod(t, r, "POST")
133 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
134 th.TestJSONRequest(t, r, `{ "changePassword": { "adminPass": "new-password" } }`)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400135
136 w.WriteHeader(http.StatusAccepted)
137 })
138
Ash Wilsone77ffb02014-10-20 13:10:26 -0400139 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
140 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400141}
142
143func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400144 th.SetupHTTP()
145 defer th.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400146
Ash Wilsone77ffb02014-10-20 13:10:26 -0400147 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
148 th.TestMethod(t, r, "POST")
149 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
150 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
Ash Wilson8d368e92014-09-25 10:49:07 -0400151
152 w.WriteHeader(http.StatusAccepted)
153 })
154
Ash Wilsone77ffb02014-10-20 13:10:26 -0400155 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
156 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400157}
158
159func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400160 th.SetupHTTP()
161 defer th.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400162
Ash Wilsone77ffb02014-10-20 13:10:26 -0400163 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
164 th.TestMethod(t, r, "POST")
165 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
166 th.TestJSONRequest(t, r, `
Ash Wilson077f8772014-09-25 10:57:13 -0400167 {
168 "rebuild": {
169 "name": "new-name",
170 "adminPass": "swordfish",
171 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
172 "accessIPv4": "1.2.3.4"
173 }
174 }
175 `)
176
177 w.WriteHeader(http.StatusAccepted)
178 w.Header().Add("Content-Type", "application/json")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400179 fmt.Fprintf(w, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400180 })
181
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200182 opts := RebuildOpts{
183 Name: "new-name",
184 AdminPass: "swordfish",
185 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
186 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400187 }
188
Ash Wilsone77ffb02014-10-20 13:10:26 -0400189 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
190 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200191
Ash Wilsond3532cd2014-10-21 14:37:47 -0400192 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400193}
194
195func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400196 th.SetupHTTP()
197 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400198
Ash Wilsone77ffb02014-10-20 13:10:26 -0400199 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
200 th.TestMethod(t, r, "POST")
201 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
202 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400203
204 w.WriteHeader(http.StatusAccepted)
205 })
206
Ash Wilsone77ffb02014-10-20 13:10:26 -0400207 res := Resize(client.ServiceClient(), "1234asdf", "2")
208 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400209}
210
211func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400212 th.SetupHTTP()
213 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400214
Ash Wilsone77ffb02014-10-20 13:10:26 -0400215 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
216 th.TestMethod(t, r, "POST")
217 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
218 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400219
220 w.WriteHeader(http.StatusNoContent)
221 })
222
Ash Wilsone77ffb02014-10-20 13:10:26 -0400223 res := ConfirmResize(client.ServiceClient(), "1234asdf")
224 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400225}
226
227func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400228 th.SetupHTTP()
229 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400230
Ash Wilsone77ffb02014-10-20 13:10:26 -0400231 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
232 th.TestMethod(t, r, "POST")
233 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
234 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400235
236 w.WriteHeader(http.StatusAccepted)
237 })
238
Ash Wilsone77ffb02014-10-20 13:10:26 -0400239 res := RevertResize(client.ServiceClient(), "1234asdf")
240 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400241}