blob: d878bd04e8e82eb79d2d383fe3cf96cb87c4c787 [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
Kevin Pike92e10b52015-04-10 15:16:57 -07004 "encoding/base64"
Kevin Pikea2bfaea2015-04-21 11:45:59 -07005 "encoding/json"
Ash Wilsonad21c712014-09-25 10:15:22 -04006 "net/http"
7 "testing"
8
Ash Wilsonad21c712014-09-25 10:15:22 -04009 "github.com/rackspace/gophercloud/pagination"
Ash Wilsone77ffb02014-10-20 13:10:26 -040010 th "github.com/rackspace/gophercloud/testhelper"
11 "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040012)
13
Ash Wilsonad21c712014-09-25 10:15:22 -040014func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040015 th.SetupHTTP()
16 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040017 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040018
Ash Wilsonad21c712014-09-25 10:15:22 -040019 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040020 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040021 pages++
22
23 actual, err := ExtractServers(page)
24 if err != nil {
25 return false, err
26 }
27
28 if len(actual) != 2 {
29 t.Fatalf("Expected 2 servers, got %d", len(actual))
30 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040031 th.CheckDeepEquals(t, ServerHerp, actual[0])
32 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040033
34 return true, nil
35 })
36
Ash Wilsone77ffb02014-10-20 13:10:26 -040037 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020038
Ash Wilsonad21c712014-09-25 10:15:22 -040039 if pages != 1 {
40 t.Errorf("Expected 1 page, saw %d", pages)
41 }
42}
43
Jon Perrittd27a9c72015-02-18 11:33:28 -070044func TestListAllServers(t *testing.T) {
45 th.SetupHTTP()
46 defer th.TeardownHTTP()
47 HandleServerListSuccessfully(t)
48
49 allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages()
50 th.AssertNoErr(t, err)
51 actual, err := ExtractServers(allPages)
52 th.AssertNoErr(t, err)
53 th.CheckDeepEquals(t, ServerHerp, actual[0])
54 th.CheckDeepEquals(t, ServerDerp, actual[1])
55}
56
Ash Wilsonad21c712014-09-25 10:15:22 -040057func TestCreateServer(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 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040061
Ash Wilson664fe332014-10-21 17:47:49 -040062 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040063 Name: "derp",
64 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
65 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040066 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040067 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040068
Ash Wilsond3532cd2014-10-21 14:37:47 -040069 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040070}
71
72func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040073 th.SetupHTTP()
74 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040075 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040076
Jamie Hannaford34732fe2014-10-27 11:29:36 +010077 res := Delete(client.ServiceClient(), "asdfasdfasdf")
78 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040079}
80
81func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040082 th.SetupHTTP()
83 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040084 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040085
Ash Wilsone77ffb02014-10-20 13:10:26 -040086 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040087 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040088 if err != nil {
89 t.Fatalf("Unexpected Get error: %v", err)
90 }
91
Ash Wilsond3532cd2014-10-21 14:37:47 -040092 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040093}
94
95func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040096 th.SetupHTTP()
97 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040098 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -040099
Ash Wilsone77ffb02014-10-20 13:10:26 -0400100 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400101 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400102 if err != nil {
103 t.Fatalf("Unexpected Update error: %v", err)
104 }
105
Ash Wilsond3532cd2014-10-21 14:37:47 -0400106 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400107}
108
109func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400110 th.SetupHTTP()
111 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400112 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400113
Ash Wilsone77ffb02014-10-20 13:10:26 -0400114 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
115 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400116}
117
118func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400119 th.SetupHTTP()
120 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400121 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400122
Ash Wilsone77ffb02014-10-20 13:10:26 -0400123 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
124 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400125}
126
127func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400128 th.SetupHTTP()
129 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400130 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400131
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200132 opts := RebuildOpts{
133 Name: "new-name",
134 AdminPass: "swordfish",
135 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
136 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400137 }
138
Ash Wilsone77ffb02014-10-20 13:10:26 -0400139 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
140 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200141
Ash Wilsond3532cd2014-10-21 14:37:47 -0400142 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400143}
144
145func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400146 th.SetupHTTP()
147 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400148
Ash Wilsone77ffb02014-10-20 13:10:26 -0400149 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
150 th.TestMethod(t, r, "POST")
151 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
152 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400153
154 w.WriteHeader(http.StatusAccepted)
155 })
156
Ash Wilson5f7cf182014-10-23 08:35:24 -0400157 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400158 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400159}
160
161func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400162 th.SetupHTTP()
163 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400164
Ash Wilsone77ffb02014-10-20 13:10:26 -0400165 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
166 th.TestMethod(t, r, "POST")
167 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
168 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400169
170 w.WriteHeader(http.StatusNoContent)
171 })
172
Ash Wilsone77ffb02014-10-20 13:10:26 -0400173 res := ConfirmResize(client.ServiceClient(), "1234asdf")
174 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400175}
176
177func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400178 th.SetupHTTP()
179 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400180
Ash Wilsone77ffb02014-10-20 13:10:26 -0400181 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
182 th.TestMethod(t, r, "POST")
183 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
184 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400185
186 w.WriteHeader(http.StatusAccepted)
187 })
188
Ash Wilsone77ffb02014-10-20 13:10:26 -0400189 res := RevertResize(client.ServiceClient(), "1234asdf")
190 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400191}
Alex Gaynor810d4892014-11-12 15:43:36 -0800192
193func TestRescue(t *testing.T) {
194 th.SetupHTTP()
195 defer th.TeardownHTTP()
196
Alex Gaynor6c003d22014-11-13 13:52:05 -0800197 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800198
Alex Gaynor40449ed2014-11-12 16:28:06 -0800199 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800200 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800201 })
202 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800203 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800204 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800205}
Jon Perrittcc77da62014-11-16 13:14:21 -0700206
Jon Perritt78c57ce2014-11-20 11:07:18 -0700207func TestGetMetadatum(t *testing.T) {
208 th.SetupHTTP()
209 defer th.TeardownHTTP()
210
211 HandleMetadatumGetSuccessfully(t)
212
213 expected := map[string]string{"foo": "bar"}
214 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
215 th.AssertNoErr(t, err)
216 th.AssertDeepEquals(t, expected, actual)
217}
218
219func TestCreateMetadatum(t *testing.T) {
220 th.SetupHTTP()
221 defer th.TeardownHTTP()
222
223 HandleMetadatumCreateSuccessfully(t)
224
225 expected := map[string]string{"foo": "bar"}
226 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
227 th.AssertNoErr(t, err)
228 th.AssertDeepEquals(t, expected, actual)
229}
230
231func TestDeleteMetadatum(t *testing.T) {
232 th.SetupHTTP()
233 defer th.TeardownHTTP()
234
235 HandleMetadatumDeleteSuccessfully(t)
236
237 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
238 th.AssertNoErr(t, err)
239}
240
Jon Perrittcc77da62014-11-16 13:14:21 -0700241func TestGetMetadata(t *testing.T) {
242 th.SetupHTTP()
243 defer th.TeardownHTTP()
244
245 HandleMetadataGetSuccessfully(t)
246
Jon Perritt78c57ce2014-11-20 11:07:18 -0700247 expected := map[string]string{"foo": "bar", "this": "that"}
248 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700249 th.AssertNoErr(t, err)
250 th.AssertDeepEquals(t, expected, actual)
251}
252
Jon Perritt789f8322014-11-21 08:20:04 -0700253func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700254 th.SetupHTTP()
255 defer th.TeardownHTTP()
256
Jon Perritt789f8322014-11-21 08:20:04 -0700257 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700258
Jon Perrittcc77da62014-11-16 13:14:21 -0700259 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700260 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700261 "foo": "bar",
262 "this": "that",
263 }).Extract()
264 th.AssertNoErr(t, err)
265 th.AssertDeepEquals(t, expected, actual)
266}
267
Jon Perritt78c57ce2014-11-20 11:07:18 -0700268func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700269 th.SetupHTTP()
270 defer th.TeardownHTTP()
271
Jon Perritt78c57ce2014-11-20 11:07:18 -0700272 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700273
274 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700275 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700276 "foo": "baz",
277 "this": "those",
278 }).Extract()
279 th.AssertNoErr(t, err)
280 th.AssertDeepEquals(t, expected, actual)
281}
Jon Perritt5cb49482015-02-19 12:19:58 -0700282
283func TestListAddresses(t *testing.T) {
284 th.SetupHTTP()
285 defer th.TeardownHTTP()
286 HandleAddressListSuccessfully(t)
287
288 expected := ListAddressesExpected
289 pages := 0
290 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
291 pages++
292
293 actual, err := ExtractAddresses(page)
294 th.AssertNoErr(t, err)
295
296 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700297 t.Fatalf("Expected 2 networks, got %d", len(actual))
298 }
299 th.CheckDeepEquals(t, expected, actual)
300
301 return true, nil
302 })
303 th.AssertNoErr(t, err)
304 th.CheckEquals(t, 1, pages)
305}
306
307func TestListAddressesByNetwork(t *testing.T) {
308 th.SetupHTTP()
309 defer th.TeardownHTTP()
310 HandleNetworkAddressListSuccessfully(t)
311
312 expected := ListNetworkAddressesExpected
313 pages := 0
314 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
315 pages++
316
317 actual, err := ExtractNetworkAddresses(page)
318 th.AssertNoErr(t, err)
319
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700320 if len(actual) != 2 {
321 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700322 }
323 th.CheckDeepEquals(t, expected, actual)
324
325 return true, nil
326 })
327 th.AssertNoErr(t, err)
328 th.CheckEquals(t, 1, pages)
329}
Kevin Pike92e10b52015-04-10 15:16:57 -0700330
331func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700332 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700333 contents := []byte("asdfasdf")
334
335 personality := Personality{
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700336 &File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700337 Path: name,
338 Contents: contents,
339 },
340 }
341
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700342 data, err := json.Marshal(personality)
343 if err != nil {
344 t.Fatal(err)
345 }
346
347 var actual []map[string]string
348 err = json.Unmarshal(data, &actual)
349 if err != nil {
350 t.Fatal(err)
351 }
352
353 if len(actual) != 1 {
354 t.Fatal("expected personality length 1")
355 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700356
357 if actual[0]["path"] != name {
358 t.Fatal("file path incorrect")
359 }
360
361 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
362 t.Fatal("file contents incorrect")
363 }
364}