blob: 6e23c52bb5a9d26d7ae0b395871b3459c3ce7dcc [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
Ian Duffy370c4302016-01-21 10:44:56 +000081func TestForceDeleteServer(t *testing.T) {
82 th.SetupHTTP()
83 defer th.TeardownHTTP()
84 HandleServerForceDeletionSuccessfully(t)
85
86 res := ForceDelete(client.ServiceClient(), "asdfasdfasdf")
87 th.AssertNoErr(t, res.Err)
88}
89
Ash Wilsonad21c712014-09-25 10:15:22 -040090func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040091 th.SetupHTTP()
92 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040093 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040094
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 Wilson189c95c2014-10-23 11:41:35 -0400107 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400108
Ash Wilsone77ffb02014-10-20 13:10:26 -0400109 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400110 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400111 if err != nil {
112 t.Fatalf("Unexpected Update error: %v", err)
113 }
114
Ash Wilsond3532cd2014-10-21 14:37:47 -0400115 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400116}
117
118func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400119 th.SetupHTTP()
120 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400121 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400122
Ash Wilsone77ffb02014-10-20 13:10:26 -0400123 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
124 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400125}
126
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100127func TestGetPassword(t *testing.T) {
128 th.SetupHTTP()
129 defer th.TeardownHTTP()
130 HandlePasswordGetSuccessfully(t)
131
132 res := GetPassword(client.ServiceClient(), "1234asdf")
133 th.AssertNoErr(t, res.Err)
134}
135
Ash Wilsonad21c712014-09-25 10:15:22 -0400136func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400137 th.SetupHTTP()
138 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400139 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400140
Ash Wilsone77ffb02014-10-20 13:10:26 -0400141 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
142 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400143}
144
145func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400146 th.SetupHTTP()
147 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400148 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400149
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200150 opts := RebuildOpts{
151 Name: "new-name",
152 AdminPass: "swordfish",
153 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
154 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400155 }
156
Ash Wilsone77ffb02014-10-20 13:10:26 -0400157 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
158 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200159
Ash Wilsond3532cd2014-10-21 14:37:47 -0400160 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400161}
162
163func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400164 th.SetupHTTP()
165 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400166
Ash Wilsone77ffb02014-10-20 13:10:26 -0400167 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
168 th.TestMethod(t, r, "POST")
169 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
170 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400171
172 w.WriteHeader(http.StatusAccepted)
173 })
174
Ash Wilson5f7cf182014-10-23 08:35:24 -0400175 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400176 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400177}
178
179func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400180 th.SetupHTTP()
181 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400182
Ash Wilsone77ffb02014-10-20 13:10:26 -0400183 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
184 th.TestMethod(t, r, "POST")
185 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
186 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400187
188 w.WriteHeader(http.StatusNoContent)
189 })
190
Ash Wilsone77ffb02014-10-20 13:10:26 -0400191 res := ConfirmResize(client.ServiceClient(), "1234asdf")
192 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400193}
194
195func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400196 th.SetupHTTP()
197 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -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, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400203
204 w.WriteHeader(http.StatusAccepted)
205 })
206
Ash Wilsone77ffb02014-10-20 13:10:26 -0400207 res := RevertResize(client.ServiceClient(), "1234asdf")
208 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400209}
Alex Gaynor810d4892014-11-12 15:43:36 -0800210
211func TestRescue(t *testing.T) {
212 th.SetupHTTP()
213 defer th.TeardownHTTP()
214
Alex Gaynor6c003d22014-11-13 13:52:05 -0800215 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800216
Alex Gaynor40449ed2014-11-12 16:28:06 -0800217 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800218 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800219 })
220 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800221 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800222 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800223}
Jon Perrittcc77da62014-11-16 13:14:21 -0700224
Jon Perritt78c57ce2014-11-20 11:07:18 -0700225func TestGetMetadatum(t *testing.T) {
226 th.SetupHTTP()
227 defer th.TeardownHTTP()
228
229 HandleMetadatumGetSuccessfully(t)
230
231 expected := map[string]string{"foo": "bar"}
232 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
233 th.AssertNoErr(t, err)
234 th.AssertDeepEquals(t, expected, actual)
235}
236
237func TestCreateMetadatum(t *testing.T) {
238 th.SetupHTTP()
239 defer th.TeardownHTTP()
240
241 HandleMetadatumCreateSuccessfully(t)
242
243 expected := map[string]string{"foo": "bar"}
244 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
245 th.AssertNoErr(t, err)
246 th.AssertDeepEquals(t, expected, actual)
247}
248
249func TestDeleteMetadatum(t *testing.T) {
250 th.SetupHTTP()
251 defer th.TeardownHTTP()
252
253 HandleMetadatumDeleteSuccessfully(t)
254
255 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
256 th.AssertNoErr(t, err)
257}
258
Jon Perrittcc77da62014-11-16 13:14:21 -0700259func TestGetMetadata(t *testing.T) {
260 th.SetupHTTP()
261 defer th.TeardownHTTP()
262
263 HandleMetadataGetSuccessfully(t)
264
Jon Perritt78c57ce2014-11-20 11:07:18 -0700265 expected := map[string]string{"foo": "bar", "this": "that"}
266 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700267 th.AssertNoErr(t, err)
268 th.AssertDeepEquals(t, expected, actual)
269}
270
Jon Perritt789f8322014-11-21 08:20:04 -0700271func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700272 th.SetupHTTP()
273 defer th.TeardownHTTP()
274
Jon Perritt789f8322014-11-21 08:20:04 -0700275 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700276
Jon Perrittcc77da62014-11-16 13:14:21 -0700277 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700278 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700279 "foo": "bar",
280 "this": "that",
281 }).Extract()
282 th.AssertNoErr(t, err)
283 th.AssertDeepEquals(t, expected, actual)
284}
285
Jon Perritt78c57ce2014-11-20 11:07:18 -0700286func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700287 th.SetupHTTP()
288 defer th.TeardownHTTP()
289
Jon Perritt78c57ce2014-11-20 11:07:18 -0700290 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700291
292 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700293 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700294 "foo": "baz",
295 "this": "those",
296 }).Extract()
297 th.AssertNoErr(t, err)
298 th.AssertDeepEquals(t, expected, actual)
299}
Jon Perritt5cb49482015-02-19 12:19:58 -0700300
301func TestListAddresses(t *testing.T) {
302 th.SetupHTTP()
303 defer th.TeardownHTTP()
304 HandleAddressListSuccessfully(t)
305
306 expected := ListAddressesExpected
307 pages := 0
308 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
309 pages++
310
311 actual, err := ExtractAddresses(page)
312 th.AssertNoErr(t, err)
313
314 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700315 t.Fatalf("Expected 2 networks, got %d", len(actual))
316 }
317 th.CheckDeepEquals(t, expected, actual)
318
319 return true, nil
320 })
321 th.AssertNoErr(t, err)
322 th.CheckEquals(t, 1, pages)
323}
324
325func TestListAddressesByNetwork(t *testing.T) {
326 th.SetupHTTP()
327 defer th.TeardownHTTP()
328 HandleNetworkAddressListSuccessfully(t)
329
330 expected := ListNetworkAddressesExpected
331 pages := 0
332 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
333 pages++
334
335 actual, err := ExtractNetworkAddresses(page)
336 th.AssertNoErr(t, err)
337
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700338 if len(actual) != 2 {
339 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700340 }
341 th.CheckDeepEquals(t, expected, actual)
342
343 return true, nil
344 })
345 th.AssertNoErr(t, err)
346 th.CheckEquals(t, 1, pages)
347}
Kevin Pike92e10b52015-04-10 15:16:57 -0700348
einarf2fc665e2015-04-16 20:16:21 +0000349func TestCreateServerImage(t *testing.T) {
350 th.SetupHTTP()
351 defer th.TeardownHTTP()
352 HandleCreateServerImageSuccessfully(t)
353
einarf4e5fdaf2015-04-16 23:14:59 +0000354 _, err := CreateImage(client.ServiceClient(), "serverimage", CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000355 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000356}
Kevin Pike25d45692015-04-23 17:20:09 -0700357
Kevin Pike92e10b52015-04-10 15:16:57 -0700358func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700359 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700360 contents := []byte("asdfasdf")
361
362 personality := Personality{
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700363 &File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700364 Path: name,
365 Contents: contents,
366 },
367 }
368
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700369 data, err := json.Marshal(personality)
370 if err != nil {
371 t.Fatal(err)
372 }
373
374 var actual []map[string]string
375 err = json.Unmarshal(data, &actual)
376 if err != nil {
377 t.Fatal(err)
378 }
379
380 if len(actual) != 1 {
381 t.Fatal("expected personality length 1")
382 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700383
384 if actual[0]["path"] != name {
385 t.Fatal("file path incorrect")
386 }
387
388 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
389 t.Fatal("file contents incorrect")
390 }
391}