blob: 4ca6f010652c4b4ae6a03ca9c221ab87feff0f46 [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 Wilson189c95c2014-10-23 11:41:35 -040086 HandleServerGetSuccessfully(t)
Ash Wilsona612f1f2014-09-25 10:42:40 -040087
Ash Wilsone77ffb02014-10-20 13:10:26 -040088 client := client.ServiceClient()
Ash Wilsond27e0ff2014-09-25 11:50:31 -040089 actual, err := Get(client, "1234asdf").Extract()
Ash Wilsona612f1f2014-09-25 10:42:40 -040090 if err != nil {
91 t.Fatalf("Unexpected Get error: %v", err)
92 }
93
Ash Wilsond3532cd2014-10-21 14:37:47 -040094 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -040095}
96
97func TestUpdateServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -040098 th.SetupHTTP()
99 defer th.TeardownHTTP()
Ash Wilson189c95c2014-10-23 11:41:35 -0400100 HandleServerUpdateSuccessfully(t)
Ash Wilson0aac3a82014-09-25 10:45:03 -0400101
Ash Wilsone77ffb02014-10-20 13:10:26 -0400102 client := client.ServiceClient()
Ash Wilsondcbc8fb2014-09-29 09:05:44 -0400103 actual, err := Update(client, "1234asdf", UpdateOpts{Name: "new-name"}).Extract()
Ash Wilson0aac3a82014-09-25 10:45:03 -0400104 if err != nil {
105 t.Fatalf("Unexpected Update error: %v", err)
106 }
107
Ash Wilsond3532cd2014-10-21 14:37:47 -0400108 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400109}
110
111func TestChangeServerAdminPassword(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400112 th.SetupHTTP()
113 defer th.TeardownHTTP()
Ash Wilson1c1eb882014-10-21 18:14:31 -0400114 HandleAdminPasswordChangeSuccessfully(t)
Ash Wilsonfb99ec72014-09-25 10:48:51 -0400115
Ash Wilsone77ffb02014-10-20 13:10:26 -0400116 res := ChangeAdminPassword(client.ServiceClient(), "1234asdf", "new-password")
117 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400118}
119
120func TestRebootServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400121 th.SetupHTTP()
122 defer th.TeardownHTTP()
Ash Wilson2295ba52014-10-21 18:19:28 -0400123 HandleRebootSuccessfully(t)
Ash Wilson8d368e92014-09-25 10:49:07 -0400124
Ash Wilsone77ffb02014-10-20 13:10:26 -0400125 res := Reboot(client.ServiceClient(), "1234asdf", SoftReboot)
126 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400127}
128
129func TestRebuildServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400130 th.SetupHTTP()
131 defer th.TeardownHTTP()
Ash Wilsonacf49c62014-10-21 18:25:11 -0400132 HandleRebuildSuccessfully(t, SingleServerBody)
Ash Wilson077f8772014-09-25 10:57:13 -0400133
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200134 opts := RebuildOpts{
135 Name: "new-name",
136 AdminPass: "swordfish",
137 ImageID: "http://104.130.131.164:8774/fcad67a6189847c4aecfa3c81a05783b/images/f90f6034-2570-4974-8351-6b49732ef2eb",
138 AccessIPv4: "1.2.3.4",
Ash Wilson077f8772014-09-25 10:57:13 -0400139 }
140
Ash Wilsone77ffb02014-10-20 13:10:26 -0400141 actual, err := Rebuild(client.ServiceClient(), "1234asdf", opts).Extract()
142 th.AssertNoErr(t, err)
Jamie Hannaford6c9eb602014-10-16 16:28:07 +0200143
Ash Wilsond3532cd2014-10-21 14:37:47 -0400144 th.CheckDeepEquals(t, ServerDerp, *actual)
Ash Wilsonad21c712014-09-25 10:15:22 -0400145}
146
147func TestResizeServer(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400148 th.SetupHTTP()
149 defer th.TeardownHTTP()
Ash Wilson45181f42014-09-25 11:00:16 -0400150
Ash Wilsone77ffb02014-10-20 13:10:26 -0400151 th.Mux.HandleFunc("/servers/1234asdf/action", func(w http.ResponseWriter, r *http.Request) {
152 th.TestMethod(t, r, "POST")
153 th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
154 th.TestJSONRequest(t, r, `{ "resize": { "flavorRef": "2" } }`)
Ash Wilson45181f42014-09-25 11:00:16 -0400155
156 w.WriteHeader(http.StatusAccepted)
157 })
158
Ash Wilson5f7cf182014-10-23 08:35:24 -0400159 res := Resize(client.ServiceClient(), "1234asdf", ResizeOpts{FlavorRef: "2"})
Ash Wilsone77ffb02014-10-20 13:10:26 -0400160 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400161}
162
163func TestConfirmResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400164 th.SetupHTTP()
165 defer th.TeardownHTTP()
Ash Wilsone2bffd52014-09-25 11:11:43 -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, `{ "confirmResize": null }`)
Ash Wilsone2bffd52014-09-25 11:11:43 -0400171
172 w.WriteHeader(http.StatusNoContent)
173 })
174
Ash Wilsone77ffb02014-10-20 13:10:26 -0400175 res := ConfirmResize(client.ServiceClient(), "1234asdf")
176 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400177}
178
179func TestRevertResize(t *testing.T) {
Ash Wilsone77ffb02014-10-20 13:10:26 -0400180 th.SetupHTTP()
181 defer th.TeardownHTTP()
Ash Wilson8deb38c2014-09-25 11:11:53 -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, `{ "revertResize": null }`)
Ash Wilson8deb38c2014-09-25 11:11:53 -0400187
188 w.WriteHeader(http.StatusAccepted)
189 })
190
Ash Wilsone77ffb02014-10-20 13:10:26 -0400191 res := RevertResize(client.ServiceClient(), "1234asdf")
192 th.AssertNoErr(t, res.Err)
Ash Wilsonad21c712014-09-25 10:15:22 -0400193}