blob: abfdec71cd88fe9633d182342601073cec3a765c [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
Ash Wilsonad21c712014-09-25 10:15:22 -04004 "net/http"
5 "testing"
6
Ash Wilsonad21c712014-09-25 10:15:22 -04007 "github.com/rackspace/gophercloud/pagination"
Ash Wilsone77ffb02014-10-20 13:10:26 -04008 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040010)
11
Ash Wilsonad21c712014-09-25 10:15:22 -040012func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040013 th.SetupHTTP()
14 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040015 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040016
Ash Wilsonad21c712014-09-25 10:15:22 -040017 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040018 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040019 pages++
20
21 actual, err := ExtractServers(page)
22 if err != nil {
23 return false, err
24 }
25
26 if len(actual) != 2 {
27 t.Fatalf("Expected 2 servers, got %d", len(actual))
28 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040029 th.CheckDeepEquals(t, ServerHerp, actual[0])
30 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040031
32 return true, nil
33 })
34
Ash Wilsone77ffb02014-10-20 13:10:26 -040035 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020036
Ash Wilsonad21c712014-09-25 10:15:22 -040037 if pages != 1 {
38 t.Errorf("Expected 1 page, saw %d", pages)
39 }
40}
41
42func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040043 th.SetupHTTP()
44 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040045 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040046
Ash Wilson664fe332014-10-21 17:47:49 -040047 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040048 Name: "derp",
49 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
50 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040051 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040052 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040053
Ash Wilsond3532cd2014-10-21 14:37:47 -040054 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040055}
56
57func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040058 th.SetupHTTP()
59 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040060 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040061
Jamie Hannaford34732fe2014-10-27 11:29:36 +010062 res := Delete(client.ServiceClient(), "asdfasdfasdf")
63 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040064}
65
66func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040067 th.SetupHTTP()
68 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040069 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040070
Ash Wilsone77ffb02014-10-20 13:10:26 -040071 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040072 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040073 if err != nil {
74 t.Fatalf("Unexpected Get error: %v", err)
75 }
76
Ash Wilsond3532cd2014-10-21 14:37:47 -040077 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040078}
79
80func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040081 th.SetupHTTP()
82 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040083 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -040084
Ash Wilsone77ffb02014-10-20 13:10:26 -040085 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -040086 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -040087 if err != nil {
88 t.Fatalf("Unexpected Update error: %v", err)
89 }
90
Ash Wilsond3532cd2014-10-21 14:37:47 -040091 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040092}
93
94func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040095 th.SetupHTTP()
96 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -040097 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -040098
Ash Wilsone77ffb02014-10-20 13:10:26 -040099 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
100 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400101}
102
103func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400104 th.SetupHTTP()
105 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400106 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400107
Ash Wilsone77ffb02014-10-20 13:10:26 -0400108 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
109 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400110}
111
112func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400113 th.SetupHTTP()
114 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400115 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400116
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200117 opts := RebuildOpts{
118 Name: "new-name",
119 AdminPass: "swordfish",
120 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
121 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400122 }
123
Ash Wilsone77ffb02014-10-20 13:10:26 -0400124 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
125 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200126
Ash Wilsond3532cd2014-10-21 14:37:47 -0400127 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400128}
129
130func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400131 th.SetupHTTP()
132 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400133
Ash Wilsone77ffb02014-10-20 13:10:26 -0400134 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
135 th.TestMethod(t, r, "POST")
136 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
137 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400138
139 w.WriteHeader(http.StatusAccepted)
140 })
141
Ash Wilson5f7cf182014-10-23 08:35:24 -0400142 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400143 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400144}
145
146func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400147 th.SetupHTTP()
148 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400149
Ash Wilsone77ffb02014-10-20 13:10:26 -0400150 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
151 th.TestMethod(t, r, "POST")
152 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
153 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400154
155 w.WriteHeader(http.StatusNoContent)
156 })
157
Ash Wilsone77ffb02014-10-20 13:10:26 -0400158 res := ConfirmResize(client.ServiceClient(), "1234asdf")
159 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400160}
161
162func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400163 th.SetupHTTP()
164 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400165
Ash Wilsone77ffb02014-10-20 13:10:26 -0400166 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
167 th.TestMethod(t, r, "POST")
168 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
169 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400170
171 w.WriteHeader(http.StatusAccepted)
172 })
173
Ash Wilsone77ffb02014-10-20 13:10:26 -0400174 res := RevertResize(client.ServiceClient(), "1234asdf")
175 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400176}
Alex Gaynor810d4892014-11-12 15:43:36 -0800177
178func TestRescue(t *testing.T) {
179 th.SetupHTTP()
180 defer th.TeardownHTTP()
181
Alex Gaynor6c003d22014-11-13 13:52:05 -0800182 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800183
Alex Gaynor40449ed2014-11-12 16:28:06 -0800184 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800185 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800186 })
187 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800188 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800189 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800190}
Jon Perrittcc77da62014-11-16 13:14:21 -0700191
Jon Perritt78c57ce2014-11-20 11:07:18 -0700192func TestGetMetadatum(t *testing.T) {
193 th.SetupHTTP()
194 defer th.TeardownHTTP()
195
196 HandleMetadatumGetSuccessfully(t)
197
198 expected := map[string]string{"foo": "bar"}
199 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
200 th.AssertNoErr(t, err)
201 th.AssertDeepEquals(t, expected, actual)
202}
203
204func TestCreateMetadatum(t *testing.T) {
205 th.SetupHTTP()
206 defer th.TeardownHTTP()
207
208 HandleMetadatumCreateSuccessfully(t)
209
210 expected := map[string]string{"foo": "bar"}
211 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
212 th.AssertNoErr(t, err)
213 th.AssertDeepEquals(t, expected, actual)
214}
215
216func TestDeleteMetadatum(t *testing.T) {
217 th.SetupHTTP()
218 defer th.TeardownHTTP()
219
220 HandleMetadatumDeleteSuccessfully(t)
221
222 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
223 th.AssertNoErr(t, err)
224}
225
Jon Perrittcc77da62014-11-16 13:14:21 -0700226func TestGetMetadata(t *testing.T) {
227 th.SetupHTTP()
228 defer th.TeardownHTTP()
229
230 HandleMetadataGetSuccessfully(t)
231
Jon Perritt78c57ce2014-11-20 11:07:18 -0700232 expected := map[string]string{"foo": "bar", "this": "that"}
233 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700234 th.AssertNoErr(t, err)
235 th.AssertDeepEquals(t, expected, actual)
236}
237
Jon Perritt789f8322014-11-21 08:20:04 -0700238func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700239 th.SetupHTTP()
240 defer th.TeardownHTTP()
241
Jon Perritt789f8322014-11-21 08:20:04 -0700242 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700243
Jon Perrittcc77da62014-11-16 13:14:21 -0700244 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700245 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700246 "foo": "bar",
247 "this": "that",
248 }).Extract()
249 th.AssertNoErr(t, err)
250 th.AssertDeepEquals(t, expected, actual)
251}
252
Jon Perritt78c57ce2014-11-20 11:07:18 -0700253func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700254 th.SetupHTTP()
255 defer th.TeardownHTTP()
256
Jon Perritt78c57ce2014-11-20 11:07:18 -0700257 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700258
259 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700260 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700261 "foo": "baz",
262 "this": "those",
263 }).Extract()
264 th.AssertNoErr(t, err)
265 th.AssertDeepEquals(t, expected, actual)
266}
Jon Perritt5cb49482015-02-19 12:19:58 -0700267
268func TestListAddresses(t *testing.T) {
269 th.SetupHTTP()
270 defer th.TeardownHTTP()
271 HandleAddressListSuccessfully(t)
272
273 expected := ListAddressesExpected
274 pages := 0
275 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
276 pages++
277
278 actual, err := ExtractAddresses(page)
279 th.AssertNoErr(t, err)
280
281 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700282 t.Fatalf("Expected 2 networks, got %d", len(actual))
283 }
284 th.CheckDeepEquals(t, expected, actual)
285
286 return true, nil
287 })
288 th.AssertNoErr(t, err)
289 th.CheckEquals(t, 1, pages)
290}
291
292func TestListAddressesByNetwork(t *testing.T) {
293 th.SetupHTTP()
294 defer th.TeardownHTTP()
295 HandleNetworkAddressListSuccessfully(t)
296
297 expected := ListNetworkAddressesExpected
298 pages := 0
299 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
300 pages++
301
302 actual, err := ExtractNetworkAddresses(page)
303 th.AssertNoErr(t, err)
304
305 if len(actual) != 1 {
306 t.Fatalf("Expected 1 network, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700307 }
308 th.CheckDeepEquals(t, expected, actual)
309
310 return true, nil
311 })
312 th.AssertNoErr(t, err)
313 th.CheckEquals(t, 1, pages)
314}