blob: f3b8a7febc7a43deaf45d60d2df88c417747b2be [file] [log] [blame]
Ash Wilsonad21c712014-09-25 10:15:22 -04001package servers
2
3import (
4 "fmt"
5 "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 Wilsonad21c712014-09-25 10:15:22 -040016
Ash Wilsone77ffb02014-10-20 13:10:26 -040017 th.Mux.HandleFunc("/servers/detail", func(w http.ResponseWriter, r *http.Request) {
18 th.TestMethod(t, r, "GET")
19 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
Ash Wilsonad21c712014-09-25 10:15:22 -040020
21 w.Header().Add("Content-Type", "application/json")
22 r.ParseForm()
23 marker := r.Form.Get("marker")
24 switch marker {
25 case "":
Ash Wilsone77ffb02014-10-20 13:10:26 -040026 fmt.Fprintf(w, ServerListBody)
Ash Wilsonad21c712014-09-25 10:15:22 -040027 case "9e5476bd-a4ec-4653-93d6-72c93aa682ba":
28 fmt.Fprintf(w, `{ "servers": [] }`)
29 default:
30 t.Fatalf("/servers/detail invoked with unexpected marker=[%s]", marker)
31 }
32 })
33
Ash Wilsonad21c712014-09-25 10:15:22 -040034 pages := 0
Ash Wilsone77ffb02014-10-20 13:10:26 -040035 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
Ash Wilsonad21c712014-09-25 10:15:22 -040036 pages++
37
38 actual, err := ExtractServers(page)
39 if err != nil {
40 return false, err
41 }
42
43 if len(actual) != 2 {
44 t.Fatalf("Expected 2 servers, got %d", len(actual))
45 }
Ash Wilsond3532cd2014-10-21 14:37:47 -040046 th.CheckDeepEquals(t, ServerHerp, actual[0])
47 th.CheckDeepEquals(t, ServerDerp, actual[1])
Ash Wilsonad21c712014-09-25 10:15:22 -040048
49 return true, nil
50 })
51
Ash Wilsone77ffb02014-10-20 13:10:26 -040052 th.AssertNoErr(t, err)
Jamie Hannafordcf001722014-10-16 12:54:07 +020053
Ash Wilsonad21c712014-09-25 10:15:22 -040054 if pages != 1 {
55 t.Errorf("Expected 1 page, saw %d", pages)
56 }
57}
58
59func 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
Ash Wilson664fe332014-10-21 17:47:49 -040064 actual, err := Create(client.ServiceClient(), 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
74func TestDeleteServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040075 th.SetupHTTP()
76 defer th.TeardownHTTP()
Ash Wilson664fe332014-10-21 17:47:49 -040077 HandleServerDeletionSuccessfully(t)
Ash Wilsonaff36272014-09-25 10:40:05 -040078
Ash Wilson664fe332014-10-21 17:47:49 -040079 err := Delete(client.ServiceClient(), "asdfasdfasdf")
80 th.AssertNoErr(t, err)
Ash Wilsonad21c712014-09-25 10:15:22 -040081}
82
83func TestGetServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040084 th.SetupHTTP()
85 defer th.TeardownHTTP()
Ash Wilsona612f1f2014-09-25 10:42:40 -040086
Ash Wilsone77ffb02014-10-20 13:10:26 -040087 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
88 th.TestMethod(t, r, "GET")
89 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
90 th.TestHeader(t, r, "Accept", "application/json")
Ash Wilsona612f1f2014-09-25 10:42:40 -040091
Ash Wilsone77ffb02014-10-20 13:10:26 -040092 fmt.Fprintf(w, SingleServerBody)
Ash Wilsona612f1f2014-09-25 10:42:40 -040093 })
94
Ash Wilsone77ffb02014-10-20 13:10:26 -040095 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040096 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040097 if err != nil {
98 t.Fatalf("Unexpected Get error: %v", err)
99 }
100
Ash Wilsond3532cd2014-10-21 14:37:47 -0400101 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400102}
103
104func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400105 th.SetupHTTP()
106 defer th.TeardownHTTP()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400107
Ash Wilsone77ffb02014-10-20 13:10:26 -0400108 th.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
109 th.TestMethod(t, r, "PUT")
110 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
111 th.TestHeader(t, r, "Accept", "application/json")
112 th.TestHeader(t, r, "Content-Type", "application/json")
113 th.TestJSONRequest(t, r, `{ "server": { "name": "new-name" } }`)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400114
Ash Wilsone77ffb02014-10-20 13:10:26 -0400115 fmt.Fprintf(w, SingleServerBody)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400116 })
117
Ash Wilsone77ffb02014-10-20 13:10:26 -0400118 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400119 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400120 if err != nil {
121 t.Fatalf("Unexpected Update error: %v", err)
122 }
123
Ash Wilsond3532cd2014-10-21 14:37:47 -0400124 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400125}
126
127func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400128 th.SetupHTTP()
129 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400130 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400131
Ash Wilsone77ffb02014-10-20 13:10:26 -0400132 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
133 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400134}
135
136func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400137 th.SetupHTTP()
138 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400139 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400140
Ash Wilsone77ffb02014-10-20 13:10:26 -0400141 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
142 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400143}
144
145func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400146 th.SetupHTTP()
147 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400148 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400149
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200150 opts := RebuildOpts{
151 Name: "new-name",
152 AdminPass: "swordfish",
153 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
154 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400155 }
156
Ash Wilsone77ffb02014-10-20 13:10:26 -0400157 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
158 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200159
Ash Wilsond3532cd2014-10-21 14:37:47 -0400160 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400161}
162
163func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400164 th.SetupHTTP()
165 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400166
Ash Wilsone77ffb02014-10-20 13:10:26 -0400167 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
168 th.TestMethod(t, r, "POST")
169 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
170 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400171
172 w.WriteHeader(http.StatusAccepted)
173 })
174
Ash Wilson5f7cf182014-10-23 08:35:24 -0400175 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400176 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400177}
178
179func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400180 th.SetupHTTP()
181 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -0400182
Ash Wilsone77ffb02014-10-20 13:10:26 -0400183 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
184 th.TestMethod(t, r, "POST")
185 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
186 th.TestJSONRequest(t, r, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400187
188 w.WriteHeader(http.StatusNoContent)
189 })
190
Ash Wilsone77ffb02014-10-20 13:10:26 -0400191 res := ConfirmResize(client.ServiceClient(), "1234asdf")
192 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400193}
194
195func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400196 th.SetupHTTP()
197 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -0400198
Ash Wilsone77ffb02014-10-20 13:10:26 -0400199 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
200 th.TestMethod(t, r, "POST")
201 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
202 th.TestJSONRequest(t, r, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400203
204 w.WriteHeader(http.StatusAccepted)
205 })
206
Ash Wilsone77ffb02014-10-20 13:10:26 -0400207 res := RevertResize(client.ServiceClient(), "1234asdf")
208 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400209}