github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/security/rules/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/security/rules" 10 "github.com/gophercloud/gophercloud/pagination" 11 th "github.com/gophercloud/gophercloud/testhelper" 12 ) 13 14 func TestList(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "GET") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, ` 26 { 27 "security_group_rules": [ 28 { 29 "direction": "egress", 30 "ethertype": "IPv6", 31 "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff", 32 "port_range_max": null, 33 "port_range_min": null, 34 "protocol": null, 35 "remote_group_id": null, 36 "remote_ip_prefix": null, 37 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 38 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 39 }, 40 { 41 "direction": "egress", 42 "ethertype": "IPv4", 43 "id": "93aa42e5-80db-4581-9391-3a608bd0e448", 44 "port_range_max": null, 45 "port_range_min": null, 46 "protocol": null, 47 "remote_group_id": null, 48 "remote_ip_prefix": null, 49 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 50 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 51 } 52 ] 53 } 54 `) 55 }) 56 57 count := 0 58 59 rules.List(fake.ServiceClient(), rules.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 60 count++ 61 actual, err := rules.ExtractRules(page) 62 if err != nil { 63 t.Errorf("Failed to extract secrules: %v", err) 64 return false, err 65 } 66 67 expected := []rules.SecGroupRule{ 68 { 69 Description: "", 70 Direction: "egress", 71 EtherType: "IPv6", 72 ID: "3c0e45ff-adaf-4124-b083-bf390e5482ff", 73 PortRangeMax: 0, 74 PortRangeMin: 0, 75 Protocol: "", 76 RemoteGroupID: "", 77 RemoteIPPrefix: "", 78 SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 79 TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", 80 }, 81 { 82 Direction: "egress", 83 EtherType: "IPv4", 84 ID: "93aa42e5-80db-4581-9391-3a608bd0e448", 85 PortRangeMax: 0, 86 PortRangeMin: 0, 87 Protocol: "", 88 RemoteGroupID: "", 89 RemoteIPPrefix: "", 90 SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 91 TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", 92 }, 93 } 94 95 th.CheckDeepEquals(t, expected, actual) 96 97 return true, nil 98 }) 99 100 if count != 1 { 101 t.Errorf("Expected 1 page, got %d", count) 102 } 103 } 104 105 func TestCreate(t *testing.T) { 106 th.SetupHTTP() 107 defer th.TeardownHTTP() 108 109 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 110 th.TestMethod(t, r, "POST") 111 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 112 th.TestHeader(t, r, "Content-Type", "application/json") 113 th.TestHeader(t, r, "Accept", "application/json") 114 th.TestJSONRequest(t, r, ` 115 { 116 "security_group_rule": { 117 "description": "test description of rule", 118 "direction": "ingress", 119 "port_range_min": 80, 120 "ethertype": "IPv4", 121 "port_range_max": 80, 122 "protocol": "tcp", 123 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 124 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a" 125 } 126 } 127 `) 128 129 w.Header().Add("Content-Type", "application/json") 130 w.WriteHeader(http.StatusCreated) 131 132 fmt.Fprintf(w, ` 133 { 134 "security_group_rule": { 135 "description": "test description of rule", 136 "direction": "ingress", 137 "ethertype": "IPv4", 138 "id": "2bc0accf-312e-429a-956e-e4407625eb62", 139 "port_range_max": 80, 140 "port_range_min": 80, 141 "protocol": "tcp", 142 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 143 "remote_ip_prefix": null, 144 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a", 145 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 146 } 147 } 148 `) 149 }) 150 151 opts := rules.CreateOpts{ 152 Description: "test description of rule", 153 Direction: "ingress", 154 PortRangeMin: 80, 155 EtherType: rules.EtherType4, 156 PortRangeMax: 80, 157 Protocol: "tcp", 158 RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 159 SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a", 160 } 161 _, err := rules.Create(fake.ServiceClient(), opts).Extract() 162 th.AssertNoErr(t, err) 163 } 164 165 func TestRequiredCreateOpts(t *testing.T) { 166 res := rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress}) 167 if res.Err == nil { 168 t.Fatalf("Expected error, got none") 169 } 170 res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4}) 171 if res.Err == nil { 172 t.Fatalf("Expected error, got none") 173 } 174 res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4}) 175 if res.Err == nil { 176 t.Fatalf("Expected error, got none") 177 } 178 res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4, SecGroupID: "something", Protocol: "foo"}) 179 if res.Err == nil { 180 t.Fatalf("Expected error, got none") 181 } 182 } 183 184 func TestGet(t *testing.T) { 185 th.SetupHTTP() 186 defer th.TeardownHTTP() 187 188 th.Mux.HandleFunc("/v2.0/security-group-rules/3c0e45ff-adaf-4124-b083-bf390e5482ff", func(w http.ResponseWriter, r *http.Request) { 189 th.TestMethod(t, r, "GET") 190 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 191 192 w.Header().Add("Content-Type", "application/json") 193 w.WriteHeader(http.StatusOK) 194 195 fmt.Fprintf(w, ` 196 { 197 "security_group_rule": { 198 "direction": "egress", 199 "ethertype": "IPv6", 200 "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff", 201 "port_range_max": null, 202 "port_range_min": null, 203 "protocol": null, 204 "remote_group_id": null, 205 "remote_ip_prefix": null, 206 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 207 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 208 } 209 } 210 `) 211 }) 212 213 sr, err := rules.Get(fake.ServiceClient(), "3c0e45ff-adaf-4124-b083-bf390e5482ff").Extract() 214 th.AssertNoErr(t, err) 215 216 th.AssertEquals(t, "egress", sr.Direction) 217 th.AssertEquals(t, "IPv6", sr.EtherType) 218 th.AssertEquals(t, "3c0e45ff-adaf-4124-b083-bf390e5482ff", sr.ID) 219 th.AssertEquals(t, 0, sr.PortRangeMax) 220 th.AssertEquals(t, 0, sr.PortRangeMin) 221 th.AssertEquals(t, "", sr.Protocol) 222 th.AssertEquals(t, "", sr.RemoteGroupID) 223 th.AssertEquals(t, "", sr.RemoteIPPrefix) 224 th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sr.SecGroupID) 225 th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sr.TenantID) 226 } 227 228 func TestDelete(t *testing.T) { 229 th.SetupHTTP() 230 defer th.TeardownHTTP() 231 232 th.Mux.HandleFunc("/v2.0/security-group-rules/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { 233 th.TestMethod(t, r, "DELETE") 234 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 235 w.WriteHeader(http.StatusNoContent) 236 }) 237 238 res := rules.Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304") 239 th.AssertNoErr(t, res.Err) 240 }