blob: 51d52fbd6a88461818128b481205f94c5b42a1d4 [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 Wilson1c1eb882014-10-21 18:14:31 -0400130 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400131
Ash Wilsone77ffb02014-10-20 13:10:26 -0400132 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
133 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400134}
135
136func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400137 th.SetupHTTP()
138 defer th.TeardownHTTP()
Ash Wilson8d368e92014-09-25 10:49:07 -0400139
Ash Wilsone77ffb02014-10-20 13:10:26 -0400140 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
141 th.TestMethod(t, r, "POST")
142 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
143 th.TestJSONRequest(t, r, `{ "reboot": { "type": "SOFT" } }`)
Ash Wilson8d368e92014-09-25 10:49:07 -0400144
145 w.WriteHeader(http.StatusAccepted)
146 })
147
Ash Wilsone77ffb02014-10-20 13:10:26 -0400148 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
149 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400150}
151
152func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400153 th.SetupHTTP()
154 defer th.TeardownHTTP()
Ash Wilson077f8772014-09-25 10:57:13 -0400155
Ash Wilsone77ffb02014-10-20 13:10:26 -0400156 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
157 th.TestMethod(t, r, "POST")
158 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
159 th.TestJSONRequest(t, r, `
Ash Wilson077f8772014-09-25 10:57:13 -0400160 {
161 "rebuild": {
162 "name": "new-name",
163 "adminPass": "swordfish",
164 "imageRef": "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
165 "accessIPv4": "1.2.3.4"
166 }
167 }
168 `)
169
170 w.WriteHeader(http.StatusAccepted)
171 w.Header().Add("Content-Type", "application/json")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400172 fmt.Fprintf(w, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400173 })
174
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200175 opts := RebuildOpts{
176 Name: "new-name",
177 AdminPass: "swordfish",
178 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
179 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400180 }
181
Ash Wilsone77ffb02014-10-20 13:10:26 -0400182 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
183 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200184
Ash Wilsond3532cd2014-10-21 14:37:47 -0400185 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400186}
187
188func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400189 th.SetupHTTP()
190 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400191
Ash Wilsone77ffb02014-10-20 13:10:26 -0400192 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
193 th.TestMethod(t, r, "POST")
194 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
195 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400196
197 w.WriteHeader(http.StatusAccepted)
198 })
199
Ash Wilsone77ffb02014-10-20 13:10:26 -0400200 res := Resize(client.ServiceClient(), "1234asdf", "2")
201 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400202}
203
204func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400205 th.SetupHTTP()
206 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400207
Ash Wilsone77ffb02014-10-20 13:10:26 -0400208 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
209 th.TestMethod(t, r, "POST")
210 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
211 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400212
213 w.WriteHeader(http.StatusNoContent)
214 })
215
Ash Wilsone77ffb02014-10-20 13:10:26 -0400216 res := ConfirmResize(client.ServiceClient(), "1234asdf")
217 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400218}
219
220func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400221 th.SetupHTTP()
222 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400223
Ash Wilsone77ffb02014-10-20 13:10:26 -0400224 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
225 th.TestMethod(t, r, "POST")
226 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
227 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400228
229 w.WriteHeader(http.StatusAccepted)
230 })
231
Ash Wilsone77ffb02014-10-20 13:10:26 -0400232 res := RevertResize(client.ServiceClient(), "1234asdf")
233 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400234}