blob: 6d792d5fb1d3e87579ed8844f2a5e48b9323010c [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
Ash Wilsonad21c712014-09-25 10:15:22 -040092func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040093 th.SetupHTTP()
94 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040095 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040096
jrperritt3d966162016-06-06 14:08:54 -050097 res := servers.Delete(client.ServiceClient(), "asdfasdfasdf")
Jamie Hannaford34732fe2014-10-27 11:29:36 +010098 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040099}
100
Ian Duffy370c4302016-01-21 10:44:56 +0000101func TestForceDeleteServer(t *testing.T) {
102 th.SetupHTTP()
103 defer th.TeardownHTTP()
104 HandleServerForceDeletionSuccessfully(t)
105
jrperritt3d966162016-06-06 14:08:54 -0500106 res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf")
Ian Duffy370c4302016-01-21 10:44:56 +0000107 th.AssertNoErr(t, res.Err)
108}
109
Ash Wilsonad21c712014-09-25 10:15:22 -0400110func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400111 th.SetupHTTP()
112 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400113 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400114
Ash Wilsone77ffb02014-10-20 13:10:26 -0400115 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500116 actual, err := servers.Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400117 if err != nil {
118 t.Fatalf("Unexpected Get error: %v", err)
119 }
120
Ash Wilsond3532cd2014-10-21 14:37:47 -0400121 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400122}
123
124func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400125 th.SetupHTTP()
126 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400127 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400128
Ash Wilsone77ffb02014-10-20 13:10:26 -0400129 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500130 actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400131 if err != nil {
132 t.Fatalf("Unexpected Update error: %v", err)
133 }
134
Ash Wilsond3532cd2014-10-21 14:37:47 -0400135 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400136}
137
138func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400139 th.SetupHTTP()
140 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400141 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400142
jrperritt3d966162016-06-06 14:08:54 -0500143 res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400144 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400145}
146
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100147func TestGetPassword(t *testing.T) {
148 th.SetupHTTP()
149 defer th.TeardownHTTP()
150 HandlePasswordGetSuccessfully(t)
151
jrperritt3d966162016-06-06 14:08:54 -0500152 res := servers.GetPassword(client.ServiceClient(), "1234asdf")
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100153 th.AssertNoErr(t, res.Err)
154}
155
Ash Wilsonad21c712014-09-25 10:15:22 -0400156func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400157 th.SetupHTTP()
158 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400159 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400160
jrperritt3d966162016-06-06 14:08:54 -0500161 res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{
162 Type: servers.SoftReboot,
Jon Perrittf094fef2016-03-07 01:41:59 -0600163 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400164 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400165}
166
167func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400168 th.SetupHTTP()
169 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400170 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400171
jrperritt3d966162016-06-06 14:08:54 -0500172 opts := servers.RebuildOpts{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200173 Name: "new-name",
174 AdminPass: "swordfish",
175 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
176 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400177 }
178
jrperritt3d966162016-06-06 14:08:54 -0500179 actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
Ash Wilsone77ffb02014-10-20 13:10:26 -0400180 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200181
Ash Wilsond3532cd2014-10-21 14:37:47 -0400182 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400183}
184
185func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400186 th.SetupHTTP()
187 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400188
Ash Wilsone77ffb02014-10-20 13:10:26 -0400189 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
190 th.TestMethod(t, r, "POST")
191 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
192 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400193
194 w.WriteHeader(http.StatusAccepted)
195 })
196
jrperritt3d966162016-06-06 14:08:54 -0500197 res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400198 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400199}
200
201func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400202 th.SetupHTTP()
203 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -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, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400209
210 w.WriteHeader(http.StatusNoContent)
211 })
212
jrperritt3d966162016-06-06 14:08:54 -0500213 res := servers.ConfirmResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400214 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400215}
216
217func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400218 th.SetupHTTP()
219 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -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, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400225
226 w.WriteHeader(http.StatusAccepted)
227 })
228
jrperritt3d966162016-06-06 14:08:54 -0500229 res := servers.RevertResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400230 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400231}
Alex Gaynor810d4892014-11-12 15:43:36 -0800232
233func TestRescue(t *testing.T) {
234 th.SetupHTTP()
235 defer th.TeardownHTTP()
236
Alex Gaynor6c003d22014-11-13 13:52:05 -0800237 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800238
jrperritt3d966162016-06-06 14:08:54 -0500239 res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800240 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800241 })
242 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800243 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800244 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800245}
Jon Perrittcc77da62014-11-16 13:14:21 -0700246
Jon Perritt78c57ce2014-11-20 11:07:18 -0700247func TestGetMetadatum(t *testing.T) {
248 th.SetupHTTP()
249 defer th.TeardownHTTP()
250
251 HandleMetadatumGetSuccessfully(t)
252
253 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500254 actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700255 th.AssertNoErr(t, err)
256 th.AssertDeepEquals(t, expected, actual)
257}
258
259func TestCreateMetadatum(t *testing.T) {
260 th.SetupHTTP()
261 defer th.TeardownHTTP()
262
263 HandleMetadatumCreateSuccessfully(t)
264
265 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500266 actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700267 th.AssertNoErr(t, err)
268 th.AssertDeepEquals(t, expected, actual)
269}
270
271func TestDeleteMetadatum(t *testing.T) {
272 th.SetupHTTP()
273 defer th.TeardownHTTP()
274
275 HandleMetadatumDeleteSuccessfully(t)
276
jrperritt3d966162016-06-06 14:08:54 -0500277 err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700278 th.AssertNoErr(t, err)
279}
280
Jon Perrittcc77da62014-11-16 13:14:21 -0700281func TestGetMetadata(t *testing.T) {
282 th.SetupHTTP()
283 defer th.TeardownHTTP()
284
285 HandleMetadataGetSuccessfully(t)
286
Jon Perritt78c57ce2014-11-20 11:07:18 -0700287 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500288 actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700289 th.AssertNoErr(t, err)
290 th.AssertDeepEquals(t, expected, actual)
291}
292
Jon Perritt789f8322014-11-21 08:20:04 -0700293func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700294 th.SetupHTTP()
295 defer th.TeardownHTTP()
296
Jon Perritt789f8322014-11-21 08:20:04 -0700297 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700298
Jon Perrittcc77da62014-11-16 13:14:21 -0700299 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500300 actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700301 "foo": "bar",
302 "this": "that",
303 }).Extract()
304 th.AssertNoErr(t, err)
305 th.AssertDeepEquals(t, expected, actual)
306}
307
Jon Perritt78c57ce2014-11-20 11:07:18 -0700308func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700309 th.SetupHTTP()
310 defer th.TeardownHTTP()
311
Jon Perritt78c57ce2014-11-20 11:07:18 -0700312 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700313
314 expected := map[string]string{"foo": "baz", "this": "those"}
jrperritt3d966162016-06-06 14:08:54 -0500315 actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700316 "foo": "baz",
317 "this": "those",
318 }).Extract()
319 th.AssertNoErr(t, err)
320 th.AssertDeepEquals(t, expected, actual)
321}
Jon Perritt5cb49482015-02-19 12:19:58 -0700322
323func TestListAddresses(t *testing.T) {
324 th.SetupHTTP()
325 defer th.TeardownHTTP()
326 HandleAddressListSuccessfully(t)
327
328 expected := ListAddressesExpected
329 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500330 err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt5cb49482015-02-19 12:19:58 -0700331 pages++
332
jrperritt3d966162016-06-06 14:08:54 -0500333 actual, err := servers.ExtractAddresses(page)
Jon Perritt5cb49482015-02-19 12:19:58 -0700334 th.AssertNoErr(t, err)
335
336 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700337 t.Fatalf("Expected 2 networks, got %d", len(actual))
338 }
339 th.CheckDeepEquals(t, expected, actual)
340
341 return true, nil
342 })
343 th.AssertNoErr(t, err)
344 th.CheckEquals(t, 1, pages)
345}
346
347func TestListAddressesByNetwork(t *testing.T) {
348 th.SetupHTTP()
349 defer th.TeardownHTTP()
350 HandleNetworkAddressListSuccessfully(t)
351
352 expected := ListNetworkAddressesExpected
353 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500354 err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt04d073c2015-02-19 21:46:23 -0700355 pages++
356
jrperritt3d966162016-06-06 14:08:54 -0500357 actual, err := servers.ExtractNetworkAddresses(page)
Jon Perritt04d073c2015-02-19 21:46:23 -0700358 th.AssertNoErr(t, err)
359
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700360 if len(actual) != 2 {
361 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700362 }
363 th.CheckDeepEquals(t, expected, actual)
364
365 return true, nil
366 })
367 th.AssertNoErr(t, err)
368 th.CheckEquals(t, 1, pages)
369}
Kevin Pike92e10b52015-04-10 15:16:57 -0700370
einarf2fc665e2015-04-16 20:16:21 +0000371func TestCreateServerImage(t *testing.T) {
372 th.SetupHTTP()
373 defer th.TeardownHTTP()
374 HandleCreateServerImageSuccessfully(t)
375
jrperritt3d966162016-06-06 14:08:54 -0500376 _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000377 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000378}
Kevin Pike25d45692015-04-23 17:20:09 -0700379
Kevin Pike92e10b52015-04-10 15:16:57 -0700380func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700381 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700382 contents := []byte("asdfasdf")
383
jrperritt3d966162016-06-06 14:08:54 -0500384 personality := servers.Personality{
385 &servers.File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700386 Path: name,
387 Contents: contents,
388 },
389 }
390
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700391 data, err := json.Marshal(personality)
392 if err != nil {
393 t.Fatal(err)
394 }
395
396 var actual []map[string]string
397 err = json.Unmarshal(data, &actual)
398 if err != nil {
399 t.Fatal(err)
400 }
401
402 if len(actual) != 1 {
403 t.Fatal("expected personality length 1")
404 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700405
406 if actual[0]["path"] != name {
407 t.Fatal("file path incorrect")
408 }
409
410 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
411 t.Fatal("file contents incorrect")
412 }
413}