blob: 66448a7a0816263425914911192d0b114f3c4614 [file] [log] [blame]
Jamie Hannaford9fdda582015-02-10 12:15:43 +01001package instances
2
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
8 th "github.com/rackspace/gophercloud/testhelper"
9 fake "github.com/rackspace/gophercloud/testhelper/client"
10)
11
Jamie Hannaford821015f2015-02-10 12:58:36 +010012const singleInstanceJson = `
13{
14 "instance": {
15 "created": "2014-02-13T21:47:13",
16 "datastore": {
17 "type": "mysql",
18 "version": "5.6"
19 },
20 "flavor": {
21 "id": "1",
22 "links": [
23 {
24 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
25 "rel": "self"
26 },
27 {
28 "href": "https://my-openstack.com/v1.0/1234/flavors/1",
29 "rel": "bookmark"
30 }
31 ]
32 },
33 "links": [
34 {
35 "href": "https://my-openstack.com/v1.0/1234/instances/1",
36 "rel": "self"
37 }
38 ],
39 "hostname": "e09ad9a3f73309469cf1f43d11e79549caf9acf2.my-openstack.com",
40 "id": "d4603f69-ec7e-4e9b-803f-600b9205576f",
41 "name": "json_rack_instance",
42 "status": "BUILD",
43 "updated": "2014-02-13T21:47:13",
44 "volume": {
45 "size": 2
46 }
47 }
48}
49`
50
Jamie Hannaford9fdda582015-02-10 12:15:43 +010051func HandleCreateInstanceSuccessfully(t *testing.T) {
52 th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
53 th.TestMethod(t, r, "POST")
54 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
55
56 w.Header().Set("Content-Type", "application/json")
57 w.WriteHeader(http.StatusOK)
58
59 th.TestJSONRequest(t, r, `
60{
61 "instance": {
62 "databases": [
63 {
64 "character_set": "utf8",
65 "collate": "utf8_general_ci",
66 "name": "sampledb"
67 },
68 {
69 "name": "nextround"
70 }
71 ],
72 "flavorRef": "1",
73 "name": "json_rack_instance",
74 "users": [
75 {
76 "databases": [
77 {
78 "name": "sampledb"
79 }
80 ],
81 "name": "demouser",
82 "password": "demopassword"
83 }
84 ],
85 "volume": {
86 "size": 2
87 }
88 }
89}
90`)
91
Jamie Hannaford821015f2015-02-10 12:58:36 +010092 fmt.Fprintf(w, singleInstanceJson)
Jamie Hannaford9fdda582015-02-10 12:15:43 +010093 })
94}
Jamie Hannaford90684242015-02-10 12:46:07 +010095
96func HandleListInstanceSuccessfully(t *testing.T) {
97 th.Mux.HandleFunc("/instances", func(w http.ResponseWriter, r *http.Request) {
98 th.TestMethod(t, r, "GET")
99 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
100
101 w.Header().Add("Content-Type", "application/json")
102
103 fmt.Fprintf(w, `
104{
105 "instances": [
106 {
107 "name": "xml_rack_instance",
108 "status": "ACTIVE",
109 "volume": {
110 "size": 2
111 },
112 "flavor": {
113 "id": "1",
114 "links": [
115 {
116 "href": "https://openstack.example.com/v1.0/1234/flavors/1",
117 "rel": "self"
118 },
119 {
120 "href": "https://openstack.example.com/flavors/1",
121 "rel": "bookmark"
122 }
123 ]
124 },
125 "id": "8fb081af-f237-44f5-80cc-b46be1840ca9",
126 "links": [
127 {
128 "href": "https://openstack.example.com/v1.0/1234/instances/8fb081af-f237-44f5-80cc-b46be1840ca9",
129 "rel": "self"
Jamie Hannaford90684242015-02-10 12:46:07 +0100130 }
131 ]
132 }
133 ]
134}
135`)
136 })
137}
Jamie Hannaford821015f2015-02-10 12:58:36 +0100138
139func HandleGetInstanceSuccessfully(t *testing.T, id string) {
140 th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
141 th.TestMethod(t, r, "GET")
142 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
143
144 w.Header().Add("Content-Type", "application/json")
145
146 fmt.Fprintf(w, singleInstanceJson)
147 })
148}
Jamie Hannaford5b16b632015-02-10 13:36:23 +0100149
150func HandleDeleteInstanceSuccessfully(t *testing.T, id string) {
151 th.Mux.HandleFunc("/instances/"+id, func(w http.ResponseWriter, r *http.Request) {
152 th.TestMethod(t, r, "DELETE")
153 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
154 w.WriteHeader(http.StatusAccepted)
155 })
156}
Jamie Hannaford94164fa2015-02-10 13:58:45 +0100157
158func HandleEnableRootUserSuccessfully(t *testing.T, id string) {
159 th.Mux.HandleFunc("/instances/"+id+"/root", func(w http.ResponseWriter, r *http.Request) {
160 th.TestMethod(t, r, "POST")
161 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
162
163 w.WriteHeader(http.StatusOK)
164 fmt.Fprintf(w, `{"user":{"name":"root","password":"secretsecret"}}`)
165 })
166}
Jamie Hannaforda74d4252015-02-10 15:35:01 +0100167
168func HandleIsRootEnabledSuccessfully(t *testing.T, id string) {
169 th.Mux.HandleFunc("/instances/"+id+"/root", func(w http.ResponseWriter, r *http.Request) {
170 th.TestMethod(t, r, "GET")
171 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
172
173 w.WriteHeader(http.StatusOK)
174 fmt.Fprintf(w, `{"rootEnabled":true}`)
175 })
176}
Jamie Hannaford219ca592015-02-10 15:59:05 +0100177
178func HandleRestartSuccessfully(t *testing.T, id string) {
179 th.Mux.HandleFunc("/instances/"+id+"/action", func(w http.ResponseWriter, r *http.Request) {
180 th.TestMethod(t, r, "POST")
181 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
182 th.TestJSONRequest(t, r, `{"restart": true}`)
183 w.WriteHeader(http.StatusAccepted)
184 })
185}
186
187func HandleResizeInstanceSuccessfully(t *testing.T, id string) {
188 th.Mux.HandleFunc("/instances/"+id+"/action", func(w http.ResponseWriter, r *http.Request) {
189 th.TestMethod(t, r, "POST")
190 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
191 th.TestJSONRequest(t, r, `{"resize": {"flavorRef": "2"}}`)
192 w.WriteHeader(http.StatusAccepted)
193 })
194}
195
196func HandleResizeVolSuccessfully(t *testing.T, id string) {
197 th.Mux.HandleFunc("/instances/"+id+"/action", func(w http.ResponseWriter, r *http.Request) {
198 th.TestMethod(t, r, "POST")
199 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
200 th.TestJSONRequest(t, r, `{"resize": {"volume": {"size": 4}}}`)
201 w.WriteHeader(http.StatusAccepted)
202 })
203}