github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/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/huaweicloud/golangsdk/openstack/networking/v2/common" 9 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/security/rules" 10 "github.com/huaweicloud/golangsdk/pagination" 11 th "github.com/huaweicloud/golangsdk/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 Direction: "egress", 70 EtherType: "IPv6", 71 ID: "3c0e45ff-adaf-4124-b083-bf390e5482ff", 72 PortRangeMax: 0, 73 PortRangeMin: 0, 74 Protocol: "", 75 RemoteGroupID: "", 76 RemoteIPPrefix: "", 77 SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 78 TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", 79 }, 80 { 81 Direction: "egress", 82 EtherType: "IPv4", 83 ID: "93aa42e5-80db-4581-9391-3a608bd0e448", 84 PortRangeMax: 0, 85 PortRangeMin: 0, 86 Protocol: "", 87 RemoteGroupID: "", 88 RemoteIPPrefix: "", 89 SecGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 90 TenantID: "e4f50856753b4dc6afee5fa6b9b6c550", 91 }, 92 } 93 94 th.CheckDeepEquals(t, expected, actual) 95 96 return true, nil 97 }) 98 99 if count != 1 { 100 t.Errorf("Expected 1 page, got %d", count) 101 } 102 } 103 104 func TestCreate(t *testing.T) { 105 th.SetupHTTP() 106 defer th.TeardownHTTP() 107 108 th.Mux.HandleFunc("/v2.0/security-group-rules", func(w http.ResponseWriter, r *http.Request) { 109 th.TestMethod(t, r, "POST") 110 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 111 th.TestHeader(t, r, "Content-Type", "application/json") 112 th.TestHeader(t, r, "Accept", "application/json") 113 th.TestJSONRequest(t, r, ` 114 { 115 "security_group_rule": { 116 "direction": "ingress", 117 "port_range_min": 80, 118 "ethertype": "IPv4", 119 "port_range_max": 80, 120 "protocol": "tcp", 121 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 122 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a" 123 } 124 } 125 `) 126 127 w.Header().Add("Content-Type", "application/json") 128 w.WriteHeader(http.StatusCreated) 129 130 fmt.Fprintf(w, ` 131 { 132 "security_group_rule": { 133 "direction": "ingress", 134 "ethertype": "IPv4", 135 "id": "2bc0accf-312e-429a-956e-e4407625eb62", 136 "port_range_max": 80, 137 "port_range_min": 80, 138 "protocol": "tcp", 139 "remote_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 140 "remote_ip_prefix": null, 141 "security_group_id": "a7734e61-b545-452d-a3cd-0189cbd9747a", 142 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 143 } 144 } 145 `) 146 }) 147 148 opts := rules.CreateOpts{ 149 Direction: "ingress", 150 PortRangeMin: 80, 151 EtherType: rules.EtherType4, 152 PortRangeMax: 80, 153 Protocol: "tcp", 154 RemoteGroupID: "85cc3048-abc3-43cc-89b3-377341426ac5", 155 SecGroupID: "a7734e61-b545-452d-a3cd-0189cbd9747a", 156 } 157 _, err := rules.Create(fake.ServiceClient(), opts).Extract() 158 th.AssertNoErr(t, err) 159 } 160 161 func TestRequiredCreateOpts(t *testing.T) { 162 res := rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress}) 163 if res.Err == nil { 164 t.Fatalf("Expected error, got none") 165 } 166 res = rules.Create(fake.ServiceClient(), rules.CreateOpts{Direction: rules.DirIngress, EtherType: rules.EtherType4}) 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, SecGroupID: "something", Protocol: "foo"}) 175 if res.Err == nil { 176 t.Fatalf("Expected error, got none") 177 } 178 } 179 180 func TestGet(t *testing.T) { 181 th.SetupHTTP() 182 defer th.TeardownHTTP() 183 184 th.Mux.HandleFunc("/v2.0/security-group-rules/3c0e45ff-adaf-4124-b083-bf390e5482ff", func(w http.ResponseWriter, r *http.Request) { 185 th.TestMethod(t, r, "GET") 186 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 187 188 w.Header().Add("Content-Type", "application/json") 189 w.WriteHeader(http.StatusOK) 190 191 fmt.Fprintf(w, ` 192 { 193 "security_group_rule": { 194 "direction": "egress", 195 "ethertype": "IPv6", 196 "id": "3c0e45ff-adaf-4124-b083-bf390e5482ff", 197 "port_range_max": null, 198 "port_range_min": null, 199 "protocol": null, 200 "remote_group_id": null, 201 "remote_ip_prefix": null, 202 "security_group_id": "85cc3048-abc3-43cc-89b3-377341426ac5", 203 "tenant_id": "e4f50856753b4dc6afee5fa6b9b6c550" 204 } 205 } 206 `) 207 }) 208 209 sr, err := rules.Get(fake.ServiceClient(), "3c0e45ff-adaf-4124-b083-bf390e5482ff").Extract() 210 th.AssertNoErr(t, err) 211 212 th.AssertEquals(t, "egress", sr.Direction) 213 th.AssertEquals(t, "IPv6", sr.EtherType) 214 th.AssertEquals(t, "3c0e45ff-adaf-4124-b083-bf390e5482ff", sr.ID) 215 th.AssertEquals(t, 0, sr.PortRangeMax) 216 th.AssertEquals(t, 0, sr.PortRangeMin) 217 th.AssertEquals(t, "", sr.Protocol) 218 th.AssertEquals(t, "", sr.RemoteGroupID) 219 th.AssertEquals(t, "", sr.RemoteIPPrefix) 220 th.AssertEquals(t, "85cc3048-abc3-43cc-89b3-377341426ac5", sr.SecGroupID) 221 th.AssertEquals(t, "e4f50856753b4dc6afee5fa6b9b6c550", sr.TenantID) 222 } 223 224 func TestDelete(t *testing.T) { 225 th.SetupHTTP() 226 defer th.TeardownHTTP() 227 228 th.Mux.HandleFunc("/v2.0/security-group-rules/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { 229 th.TestMethod(t, r, "DELETE") 230 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 231 w.WriteHeader(http.StatusNoContent) 232 }) 233 234 res := rules.Delete(fake.ServiceClient(), "4ec89087-d057-4e2c-911f-60a3b47ee304") 235 th.AssertNoErr(t, res.Err) 236 }