blob: 15d6eb5862ee9a428fab4ffb6c849c45f50001f0 [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
Gavin Williamscd65a062016-11-08 19:05:47 +0000110func TestCreateServerWithUserdataString(t *testing.T) {
111 th.SetupHTTP()
112 defer th.TeardownHTTP()
113 HandleServerCreationWithUserdata(t, SingleServerBody)
114
115 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
116 Name: "derp",
117 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
118 FlavorRef: "1",
119 UserData: []byte("userdata string"),
120 }).Extract()
121 th.AssertNoErr(t, err)
122
123 th.CheckDeepEquals(t, ServerDerp, *actual)
124}
125
126func TestCreateServerWithUserdataEncoded(t *testing.T) {
127 th.SetupHTTP()
128 defer th.TeardownHTTP()
129 HandleServerCreationWithUserdata(t, SingleServerBody)
130
131 encoded := base64.StdEncoding.EncodeToString([]byte("userdata string"))
132
133 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
134 Name: "derp",
135 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
136 FlavorRef: "1",
137 UserData: []byte(encoded),
138 }).Extract()
139 th.AssertNoErr(t, err)
140
141 th.CheckDeepEquals(t, ServerDerp, *actual)
142}
143
jrperritt0d7ed5d2016-08-16 11:23:26 -0500144func TestCreateServerWithImageNameAndFlavorName(t *testing.T) {
145 th.SetupHTTP()
146 defer th.TeardownHTTP()
147 HandleServerCreationSuccessfully(t, SingleServerBody)
148
149 actual, err := servers.Create(client.ServiceClient(), servers.CreateOpts{
150 Name: "derp",
151 ImageName: "cirros-0.3.2-x86_64-disk",
152 FlavorName: "m1.tiny",
153 ServiceClient: client.ServiceClient(),
154 }).Extract()
155 th.AssertNoErr(t, err)
156
157 th.CheckDeepEquals(t, ServerDerp, *actual)
158}
159
Ash Wilsonad21c712014-09-25 10:15:22 -0400160func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400161 th.SetupHTTP()
162 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -0400163 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -0400164
jrperritt3d966162016-06-06 14:08:54 -0500165 res := servers.Delete(client.ServiceClient(), "asdfasdfasdf")
Jamie Hannaford34732fe2014-10-27 11:29:36 +0100166 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400167}
168
Ian Duffy370c4302016-01-21 10:44:56 +0000169func TestForceDeleteServer(t *testing.T) {
170 th.SetupHTTP()
171 defer th.TeardownHTTP()
172 HandleServerForceDeletionSuccessfully(t)
173
jrperritt3d966162016-06-06 14:08:54 -0500174 res := servers.ForceDelete(client.ServiceClient(), "asdfasdfasdf")
Ian Duffy370c4302016-01-21 10:44:56 +0000175 th.AssertNoErr(t, res.Err)
176}
177
Ash Wilsonad21c712014-09-25 10:15:22 -0400178func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400179 th.SetupHTTP()
180 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400181 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400182
Ash Wilsone77ffb02014-10-20 13:10:26 -0400183 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500184 actual, err := servers.Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400185 if err != nil {
186 t.Fatalf("Unexpected Get error: %v", err)
187 }
188
Ash Wilsond3532cd2014-10-21 14:37:47 -0400189 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400190}
191
192func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400193 th.SetupHTTP()
194 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400195 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400196
Ash Wilsone77ffb02014-10-20 13:10:26 -0400197 client := client.ServiceClient()
jrperritt3d966162016-06-06 14:08:54 -0500198 actual, err := servers.Update(client, "1234asdf", servers.UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400199 if err != nil {
200 t.Fatalf("Unexpected Update error: %v", err)
201 }
202
Ash Wilsond3532cd2014-10-21 14:37:47 -0400203 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400204}
205
206func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400207 th.SetupHTTP()
208 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400209 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400210
jrperritt3d966162016-06-06 14:08:54 -0500211 res := servers.ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400212 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400213}
214
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100215func TestGetPassword(t *testing.T) {
216 th.SetupHTTP()
217 defer th.TeardownHTTP()
218 HandlePasswordGetSuccessfully(t)
219
jrperritt3d966162016-06-06 14:08:54 -0500220 res := servers.GetPassword(client.ServiceClient(), "1234asdf")
Rickard von Essen5b8bbff2016-02-16 07:48:20 +0100221 th.AssertNoErr(t, res.Err)
222}
223
Ash Wilsonad21c712014-09-25 10:15:22 -0400224func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400225 th.SetupHTTP()
226 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400227 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400228
jrperritt3d966162016-06-06 14:08:54 -0500229 res := servers.Reboot(client.ServiceClient(), "1234asdf", &servers.RebootOpts{
230 Type: servers.SoftReboot,
Jon Perrittf094fef2016-03-07 01:41:59 -0600231 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400232 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400233}
234
235func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400236 th.SetupHTTP()
237 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400238 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400239
jrperritt3d966162016-06-06 14:08:54 -0500240 opts := servers.RebuildOpts{
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200241 Name: "new-name",
242 AdminPass: "swordfish",
243 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
244 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400245 }
246
jrperritt3d966162016-06-06 14:08:54 -0500247 actual, err := servers.Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
Ash Wilsone77ffb02014-10-20 13:10:26 -0400248 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200249
Ash Wilsond3532cd2014-10-21 14:37:47 -0400250 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400251}
252
253func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400254 th.SetupHTTP()
255 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400256
Ash Wilsone77ffb02014-10-20 13:10:26 -0400257 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
258 th.TestMethod(t, r, "POST")
259 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
260 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400261
262 w.WriteHeader(http.StatusAccepted)
263 })
264
jrperritt3d966162016-06-06 14:08:54 -0500265 res := servers.Resize(client.ServiceClient(), "1234asdf", servers.ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400266 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400267}
268
269func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400270 th.SetupHTTP()
271 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400272
Ash Wilsone77ffb02014-10-20 13:10:26 -0400273 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
274 th.TestMethod(t, r, "POST")
275 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
276 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400277
278 w.WriteHeader(http.StatusNoContent)
279 })
280
jrperritt3d966162016-06-06 14:08:54 -0500281 res := servers.ConfirmResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400282 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400283}
284
285func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400286 th.SetupHTTP()
287 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400288
Ash Wilsone77ffb02014-10-20 13:10:26 -0400289 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
290 th.TestMethod(t, r, "POST")
291 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
292 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400293
294 w.WriteHeader(http.StatusAccepted)
295 })
296
jrperritt3d966162016-06-06 14:08:54 -0500297 res := servers.RevertResize(client.ServiceClient(), "1234asdf")
Ash Wilsone77ffb02014-10-20 13:10:26 -0400298 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400299}
Alex Gaynor810d4892014-11-12 15:43:36 -0800300
301func TestRescue(t *testing.T) {
302 th.SetupHTTP()
303 defer th.TeardownHTTP()
304
Alex Gaynor6c003d22014-11-13 13:52:05 -0800305 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800306
jrperritt3d966162016-06-06 14:08:54 -0500307 res := servers.Rescue(client.ServiceClient(), "1234asdf", servers.RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800308 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800309 })
310 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800311 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800312 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800313}
Jon Perrittcc77da62014-11-16 13:14:21 -0700314
Jon Perritt78c57ce2014-11-20 11:07:18 -0700315func TestGetMetadatum(t *testing.T) {
316 th.SetupHTTP()
317 defer th.TeardownHTTP()
318
319 HandleMetadatumGetSuccessfully(t)
320
321 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500322 actual, err := servers.Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700323 th.AssertNoErr(t, err)
324 th.AssertDeepEquals(t, expected, actual)
325}
326
327func TestCreateMetadatum(t *testing.T) {
328 th.SetupHTTP()
329 defer th.TeardownHTTP()
330
331 HandleMetadatumCreateSuccessfully(t)
332
333 expected := map[string]string{"foo": "bar"}
jrperritt3d966162016-06-06 14:08:54 -0500334 actual, err := servers.CreateMetadatum(client.ServiceClient(), "1234asdf", servers.MetadatumOpts{"foo": "bar"}).Extract()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700335 th.AssertNoErr(t, err)
336 th.AssertDeepEquals(t, expected, actual)
337}
338
339func TestDeleteMetadatum(t *testing.T) {
340 th.SetupHTTP()
341 defer th.TeardownHTTP()
342
343 HandleMetadatumDeleteSuccessfully(t)
344
jrperritt3d966162016-06-06 14:08:54 -0500345 err := servers.DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
Jon Perritt78c57ce2014-11-20 11:07:18 -0700346 th.AssertNoErr(t, err)
347}
348
Jon Perrittcc77da62014-11-16 13:14:21 -0700349func TestGetMetadata(t *testing.T) {
350 th.SetupHTTP()
351 defer th.TeardownHTTP()
352
353 HandleMetadataGetSuccessfully(t)
354
Jon Perritt78c57ce2014-11-20 11:07:18 -0700355 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500356 actual, err := servers.Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700357 th.AssertNoErr(t, err)
358 th.AssertDeepEquals(t, expected, actual)
359}
360
Jon Perritt789f8322014-11-21 08:20:04 -0700361func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700362 th.SetupHTTP()
363 defer th.TeardownHTTP()
364
Jon Perritt789f8322014-11-21 08:20:04 -0700365 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700366
Jon Perrittcc77da62014-11-16 13:14:21 -0700367 expected := map[string]string{"foo": "bar", "this": "that"}
jrperritt3d966162016-06-06 14:08:54 -0500368 actual, err := servers.ResetMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700369 "foo": "bar",
370 "this": "that",
371 }).Extract()
372 th.AssertNoErr(t, err)
373 th.AssertDeepEquals(t, expected, actual)
374}
375
Jon Perritt78c57ce2014-11-20 11:07:18 -0700376func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700377 th.SetupHTTP()
378 defer th.TeardownHTTP()
379
Jon Perritt78c57ce2014-11-20 11:07:18 -0700380 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700381
382 expected := map[string]string{"foo": "baz", "this": "those"}
jrperritt3d966162016-06-06 14:08:54 -0500383 actual, err := servers.UpdateMetadata(client.ServiceClient(), "1234asdf", servers.MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700384 "foo": "baz",
385 "this": "those",
386 }).Extract()
387 th.AssertNoErr(t, err)
388 th.AssertDeepEquals(t, expected, actual)
389}
Jon Perritt5cb49482015-02-19 12:19:58 -0700390
391func TestListAddresses(t *testing.T) {
392 th.SetupHTTP()
393 defer th.TeardownHTTP()
394 HandleAddressListSuccessfully(t)
395
396 expected := ListAddressesExpected
397 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500398 err := servers.ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt5cb49482015-02-19 12:19:58 -0700399 pages++
400
jrperritt3d966162016-06-06 14:08:54 -0500401 actual, err := servers.ExtractAddresses(page)
Jon Perritt5cb49482015-02-19 12:19:58 -0700402 th.AssertNoErr(t, err)
403
404 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700405 t.Fatalf("Expected 2 networks, got %d", len(actual))
406 }
407 th.CheckDeepEquals(t, expected, actual)
408
409 return true, nil
410 })
411 th.AssertNoErr(t, err)
412 th.CheckEquals(t, 1, pages)
413}
414
415func TestListAddressesByNetwork(t *testing.T) {
416 th.SetupHTTP()
417 defer th.TeardownHTTP()
418 HandleNetworkAddressListSuccessfully(t)
419
420 expected := ListNetworkAddressesExpected
421 pages := 0
jrperritt3d966162016-06-06 14:08:54 -0500422 err := servers.ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
Jon Perritt04d073c2015-02-19 21:46:23 -0700423 pages++
424
jrperritt3d966162016-06-06 14:08:54 -0500425 actual, err := servers.ExtractNetworkAddresses(page)
Jon Perritt04d073c2015-02-19 21:46:23 -0700426 th.AssertNoErr(t, err)
427
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700428 if len(actual) != 2 {
429 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700430 }
431 th.CheckDeepEquals(t, expected, actual)
432
433 return true, nil
434 })
435 th.AssertNoErr(t, err)
436 th.CheckEquals(t, 1, pages)
437}
Kevin Pike92e10b52015-04-10 15:16:57 -0700438
einarf2fc665e2015-04-16 20:16:21 +0000439func TestCreateServerImage(t *testing.T) {
440 th.SetupHTTP()
441 defer th.TeardownHTTP()
442 HandleCreateServerImageSuccessfully(t)
443
jrperritt3d966162016-06-06 14:08:54 -0500444 _, err := servers.CreateImage(client.ServiceClient(), "serverimage", servers.CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000445 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000446}
Kevin Pike25d45692015-04-23 17:20:09 -0700447
Kevin Pike92e10b52015-04-10 15:16:57 -0700448func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700449 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700450 contents := []byte("asdfasdf")
451
jrperritt3d966162016-06-06 14:08:54 -0500452 personality := servers.Personality{
453 &servers.File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700454 Path: name,
455 Contents: contents,
456 },
457 }
458
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700459 data, err := json.Marshal(personality)
460 if err != nil {
461 t.Fatal(err)
462 }
463
464 var actual []map[string]string
465 err = json.Unmarshal(data, &actual)
466 if err != nil {
467 t.Fatal(err)
468 }
469
470 if len(actual) != 1 {
471 t.Fatal("expected personality length 1")
472 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700473
474 if actual[0]["path"] != name {
475 t.Fatal("file path incorrect")
476 }
477
478 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
479 t.Fatal("file contents incorrect")
480 }
481}