blob: 931ab36c798d4c912de571ea3dcdcc6af9f21a6c [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
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
Jon Perritt27249f42016-02-18 10:35:59 -06009 "github.com/gophercloud/gophercloud/pagination"
10 th "github.com/gophercloud/gophercloud/testhelper"
11 "github.com/gophercloud/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040012)
13
Ash Wilsonad21c712014-09-25 10:15:22 -040014func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040015 th.SetupHTTP()
16 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040017 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040018
Ash Wilsonad21c712014-09-25 10:15:22 -040019 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040020 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040021 pages++
22
23 actual, err := ExtractServers(page)
24 if err != nil {
25 return false, err
26 }
27
28 if len(actual) != 2 {
29 t.Fatalf("Expected 2 servers, got %d", len(actual))
30 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040031 th.CheckDeepEquals(t, ServerHerp, actual[0])
32 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040033
34 return true, nil
35 })
36
Ash Wilsone77ffb02014-10-20 13:10:26 -040037 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020038
Ash Wilsonad21c712014-09-25 10:15:22 -040039 if pages != 1 {
40 t.Errorf("Expected 1 page, saw %d", pages)
41 }
42}
43
Jon Perrittd27a9c72015-02-18 11:33:28 -070044func TestListAllServers(t *testing.T) {
45 th.SetupHTTP()
46 defer th.TeardownHTTP()
47 HandleServerListSuccessfully(t)
48
49 allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages()
50 th.AssertNoErr(t, err)
51 actual, err := ExtractServers(allPages)
52 th.AssertNoErr(t, err)
53 th.CheckDeepEquals(t, ServerHerp, actual[0])
54 th.CheckDeepEquals(t, ServerDerp, actual[1])
55}
56
Ash Wilsonad21c712014-09-25 10:15:22 -040057func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040058 th.SetupHTTP()
59 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040060 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040061
Ash Wilson664fe332014-10-21 17:47:49 -040062 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040063 Name: "derp",
64 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
65 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040066 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040067 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040068
Ash Wilsond3532cd2014-10-21 14:37:47 -040069 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040070}
71
Jon Perrittdb0ae142016-03-13 00:33:41 -060072func TestCreateServerWithCustomField(t *testing.T) {
73 th.SetupHTTP()
74 defer th.TeardownHTTP()
75 HandleServerCreationWithCustomFieldSuccessfully(t, SingleServerBody)
76
77 actual, err := Create(client.ServiceClient(), CreateOptsWithCustomField{
78 CreateOpts: CreateOpts{
79 Name: "derp",
80 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
81 FlavorRef: "1",
82 },
83 Foo: "bar",
84 }).Extract()
85 th.AssertNoErr(t, err)
86
87 th.CheckDeepEquals(t, ServerDerp, *actual)
88}
89
Ash Wilsonad21c712014-09-25 10:15:22 -040090func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040091 th.SetupHTTP()
92 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040093 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040094
Jamie Hannaford34732fe2014-10-27 11:29:36 +010095 res := Delete(client.ServiceClient(), "asdfasdfasdf")
96 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040097}
98
Ian Duffy370c4302016-01-21 10:44:56 +000099func TestForceDeleteServer(t *testing.T) {
100 th.SetupHTTP()
101 defer th.TeardownHTTP()
102 HandleServerForceDeletionSuccessfully(t)
103
104 res := ForceDelete(client.ServiceClient(), "asdfasdfasdf")
105 th.AssertNoErr(t, res.Err)
106}
107
Ash Wilsonad21c712014-09-25 10:15:22 -0400108func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400109 th.SetupHTTP()
110 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400111 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -0400112
Ash Wilsone77ffb02014-10-20 13:10:26 -0400113 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -0400114 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -0400115 if err != nil {
116 t.Fatalf("Unexpected Get error: %v", err)
117 }
118
Ash Wilsond3532cd2014-10-21 14:37:47 -0400119 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400120}
121
122func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400123 th.SetupHTTP()
124 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400125 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400126
Ash Wilsone77ffb02014-10-20 13:10:26 -0400127 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400128 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400129 if err != nil {
130 t.Fatalf("Unexpected Update error: %v", err)
131 }
132
Ash Wilsond3532cd2014-10-21 14:37:47 -0400133 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400134}
135
136func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400137 th.SetupHTTP()
138 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400139 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400140
Ash Wilsone77ffb02014-10-20 13:10:26 -0400141 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
142 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400143}
144
145func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400146 th.SetupHTTP()
147 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400148 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400149
Jon Perrittf094fef2016-03-07 01:41:59 -0600150 res := Reboot(client.ServiceClient(), "1234asdf", &RebootOpts{
151 Type: SoftReboot,
152 })
Ash Wilsone77ffb02014-10-20 13:10:26 -0400153 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400154}
155
156func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400157 th.SetupHTTP()
158 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400159 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400160
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200161 opts := RebuildOpts{
162 Name: "new-name",
163 AdminPass: "swordfish",
164 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
165 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400166 }
167
Ash Wilsone77ffb02014-10-20 13:10:26 -0400168 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
169 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200170
Ash Wilsond3532cd2014-10-21 14:37:47 -0400171 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400172}
173
174func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400175 th.SetupHTTP()
176 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400177
Ash Wilsone77ffb02014-10-20 13:10:26 -0400178 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
179 th.TestMethod(t, r, "POST")
180 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
181 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400182
183 w.WriteHeader(http.StatusAccepted)
184 })
185
Ash Wilson5f7cf182014-10-23 08:35:24 -0400186 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400187 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400188}
189
190func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400191 th.SetupHTTP()
192 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400193
Ash Wilsone77ffb02014-10-20 13:10:26 -0400194 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
195 th.TestMethod(t, r, "POST")
196 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
197 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400198
199 w.WriteHeader(http.StatusNoContent)
200 })
201
Ash Wilsone77ffb02014-10-20 13:10:26 -0400202 res := ConfirmResize(client.ServiceClient(), "1234asdf")
203 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400204}
205
206func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400207 th.SetupHTTP()
208 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400209
Ash Wilsone77ffb02014-10-20 13:10:26 -0400210 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
211 th.TestMethod(t, r, "POST")
212 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
213 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400214
215 w.WriteHeader(http.StatusAccepted)
216 })
217
Ash Wilsone77ffb02014-10-20 13:10:26 -0400218 res := RevertResize(client.ServiceClient(), "1234asdf")
219 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400220}
Alex Gaynor810d4892014-11-12 15:43:36 -0800221
222func TestRescue(t *testing.T) {
223 th.SetupHTTP()
224 defer th.TeardownHTTP()
225
Alex Gaynor6c003d22014-11-13 13:52:05 -0800226 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800227
Alex Gaynor40449ed2014-11-12 16:28:06 -0800228 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800229 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800230 })
231 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800232 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800233 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800234}
Jon Perrittcc77da62014-11-16 13:14:21 -0700235
Jon Perritt78c57ce2014-11-20 11:07:18 -0700236func TestGetMetadatum(t *testing.T) {
237 th.SetupHTTP()
238 defer th.TeardownHTTP()
239
240 HandleMetadatumGetSuccessfully(t)
241
242 expected := map[string]string{"foo": "bar"}
243 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
244 th.AssertNoErr(t, err)
245 th.AssertDeepEquals(t, expected, actual)
246}
247
248func TestCreateMetadatum(t *testing.T) {
249 th.SetupHTTP()
250 defer th.TeardownHTTP()
251
252 HandleMetadatumCreateSuccessfully(t)
253
254 expected := map[string]string{"foo": "bar"}
255 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
256 th.AssertNoErr(t, err)
257 th.AssertDeepEquals(t, expected, actual)
258}
259
260func TestDeleteMetadatum(t *testing.T) {
261 th.SetupHTTP()
262 defer th.TeardownHTTP()
263
264 HandleMetadatumDeleteSuccessfully(t)
265
266 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
267 th.AssertNoErr(t, err)
268}
269
Jon Perrittcc77da62014-11-16 13:14:21 -0700270func TestGetMetadata(t *testing.T) {
271 th.SetupHTTP()
272 defer th.TeardownHTTP()
273
274 HandleMetadataGetSuccessfully(t)
275
Jon Perritt78c57ce2014-11-20 11:07:18 -0700276 expected := map[string]string{"foo": "bar", "this": "that"}
277 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700278 th.AssertNoErr(t, err)
279 th.AssertDeepEquals(t, expected, actual)
280}
281
Jon Perritt789f8322014-11-21 08:20:04 -0700282func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700283 th.SetupHTTP()
284 defer th.TeardownHTTP()
285
Jon Perritt789f8322014-11-21 08:20:04 -0700286 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700287
Jon Perrittcc77da62014-11-16 13:14:21 -0700288 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700289 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700290 "foo": "bar",
291 "this": "that",
292 }).Extract()
293 th.AssertNoErr(t, err)
294 th.AssertDeepEquals(t, expected, actual)
295}
296
Jon Perritt78c57ce2014-11-20 11:07:18 -0700297func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700298 th.SetupHTTP()
299 defer th.TeardownHTTP()
300
Jon Perritt78c57ce2014-11-20 11:07:18 -0700301 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700302
303 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700304 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700305 "foo": "baz",
306 "this": "those",
307 }).Extract()
308 th.AssertNoErr(t, err)
309 th.AssertDeepEquals(t, expected, actual)
310}
Jon Perritt5cb49482015-02-19 12:19:58 -0700311
312func TestListAddresses(t *testing.T) {
313 th.SetupHTTP()
314 defer th.TeardownHTTP()
315 HandleAddressListSuccessfully(t)
316
317 expected := ListAddressesExpected
318 pages := 0
319 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
320 pages++
321
322 actual, err := ExtractAddresses(page)
323 th.AssertNoErr(t, err)
324
325 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700326 t.Fatalf("Expected 2 networks, got %d", len(actual))
327 }
328 th.CheckDeepEquals(t, expected, actual)
329
330 return true, nil
331 })
332 th.AssertNoErr(t, err)
333 th.CheckEquals(t, 1, pages)
334}
335
336func TestListAddressesByNetwork(t *testing.T) {
337 th.SetupHTTP()
338 defer th.TeardownHTTP()
339 HandleNetworkAddressListSuccessfully(t)
340
341 expected := ListNetworkAddressesExpected
342 pages := 0
343 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
344 pages++
345
346 actual, err := ExtractNetworkAddresses(page)
347 th.AssertNoErr(t, err)
348
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700349 if len(actual) != 2 {
350 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700351 }
352 th.CheckDeepEquals(t, expected, actual)
353
354 return true, nil
355 })
356 th.AssertNoErr(t, err)
357 th.CheckEquals(t, 1, pages)
358}
Kevin Pike92e10b52015-04-10 15:16:57 -0700359
einarf2fc665e2015-04-16 20:16:21 +0000360func TestCreateServerImage(t *testing.T) {
361 th.SetupHTTP()
362 defer th.TeardownHTTP()
363 HandleCreateServerImageSuccessfully(t)
364
einarf4e5fdaf2015-04-16 23:14:59 +0000365 _, err := CreateImage(client.ServiceClient(), "serverimage", CreateImageOpts{Name: "test"}).ExtractImageID()
einarf2fc665e2015-04-16 20:16:21 +0000366 th.AssertNoErr(t, err)
einarf2fc665e2015-04-16 20:16:21 +0000367}
Kevin Pike25d45692015-04-23 17:20:09 -0700368
Kevin Pike92e10b52015-04-10 15:16:57 -0700369func TestMarshalPersonality(t *testing.T) {
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700370 name := "/etc/test"
Kevin Pike92e10b52015-04-10 15:16:57 -0700371 contents := []byte("asdfasdf")
372
373 personality := Personality{
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700374 &File{
Kevin Pike92e10b52015-04-10 15:16:57 -0700375 Path: name,
376 Contents: contents,
377 },
378 }
379
Kevin Pikea2bfaea2015-04-21 11:45:59 -0700380 data, err := json.Marshal(personality)
381 if err != nil {
382 t.Fatal(err)
383 }
384
385 var actual []map[string]string
386 err = json.Unmarshal(data, &actual)
387 if err != nil {
388 t.Fatal(err)
389 }
390
391 if len(actual) != 1 {
392 t.Fatal("expected personality length 1")
393 }
Kevin Pike92e10b52015-04-10 15:16:57 -0700394
395 if actual[0]["path"] != name {
396 t.Fatal("file path incorrect")
397 }
398
399 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
400 t.Fatal("file contents incorrect")
401 }
402}