blob: b651b92bcb2215a8f1618c6d4ca98f106d1afa41 [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
jrperritt0d7ed5d2016-08-16 11:23:26 -050092func TestCreateServerWithImageNameAndFlavorName(t *testing.T) {
93 th.SetupHTTP()
94 defer th.TeardownHTTP()
95 HandleServerCreationSuccessfully(t, SingleServerBody)
96
97 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
98 Name: "derp",
99 ImageName: "cirros-0.3.2-x86_64-disk",
100 FlavorName: "m1.tiny",
101 ServiceClient: client.ServiceClient(),
102 }).Extract()
103 th.AssertNoErr(t, err)
104
105 th.CheckDeepEquals(t, ServerDerp, *actual)
106}
107
Ash Wilsonad21c712014-09-25 10:15:22 -0400108func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400109 th.SetupHTTP()
110 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -0400111 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -0400112
jrperritt3d966162016-06-06 14:08:54 -0500113 res := servers.Delete(client.ServiceClient(), "asdfasdfasdf")
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100114 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400115}
116
Ian Duffy370c4302016-01-21 10:44:56 +0000117func TestForceDeleteServer(t *testing.T) {
118 th.SetupHTTP()
119 defer th.TeardownHTTP()
120 HandleServerForceDeletionSuccessfully(t)
121
jrperritt3d966162016-06-06 14:08:54 -0500122 res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf")
Ian Duffy370c4302016-01-21 10:44:56 +0000123 th.AssertNoErr(t, res.Err)
124}
125
Ash Wilsonad21c712014-09-25 10:15:22 -0400126func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400127 th.SetupHTTP()
128 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400129 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400130
Ash Wilsone77ffb02014-10-20 13:10:26 -0400131 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500132 actual, err := servers.Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400133 if err != nil {
134 t.Fatalf("Unexpected Get error: %v", err)
135 }
136
Ash Wilsond3532cd2014-10-21 14:37:47 -0400137 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400138}
139
140func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400141 th.SetupHTTP()
142 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400143 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400144
Ash Wilsone77ffb02014-10-20 13:10:26 -0400145 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500146 actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400147 if err != nil {
148 t.Fatalf("Unexpected Update error: %v", err)
149 }
150
Ash Wilsond3532cd2014-10-21 14:37:47 -0400151 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400152}
153
154func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400155 th.SetupHTTP()
156 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400157 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400158
jrperritt3d966162016-06-06 14:08:54 -0500159 res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400160 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400161}
162
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100163func TestGetPassword(t *testing.T) {
164 th.SetupHTTP()
165 defer th.TeardownHTTP()
166 HandlePasswordGetSuccessfully(t)
167
jrperritt3d966162016-06-06 14:08:54 -0500168 res := servers.GetPassword(client.ServiceClient(), "1234asdf")
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100169 th.AssertNoErr(t, res.Err)
170}
171
Ash Wilsonad21c712014-09-25 10:15:22 -0400172func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400173 th.SetupHTTP()
174 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400175 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400176
jrperritt3d966162016-06-06 14:08:54 -0500177 res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{
178 Type: servers.SoftReboot,
Jon Perrittf094fef2016-03-07 01:41:59 -0600179 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400180 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400181}
182
183func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400184 th.SetupHTTP()
185 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400186 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400187
jrperritt3d966162016-06-06 14:08:54 -0500188 opts := servers.RebuildOpts{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200189 Name: "new-name",
190 AdminPass: "swordfish",
191 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
192 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400193 }
194
jrperritt3d966162016-06-06 14:08:54 -0500195 actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
Ash Wilsone77ffb02014-10-20 13:10:26 -0400196 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200197
Ash Wilsond3532cd2014-10-21 14:37:47 -0400198 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400199}
200
201func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400202 th.SetupHTTP()
203 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400204
Ash Wilsone77ffb02014-10-20 13:10:26 -0400205 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
206 th.TestMethod(t, r, "POST")
207 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
208 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400209
210 w.WriteHeader(http.StatusAccepted)
211 })
212
jrperritt3d966162016-06-06 14:08:54 -0500213 res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400214 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400215}
216
217func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400218 th.SetupHTTP()
219 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400220
Ash Wilsone77ffb02014-10-20 13:10:26 -0400221 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
222 th.TestMethod(t, r, "POST")
223 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
224 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400225
226 w.WriteHeader(http.StatusNoContent)
227 })
228
jrperritt3d966162016-06-06 14:08:54 -0500229 res := servers.ConfirmResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400230 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400231}
232
233func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400234 th.SetupHTTP()
235 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400236
Ash Wilsone77ffb02014-10-20 13:10:26 -0400237 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
238 th.TestMethod(t, r, "POST")
239 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
240 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400241
242 w.WriteHeader(http.StatusAccepted)
243 })
244
jrperritt3d966162016-06-06 14:08:54 -0500245 res := servers.RevertResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400246 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400247}
Alex Gaynor810d4892014-11-12 15:43:36 -0800248
249func TestRescue(t *testing.T) {
250 th.SetupHTTP()
251 defer th.TeardownHTTP()
252
Alex Gaynor6c003d22014-11-13 13:52:05 -0800253 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800254
jrperritt3d966162016-06-06 14:08:54 -0500255 res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800256 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800257 })
258 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800259 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800260 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800261}
Jon Perrittcc77da62014-11-16 13:14:21 -0700262
Jon Perritt78c57ce2014-11-20 11:07:18 -0700263func TestGetMetadatum(t *testing.T) {
264 th.SetupHTTP()
265 defer th.TeardownHTTP()
266
267 HandleMetadatumGetSuccessfully(t)
268
269 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500270 actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700271 th.AssertNoErr(t, err)
272 th.AssertDeepEquals(t, expected, actual)
273}
274
275func TestCreateMetadatum(t *testing.T) {
276 th.SetupHTTP()
277 defer th.TeardownHTTP()
278
279 HandleMetadatumCreateSuccessfully(t)
280
281 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500282 actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700283 th.AssertNoErr(t, err)
284 th.AssertDeepEquals(t, expected, actual)
285}
286
287func TestDeleteMetadatum(t *testing.T) {
288 th.SetupHTTP()
289 defer th.TeardownHTTP()
290
291 HandleMetadatumDeleteSuccessfully(t)
292
jrperritt3d966162016-06-06 14:08:54 -0500293 err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700294 th.AssertNoErr(t, err)
295}
296
Jon Perrittcc77da62014-11-16 13:14:21 -0700297func TestGetMetadata(t *testing.T) {
298 th.SetupHTTP()
299 defer th.TeardownHTTP()
300
301 HandleMetadataGetSuccessfully(t)
302
Jon Perritt78c57ce2014-11-20 11:07:18 -0700303 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500304 actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700305 th.AssertNoErr(t, err)
306 th.AssertDeepEquals(t, expected, actual)
307}
308
Jon Perritt789f8322014-11-21 08:20:04 -0700309func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700310 th.SetupHTTP()
311 defer th.TeardownHTTP()
312
Jon Perritt789f8322014-11-21 08:20:04 -0700313 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700314
Jon Perrittcc77da62014-11-16 13:14:21 -0700315 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500316 actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700317 "foo": "bar",
318 "this": "that",
319 }).Extract()
320 th.AssertNoErr(t, err)
321 th.AssertDeepEquals(t, expected, actual)
322}
323
Jon Perritt78c57ce2014-11-20 11:07:18 -0700324func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700325 th.SetupHTTP()
326 defer th.TeardownHTTP()
327
Jon Perritt78c57ce2014-11-20 11:07:18 -0700328 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700329
330 expected := map[string]string{"foo": "baz", "this": "those"}
jrperritt3d966162016-06-06 14:08:54 -0500331 actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700332 "foo": "baz",
333 "this": "those",
334 }).Extract()
335 th.AssertNoErr(t, err)
336 th.AssertDeepEquals(t, expected, actual)
337}
Jon Perritt5cb49482015-02-19 12:19:58 -0700338
339func TestListAddresses(t *testing.T) {
340 th.SetupHTTP()
341 defer th.TeardownHTTP()
342 HandleAddressListSuccessfully(t)
343
344 expected := ListAddressesExpected
345 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500346 err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt5cb49482015-02-19 12:19:58 -0700347 pages++
348
jrperritt3d966162016-06-06 14:08:54 -0500349 actual, err := servers.ExtractAddresses(page)
Jon Perritt5cb49482015-02-19 12:19:58 -0700350 th.AssertNoErr(t, err)
351
352 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700353 t.Fatalf("Expected 2 networks, got %d", len(actual))
354 }
355 th.CheckDeepEquals(t, expected, actual)
356
357 return true, nil
358 })
359 th.AssertNoErr(t, err)
360 th.CheckEquals(t, 1, pages)
361}
362
363func TestListAddressesByNetwork(t *testing.T) {
364 th.SetupHTTP()
365 defer th.TeardownHTTP()
366 HandleNetworkAddressListSuccessfully(t)
367
368 expected := ListNetworkAddressesExpected
369 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500370 err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt04d073c2015-02-19 21:46:23 -0700371 pages++
372
jrperritt3d966162016-06-06 14:08:54 -0500373 actual, err := servers.ExtractNetworkAddresses(page)
Jon Perritt04d073c2015-02-19 21:46:23 -0700374 th.AssertNoErr(t, err)
375
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700376 if len(actual) != 2 {
377 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700378 }
379 th.CheckDeepEquals(t, expected, actual)
380
381 return true, nil
382 })
383 th.AssertNoErr(t, err)
384 th.CheckEquals(t, 1, pages)
385}
Kevin Pike92e10b52015-04-10 15:16:57 -0700386
einarf2fc665e2015-04-16 20:16:21 +0000387func TestCreateServerImage(t *testing.T) {
388 th.SetupHTTP()
389 defer th.TeardownHTTP()
390 HandleCreateServerImageSuccessfully(t)
391
jrperritt3d966162016-06-06 14:08:54 -0500392 _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000393 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000394}
Kevin Pike25d45692015-04-23 17:20:09 -0700395
Kevin Pike92e10b52015-04-10 15:16:57 -0700396func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700397 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700398 contents := []byte("asdfasdf")
399
jrperritt3d966162016-06-06 14:08:54 -0500400 personality := servers.Personality{
401 &servers.File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700402 Path: name,
403 Contents: contents,
404 },
405 }
406
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700407 data, err := json.Marshal(personality)
408 if err != nil {
409 t.Fatal(err)
410 }
411
412 var actual []map[string]string
413 err = json.Unmarshal(data, &actual)
414 if err != nil {
415 t.Fatal(err)
416 }
417
418 if len(actual) != 1 {
419 t.Fatal("expected personality length 1")
420 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700421
422 if actual[0]["path"] != name {
423 t.Fatal("file path incorrect")
424 }
425
426 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
427 t.Fatal("file contents incorrect")
428 }
429}