blob: 418a730f455beb1359591a662d911b9b82e14650 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Ash Wilsonad21c712014-09-25 10:15:22 -04002
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
jrperritt3d966162016-06-06 14:08:54 -05009 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
Jon Perritt27249f42016-02-18 10:35:59 -060010 "github.com/gophercloud/gophercloud/pagination"
11 th "github.com/gophercloud/gophercloud/testhelper"
12 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040013)
14
Ash Wilsonad21c712014-09-25 10:15:22 -040015func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040016 th.SetupHTTP()
17 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040018 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040019
Ash Wilsonad21c712014-09-25 10:15:22 -040020 pages := 0
jrperritt3d966162016-06-06 14:08:54 -050021 err := servers.List(client.ServiceClient(), servers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040022 pages++
23
jrperritt3d966162016-06-06 14:08:54 -050024 actual, err := servers.ExtractServers(page)
Ash Wilsonad21c712014-09-25 10:15:22 -040025 if err != nil {
26 return false, err
27 }
28
jrperritt1fa92502016-07-21 19:22:59 -050029 if len(actual) != 3 {
30 t.Fatalf("Expected 3 servers, got %d", len(actual))
Ash Wilsonad21c712014-09-25 10:15:22 -040031 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040032 th.CheckDeepEquals(t, ServerHerp, actual[0])
33 th.CheckDeepEquals(t, ServerDerp, actual[1])
jrperritt1fa92502016-07-21 19:22:59 -050034 th.CheckDeepEquals(t, ServerMerp, actual[2])
Ash Wilsonad21c712014-09-25 10:15:22 -040035
36 return true, nil
37 })
38
Ash Wilsone77ffb02014-10-20 13:10:26 -040039 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020040
Ash Wilsonad21c712014-09-25 10:15:22 -040041 if pages != 1 {
42 t.Errorf("Expected 1 page, saw %d", pages)
43 }
44}
45
Jon Perrittd27a9c72015-02-18 11:33:28 -070046func TestListAllServers(t *testing.T) {
47 th.SetupHTTP()
48 defer th.TeardownHTTP()
49 HandleServerListSuccessfully(t)
50
jrperritt3d966162016-06-06 14:08:54 -050051 allPages, err := servers.List(client.ServiceClient(), servers.ListOpts{}).AllPages()
Jon Perrittd27a9c72015-02-18 11:33:28 -070052 th.AssertNoErr(t, err)
jrperritt3d966162016-06-06 14:08:54 -050053 actual, err := servers.ExtractServers(allPages)
Jon Perrittd27a9c72015-02-18 11:33:28 -070054 th.AssertNoErr(t, err)
55 th.CheckDeepEquals(t, ServerHerp, actual[0])
56 th.CheckDeepEquals(t, ServerDerp, actual[1])
57}
58
Ash Wilsonad21c712014-09-25 10:15:22 -040059func 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
jrperritt3d966162016-06-06 14:08:54 -050064 actual, err := servers.Create(client.ServiceClient(), servers.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
Jon Perrittdb0ae142016-03-13 00:33:41 -060074func TestCreateServerWithCustomField(t *testing.T) {
75 th.SetupHTTP()
76 defer th.TeardownHTTP()
77 HandleServerCreationWithCustomFieldSuccessfully(t, SingleServerBody)
78
jrperritt3d966162016-06-06 14:08:54 -050079 actual, err := servers.Create(client.ServiceClient(), CreateOptsWithCustomField{
80 CreateOpts: servers.CreateOpts{
Jon Perrittdb0ae142016-03-13 00:33:41 -060081 Name: "derp",
82 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
83 FlavorRef: "1",
84 },
85 Foo: "bar",
86 }).Extract()
87 th.AssertNoErr(t, err)
88
89 th.CheckDeepEquals(t, ServerDerp, *actual)
90}
91
Joe Topjianf464c962016-09-12 08:02:43 -060092func TestCreateServerWithMetadata(t *testing.T) {
93 th.SetupHTTP()
94 defer th.TeardownHTTP()
95 HandleServerCreationWithMetadata(t, SingleServerBody)
96
97 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
98 Name: "derp",
99 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
100 FlavorRef: "1",
101 Metadata: map[string]string{
102 "abc": "def",
103 },
104 }).Extract()
105 th.AssertNoErr(t, err)
106
107 th.CheckDeepEquals(t, ServerDerp, *actual)
108}
109
jrperritt0d7ed5d2016-08-16 11:23:26 -0500110func TestCreateServerWithImageNameAndFlavorName(t *testing.T) {
111 th.SetupHTTP()
112 defer th.TeardownHTTP()
113 HandleServerCreationSuccessfully(t, SingleServerBody)
114
115 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
116 Name: "derp",
117 ImageName: "cirros-0.3.2-x86_64-disk",
118 FlavorName: "m1.tiny",
119 ServiceClient: client.ServiceClient(),
120 }).Extract()
121 th.AssertNoErr(t, err)
122
123 th.CheckDeepEquals(t, ServerDerp, *actual)
124}
125
Ash Wilsonad21c712014-09-25 10:15:22 -0400126func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400127 th.SetupHTTP()
128 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -0400129 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -0400130
jrperritt3d966162016-06-06 14:08:54 -0500131 res := servers.Delete(client.ServiceClient(), "asdfasdfasdf")
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100132 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400133}
134
Ian Duffy370c4302016-01-21 10:44:56 +0000135func TestForceDeleteServer(t *testing.T) {
136 th.SetupHTTP()
137 defer th.TeardownHTTP()
138 HandleServerForceDeletionSuccessfully(t)
139
jrperritt3d966162016-06-06 14:08:54 -0500140 res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf")
Ian Duffy370c4302016-01-21 10:44:56 +0000141 th.AssertNoErr(t, res.Err)
142}
143
Ash Wilsonad21c712014-09-25 10:15:22 -0400144func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400145 th.SetupHTTP()
146 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400147 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400148
Ash Wilsone77ffb02014-10-20 13:10:26 -0400149 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500150 actual, err := servers.Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400151 if err != nil {
152 t.Fatalf("Unexpected Get error: %v", err)
153 }
154
Ash Wilsond3532cd2014-10-21 14:37:47 -0400155 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400156}
157
158func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400159 th.SetupHTTP()
160 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400161 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400162
Ash Wilsone77ffb02014-10-20 13:10:26 -0400163 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500164 actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400165 if err != nil {
166 t.Fatalf("Unexpected Update error: %v", err)
167 }
168
Ash Wilsond3532cd2014-10-21 14:37:47 -0400169 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400170}
171
172func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400173 th.SetupHTTP()
174 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400175 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400176
jrperritt3d966162016-06-06 14:08:54 -0500177 res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400178 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400179}
180
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100181func TestGetPassword(t *testing.T) {
182 th.SetupHTTP()
183 defer th.TeardownHTTP()
184 HandlePasswordGetSuccessfully(t)
185
jrperritt3d966162016-06-06 14:08:54 -0500186 res := servers.GetPassword(client.ServiceClient(), "1234asdf")
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100187 th.AssertNoErr(t, res.Err)
188}
189
Ash Wilsonad21c712014-09-25 10:15:22 -0400190func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400191 th.SetupHTTP()
192 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400193 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400194
jrperritt3d966162016-06-06 14:08:54 -0500195 res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{
196 Type: servers.SoftReboot,
Jon Perrittf094fef2016-03-07 01:41:59 -0600197 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400198 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400199}
200
201func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400202 th.SetupHTTP()
203 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400204 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400205
jrperritt3d966162016-06-06 14:08:54 -0500206 opts := servers.RebuildOpts{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200207 Name: "new-name",
208 AdminPass: "swordfish",
209 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
210 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400211 }
212
jrperritt3d966162016-06-06 14:08:54 -0500213 actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
Ash Wilsone77ffb02014-10-20 13:10:26 -0400214 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200215
Ash Wilsond3532cd2014-10-21 14:37:47 -0400216 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400217}
218
219func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400220 th.SetupHTTP()
221 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400222
Ash Wilsone77ffb02014-10-20 13:10:26 -0400223 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
224 th.TestMethod(t, r, "POST")
225 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
226 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400227
228 w.WriteHeader(http.StatusAccepted)
229 })
230
jrperritt3d966162016-06-06 14:08:54 -0500231 res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400232 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400233}
234
235func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400236 th.SetupHTTP()
237 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400238
Ash Wilsone77ffb02014-10-20 13:10:26 -0400239 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
240 th.TestMethod(t, r, "POST")
241 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
242 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400243
244 w.WriteHeader(http.StatusNoContent)
245 })
246
jrperritt3d966162016-06-06 14:08:54 -0500247 res := servers.ConfirmResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400248 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400249}
250
251func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400252 th.SetupHTTP()
253 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400254
Ash Wilsone77ffb02014-10-20 13:10:26 -0400255 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
256 th.TestMethod(t, r, "POST")
257 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
258 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400259
260 w.WriteHeader(http.StatusAccepted)
261 })
262
jrperritt3d966162016-06-06 14:08:54 -0500263 res := servers.RevertResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400264 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400265}
Alex Gaynor810d4892014-11-12 15:43:36 -0800266
267func TestRescue(t *testing.T) {
268 th.SetupHTTP()
269 defer th.TeardownHTTP()
270
Alex Gaynor6c003d22014-11-13 13:52:05 -0800271 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800272
jrperritt3d966162016-06-06 14:08:54 -0500273 res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800274 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800275 })
276 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800277 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800278 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800279}
Jon Perrittcc77da62014-11-16 13:14:21 -0700280
Jon Perritt78c57ce2014-11-20 11:07:18 -0700281func TestGetMetadatum(t *testing.T) {
282 th.SetupHTTP()
283 defer th.TeardownHTTP()
284
285 HandleMetadatumGetSuccessfully(t)
286
287 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500288 actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700289 th.AssertNoErr(t, err)
290 th.AssertDeepEquals(t, expected, actual)
291}
292
293func TestCreateMetadatum(t *testing.T) {
294 th.SetupHTTP()
295 defer th.TeardownHTTP()
296
297 HandleMetadatumCreateSuccessfully(t)
298
299 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500300 actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700301 th.AssertNoErr(t, err)
302 th.AssertDeepEquals(t, expected, actual)
303}
304
305func TestDeleteMetadatum(t *testing.T) {
306 th.SetupHTTP()
307 defer th.TeardownHTTP()
308
309 HandleMetadatumDeleteSuccessfully(t)
310
jrperritt3d966162016-06-06 14:08:54 -0500311 err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700312 th.AssertNoErr(t, err)
313}
314
Jon Perrittcc77da62014-11-16 13:14:21 -0700315func TestGetMetadata(t *testing.T) {
316 th.SetupHTTP()
317 defer th.TeardownHTTP()
318
319 HandleMetadataGetSuccessfully(t)
320
Jon Perritt78c57ce2014-11-20 11:07:18 -0700321 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500322 actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700323 th.AssertNoErr(t, err)
324 th.AssertDeepEquals(t, expected, actual)
325}
326
Jon Perritt789f8322014-11-21 08:20:04 -0700327func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700328 th.SetupHTTP()
329 defer th.TeardownHTTP()
330
Jon Perritt789f8322014-11-21 08:20:04 -0700331 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700332
Jon Perrittcc77da62014-11-16 13:14:21 -0700333 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500334 actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700335 "foo": "bar",
336 "this": "that",
337 }).Extract()
338 th.AssertNoErr(t, err)
339 th.AssertDeepEquals(t, expected, actual)
340}
341
Jon Perritt78c57ce2014-11-20 11:07:18 -0700342func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700343 th.SetupHTTP()
344 defer th.TeardownHTTP()
345
Jon Perritt78c57ce2014-11-20 11:07:18 -0700346 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700347
348 expected := map[string]string{"foo": "baz", "this": "those"}
jrperritt3d966162016-06-06 14:08:54 -0500349 actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700350 "foo": "baz",
351 "this": "those",
352 }).Extract()
353 th.AssertNoErr(t, err)
354 th.AssertDeepEquals(t, expected, actual)
355}
Jon Perritt5cb49482015-02-19 12:19:58 -0700356
357func TestListAddresses(t *testing.T) {
358 th.SetupHTTP()
359 defer th.TeardownHTTP()
360 HandleAddressListSuccessfully(t)
361
362 expected := ListAddressesExpected
363 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500364 err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt5cb49482015-02-19 12:19:58 -0700365 pages++
366
jrperritt3d966162016-06-06 14:08:54 -0500367 actual, err := servers.ExtractAddresses(page)
Jon Perritt5cb49482015-02-19 12:19:58 -0700368 th.AssertNoErr(t, err)
369
370 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700371 t.Fatalf("Expected 2 networks, got %d", len(actual))
372 }
373 th.CheckDeepEquals(t, expected, actual)
374
375 return true, nil
376 })
377 th.AssertNoErr(t, err)
378 th.CheckEquals(t, 1, pages)
379}
380
381func TestListAddressesByNetwork(t *testing.T) {
382 th.SetupHTTP()
383 defer th.TeardownHTTP()
384 HandleNetworkAddressListSuccessfully(t)
385
386 expected := ListNetworkAddressesExpected
387 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500388 err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt04d073c2015-02-19 21:46:23 -0700389 pages++
390
jrperritt3d966162016-06-06 14:08:54 -0500391 actual, err := servers.ExtractNetworkAddresses(page)
Jon Perritt04d073c2015-02-19 21:46:23 -0700392 th.AssertNoErr(t, err)
393
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700394 if len(actual) != 2 {
395 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700396 }
397 th.CheckDeepEquals(t, expected, actual)
398
399 return true, nil
400 })
401 th.AssertNoErr(t, err)
402 th.CheckEquals(t, 1, pages)
403}
Kevin Pike92e10b52015-04-10 15:16:57 -0700404
einarf2fc665e2015-04-16 20:16:21 +0000405func TestCreateServerImage(t *testing.T) {
406 th.SetupHTTP()
407 defer th.TeardownHTTP()
408 HandleCreateServerImageSuccessfully(t)
409
jrperritt3d966162016-06-06 14:08:54 -0500410 _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000411 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000412}
Kevin Pike25d45692015-04-23 17:20:09 -0700413
Kevin Pike92e10b52015-04-10 15:16:57 -0700414func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700415 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700416 contents := []byte("asdfasdf")
417
jrperritt3d966162016-06-06 14:08:54 -0500418 personality := servers.Personality{
419 &servers.File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700420 Path: name,
421 Contents: contents,
422 },
423 }
424
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700425 data, err := json.Marshal(personality)
426 if err != nil {
427 t.Fatal(err)
428 }
429
430 var actual []map[string]string
431 err = json.Unmarshal(data, &actual)
432 if err != nil {
433 t.Fatal(err)
434 }
435
436 if len(actual) != 1 {
437 t.Fatal("expected personality length 1")
438 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700439
440 if actual[0]["path"] != name {
441 t.Fatal("file path incorrect")
442 }
443
444 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
445 t.Fatal("file contents incorrect")
446 }
447}