blob: 83fcdb0ee0f6094a464f2090be10c7704e73f819 [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
Kevin Pike92e10b52015-04-10 15:16:57 -07004 "encoding/base64"
Ash Wilsonad21c712014-09-25 10:15:22 -04005 "net/http"
6 "testing"
7
Ash Wilsonad21c712014-09-25 10:15:22 -04008 "github.com/rackspace/gophercloud/pagination"
Ash Wilsone77ffb02014-10-20 13:10:26 -04009 th "github.com/rackspace/gophercloud/testhelper"
10 "github.com/rackspace/gophercloud/testhelper/client"
Ash Wilsonad21c712014-09-25 10:15:22 -040011)
12
Ash Wilsonad21c712014-09-25 10:15:22 -040013func TestListServers(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040014 th.SetupHTTP()
15 defer th.TeardownHTTP()
Ash Wilsona70510a2014-10-23 11:54:03 -040016 HandleServerListSuccessfully(t)
Ash Wilsonad21c712014-09-25 10:15:22 -040017
Ash Wilsonad21c712014-09-25 10:15:22 -040018 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040019 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040020 pages++
21
22 actual, err := ExtractServers(page)
23 if err != nil {
24 return false, err
25 }
26
27 if len(actual) != 2 {
28 t.Fatalf("Expected 2 servers, got %d", len(actual))
29 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040030 th.CheckDeepEquals(t, ServerHerp, actual[0])
31 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040032
33 return true, nil
34 })
35
Ash Wilsone77ffb02014-10-20 13:10:26 -040036 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020037
Ash Wilsonad21c712014-09-25 10:15:22 -040038 if pages != 1 {
39 t.Errorf("Expected 1 page, saw %d", pages)
40 }
41}
42
Jon Perrittd27a9c72015-02-18 11:33:28 -070043func TestListAllServers(t *testing.T) {
44 th.SetupHTTP()
45 defer th.TeardownHTTP()
46 HandleServerListSuccessfully(t)
47
48 allPages, err := List(client.ServiceClient(), ListOpts{}).AllPages()
49 th.AssertNoErr(t, err)
50 actual, err := ExtractServers(allPages)
51 th.AssertNoErr(t, err)
52 th.CheckDeepEquals(t, ServerHerp, actual[0])
53 th.CheckDeepEquals(t, ServerDerp, actual[1])
54}
55
Ash Wilsonad21c712014-09-25 10:15:22 -040056func TestCreateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040057 th.SetupHTTP()
58 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040059 HandleServerCreationSuccessfully(t, SingleServerBody)
Ash Wilson3204d0d2014-09-25 10:37:44 -040060
Ash Wilson664fe332014-10-21 17:47:49 -040061 actual, err := Create(client.ServiceClient(), CreateOpts{
Ash Wilson6a310e02014-09-29 08:24:02 -040062 Name: "derp",
63 ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
64 FlavorRef: "1",
Ash Wilsond27e0ff2014-09-25 11:50:31 -040065 }).Extract()
Ash Wilson664fe332014-10-21 17:47:49 -040066 th.AssertNoErr(t, err)
Ash Wilson3204d0d2014-09-25 10:37:44 -040067
Ash Wilsond3532cd2014-10-21 14:37:47 -040068 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040069}
70
71func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040072 th.SetupHTTP()
73 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040074 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040075
Jamie Hannaford34732fe2014-10-27 11:29:36 +010076 res := Delete(client.ServiceClient(), "asdfasdfasdf")
77 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -040078}
79
80func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040081 th.SetupHTTP()
82 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040083 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040084
Ash Wilsone77ffb02014-10-20 13:10:26 -040085 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040086 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040087 if err != nil {
88 t.Fatalf("Unexpected Get error: %v", err)
89 }
90
Ash Wilsond3532cd2014-10-21 14:37:47 -040091 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040092}
93
94func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040095 th.SetupHTTP()
96 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -040097 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -040098
Ash Wilsone77ffb02014-10-20 13:10:26 -040099 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400100 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400101 if err != nil {
102 t.Fatalf("Unexpected Update error: %v", err)
103 }
104
Ash Wilsond3532cd2014-10-21 14:37:47 -0400105 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400106}
107
108func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400109 th.SetupHTTP()
110 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400111 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400112
Ash Wilsone77ffb02014-10-20 13:10:26 -0400113 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
114 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400115}
116
117func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400118 th.SetupHTTP()
119 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400120 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400121
Ash Wilsone77ffb02014-10-20 13:10:26 -0400122 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
123 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400124}
125
126func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400127 th.SetupHTTP()
128 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400129 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400130
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200131 opts := RebuildOpts{
132 Name: "new-name",
133 AdminPass: "swordfish",
134 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
135 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400136 }
137
Ash Wilsone77ffb02014-10-20 13:10:26 -0400138 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
139 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200140
Ash Wilsond3532cd2014-10-21 14:37:47 -0400141 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400142}
143
144func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400145 th.SetupHTTP()
146 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400147
Ash Wilsone77ffb02014-10-20 13:10:26 -0400148 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
149 th.TestMethod(t, r, "POST")
150 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
151 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400152
153 w.WriteHeader(http.StatusAccepted)
154 })
155
Ash Wilson5f7cf182014-10-23 08:35:24 -0400156 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400157 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400158}
159
160func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400161 th.SetupHTTP()
162 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400163
Ash Wilsone77ffb02014-10-20 13:10:26 -0400164 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
165 th.TestMethod(t, r, "POST")
166 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
167 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400168
169 w.WriteHeader(http.StatusNoContent)
170 })
171
Ash Wilsone77ffb02014-10-20 13:10:26 -0400172 res := ConfirmResize(client.ServiceClient(), "1234asdf")
173 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400174}
175
176func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400177 th.SetupHTTP()
178 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400179
Ash Wilsone77ffb02014-10-20 13:10:26 -0400180 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
181 th.TestMethod(t, r, "POST")
182 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
183 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400184
185 w.WriteHeader(http.StatusAccepted)
186 })
187
Ash Wilsone77ffb02014-10-20 13:10:26 -0400188 res := RevertResize(client.ServiceClient(), "1234asdf")
189 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400190}
Alex Gaynor810d4892014-11-12 15:43:36 -0800191
192func TestRescue(t *testing.T) {
193 th.SetupHTTP()
194 defer th.TeardownHTTP()
195
Alex Gaynor6c003d22014-11-13 13:52:05 -0800196 HandleServerRescueSuccessfully(t)
Alex Gaynor810d4892014-11-12 15:43:36 -0800197
Alex Gaynor40449ed2014-11-12 16:28:06 -0800198 res := Rescue(client.ServiceClient(), "1234asdf", RescueOpts{
Alex Gaynor31168172014-11-12 16:27:47 -0800199 AdminPass: "1234567890",
Alex Gaynor810d4892014-11-12 15:43:36 -0800200 })
201 th.AssertNoErr(t, res.Err)
Alex Gaynor7f3b06e2014-11-13 09:54:03 -0800202 adminPass, _ := res.Extract()
Alex Gaynor0160cff2014-11-13 10:17:48 -0800203 th.AssertEquals(t, "1234567890", adminPass)
Alex Gaynor810d4892014-11-12 15:43:36 -0800204}
Jon Perrittcc77da62014-11-16 13:14:21 -0700205
Jon Perritt78c57ce2014-11-20 11:07:18 -0700206func TestGetMetadatum(t *testing.T) {
207 th.SetupHTTP()
208 defer th.TeardownHTTP()
209
210 HandleMetadatumGetSuccessfully(t)
211
212 expected := map[string]string{"foo": "bar"}
213 actual, err := Metadatum(client.ServiceClient(), "1234asdf", "foo").Extract()
214 th.AssertNoErr(t, err)
215 th.AssertDeepEquals(t, expected, actual)
216}
217
218func TestCreateMetadatum(t *testing.T) {
219 th.SetupHTTP()
220 defer th.TeardownHTTP()
221
222 HandleMetadatumCreateSuccessfully(t)
223
224 expected := map[string]string{"foo": "bar"}
225 actual, err := CreateMetadatum(client.ServiceClient(), "1234asdf", MetadatumOpts{"foo": "bar"}).Extract()
226 th.AssertNoErr(t, err)
227 th.AssertDeepEquals(t, expected, actual)
228}
229
230func TestDeleteMetadatum(t *testing.T) {
231 th.SetupHTTP()
232 defer th.TeardownHTTP()
233
234 HandleMetadatumDeleteSuccessfully(t)
235
236 err := DeleteMetadatum(client.ServiceClient(), "1234asdf", "foo").ExtractErr()
237 th.AssertNoErr(t, err)
238}
239
Jon Perrittcc77da62014-11-16 13:14:21 -0700240func TestGetMetadata(t *testing.T) {
241 th.SetupHTTP()
242 defer th.TeardownHTTP()
243
244 HandleMetadataGetSuccessfully(t)
245
Jon Perritt78c57ce2014-11-20 11:07:18 -0700246 expected := map[string]string{"foo": "bar", "this": "that"}
247 actual, err := Metadata(client.ServiceClient(), "1234asdf").Extract()
Jon Perrittcc77da62014-11-16 13:14:21 -0700248 th.AssertNoErr(t, err)
249 th.AssertDeepEquals(t, expected, actual)
250}
251
Jon Perritt789f8322014-11-21 08:20:04 -0700252func TestResetMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700253 th.SetupHTTP()
254 defer th.TeardownHTTP()
255
Jon Perritt789f8322014-11-21 08:20:04 -0700256 HandleMetadataResetSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700257
Jon Perrittcc77da62014-11-16 13:14:21 -0700258 expected := map[string]string{"foo": "bar", "this": "that"}
Jon Perritt789f8322014-11-21 08:20:04 -0700259 actual, err := ResetMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700260 "foo": "bar",
261 "this": "that",
262 }).Extract()
263 th.AssertNoErr(t, err)
264 th.AssertDeepEquals(t, expected, actual)
265}
266
Jon Perritt78c57ce2014-11-20 11:07:18 -0700267func TestUpdateMetadata(t *testing.T) {
Jon Perrittcc77da62014-11-16 13:14:21 -0700268 th.SetupHTTP()
269 defer th.TeardownHTTP()
270
Jon Perritt78c57ce2014-11-20 11:07:18 -0700271 HandleMetadataUpdateSuccessfully(t)
Jon Perrittcc77da62014-11-16 13:14:21 -0700272
273 expected := map[string]string{"foo": "baz", "this": "those"}
Jon Perritt78c57ce2014-11-20 11:07:18 -0700274 actual, err := UpdateMetadata(client.ServiceClient(), "1234asdf", MetadataOpts{
Jon Perrittcc77da62014-11-16 13:14:21 -0700275 "foo": "baz",
276 "this": "those",
277 }).Extract()
278 th.AssertNoErr(t, err)
279 th.AssertDeepEquals(t, expected, actual)
280}
Jon Perritt5cb49482015-02-19 12:19:58 -0700281
282func TestListAddresses(t *testing.T) {
283 th.SetupHTTP()
284 defer th.TeardownHTTP()
285 HandleAddressListSuccessfully(t)
286
287 expected := ListAddressesExpected
288 pages := 0
289 err := ListAddresses(client.ServiceClient(), "asdfasdfasdf").EachPage(func(page pagination.Page) (bool, error) {
290 pages++
291
292 actual, err := ExtractAddresses(page)
293 th.AssertNoErr(t, err)
294
295 if len(actual) != 2 {
Jon Perritt04d073c2015-02-19 21:46:23 -0700296 t.Fatalf("Expected 2 networks, got %d", len(actual))
297 }
298 th.CheckDeepEquals(t, expected, actual)
299
300 return true, nil
301 })
302 th.AssertNoErr(t, err)
303 th.CheckEquals(t, 1, pages)
304}
305
306func TestListAddressesByNetwork(t *testing.T) {
307 th.SetupHTTP()
308 defer th.TeardownHTTP()
309 HandleNetworkAddressListSuccessfully(t)
310
311 expected := ListNetworkAddressesExpected
312 pages := 0
313 err := ListAddressesByNetwork(client.ServiceClient(), "asdfasdfasdf", "public").EachPage(func(page pagination.Page) (bool, error) {
314 pages++
315
316 actual, err := ExtractNetworkAddresses(page)
317 th.AssertNoErr(t, err)
318
Jon Perrittb51ba9c2015-02-23 10:56:35 -0700319 if len(actual) != 2 {
320 t.Fatalf("Expected 2 addresses, got %d", len(actual))
Jon Perritt5cb49482015-02-19 12:19:58 -0700321 }
322 th.CheckDeepEquals(t, expected, actual)
323
324 return true, nil
325 })
326 th.AssertNoErr(t, err)
327 th.CheckEquals(t, 1, pages)
328}
Kevin Pike92e10b52015-04-10 15:16:57 -0700329
330func TestMarshalPersonality(t *testing.T) {
331 name := "test"
332 contents := []byte("asdfasdf")
333
334 personality := Personality{
335 File{
336 Path: name,
337 Contents: contents,
338 },
339 }
340
341 actual := personality.Marshal()
342
343 if actual[0]["path"] != name {
344 t.Fatal("file path incorrect")
345 }
346
347 if actual[0]["contents"] != base64.StdEncoding.EncodeToString(contents) {
348 t.Fatal("file contents incorrect")
349 }
350}