blob: 7db6b933508d909bef129dc743ad82bb8e2facff [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
29 if len(actual) != 2 {
30 t.Fatalf("Expected 2 servers, got %d", len(actual))
31 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040032 th.CheckDeepEquals(t, ServerHerp, actual[0])
33 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040034
35 return true, nil
36 })
37
Ash Wilsone77ffb02014-10-20 13:10:26 -040038 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020039
Ash Wilsonad21c712014-09-25 10:15:22 -040040 if pages != 1 {
41 t.Errorf("Expected 1 page, saw %d", pages)
42 }
43}
44
Jon Perrittd27a9c72015-02-18 11:33:28 -070045func TestListAllServers(t *testing.T) {
46 th.SetupHTTP()
47 defer th.TeardownHTTP()
48 HandleServerListSuccessfully(t)
49
jrperritt3d966162016-06-06 14:08:54 -050050 allPages, err := servers.List(client.ServiceClient(), servers.ListOpts{}).AllPages()
Jon Perrittd27a9c72015-02-18 11:33:28 -070051 th.AssertNoErr(t, err)
jrperritt3d966162016-06-06 14:08:54 -050052 actual, err := servers.ExtractServers(allPages)
Jon Perrittd27a9c72015-02-18 11:33:28 -070053 th.AssertNoErr(t, err)
54 th.CheckDeepEquals(t, ServerHerp, actual[0])
55 th.CheckDeepEquals(t, ServerDerp, actual[1])
56}
57
Ash Wilsonad21c712014-09-25 10:15:22 -040058func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040059 th.SetupHTTP()
60 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040061 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040062
jrperritt3d966162016-06-06 14:08:54 -050063 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040064 Name: "derp",
65 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
66 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040067 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040068 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040069
Ash Wilsond3532cd2014-10-21 14:37:47 -040070 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040071}
72
Jon Perrittdb0ae142016-03-13 00:33:41 -060073func TestCreateServerWithCustomField(t *testing.T) {
74 th.SetupHTTP()
75 defer th.TeardownHTTP()
76 HandleServerCreationWithCustomFieldSuccessfully(t, SingleServerBody)
77
jrperritt3d966162016-06-06 14:08:54 -050078 actual, err := servers.Create(client.ServiceClient(), CreateOptsWithCustomField{
79 CreateOpts: servers.CreateOpts{
Jon Perrittdb0ae142016-03-13 00:33:41 -060080 Name: "derp",
81 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
82 FlavorRef: "1",
83 },
84 Foo: "bar",
85 }).Extract()
86 th.AssertNoErr(t, err)
87
88 th.CheckDeepEquals(t, ServerDerp, *actual)
89}
90
Ash Wilsonad21c712014-09-25 10:15:22 -040091func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040092 th.SetupHTTP()
93 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040094 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040095
jrperritt3d966162016-06-06 14:08:54 -050096 res := servers.Delete(client.ServiceClient(), "asdfasdfasdf")
Jamie Hannaford34732fe2014-10-27 11:29:36 +010097 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040098}
99
Ian Duffy370c4302016-01-21 10:44:56 +0000100func TestForceDeleteServer(t *testing.T) {
101 th.SetupHTTP()
102 defer th.TeardownHTTP()
103 HandleServerForceDeletionSuccessfully(t)
104
jrperritt3d966162016-06-06 14:08:54 -0500105 res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf")
Ian Duffy370c4302016-01-21 10:44:56 +0000106 th.AssertNoErr(t, res.Err)
107}
108
Ash Wilsonad21c712014-09-25 10:15:22 -0400109func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400110 th.SetupHTTP()
111 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400112 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400113
Ash Wilsone77ffb02014-10-20 13:10:26 -0400114 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500115 actual, err := servers.Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400116 if err != nil {
117 t.Fatalf("Unexpected Get error: %v", err)
118 }
119
Ash Wilsond3532cd2014-10-21 14:37:47 -0400120 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400121}
122
123func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400124 th.SetupHTTP()
125 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400126 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400127
Ash Wilsone77ffb02014-10-20 13:10:26 -0400128 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500129 actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400130 if err != nil {
131 t.Fatalf("Unexpected Update error: %v", err)
132 }
133
Ash Wilsond3532cd2014-10-21 14:37:47 -0400134 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400135}
136
137func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400138 th.SetupHTTP()
139 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400140 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400141
jrperritt3d966162016-06-06 14:08:54 -0500142 res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400143 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400144}
145
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100146func TestGetPassword(t *testing.T) {
147 th.SetupHTTP()
148 defer th.TeardownHTTP()
149 HandlePasswordGetSuccessfully(t)
150
jrperritt3d966162016-06-06 14:08:54 -0500151 res := servers.GetPassword(client.ServiceClient(), "1234asdf")
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100152 th.AssertNoErr(t, res.Err)
153}
154
Ash Wilsonad21c712014-09-25 10:15:22 -0400155func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400156 th.SetupHTTP()
157 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400158 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400159
jrperritt3d966162016-06-06 14:08:54 -0500160 res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{
161 Type: servers.SoftReboot,
Jon Perrittf094fef2016-03-07 01:41:59 -0600162 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400163 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400164}
165
166func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400167 th.SetupHTTP()
168 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400169 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400170
jrperritt3d966162016-06-06 14:08:54 -0500171 opts := servers.RebuildOpts{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200172 Name: "new-name",
173 AdminPass: "swordfish",
174 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
175 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400176 }
177
jrperritt3d966162016-06-06 14:08:54 -0500178 actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
Ash Wilsone77ffb02014-10-20 13:10:26 -0400179 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200180
Ash Wilsond3532cd2014-10-21 14:37:47 -0400181 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400182}
183
184func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400185 th.SetupHTTP()
186 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400187
Ash Wilsone77ffb02014-10-20 13:10:26 -0400188 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
189 th.TestMethod(t, r, "POST")
190 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
191 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400192
193 w.WriteHeader(http.StatusAccepted)
194 })
195
jrperritt3d966162016-06-06 14:08:54 -0500196 res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400197 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400198}
199
200func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400201 th.SetupHTTP()
202 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400203
Ash Wilsone77ffb02014-10-20 13:10:26 -0400204 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
205 th.TestMethod(t, r, "POST")
206 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
207 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400208
209 w.WriteHeader(http.StatusNoContent)
210 })
211
jrperritt3d966162016-06-06 14:08:54 -0500212 res := servers.ConfirmResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400213 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400214}
215
216func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400217 th.SetupHTTP()
218 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400219
Ash Wilsone77ffb02014-10-20 13:10:26 -0400220 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
221 th.TestMethod(t, r, "POST")
222 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
223 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400224
225 w.WriteHeader(http.StatusAccepted)
226 })
227
jrperritt3d966162016-06-06 14:08:54 -0500228 res := servers.RevertResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400229 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400230}
Alex Gaynor810d4892014-11-12 15:43:36 -0800231
232func TestRescue(t *testing.T) {
233 th.SetupHTTP()
234 defer th.TeardownHTTP()
235
Alex Gaynor6c003d22014-11-13 13:52:05 -0800236 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800237
jrperritt3d966162016-06-06 14:08:54 -0500238 res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800239 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800240 })
241 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800242 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800243 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800244}
Jon Perrittcc77da62014-11-16 13:14:21 -0700245
Jon Perritt78c57ce2014-11-20 11:07:18 -0700246func TestGetMetadatum(t *testing.T) {
247 th.SetupHTTP()
248 defer th.TeardownHTTP()
249
250 HandleMetadatumGetSuccessfully(t)
251
252 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500253 actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700254 th.AssertNoErr(t, err)
255 th.AssertDeepEquals(t, expected, actual)
256}
257
258func TestCreateMetadatum(t *testing.T) {
259 th.SetupHTTP()
260 defer th.TeardownHTTP()
261
262 HandleMetadatumCreateSuccessfully(t)
263
264 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500265 actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700266 th.AssertNoErr(t, err)
267 th.AssertDeepEquals(t, expected, actual)
268}
269
270func TestDeleteMetadatum(t *testing.T) {
271 th.SetupHTTP()
272 defer th.TeardownHTTP()
273
274 HandleMetadatumDeleteSuccessfully(t)
275
jrperritt3d966162016-06-06 14:08:54 -0500276 err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700277 th.AssertNoErr(t, err)
278}
279
Jon Perrittcc77da62014-11-16 13:14:21 -0700280func TestGetMetadata(t *testing.T) {
281 th.SetupHTTP()
282 defer th.TeardownHTTP()
283
284 HandleMetadataGetSuccessfully(t)
285
Jon Perritt78c57ce2014-11-20 11:07:18 -0700286 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500287 actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700288 th.AssertNoErr(t, err)
289 th.AssertDeepEquals(t, expected, actual)
290}
291
Jon Perritt789f8322014-11-21 08:20:04 -0700292func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700293 th.SetupHTTP()
294 defer th.TeardownHTTP()
295
Jon Perritt789f8322014-11-21 08:20:04 -0700296 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700297
Jon Perrittcc77da62014-11-16 13:14:21 -0700298 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500299 actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700300 "foo": "bar",
301 "this": "that",
302 }).Extract()
303 th.AssertNoErr(t, err)
304 th.AssertDeepEquals(t, expected, actual)
305}
306
Jon Perritt78c57ce2014-11-20 11:07:18 -0700307func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700308 th.SetupHTTP()
309 defer th.TeardownHTTP()
310
Jon Perritt78c57ce2014-11-20 11:07:18 -0700311 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700312
313 expected := map[string]string{"foo": "baz", "this": "those"}
jrperritt3d966162016-06-06 14:08:54 -0500314 actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700315 "foo": "baz",
316 "this": "those",
317 }).Extract()
318 th.AssertNoErr(t, err)
319 th.AssertDeepEquals(t, expected, actual)
320}
Jon Perritt5cb49482015-02-19 12:19:58 -0700321
322func TestListAddresses(t *testing.T) {
323 th.SetupHTTP()
324 defer th.TeardownHTTP()
325 HandleAddressListSuccessfully(t)
326
327 expected := ListAddressesExpected
328 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500329 err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt5cb49482015-02-19 12:19:58 -0700330 pages++
331
jrperritt3d966162016-06-06 14:08:54 -0500332 actual, err := servers.ExtractAddresses(page)
Jon Perritt5cb49482015-02-19 12:19:58 -0700333 th.AssertNoErr(t, err)
334
335 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700336 t.Fatalf("Expected 2 networks, got %d", len(actual))
337 }
338 th.CheckDeepEquals(t, expected, actual)
339
340 return true, nil
341 })
342 th.AssertNoErr(t, err)
343 th.CheckEquals(t, 1, pages)
344}
345
346func TestListAddressesByNetwork(t *testing.T) {
347 th.SetupHTTP()
348 defer th.TeardownHTTP()
349 HandleNetworkAddressListSuccessfully(t)
350
351 expected := ListNetworkAddressesExpected
352 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500353 err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt04d073c2015-02-19 21:46:23 -0700354 pages++
355
jrperritt3d966162016-06-06 14:08:54 -0500356 actual, err := servers.ExtractNetworkAddresses(page)
Jon Perritt04d073c2015-02-19 21:46:23 -0700357 th.AssertNoErr(t, err)
358
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700359 if len(actual) != 2 {
360 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700361 }
362 th.CheckDeepEquals(t, expected, actual)
363
364 return true, nil
365 })
366 th.AssertNoErr(t, err)
367 th.CheckEquals(t, 1, pages)
368}
Kevin Pike92e10b52015-04-10 15:16:57 -0700369
einarf2fc665e2015-04-16 20:16:21 +0000370func TestCreateServerImage(t *testing.T) {
371 th.SetupHTTP()
372 defer th.TeardownHTTP()
373 HandleCreateServerImageSuccessfully(t)
374
jrperritt3d966162016-06-06 14:08:54 -0500375 _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000376 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000377}
Kevin Pike25d45692015-04-23 17:20:09 -0700378
Kevin Pike92e10b52015-04-10 15:16:57 -0700379func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700380 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700381 contents := []byte("asdfasdf")
382
jrperritt3d966162016-06-06 14:08:54 -0500383 personality := servers.Personality{
384 &servers.File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700385 Path: name,
386 Contents: contents,
387 },
388 }
389
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700390 data, err := json.Marshal(personality)
391 if err != nil {
392 t.Fatal(err)
393 }
394
395 var actual []map[string]string
396 err = json.Unmarshal(data, &actual)
397 if err != nil {
398 t.Fatal(err)
399 }
400
401 if len(actual) != 1 {
402 t.Fatal("expected personality length 1")
403 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700404
405 if actual[0]["path"] != name {
406 t.Fatal("file path incorrect")
407 }
408
409 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
410 t.Fatal("file contents incorrect")
411 }
412}