Keith Byrne | bda4859 | 2016-03-23 11:37:08 +0000 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 3 | package secgroups |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 10 | th "github.com/gophercloud/gophercloud/testhelper" |
| 11 | fake "github.com/gophercloud/gophercloud/testhelper/client" |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 12 | ) |
| 13 | |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 14 | const rootPath = "/os-security-groups" |
| 15 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 16 | const listGroupsJSON = ` |
| 17 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 18 | "security_groups": [ |
| 19 | { |
| 20 | "description": "default", |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 21 | "id": "{groupID}", |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 22 | "name": "default", |
| 23 | "rules": [], |
| 24 | "tenant_id": "openstack" |
| 25 | } |
| 26 | ] |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 27 | } |
| 28 | ` |
| 29 | |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 30 | func mockListGroupsResponse(t *testing.T) { |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 31 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 32 | th.TestMethod(t, r, "GET") |
| 33 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 34 | |
| 35 | w.Header().Add("Content-Type", "application/json") |
| 36 | w.WriteHeader(http.StatusOK) |
| 37 | |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 38 | fmt.Fprintf(w, listGroupsJSON) |
| 39 | }) |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 40 | } |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 41 | |
| 42 | func mockListGroupsByServerResponse(t *testing.T, serverID string) { |
Jon Perritt | 13dd142 | 2015-02-10 17:35:54 -0700 | [diff] [blame] | 43 | url := fmt.Sprintf("/servers/%s%s", serverID, rootPath) |
Jamie Hannaford | 1915179 | 2014-11-19 12:46:47 +0100 | [diff] [blame] | 44 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 45 | th.TestMethod(t, r, "GET") |
| 46 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 47 | |
| 48 | w.Header().Add("Content-Type", "application/json") |
| 49 | w.WriteHeader(http.StatusOK) |
| 50 | |
| 51 | fmt.Fprintf(w, listGroupsJSON) |
Jamie Hannaford | 924c09d | 2014-11-19 12:05:38 +0100 | [diff] [blame] | 52 | }) |
| 53 | } |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 54 | |
| 55 | func mockCreateGroupResponse(t *testing.T) { |
| 56 | th.Mux.HandleFunc(rootPath, func(w http.ResponseWriter, r *http.Request) { |
| 57 | th.TestMethod(t, r, "POST") |
| 58 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 59 | |
| 60 | th.TestJSONRequest(t, r, ` |
| 61 | { |
| 62 | "security_group": { |
| 63 | "name": "test", |
| 64 | "description": "something" |
| 65 | } |
| 66 | } |
| 67 | `) |
| 68 | |
| 69 | w.Header().Add("Content-Type", "application/json") |
| 70 | w.WriteHeader(http.StatusOK) |
| 71 | |
| 72 | fmt.Fprintf(w, ` |
| 73 | { |
| 74 | "security_group": { |
| 75 | "description": "something", |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 76 | "id": "{groupID}", |
Jamie Hannaford | a493e64 | 2014-11-19 12:40:30 +0100 | [diff] [blame] | 77 | "name": "test", |
| 78 | "rules": [], |
| 79 | "tenant_id": "openstack" |
| 80 | } |
| 81 | } |
| 82 | `) |
| 83 | }) |
| 84 | } |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 85 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 86 | func mockUpdateGroupResponse(t *testing.T, groupID string) { |
| 87 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 88 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 89 | th.TestMethod(t, r, "PUT") |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 90 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 91 | |
| 92 | th.TestJSONRequest(t, r, ` |
| 93 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 94 | "security_group": { |
Jamie Hannaford | 0e75096 | 2014-11-24 16:04:38 +0100 | [diff] [blame] | 95 | "name": "new_name", |
| 96 | "description": "new_desc" |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 97 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 98 | } |
| 99 | `) |
| 100 | |
| 101 | w.Header().Add("Content-Type", "application/json") |
| 102 | w.WriteHeader(http.StatusOK) |
| 103 | |
| 104 | fmt.Fprintf(w, ` |
| 105 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 106 | "security_group": { |
| 107 | "description": "something", |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 108 | "id": "{groupID}", |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 109 | "name": "new_name", |
| 110 | "rules": [], |
| 111 | "tenant_id": "openstack" |
| 112 | } |
Jamie Hannaford | 30c7466 | 2014-11-19 15:37:34 +0100 | [diff] [blame] | 113 | } |
| 114 | `) |
| 115 | }) |
| 116 | } |
| 117 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 118 | func mockGetGroupsResponse(t *testing.T, groupID string) { |
| 119 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 120 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 121 | th.TestMethod(t, r, "GET") |
| 122 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 123 | |
| 124 | w.Header().Add("Content-Type", "application/json") |
| 125 | w.WriteHeader(http.StatusOK) |
| 126 | |
| 127 | fmt.Fprintf(w, ` |
| 128 | { |
| 129 | "security_group": { |
| 130 | "description": "default", |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 131 | "id": "{groupID}", |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 132 | "name": "default", |
| 133 | "rules": [ |
| 134 | { |
| 135 | "from_port": 80, |
| 136 | "group": { |
| 137 | "tenant_id": "openstack", |
| 138 | "name": "default" |
| 139 | }, |
| 140 | "ip_protocol": "TCP", |
| 141 | "to_port": 85, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 142 | "parent_group_id": "{groupID}", |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 143 | "ip_range": { |
| 144 | "cidr": "0.0.0.0" |
| 145 | }, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 146 | "id": "{ruleID}" |
Jamie Hannaford | b38dd31 | 2014-11-19 13:02:11 +0100 | [diff] [blame] | 147 | } |
| 148 | ], |
| 149 | "tenant_id": "openstack" |
| 150 | } |
| 151 | } |
| 152 | `) |
| 153 | }) |
| 154 | } |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 155 | |
Jamie Hannaford | cb0c19a | 2014-11-25 11:57:35 +0100 | [diff] [blame] | 156 | func mockGetNumericIDGroupResponse(t *testing.T, groupID int) { |
| 157 | url := fmt.Sprintf("%s/%d", rootPath, groupID) |
| 158 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 159 | th.TestMethod(t, r, "GET") |
| 160 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 161 | |
| 162 | w.Header().Add("Content-Type", "application/json") |
| 163 | w.WriteHeader(http.StatusOK) |
| 164 | |
| 165 | fmt.Fprintf(w, ` |
| 166 | { |
| 167 | "security_group": { |
Jon Perritt | 1239521 | 2016-02-24 10:41:17 -0600 | [diff] [blame] | 168 | "id": "12345" |
Jamie Hannaford | cb0c19a | 2014-11-25 11:57:35 +0100 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | `) |
| 172 | }) |
| 173 | } |
| 174 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 175 | func mockDeleteGroupResponse(t *testing.T, groupID string) { |
| 176 | url := fmt.Sprintf("%s/%s", rootPath, groupID) |
Jamie Hannaford | d276e61 | 2014-11-19 13:56:28 +0100 | [diff] [blame] | 177 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 178 | th.TestMethod(t, r, "DELETE") |
| 179 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 180 | w.Header().Add("Content-Type", "application/json") |
| 181 | w.WriteHeader(http.StatusAccepted) |
| 182 | }) |
| 183 | } |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 184 | |
| 185 | func mockAddRuleResponse(t *testing.T) { |
| 186 | th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) { |
| 187 | th.TestMethod(t, r, "POST") |
| 188 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 189 | |
| 190 | th.TestJSONRequest(t, r, ` |
| 191 | { |
| 192 | "security_group_rule": { |
| 193 | "from_port": 22, |
| 194 | "ip_protocol": "TCP", |
| 195 | "to_port": 22, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 196 | "parent_group_id": "{groupID}", |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 197 | "cidr": "0.0.0.0/0" |
| 198 | } |
| 199 | } `) |
| 200 | |
| 201 | w.Header().Add("Content-Type", "application/json") |
| 202 | w.WriteHeader(http.StatusOK) |
| 203 | |
| 204 | fmt.Fprintf(w, ` |
| 205 | { |
| 206 | "security_group_rule": { |
| 207 | "from_port": 22, |
| 208 | "group": {}, |
| 209 | "ip_protocol": "TCP", |
| 210 | "to_port": 22, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 211 | "parent_group_id": "{groupID}", |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 212 | "ip_range": { |
| 213 | "cidr": "0.0.0.0/0" |
| 214 | }, |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 215 | "id": "{ruleID}" |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 216 | } |
| 217 | }`) |
| 218 | }) |
| 219 | } |
| 220 | |
Joe Topjian | 4f9dce2 | 2016-02-28 00:03:37 +0000 | [diff] [blame] | 221 | func mockAddRuleResponseICMPZero(t *testing.T) { |
| 222 | th.Mux.HandleFunc("/os-security-group-rules", func(w http.ResponseWriter, r *http.Request) { |
| 223 | th.TestMethod(t, r, "POST") |
| 224 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 225 | |
| 226 | th.TestJSONRequest(t, r, ` |
| 227 | { |
| 228 | "security_group_rule": { |
| 229 | "from_port": 0, |
| 230 | "ip_protocol": "ICMP", |
| 231 | "to_port": 0, |
| 232 | "parent_group_id": "{groupID}", |
| 233 | "cidr": "0.0.0.0/0" |
| 234 | } |
| 235 | } `) |
| 236 | |
| 237 | w.Header().Add("Content-Type", "application/json") |
| 238 | w.WriteHeader(http.StatusOK) |
| 239 | |
| 240 | fmt.Fprintf(w, ` |
| 241 | { |
| 242 | "security_group_rule": { |
| 243 | "from_port": 0, |
| 244 | "group": {}, |
| 245 | "ip_protocol": "ICMP", |
| 246 | "to_port": 0, |
| 247 | "parent_group_id": "{groupID}", |
| 248 | "ip_range": { |
| 249 | "cidr": "0.0.0.0/0" |
| 250 | }, |
| 251 | "id": "{ruleID}" |
| 252 | } |
| 253 | }`) |
| 254 | }) |
| 255 | } |
| 256 | |
Jamie Hannaford | 2f22617 | 2014-11-25 11:52:25 +0100 | [diff] [blame] | 257 | func mockDeleteRuleResponse(t *testing.T, ruleID string) { |
| 258 | url := fmt.Sprintf("/os-security-group-rules/%s", ruleID) |
Jamie Hannaford | 8badf1e | 2014-11-19 14:39:26 +0100 | [diff] [blame] | 259 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 260 | th.TestMethod(t, r, "DELETE") |
| 261 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 262 | w.Header().Add("Content-Type", "application/json") |
| 263 | w.WriteHeader(http.StatusAccepted) |
| 264 | }) |
| 265 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 266 | |
| 267 | func mockAddServerToGroupResponse(t *testing.T, serverID string) { |
| 268 | url := fmt.Sprintf("/servers/%s/action", serverID) |
| 269 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 270 | th.TestMethod(t, r, "POST") |
| 271 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 272 | |
| 273 | th.TestJSONRequest(t, r, ` |
| 274 | { |
| 275 | "addSecurityGroup": { |
| 276 | "name": "test" |
| 277 | } |
| 278 | } |
| 279 | `) |
| 280 | |
| 281 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | 5b2feb5 | 2014-11-20 12:02:15 +0100 | [diff] [blame] | 282 | w.WriteHeader(http.StatusAccepted) |
Pratik Mallya | ee675fd | 2015-09-14 14:07:30 -0500 | [diff] [blame] | 283 | fmt.Fprintf(w, `{}`) |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 284 | }) |
| 285 | } |
| 286 | |
| 287 | func mockRemoveServerFromGroupResponse(t *testing.T, serverID string) { |
| 288 | url := fmt.Sprintf("/servers/%s/action", serverID) |
| 289 | th.Mux.HandleFunc(url, func(w http.ResponseWriter, r *http.Request) { |
| 290 | th.TestMethod(t, r, "POST") |
| 291 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 292 | |
| 293 | th.TestJSONRequest(t, r, ` |
| 294 | { |
Jamie Hannaford | 334c875 | 2014-11-20 12:05:09 +0100 | [diff] [blame] | 295 | "removeSecurityGroup": { |
| 296 | "name": "test" |
| 297 | } |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 298 | } |
| 299 | `) |
| 300 | |
| 301 | w.Header().Add("Content-Type", "application/json") |
Jamie Hannaford | 5b2feb5 | 2014-11-20 12:02:15 +0100 | [diff] [blame] | 302 | w.WriteHeader(http.StatusAccepted) |
Pratik Mallya | ee675fd | 2015-09-14 14:07:30 -0500 | [diff] [blame] | 303 | fmt.Fprintf(w, `{}`) |
Jamie Hannaford | 740e4a3 | 2014-11-19 16:13:30 +0100 | [diff] [blame] | 304 | }) |
| 305 | } |