github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/networking/v2/extensions/security/rules/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 "testing" 8 9 fake "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/common" 10 "github.com/vnpaycloud-console/gophercloud/v2/openstack/networking/v2/extensions/security/rules" 11 "github.com/vnpaycloud-console/gophercloud/v2/pagination" 12 th "github.com/vnpaycloud-console/gophercloud/v2/testhelper" 13 ) 14 15 func TestList(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 20 th.TestMethod(t, r, "GET") 21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 22 23 w.Header().Add("Content-Type", "application/json") 24 w.WriteHeader(http.StatusOK) 25 26 fmt.Fprint(w, ` 27 { 28 "security_group_rules": [ 29 { 30 "direction": "egress", 31 "ethertype": "IPv6", 32 "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff", 33 "port_range_max": null, 34 "port_range_min": null, 35 "protocol": null, 36 "remote_group_id": null, 37 "remote_ip_prefix": null, 38 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 39 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 40 }, 41 { 42 "direction": "egress", 43 "ethertype": "IPv4", 44 "id": "93aa42e5-80db-4581-9391-3a608bd0e448", 45 "port_range_max": null, 46 "port_range_min": null, 47 "protocol": null, 48 "remote_group_id": null, 49 "remote_ip_prefix": null, 50 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 51 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 52 } 53 ] 54 } 55 `) 56 }) 57 58 count := 0 59 60 err := rules.List(fake.ServiceClient(), rules.ListOpts{}).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) { 61 count++ 62 actual, err := rules.ExtractRules(page) 63 if err != nil { 64 t.Errorf("Failed to extract secrules: %v", err) 65 return false, err 66 } 67 68 expected := []rules.SecGroupRule{ 69 { 70 Description: "", 71 Direction: "egress", 72 EtherType: "IPv6", 73 ID: "3c0e45ff-adaf-4124-b083-bf390e5482ff", 74 PortRangeMax: 0, 75 PortRangeMin: 0, 76 Protocol: "", 77 RemoteGroupID: "", 78 RemoteIPPrefix: "", 79 SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 80 TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", 81 }, 82 { 83 Direction: "egress", 84 EtherType: "IPv4", 85 ID: "93aa42e5-80db-4581-9391-3a608bd0e448", 86 PortRangeMax: 0, 87 PortRangeMin: 0, 88 Protocol: "", 89 RemoteGroupID: "", 90 RemoteIPPrefix: "", 91 SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 92 TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", 93 }, 94 } 95 96 th.CheckDeepEquals(t, expected, actual) 97 98 return true, nil 99 }) 100 th.AssertNoErr(t, err) 101 102 if count != 1 { 103 t.Errorf("Expected 1 page, got %d", count) 104 } 105 } 106 107 func TestCreate(t *testing.T) { 108 th.SetupHTTP() 109 defer th.TeardownHTTP() 110 111 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 112 th.TestMethod(t, r, "POST") 113 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 114 th.TestHeader(t, r, "Content-Type", "application/json") 115 th.TestHeader(t, r, "Accept", "application/json") 116 th.TestJSONRequest(t, r, ` 117 { 118 "security_group_rule": { 119 "description": "test description of rule", 120 "direction": "ingress", 121 "port_range_min": 80, 122 "ethertype": "IPv4", 123 "port_range_max": 80, 124 "protocol": "tcp", 125 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 126 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a" 127 } 128 } 129 `) 130 131 w.Header().Add("Content-Type", "application/json") 132 w.WriteHeader(http.StatusCreated) 133 134 fmt.Fprint(w, ` 135 { 136 "security_group_rule": { 137 "description": "test description of rule", 138 "direction": "ingress", 139 "ethertype": "IPv4", 140 "id": "2bc0accf-312e-429a-956e-e4407625eb62", 141 "port_range_max": 80, 142 "port_range_min": 80, 143 "protocol": "tcp", 144 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 145 "remote_ip_prefix": null, 146 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a", 147 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 148 } 149 } 150 `) 151 }) 152 153 opts := rules.CreateOpts{ 154 Description: "test description of rule", 155 Direction: "ingress", 156 PortRangeMin: 80, 157 EtherType: rules.EtherType4, 158 PortRangeMax: 80, 159 Protocol: "tcp", 160 RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 161 SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a", 162 } 163 _, err := rules.Create(context.TODO(), fake.ServiceClient(), opts).Extract() 164 th.AssertNoErr(t, err) 165 } 166 167 func TestCreateAnyProtocol(t *testing.T) { 168 th.SetupHTTP() 169 defer th.TeardownHTTP() 170 171 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 172 th.TestMethod(t, r, "POST") 173 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 174 th.TestHeader(t, r, "Content-Type", "application/json") 175 th.TestHeader(t, r, "Accept", "application/json") 176 th.TestJSONRequest(t, r, ` 177 { 178 "security_group_rule": { 179 "description": "test description of rule", 180 "direction": "ingress", 181 "port_range_min": 80, 182 "ethertype": "IPv4", 183 "port_range_max": 80, 184 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 185 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a" 186 } 187 } 188 `) 189 190 w.Header().Add("Content-Type", "application/json") 191 w.WriteHeader(http.StatusCreated) 192 193 fmt.Fprint(w, ` 194 { 195 "security_group_rule": { 196 "description": "test description of rule", 197 "direction": "ingress", 198 "ethertype": "IPv4", 199 "id": "2bc0accf-312e-429a-956e-e4407625eb62", 200 "port_range_max": 80, 201 "port_range_min": 80, 202 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 203 "remote_ip_prefix": null, 204 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a", 205 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 206 } 207 } 208 `) 209 }) 210 211 opts := rules.CreateOpts{ 212 Description: "test description of rule", 213 Direction: "ingress", 214 PortRangeMin: 80, 215 EtherType: rules.EtherType4, 216 PortRangeMax: 80, 217 Protocol: rules.ProtocolAny, 218 RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 219 SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a", 220 } 221 _, err := rules.Create(context.TODO(), fake.ServiceClient(), opts).Extract() 222 th.AssertNoErr(t, err) 223 } 224 225 func TestCreateBulk(t *testing.T) { 226 th.SetupHTTP() 227 defer th.TeardownHTTP() 228 229 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 230 th.TestMethod(t, r, "POST") 231 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 232 th.TestHeader(t, r, "Content-Type", "application/json") 233 th.TestHeader(t, r, "Accept", "application/json") 234 th.TestJSONRequest(t, r, ` 235 { 236 "security_group_rules": [ 237 { 238 "description": "test description of rule", 239 "direction": "ingress", 240 "port_range_min": 80, 241 "ethertype": "IPv4", 242 "port_range_max": 80, 243 "protocol": "tcp", 244 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 245 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a" 246 }, 247 { 248 "description": "test description of rule", 249 "direction": "ingress", 250 "port_range_min": 443, 251 "ethertype": "IPv4", 252 "port_range_max": 443, 253 "protocol": "tcp", 254 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a" 255 } 256 ] 257 } 258 `) 259 260 w.Header().Add("Content-Type", "application/json") 261 w.WriteHeader(http.StatusCreated) 262 263 fmt.Fprint(w, ` 264 { 265 "security_group_rules": [ 266 { 267 "description": "test description of rule", 268 "direction": "ingress", 269 "ethertype": "IPv4", 270 "port_range_max": 80, 271 "port_range_min": 80, 272 "protocol": "tcp", 273 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 274 "remote_ip_prefix": null, 275 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a", 276 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 277 }, 278 { 279 "description": "test description of rule", 280 "direction": "ingress", 281 "ethertype": "IPv4", 282 "port_range_max": 443, 283 "port_range_min": 443, 284 "protocol": "tcp", 285 "remote_group_id": null, 286 "remote_ip_prefix": null, 287 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a", 288 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 289 } 290 ] 291 } 292 `) 293 }) 294 295 opts := []rules.CreateOpts{ 296 { 297 Description: "test description of rule", 298 Direction: "ingress", 299 PortRangeMin: 80, 300 EtherType: rules.EtherType4, 301 PortRangeMax: 80, 302 Protocol: "tcp", 303 RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 304 SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a", 305 }, 306 { 307 Description: "test description of rule", 308 Direction: "ingress", 309 PortRangeMin: 443, 310 EtherType: rules.EtherType4, 311 PortRangeMax: 443, 312 Protocol: "tcp", 313 SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a", 314 }, 315 } 316 _, err := rules.CreateBulk(context.TODO(), fake.ServiceClient(), opts).Extract() 317 th.AssertNoErr(t, err) 318 } 319 320 func TestRequiredCreateOpts(t *testing.T) { 321 res := rules.Create(context.TODO(), fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress}) 322 if res.Err == nil { 323 t.Fatalf("Expected error, got none") 324 } 325 res = rules.Create(context.TODO(), fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4}) 326 if res.Err == nil { 327 t.Fatalf("Expected error, got none") 328 } 329 res = rules.Create(context.TODO(), fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4}) 330 if res.Err == nil { 331 t.Fatalf("Expected error, got none") 332 } 333 res = rules.Create(context.TODO(), fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4, SecGroupID: "something", Protocol: "foo"}) 334 if res.Err == nil { 335 t.Fatalf("Expected error, got none") 336 } 337 } 338 339 func TestGet(t *testing.T) { 340 th.SetupHTTP() 341 defer th.TeardownHTTP() 342 343 th.Mux.HandleFunc("/v2.0/security-group-rules/3c0e45ff-adaf-4124-b083-bf390e5482ff", func(w http.ResponseWriter, r *http.Request) { 344 th.TestMethod(t, r, "GET") 345 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 346 347 w.Header().Add("Content-Type", "application/json") 348 w.WriteHeader(http.StatusOK) 349 350 fmt.Fprint(w, ` 351 { 352 "security_group_rule": { 353 "direction": "egress", 354 "ethertype": "IPv6", 355 "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff", 356 "port_range_max": null, 357 "port_range_min": null, 358 "protocol": null, 359 "remote_group_id": null, 360 "remote_ip_prefix": null, 361 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 362 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 363 } 364 } 365 `) 366 }) 367 368 sr, err := rules.Get(context.TODO(), fake.ServiceClient(), "3c0e45ff-adaf-4124-b083-bf390e5482ff").Extract() 369 th.AssertNoErr(t, err) 370 371 th.AssertEquals(t, "egress", sr.Direction) 372 th.AssertEquals(t, "IPv6", sr.EtherType) 373 th.AssertEquals(t, "3c0e45ff-adaf-4124-b083-bf390e5482ff", sr.ID) 374 th.AssertEquals(t, 0, sr.PortRangeMax) 375 th.AssertEquals(t, 0, sr.PortRangeMin) 376 th.AssertEquals(t, "", sr.Protocol) 377 th.AssertEquals(t, "", sr.RemoteGroupID) 378 th.AssertEquals(t, "", sr.RemoteIPPrefix) 379 th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sr.SecGroupID) 380 th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sr.TenantID) 381 } 382 383 func TestDelete(t *testing.T) { 384 th.SetupHTTP() 385 defer th.TeardownHTTP() 386 387 th.Mux.HandleFunc("/v2.0/security-group-rules/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { 388 th.TestMethod(t, r, "DELETE") 389 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 390 w.WriteHeader(http.StatusNoContent) 391 }) 392 393 res := rules.Delete(context.TODO(), fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304") 394 th.AssertNoErr(t, res.Err) 395 }