github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/fwaas/policies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk" 9 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 10 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/fwaas/policies" 11 "github.com/huaweicloud/golangsdk/pagination" 12 th "github.com/huaweicloud/golangsdk/testhelper" 13 ) 14 15 func TestList(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/v2.0/fw/firewall_policies", 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.Fprintf(w, ` 27 { 28 "firewall_policies": [ 29 { 30 "name": "policy1", 31 "firewall_rules": [ 32 "75452b36-268e-4e75-aaf4-f0e7ed50bc97", 33 "c9e77ca0-1bc8-497d-904d-948107873dc6" 34 ], 35 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 36 "audited": true, 37 "shared": false, 38 "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 39 "description": "Firewall policy 1" 40 }, 41 { 42 "name": "policy2", 43 "firewall_rules": [ 44 "03d2a6ad-633f-431a-8463-4370d06a22c8" 45 ], 46 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 47 "audited": false, 48 "shared": true, 49 "id": "c854fab5-bdaf-4a86-9359-78de93e5df01", 50 "description": "Firewall policy 2" 51 } 52 ] 53 } 54 `) 55 }) 56 57 count := 0 58 59 policies.List(fake.ServiceClient(), policies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 60 count++ 61 actual, err := policies.ExtractPolicies(page) 62 if err != nil { 63 t.Errorf("Failed to extract members: %v", err) 64 return false, err 65 } 66 67 expected := []policies.Policy{ 68 { 69 Name: "policy1", 70 Rules: []string{ 71 "75452b36-268e-4e75-aaf4-f0e7ed50bc97", 72 "c9e77ca0-1bc8-497d-904d-948107873dc6", 73 }, 74 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 75 Audited: true, 76 Shared: false, 77 ID: "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 78 Description: "Firewall policy 1", 79 }, 80 { 81 Name: "policy2", 82 Rules: []string{ 83 "03d2a6ad-633f-431a-8463-4370d06a22c8", 84 }, 85 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 86 Audited: false, 87 Shared: true, 88 ID: "c854fab5-bdaf-4a86-9359-78de93e5df01", 89 Description: "Firewall policy 2", 90 }, 91 } 92 93 th.CheckDeepEquals(t, expected, actual) 94 95 return true, nil 96 }) 97 98 if count != 1 { 99 t.Errorf("Expected 1 page, got %d", count) 100 } 101 } 102 103 func TestCreate(t *testing.T) { 104 th.SetupHTTP() 105 defer th.TeardownHTTP() 106 107 th.Mux.HandleFunc("/v2.0/fw/firewall_policies", func(w http.ResponseWriter, r *http.Request) { 108 th.TestMethod(t, r, "POST") 109 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 110 th.TestHeader(t, r, "Content-Type", "application/json") 111 th.TestHeader(t, r, "Accept", "application/json") 112 th.TestJSONRequest(t, r, ` 113 { 114 "firewall_policy":{ 115 "name": "policy", 116 "firewall_rules": [ 117 "98a58c87-76be-ae7c-a74e-b77fffb88d95", 118 "11a58c87-76be-ae7c-a74e-b77fffb88a32" 119 ], 120 "description": "Firewall policy", 121 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 122 "audited": true, 123 "shared": false 124 } 125 } 126 `) 127 128 w.Header().Add("Content-Type", "application/json") 129 w.WriteHeader(http.StatusCreated) 130 131 fmt.Fprintf(w, ` 132 { 133 "firewall_policy":{ 134 "name": "policy", 135 "firewall_rules": [ 136 "98a58c87-76be-ae7c-a74e-b77fffb88d95", 137 "11a58c87-76be-ae7c-a74e-b77fffb88a32" 138 ], 139 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 140 "audited": false, 141 "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 142 "description": "Firewall policy" 143 } 144 } 145 `) 146 }) 147 148 options := policies.CreateOpts{ 149 TenantID: "9145d91459d248b1b02fdaca97c6a75d", 150 Name: "policy", 151 Description: "Firewall policy", 152 Shared: golangsdk.Disabled, 153 Audited: golangsdk.Enabled, 154 Rules: []string{ 155 "98a58c87-76be-ae7c-a74e-b77fffb88d95", 156 "11a58c87-76be-ae7c-a74e-b77fffb88a32", 157 }, 158 } 159 160 _, err := policies.Create(fake.ServiceClient(), options).Extract() 161 th.AssertNoErr(t, err) 162 } 163 164 func TestGet(t *testing.T) { 165 th.SetupHTTP() 166 defer th.TeardownHTTP() 167 168 th.Mux.HandleFunc("/v2.0/fw/firewall_policies/bcab5315-64f6-4ea3-8e58-981cc37c6f61", func(w http.ResponseWriter, r *http.Request) { 169 th.TestMethod(t, r, "GET") 170 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 171 172 w.Header().Add("Content-Type", "application/json") 173 w.WriteHeader(http.StatusOK) 174 175 fmt.Fprintf(w, ` 176 { 177 "firewall_policy":{ 178 "name": "www", 179 "firewall_rules": [ 180 "75452b36-268e-4e75-aaf4-f0e7ed50bc97", 181 "c9e77ca0-1bc8-497d-904d-948107873dc6", 182 "03d2a6ad-633f-431a-8463-4370d06a22c8" 183 ], 184 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 185 "audited": false, 186 "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 187 "description": "Firewall policy web" 188 } 189 } 190 `) 191 }) 192 193 policy, err := policies.Get(fake.ServiceClient(), "bcab5315-64f6-4ea3-8e58-981cc37c6f61").Extract() 194 th.AssertNoErr(t, err) 195 196 th.AssertEquals(t, "www", policy.Name) 197 th.AssertEquals(t, "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", policy.ID) 198 th.AssertEquals(t, "Firewall policy web", policy.Description) 199 th.AssertEquals(t, 3, len(policy.Rules)) 200 th.AssertEquals(t, "75452b36-268e-4e75-aaf4-f0e7ed50bc97", policy.Rules[0]) 201 th.AssertEquals(t, "c9e77ca0-1bc8-497d-904d-948107873dc6", policy.Rules[1]) 202 th.AssertEquals(t, "03d2a6ad-633f-431a-8463-4370d06a22c8", policy.Rules[2]) 203 th.AssertEquals(t, "9145d91459d248b1b02fdaca97c6a75d", policy.TenantID) 204 } 205 206 func TestUpdate(t *testing.T) { 207 th.SetupHTTP() 208 defer th.TeardownHTTP() 209 210 th.Mux.HandleFunc("/v2.0/fw/firewall_policies/f2b08c1e-aa81-4668-8ae1-1401bcb0576c", func(w http.ResponseWriter, r *http.Request) { 211 th.TestMethod(t, r, "PUT") 212 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 213 th.TestHeader(t, r, "Content-Type", "application/json") 214 th.TestHeader(t, r, "Accept", "application/json") 215 th.TestJSONRequest(t, r, ` 216 { 217 "firewall_policy":{ 218 "name": "policy", 219 "firewall_rules": [ 220 "98a58c87-76be-ae7c-a74e-b77fffb88d95", 221 "11a58c87-76be-ae7c-a74e-b77fffb88a32" 222 ], 223 "description": "Firewall policy" 224 } 225 } 226 `) 227 228 w.Header().Add("Content-Type", "application/json") 229 w.WriteHeader(http.StatusOK) 230 231 fmt.Fprintf(w, ` 232 { 233 "firewall_policy":{ 234 "name": "policy", 235 "firewall_rules": [ 236 "75452b36-268e-4e75-aaf4-f0e7ed50bc97", 237 "c9e77ca0-1bc8-497d-904d-948107873dc6", 238 "03d2a6ad-633f-431a-8463-4370d06a22c8" 239 ], 240 "tenant_id": "9145d91459d248b1b02fdaca97c6a75d", 241 "audited": false, 242 "id": "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", 243 "description": "Firewall policy" 244 } 245 } 246 `) 247 }) 248 249 options := policies.UpdateOpts{ 250 Name: "policy", 251 Description: "Firewall policy", 252 Rules: []string{ 253 "98a58c87-76be-ae7c-a74e-b77fffb88d95", 254 "11a58c87-76be-ae7c-a74e-b77fffb88a32", 255 }, 256 } 257 258 _, err := policies.Update(fake.ServiceClient(), "f2b08c1e-aa81-4668-8ae1-1401bcb0576c", options).Extract() 259 th.AssertNoErr(t, err) 260 } 261 262 func TestDelete(t *testing.T) { 263 th.SetupHTTP() 264 defer th.TeardownHTTP() 265 266 th.Mux.HandleFunc("/v2.0/fw/firewall_policies/4ec89077-d057-4a2b-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { 267 th.TestMethod(t, r, "DELETE") 268 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 269 w.WriteHeader(http.StatusNoContent) 270 }) 271 272 res := policies.Delete(fake.ServiceClient(), "4ec89077-d057-4a2b-911f-60a3b47ee304") 273 th.AssertNoErr(t, res.Err) 274 }