blob: 536e7f8ea1a8b25589fa8c246fc8e47607795895 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Jamie Hannaford924c09d2014-11-19 12:05:38 +01002
3import (
4 "fmt"
5 "net/http"
6 "testing"
7
Jon Perritt27249f42016-02-18 10:35:59 -06008 th "github.com/gophercloud/gophercloud/testhelper"
9 fake "github.com/gophercloud/gophercloud/testhelper/client"
Jamie Hannaford924c09d2014-11-19 12:05:38 +010010)
11
Jamie Hannaforda493e642014-11-19 12:40:30 +010012const rootPath = "/os-security-groups"
13
Jamie Hannaford19151792014-11-19 12:46:47 +010014const listGroupsJSON = `
15{
Jamie Hannaford334c8752014-11-20 12:05:09 +010016 "security_groups": [
17 {
18 "description": "default",
Jamie Hannaford2f226172014-11-25 11:52:25 +010019 "id": "{groupID}",
Jamie Hannaford334c8752014-11-20 12:05:09 +010020 "name": "default",
21 "rules": [],
22 "tenant_id": "openstack"
23 }
24 ]
Jamie Hannaford19151792014-11-19 12:46:47 +010025}
26`
27
Jamie Hannaford924c09d2014-11-19 12:05:38 +010028func mockListGroupsResponse(t *testing.T) {
Jamie Hannaforda493e642014-11-19 12:40:30 +010029 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford924c09d2014-11-19 12:05:38 +010030 th.TestMethod(t, r, "GET")
31 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
32
33 w.Header().Add("Content-Type", "application/json")
34 w.WriteHeader(http.StatusOK)
35
Jamie Hannaford19151792014-11-19 12:46:47 +010036 fmt.Fprintf(w, listGroupsJSON)
37 })
Jamie Hannaford924c09d2014-11-19 12:05:38 +010038}
Jamie Hannaford19151792014-11-19 12:46:47 +010039
40func mockListGroupsByServerResponse(t *testing.T, serverID string) {
Jon Perritt13dd1422015-02-10 17:35:54 -070041 url := fmt.Sprintf("/servers/%s%s", serverID, rootPath)
Jamie Hannaford19151792014-11-19 12:46:47 +010042 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
43 th.TestMethod(t, r, "GET")
44 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
45
46 w.Header().Add("Content-Type", "application/json")
47 w.WriteHeader(http.StatusOK)
48
49 fmt.Fprintf(w, listGroupsJSON)
Jamie Hannaford924c09d2014-11-19 12:05:38 +010050 })
51}
Jamie Hannaforda493e642014-11-19 12:40:30 +010052
53func mockCreateGroupResponse(t *testing.T) {
54 th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) {
55 th.TestMethod(t, r, "POST")
56 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
57
58 th.TestJSONRequest(t, r, `
59{
60 "security_group": {
61 "name": "test",
62 "description": "something"
63 }
64}
65 `)
66
67 w.Header().Add("Content-Type", "application/json")
68 w.WriteHeader(http.StatusOK)
69
70 fmt.Fprintf(w, `
71{
72 "security_group": {
73 "description": "something",
Jamie Hannaford2f226172014-11-25 11:52:25 +010074 "id": "{groupID}",
Jamie Hannaforda493e642014-11-19 12:40:30 +010075 "name": "test",
76 "rules": [],
77 "tenant_id": "openstack"
78 }
79}
80`)
81 })
82}
Jamie Hannafordb38dd312014-11-19 13:02:11 +010083
Jamie Hannaford2f226172014-11-25 11:52:25 +010084func mockUpdateGroupResponse(t *testing.T, groupID string) {
85 url := fmt.Sprintf("%s/%s", rootPath, groupID)
Jamie Hannaford30c74662014-11-19 15:37:34 +010086 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford740e4a32014-11-19 16:13:30 +010087 th.TestMethod(t, r, "PUT")
Jamie Hannaford30c74662014-11-19 15:37:34 +010088 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
89
90 th.TestJSONRequest(t, r, `
91{
Jamie Hannaford334c8752014-11-20 12:05:09 +010092 "security_group": {
Jamie Hannaford0e750962014-11-24 16:04:38 +010093 "name": "new_name",
94 "description": "new_desc"
Jamie Hannaford334c8752014-11-20 12:05:09 +010095 }
Jamie Hannaford30c74662014-11-19 15:37:34 +010096}
97 `)
98
99 w.Header().Add("Content-Type", "application/json")
100 w.WriteHeader(http.StatusOK)
101
102 fmt.Fprintf(w, `
103{
Jamie Hannaford334c8752014-11-20 12:05:09 +0100104 "security_group": {
105 "description": "something",
Jamie Hannaford2f226172014-11-25 11:52:25 +0100106 "id": "{groupID}",
Jamie Hannaford334c8752014-11-20 12:05:09 +0100107 "name": "new_name",
108 "rules": [],
109 "tenant_id": "openstack"
110 }
Jamie Hannaford30c74662014-11-19 15:37:34 +0100111}
112`)
113 })
114}
115
Jamie Hannaford2f226172014-11-25 11:52:25 +0100116func mockGetGroupsResponse(t *testing.T, groupID string) {
117 url := fmt.Sprintf("%s/%s", rootPath, groupID)
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100118 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
119 th.TestMethod(t, r, "GET")
120 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
121
122 w.Header().Add("Content-Type", "application/json")
123 w.WriteHeader(http.StatusOK)
124
125 fmt.Fprintf(w, `
126{
127 "security_group": {
128 "description": "default",
Jamie Hannaford2f226172014-11-25 11:52:25 +0100129 "id": "{groupID}",
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100130 "name": "default",
131 "rules": [
132 {
133 "from_port": 80,
134 "group": {
135 "tenant_id": "openstack",
136 "name": "default"
137 },
138 "ip_protocol": "TCP",
139 "to_port": 85,
Jamie Hannaford2f226172014-11-25 11:52:25 +0100140 "parent_group_id": "{groupID}",
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100141 "ip_range": {
142 "cidr": "0.0.0.0"
143 },
Jamie Hannaford2f226172014-11-25 11:52:25 +0100144 "id": "{ruleID}"
Jamie Hannafordb38dd312014-11-19 13:02:11 +0100145 }
146 ],
147 "tenant_id": "openstack"
148 }
149}
150 `)
151 })
152}
Jamie Hannafordd276e612014-11-19 13:56:28 +0100153
Jamie Hannafordcb0c19a2014-11-25 11:57:35 +0100154func mockGetNumericIDGroupResponse(t *testing.T, groupID int) {
155 url := fmt.Sprintf("%s/%d", rootPath, groupID)
156 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
157 th.TestMethod(t, r, "GET")
158 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
159
160 w.Header().Add("Content-Type", "application/json")
161 w.WriteHeader(http.StatusOK)
162
163 fmt.Fprintf(w, `
164{
165 "security_group": {
Joe Topjian368deee2017-01-12 14:19:23 -0700166 "id": %d
Jamie Hannafordcb0c19a2014-11-25 11:57:35 +0100167 }
168}
Joe Topjian368deee2017-01-12 14:19:23 -0700169 `, groupID)
170 })
171}
172
173func mockGetNumericIDGroupRuleResponse(t *testing.T, groupID int) {
174 url := fmt.Sprintf("%s/%d", rootPath, groupID)
175 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
176 th.TestMethod(t, r, "GET")
177 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
178
179 w.Header().Add("Content-Type", "application/json")
180 w.WriteHeader(http.StatusOK)
181
182 fmt.Fprintf(w, `
183{
184 "security_group": {
185 "id": %d,
186 "rules": [
187 {
188 "parent_group_id": %d,
189 "id": %d
190 }
191 ]
192 }
193}
194 `, groupID, groupID, groupID)
Jamie Hannafordcb0c19a2014-11-25 11:57:35 +0100195 })
196}
197
Jamie Hannaford2f226172014-11-25 11:52:25 +0100198func mockDeleteGroupResponse(t *testing.T, groupID string) {
199 url := fmt.Sprintf("%s/%s", rootPath, groupID)
Jamie Hannafordd276e612014-11-19 13:56:28 +0100200 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
201 th.TestMethod(t, r, "DELETE")
202 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
203 w.Header().Add("Content-Type", "application/json")
204 w.WriteHeader(http.StatusAccepted)
205 })
206}
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100207
208func mockAddRuleResponse(t *testing.T) {
209 th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
210 th.TestMethod(t, r, "POST")
211 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
212
213 th.TestJSONRequest(t, r, `
214{
215 "security_group_rule": {
216 "from_port": 22,
217 "ip_protocol": "TCP",
218 "to_port": 22,
Jamie Hannaford2f226172014-11-25 11:52:25 +0100219 "parent_group_id": "{groupID}",
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100220 "cidr": "0.0.0.0/0"
221 }
222} `)
223
224 w.Header().Add("Content-Type", "application/json")
225 w.WriteHeader(http.StatusOK)
226
227 fmt.Fprintf(w, `
228{
229 "security_group_rule": {
230 "from_port": 22,
231 "group": {},
232 "ip_protocol": "TCP",
233 "to_port": 22,
Jamie Hannaford2f226172014-11-25 11:52:25 +0100234 "parent_group_id": "{groupID}",
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100235 "ip_range": {
236 "cidr": "0.0.0.0/0"
237 },
Jamie Hannaford2f226172014-11-25 11:52:25 +0100238 "id": "{ruleID}"
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100239 }
240}`)
241 })
242}
243
Joe Topjian4f9dce22016-02-28 00:03:37 +0000244func mockAddRuleResponseICMPZero(t *testing.T) {
245 th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) {
246 th.TestMethod(t, r, "POST")
247 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
248
249 th.TestJSONRequest(t, r, `
250{
251 "security_group_rule": {
jrperritte0ba1052016-04-13 17:19:54 -0500252 "from_port": 0,
Joe Topjian4f9dce22016-02-28 00:03:37 +0000253 "ip_protocol": "ICMP",
jrperritte0ba1052016-04-13 17:19:54 -0500254 "to_port": 0,
Joe Topjian4f9dce22016-02-28 00:03:37 +0000255 "parent_group_id": "{groupID}",
256 "cidr": "0.0.0.0/0"
257 }
258} `)
259
260 w.Header().Add("Content-Type", "application/json")
261 w.WriteHeader(http.StatusOK)
262
263 fmt.Fprintf(w, `
264{
265 "security_group_rule": {
jrperritte0ba1052016-04-13 17:19:54 -0500266 "from_port": 0,
Joe Topjian4f9dce22016-02-28 00:03:37 +0000267 "group": {},
268 "ip_protocol": "ICMP",
jrperritte0ba1052016-04-13 17:19:54 -0500269 "to_port": 0,
Joe Topjian4f9dce22016-02-28 00:03:37 +0000270 "parent_group_id": "{groupID}",
271 "ip_range": {
272 "cidr": "0.0.0.0/0"
273 },
274 "id": "{ruleID}"
275 }
276}`)
277 })
278}
279
Jamie Hannaford2f226172014-11-25 11:52:25 +0100280func mockDeleteRuleResponse(t *testing.T, ruleID string) {
281 url := fmt.Sprintf("/os-security-group-rules/%s", ruleID)
Jamie Hannaford8badf1e2014-11-19 14:39:26 +0100282 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
283 th.TestMethod(t, r, "DELETE")
284 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
285 w.Header().Add("Content-Type", "application/json")
286 w.WriteHeader(http.StatusAccepted)
287 })
288}
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100289
290func mockAddServerToGroupResponse(t *testing.T, serverID string) {
291 url := fmt.Sprintf("/servers/%s/action", serverID)
292 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
293 th.TestMethod(t, r, "POST")
294 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
295
296 th.TestJSONRequest(t, r, `
297{
298 "addSecurityGroup": {
299 "name": "test"
300 }
301}
302 `)
303
304 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford5b2feb52014-11-20 12:02:15 +0100305 w.WriteHeader(http.StatusAccepted)
Pratik Mallyaee675fd2015-09-14 14:07:30 -0500306 fmt.Fprintf(w, `{}`)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100307 })
308}
309
310func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) {
311 url := fmt.Sprintf("/servers/%s/action", serverID)
312 th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) {
313 th.TestMethod(t, r, "POST")
314 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
315
316 th.TestJSONRequest(t, r, `
317{
Jamie Hannaford334c8752014-11-20 12:05:09 +0100318 "removeSecurityGroup": {
319 "name": "test"
320 }
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100321}
322 `)
323
324 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford5b2feb52014-11-20 12:02:15 +0100325 w.WriteHeader(http.StatusAccepted)
Pratik Mallyaee675fd2015-09-14 14:07:30 -0500326 fmt.Fprintf(w, `{}`)
Jamie Hannaford740e4a32014-11-19 16:13:30 +0100327 })
328}