blob: 86585102b9d68018eab3c4762e02245432c16b00 [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
Krzysztof Szukiełojć24a29ce2017-05-07 14:24:02 +02009 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/compute/v2/extensions/availabilityzones"
10 "gerrit.mcp.mirantis.net/debian/gophercloud.git/openstack/compute/v2/servers"
11 "gerrit.mcp.mirantis.net/debian/gophercloud.git/pagination"
12 th "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper"
13 "gerrit.mcp.mirantis.net/debian/gophercloud.git/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040014)
15
Ash Wilsonad21c712014-09-25 10:15:22 -040016func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040017 th.SetupHTTP()
18 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040019 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040020
Ash Wilsonad21c712014-09-25 10:15:22 -040021 pages := 0
jrperritt3d966162016-06-06 14:08:54 -050022 err := servers.List(client.ServiceClient(), servers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040023 pages++
24
jrperritt3d966162016-06-06 14:08:54 -050025 actual, err := servers.ExtractServers(page)
Ash Wilsonad21c712014-09-25 10:15:22 -040026 if err != nil {
27 return false, err
28 }
29
jrperritt1fa92502016-07-21 19:22:59 -050030 if len(actual) != 3 {
31 t.Fatalf("Expected 3 servers, got %d", len(actual))
Ash Wilsonad21c712014-09-25 10:15:22 -040032 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040033 th.CheckDeepEquals(t, ServerHerp, actual[0])
34 th.CheckDeepEquals(t, ServerDerp, actual[1])
jrperritt1fa92502016-07-21 19:22:59 -050035 th.CheckDeepEquals(t, ServerMerp, actual[2])
Ash Wilsonad21c712014-09-25 10:15:22 -040036
37 return true, nil
38 })
39
Ash Wilsone77ffb02014-10-20 13:10:26 -040040 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020041
Ash Wilsonad21c712014-09-25 10:15:22 -040042 if pages != 1 {
43 t.Errorf("Expected 1 page, saw %d", pages)
44 }
45}
46
Jon Perrittd27a9c72015-02-18 11:33:28 -070047func TestListAllServers(t *testing.T) {
48 th.SetupHTTP()
49 defer th.TeardownHTTP()
50 HandleServerListSuccessfully(t)
51
jrperritt3d966162016-06-06 14:08:54 -050052 allPages, err := servers.List(client.ServiceClient(), servers.ListOpts{}).AllPages()
Jon Perrittd27a9c72015-02-18 11:33:28 -070053 th.AssertNoErr(t, err)
jrperritt3d966162016-06-06 14:08:54 -050054 actual, err := servers.ExtractServers(allPages)
Jon Perrittd27a9c72015-02-18 11:33:28 -070055 th.AssertNoErr(t, err)
56 th.CheckDeepEquals(t, ServerHerp, actual[0])
57 th.CheckDeepEquals(t, ServerDerp, actual[1])
58}
59
Joe Topjian0f64da02017-03-09 18:59:53 -070060func TestListAllServersWithExtensions(t *testing.T) {
61 th.SetupHTTP()
62 defer th.TeardownHTTP()
63 HandleServerListSuccessfully(t)
64
65 type ServerWithExt struct {
66 servers.Server
67 availabilityzones.ServerExt
68 }
69
70 allPages, err := servers.List(client.ServiceClient(), servers.ListOpts{}).AllPages()
71 th.AssertNoErr(t, err)
72
73 var actual []ServerWithExt
74 err = servers.ExtractServersInto(allPages, &actual)
75 th.AssertNoErr(t, err)
76 th.AssertEquals(t, 3, len(actual))
77 th.AssertEquals(t, "nova", actual[0].AvailabilityZone)
78}
79
Ash Wilsonad21c712014-09-25 10:15:22 -040080func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040081 th.SetupHTTP()
82 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040083 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040084
jrperritt3d966162016-06-06 14:08:54 -050085 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040086 Name: "derp",
87 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
88 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040089 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040090 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040091
Ash Wilsond3532cd2014-10-21 14:37:47 -040092 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040093}
94
Jon Perrittdb0ae142016-03-13 00:33:41 -060095func TestCreateServerWithCustomField(t *testing.T) {
96 th.SetupHTTP()
97 defer th.TeardownHTTP()
98 HandleServerCreationWithCustomFieldSuccessfully(t, SingleServerBody)
99
jrperritt3d966162016-06-06 14:08:54 -0500100 actual, err := servers.Create(client.ServiceClient(), CreateOptsWithCustomField{
101 CreateOpts: servers.CreateOpts{
Jon Perrittdb0ae142016-03-13 00:33:41 -0600102 Name: "derp",
103 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
104 FlavorRef: "1",
105 },
106 Foo: "bar",
107 }).Extract()
108 th.AssertNoErr(t, err)
109
110 th.CheckDeepEquals(t, ServerDerp, *actual)
111}
112
Joe Topjianf464c962016-09-12 08:02:43 -0600113func TestCreateServerWithMetadata(t *testing.T) {
114 th.SetupHTTP()
115 defer th.TeardownHTTP()
116 HandleServerCreationWithMetadata(t, SingleServerBody)
117
118 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
119 Name: "derp",
120 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
121 FlavorRef: "1",
122 Metadata: map[string]string{
123 "abc": "def",
124 },
125 }).Extract()
126 th.AssertNoErr(t, err)
127
128 th.CheckDeepEquals(t, ServerDerp, *actual)
129}
130
Gavin Williamscd65a062016-11-08 19:05:47 +0000131func TestCreateServerWithUserdataString(t *testing.T) {
132 th.SetupHTTP()
133 defer th.TeardownHTTP()
134 HandleServerCreationWithUserdata(t, SingleServerBody)
135
136 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
137 Name: "derp",
138 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
139 FlavorRef: "1",
140 UserData: []byte("userdata string"),
141 }).Extract()
142 th.AssertNoErr(t, err)
143
144 th.CheckDeepEquals(t, ServerDerp, *actual)
145}
146
147func TestCreateServerWithUserdataEncoded(t *testing.T) {
148 th.SetupHTTP()
149 defer th.TeardownHTTP()
150 HandleServerCreationWithUserdata(t, SingleServerBody)
151
152 encoded := base64.StdEncoding.EncodeToString([]byte("userdata string"))
153
154 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
155 Name: "derp",
156 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
157 FlavorRef: "1",
158 UserData: []byte(encoded),
159 }).Extract()
160 th.AssertNoErr(t, err)
161
162 th.CheckDeepEquals(t, ServerDerp, *actual)
163}
164
jrperritt0d7ed5d2016-08-16 11:23:26 -0500165func TestCreateServerWithImageNameAndFlavorName(t *testing.T) {
166 th.SetupHTTP()
167 defer th.TeardownHTTP()
168 HandleServerCreationSuccessfully(t, SingleServerBody)
169
170 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
171 Name: "derp",
172 ImageName: "cirros-0.3.2-x86_64-disk",
173 FlavorName: "m1.tiny",
174 ServiceClient: client.ServiceClient(),
175 }).Extract()
176 th.AssertNoErr(t, err)
177
178 th.CheckDeepEquals(t, ServerDerp, *actual)
179}
180
Ash Wilsonad21c712014-09-25 10:15:22 -0400181func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400182 th.SetupHTTP()
183 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -0400184 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -0400185
jrperritt3d966162016-06-06 14:08:54 -0500186 res := servers.Delete(client.ServiceClient(), "asdfasdfasdf")
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100187 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400188}
189
Ian Duffy370c4302016-01-21 10:44:56 +0000190func TestForceDeleteServer(t *testing.T) {
191 th.SetupHTTP()
192 defer th.TeardownHTTP()
193 HandleServerForceDeletionSuccessfully(t)
194
jrperritt3d966162016-06-06 14:08:54 -0500195 res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf")
Ian Duffy370c4302016-01-21 10:44:56 +0000196 th.AssertNoErr(t, res.Err)
197}
198
Ash Wilsonad21c712014-09-25 10:15:22 -0400199func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400200 th.SetupHTTP()
201 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400202 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400203
Ash Wilsone77ffb02014-10-20 13:10:26 -0400204 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500205 actual, err := servers.Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400206 if err != nil {
207 t.Fatalf("Unexpected Get error: %v", err)
208 }
209
Ash Wilsond3532cd2014-10-21 14:37:47 -0400210 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400211}
212
Joe Topjian0f64da02017-03-09 18:59:53 -0700213func TestGetServerWithExtensions(t *testing.T) {
214 th.SetupHTTP()
215 defer th.TeardownHTTP()
216 HandleServerGetSuccessfully(t)
217
218 var s struct {
219 servers.Server
220 availabilityzones.ServerExt
221 }
222
223 err := servers.Get(client.ServiceClient(), "1234asdf").ExtractInto(&s)
224 th.AssertNoErr(t, err)
225 th.AssertEquals(t, "nova", s.AvailabilityZone)
226
227 err = servers.Get(client.ServiceClient(), "1234asdf").ExtractInto(s)
228 if err == nil {
229 t.Errorf("Expected error when providing non-pointer struct")
230 }
231}
232
Ash Wilsonad21c712014-09-25 10:15:22 -0400233func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400234 th.SetupHTTP()
235 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400236 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400237
Ash Wilsone77ffb02014-10-20 13:10:26 -0400238 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500239 actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400240 if err != nil {
241 t.Fatalf("Unexpected Update error: %v", err)
242 }
243
Ash Wilsond3532cd2014-10-21 14:37:47 -0400244 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400245}
246
247func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400248 th.SetupHTTP()
249 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400250 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400251
jrperritt3d966162016-06-06 14:08:54 -0500252 res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400253 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400254}
255
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100256func TestGetPassword(t *testing.T) {
257 th.SetupHTTP()
258 defer th.TeardownHTTP()
259 HandlePasswordGetSuccessfully(t)
260
jrperritt3d966162016-06-06 14:08:54 -0500261 res := servers.GetPassword(client.ServiceClient(), "1234asdf")
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100262 th.AssertNoErr(t, res.Err)
263}
264
Ash Wilsonad21c712014-09-25 10:15:22 -0400265func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400266 th.SetupHTTP()
267 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400268 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400269
jrperritt3d966162016-06-06 14:08:54 -0500270 res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{
271 Type: servers.SoftReboot,
Jon Perrittf094fef2016-03-07 01:41:59 -0600272 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400273 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400274}
275
276func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400277 th.SetupHTTP()
278 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400279 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400280
jrperritt3d966162016-06-06 14:08:54 -0500281 opts := servers.RebuildOpts{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200282 Name: "new-name",
283 AdminPass: "swordfish",
284 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
285 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400286 }
287
jrperritt3d966162016-06-06 14:08:54 -0500288 actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
Ash Wilsone77ffb02014-10-20 13:10:26 -0400289 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200290
Ash Wilsond3532cd2014-10-21 14:37:47 -0400291 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400292}
293
294func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400295 th.SetupHTTP()
296 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400297
Ash Wilsone77ffb02014-10-20 13:10:26 -0400298 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
299 th.TestMethod(t, r, "POST")
300 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
301 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400302
303 w.WriteHeader(http.StatusAccepted)
304 })
305
jrperritt3d966162016-06-06 14:08:54 -0500306 res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400307 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400308}
309
310func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400311 th.SetupHTTP()
312 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400313
Ash Wilsone77ffb02014-10-20 13:10:26 -0400314 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
315 th.TestMethod(t, r, "POST")
316 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
317 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400318
319 w.WriteHeader(http.StatusNoContent)
320 })
321
jrperritt3d966162016-06-06 14:08:54 -0500322 res := servers.ConfirmResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400323 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400324}
325
326func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400327 th.SetupHTTP()
328 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400329
Ash Wilsone77ffb02014-10-20 13:10:26 -0400330 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
331 th.TestMethod(t, r, "POST")
332 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
333 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400334
335 w.WriteHeader(http.StatusAccepted)
336 })
337
jrperritt3d966162016-06-06 14:08:54 -0500338 res := servers.RevertResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400339 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400340}
Alex Gaynor810d4892014-11-12 15:43:36 -0800341
342func TestRescue(t *testing.T) {
343 th.SetupHTTP()
344 defer th.TeardownHTTP()
345
Alex Gaynor6c003d22014-11-13 13:52:05 -0800346 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800347
jrperritt3d966162016-06-06 14:08:54 -0500348 res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800349 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800350 })
351 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800352 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800353 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800354}
Jon Perrittcc77da62014-11-16 13:14:21 -0700355
Jon Perritt78c57ce2014-11-20 11:07:18 -0700356func TestGetMetadatum(t *testing.T) {
357 th.SetupHTTP()
358 defer th.TeardownHTTP()
359
360 HandleMetadatumGetSuccessfully(t)
361
362 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500363 actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700364 th.AssertNoErr(t, err)
365 th.AssertDeepEquals(t, expected, actual)
366}
367
368func TestCreateMetadatum(t *testing.T) {
369 th.SetupHTTP()
370 defer th.TeardownHTTP()
371
372 HandleMetadatumCreateSuccessfully(t)
373
374 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500375 actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700376 th.AssertNoErr(t, err)
377 th.AssertDeepEquals(t, expected, actual)
378}
379
380func TestDeleteMetadatum(t *testing.T) {
381 th.SetupHTTP()
382 defer th.TeardownHTTP()
383
384 HandleMetadatumDeleteSuccessfully(t)
385
jrperritt3d966162016-06-06 14:08:54 -0500386 err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700387 th.AssertNoErr(t, err)
388}
389
Jon Perrittcc77da62014-11-16 13:14:21 -0700390func TestGetMetadata(t *testing.T) {
391 th.SetupHTTP()
392 defer th.TeardownHTTP()
393
394 HandleMetadataGetSuccessfully(t)
395
Jon Perritt78c57ce2014-11-20 11:07:18 -0700396 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500397 actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700398 th.AssertNoErr(t, err)
399 th.AssertDeepEquals(t, expected, actual)
400}
401
Jon Perritt789f8322014-11-21 08:20:04 -0700402func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700403 th.SetupHTTP()
404 defer th.TeardownHTTP()
405
Jon Perritt789f8322014-11-21 08:20:04 -0700406 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700407
Jon Perrittcc77da62014-11-16 13:14:21 -0700408 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500409 actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700410 "foo": "bar",
411 "this": "that",
412 }).Extract()
413 th.AssertNoErr(t, err)
414 th.AssertDeepEquals(t, expected, actual)
415}
416
Jon Perritt78c57ce2014-11-20 11:07:18 -0700417func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700418 th.SetupHTTP()
419 defer th.TeardownHTTP()
420
Jon Perritt78c57ce2014-11-20 11:07:18 -0700421 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700422
423 expected := map[string]string{"foo": "baz", "this": "those"}
jrperritt3d966162016-06-06 14:08:54 -0500424 actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700425 "foo": "baz",
426 "this": "those",
427 }).Extract()
428 th.AssertNoErr(t, err)
429 th.AssertDeepEquals(t, expected, actual)
430}
Jon Perritt5cb49482015-02-19 12:19:58 -0700431
432func TestListAddresses(t *testing.T) {
433 th.SetupHTTP()
434 defer th.TeardownHTTP()
435 HandleAddressListSuccessfully(t)
436
437 expected := ListAddressesExpected
438 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500439 err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt5cb49482015-02-19 12:19:58 -0700440 pages++
441
jrperritt3d966162016-06-06 14:08:54 -0500442 actual, err := servers.ExtractAddresses(page)
Jon Perritt5cb49482015-02-19 12:19:58 -0700443 th.AssertNoErr(t, err)
444
445 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700446 t.Fatalf("Expected 2 networks, got %d", len(actual))
447 }
448 th.CheckDeepEquals(t, expected, actual)
449
450 return true, nil
451 })
452 th.AssertNoErr(t, err)
453 th.CheckEquals(t, 1, pages)
454}
455
456func TestListAddressesByNetwork(t *testing.T) {
457 th.SetupHTTP()
458 defer th.TeardownHTTP()
459 HandleNetworkAddressListSuccessfully(t)
460
461 expected := ListNetworkAddressesExpected
462 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500463 err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt04d073c2015-02-19 21:46:23 -0700464 pages++
465
jrperritt3d966162016-06-06 14:08:54 -0500466 actual, err := servers.ExtractNetworkAddresses(page)
Jon Perritt04d073c2015-02-19 21:46:23 -0700467 th.AssertNoErr(t, err)
468
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700469 if len(actual) != 2 {
470 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700471 }
472 th.CheckDeepEquals(t, expected, actual)
473
474 return true, nil
475 })
476 th.AssertNoErr(t, err)
477 th.CheckEquals(t, 1, pages)
478}
Kevin Pike92e10b52015-04-10 15:16:57 -0700479
einarf2fc665e2015-04-16 20:16:21 +0000480func TestCreateServerImage(t *testing.T) {
481 th.SetupHTTP()
482 defer th.TeardownHTTP()
483 HandleCreateServerImageSuccessfully(t)
484
jrperritt3d966162016-06-06 14:08:54 -0500485 _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000486 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000487}
Kevin Pike25d45692015-04-23 17:20:09 -0700488
Kevin Pike92e10b52015-04-10 15:16:57 -0700489func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700490 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700491 contents := []byte("asdfasdf")
492
jrperritt3d966162016-06-06 14:08:54 -0500493 personality := servers.Personality{
494 &servers.File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700495 Path: name,
496 Contents: contents,
497 },
498 }
499
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700500 data, err := json.Marshal(personality)
501 if err != nil {
502 t.Fatal(err)
503 }
504
505 var actual []map[string]string
506 err = json.Unmarshal(data, &actual)
507 if err != nil {
508 t.Fatal(err)
509 }
510
511 if len(actual) != 1 {
512 t.Fatal("expected personality length 1")
513 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700514
515 if actual[0]["path"] != name {
516 t.Fatal("file path incorrect")
517 }
518
519 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
520 t.Fatal("file contents incorrect")
521 }
522}