github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/l7policies/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "testing" 5 6 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 7 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/l7policies" 8 "github.com/huaweicloud/golangsdk/pagination" 9 th "github.com/huaweicloud/golangsdk/testhelper" 10 ) 11 12 func TestCreateL7Policy(t *testing.T) { 13 th.SetupHTTP() 14 defer th.TeardownHTTP() 15 HandleL7PolicyCreationSuccessfully(t, SingleL7PolicyBody) 16 17 actual, err := l7policies.Create(fake.ServiceClient(), l7policies.CreateOpts{ 18 Name: "redirect-example.com", 19 ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d", 20 Action: l7policies.ActionRedirectToListener, 21 RedirectListenerID: "023f2e34-7806-443b-bfae-16c324569a3d", 22 }).Extract() 23 24 th.AssertNoErr(t, err) 25 th.CheckDeepEquals(t, L7PolicyToListener, *actual) 26 } 27 28 func TestRequiredL7PolicyCreateOpts(t *testing.T) { 29 // no param specified. 30 res := l7policies.Create(fake.ServiceClient(), l7policies.CreateOpts{}) 31 if res.Err == nil { 32 t.Fatalf("Expected error, got none") 33 } 34 35 // Action is invalid. 36 res = l7policies.Create(fake.ServiceClient(), l7policies.CreateOpts{ 37 ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d", 38 Action: l7policies.Action("invalid"), 39 }) 40 if res.Err == nil { 41 t.Fatalf("Expected error, but got none") 42 } 43 } 44 45 func TestListL7Policies(t *testing.T) { 46 th.SetupHTTP() 47 defer th.TeardownHTTP() 48 HandleL7PolicyListSuccessfully(t) 49 50 pages := 0 51 err := l7policies.List(fake.ServiceClient(), l7policies.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 52 pages++ 53 54 actual, err := l7policies.ExtractL7Policies(page) 55 if err != nil { 56 return false, err 57 } 58 59 if len(actual) != 2 { 60 t.Fatalf("Expected 2 l7policies, got %d", len(actual)) 61 } 62 th.CheckDeepEquals(t, L7PolicyToListener, actual[0]) 63 th.CheckDeepEquals(t, L7PolicyToPool, actual[1]) 64 65 return true, nil 66 }) 67 68 th.AssertNoErr(t, err) 69 70 if pages != 1 { 71 t.Errorf("Expected 1 page, saw %d", pages) 72 } 73 } 74 75 func TestListAllL7Policies(t *testing.T) { 76 th.SetupHTTP() 77 defer th.TeardownHTTP() 78 HandleL7PolicyListSuccessfully(t) 79 80 allPages, err := l7policies.List(fake.ServiceClient(), l7policies.ListOpts{}).AllPages() 81 th.AssertNoErr(t, err) 82 actual, err := l7policies.ExtractL7Policies(allPages) 83 th.AssertNoErr(t, err) 84 th.CheckDeepEquals(t, L7PolicyToListener, actual[0]) 85 th.CheckDeepEquals(t, L7PolicyToPool, actual[1]) 86 } 87 88 func TestGetL7Policy(t *testing.T) { 89 th.SetupHTTP() 90 defer th.TeardownHTTP() 91 HandleL7PolicyGetSuccessfully(t) 92 93 client := fake.ServiceClient() 94 actual, err := l7policies.Get(client, "8a1412f0-4c32-4257-8b07-af4770b604fd").Extract() 95 if err != nil { 96 t.Fatalf("Unexpected Get error: %v", err) 97 } 98 99 th.CheckDeepEquals(t, L7PolicyToListener, *actual) 100 } 101 102 func TestDeleteL7Policy(t *testing.T) { 103 th.SetupHTTP() 104 defer th.TeardownHTTP() 105 HandleL7PolicyDeletionSuccessfully(t) 106 107 res := l7policies.Delete(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd") 108 th.AssertNoErr(t, res.Err) 109 } 110 111 func TestUpdateL7PolicyWithInvalidOpts(t *testing.T) { 112 th.SetupHTTP() 113 defer th.TeardownHTTP() 114 115 res := l7policies.Update(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.UpdateOpts{ 116 Action: l7policies.Action("invalid"), 117 }) 118 if res.Err == nil { 119 t.Fatalf("Expected error, got none") 120 } 121 } 122 123 func TestCreateRule(t *testing.T) { 124 th.SetupHTTP() 125 defer th.TeardownHTTP() 126 HandleRuleCreationSuccessfully(t, SingleRuleBody) 127 128 actual, err := l7policies.CreateRule(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{ 129 RuleType: l7policies.TypePath, 130 CompareType: l7policies.CompareTypeRegex, 131 Value: "/images*", 132 }).Extract() 133 th.AssertNoErr(t, err) 134 135 th.CheckDeepEquals(t, RulePath, *actual) 136 } 137 138 func TestRequiredRuleCreateOpts(t *testing.T) { 139 th.SetupHTTP() 140 defer th.TeardownHTTP() 141 142 res := l7policies.CreateRule(fake.ServiceClient(), "", l7policies.CreateRuleOpts{}) 143 if res.Err == nil { 144 t.Fatalf("Expected error, got none") 145 } 146 res = l7policies.CreateRule(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{ 147 RuleType: l7policies.TypePath, 148 }) 149 if res.Err == nil { 150 t.Fatalf("Expected error, but got none") 151 } 152 res = l7policies.CreateRule(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{ 153 RuleType: l7policies.RuleType("invalid"), 154 CompareType: l7policies.CompareTypeRegex, 155 Value: "/images*", 156 }) 157 if res.Err == nil { 158 t.Fatalf("Expected error, but got none") 159 } 160 res = l7policies.CreateRule(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.CreateRuleOpts{ 161 RuleType: l7policies.TypePath, 162 CompareType: l7policies.CompareType("invalid"), 163 Value: "/images*", 164 }) 165 if res.Err == nil { 166 t.Fatalf("Expected error, but got none") 167 } 168 } 169 170 func TestListRules(t *testing.T) { 171 th.SetupHTTP() 172 defer th.TeardownHTTP() 173 HandleRuleListSuccessfully(t) 174 175 pages := 0 176 err := l7policies.ListRules(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.ListRulesOpts{}).EachPage(func(page pagination.Page) (bool, error) { 177 pages++ 178 179 actual, err := l7policies.ExtractRules(page) 180 if err != nil { 181 return false, err 182 } 183 184 if len(actual) != 2 { 185 t.Fatalf("Expected 2 rules, got %d", len(actual)) 186 } 187 th.CheckDeepEquals(t, RulePath, actual[0]) 188 th.CheckDeepEquals(t, RuleHostName, actual[1]) 189 190 return true, nil 191 }) 192 193 th.AssertNoErr(t, err) 194 195 if pages != 1 { 196 t.Errorf("Expected 1 page, saw %d", pages) 197 } 198 } 199 200 func TestListAllRules(t *testing.T) { 201 th.SetupHTTP() 202 defer th.TeardownHTTP() 203 HandleRuleListSuccessfully(t) 204 205 allPages, err := l7policies.ListRules(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", l7policies.ListRulesOpts{}).AllPages() 206 th.AssertNoErr(t, err) 207 208 actual, err := l7policies.ExtractRules(allPages) 209 th.AssertNoErr(t, err) 210 th.CheckDeepEquals(t, RulePath, actual[0]) 211 th.CheckDeepEquals(t, RuleHostName, actual[1]) 212 } 213 214 func TestGetRule(t *testing.T) { 215 th.SetupHTTP() 216 defer th.TeardownHTTP() 217 HandleRuleGetSuccessfully(t) 218 219 client := fake.ServiceClient() 220 actual, err := l7policies.GetRule(client, "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c").Extract() 221 if err != nil { 222 t.Fatalf("Unexpected Get error: %v", err) 223 } 224 225 th.CheckDeepEquals(t, RulePath, *actual) 226 } 227 228 func TestDeleteRule(t *testing.T) { 229 th.SetupHTTP() 230 defer th.TeardownHTTP() 231 HandleRuleDeletionSuccessfully(t) 232 233 res := l7policies.DeleteRule(fake.ServiceClient(), "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c") 234 th.AssertNoErr(t, res.Err) 235 } 236 237 func TestUpdateRule(t *testing.T) { 238 th.SetupHTTP() 239 defer th.TeardownHTTP() 240 HandleRuleUpdateSuccessfully(t) 241 242 client := fake.ServiceClient() 243 invert := false 244 key := "" 245 actual, err := l7policies.UpdateRule(client, "8a1412f0-4c32-4257-8b07-af4770b604fd", "16621dbb-a736-4888-a57a-3ecd53df784c", l7policies.UpdateRuleOpts{ 246 RuleType: l7policies.TypePath, 247 CompareType: l7policies.CompareTypeRegex, 248 Value: "/images/special*", 249 Invert: &invert, 250 Key: &key, 251 }).Extract() 252 if err != nil { 253 t.Fatalf("Unexpected Update error: %v", err) 254 } 255 256 th.CheckDeepEquals(t, RuleUpdated, *actual) 257 } 258 259 func TestUpdateRuleWithInvalidOpts(t *testing.T) { 260 th.SetupHTTP() 261 defer th.TeardownHTTP() 262 263 res := l7policies.UpdateRule(fake.ServiceClient(), "", "", l7policies.UpdateRuleOpts{ 264 RuleType: l7policies.RuleType("invalid"), 265 }) 266 if res.Err == nil { 267 t.Fatalf("Expected error, got none") 268 } 269 270 res = l7policies.UpdateRule(fake.ServiceClient(), "", "", l7policies.UpdateRuleOpts{ 271 CompareType: l7policies.CompareType("invalid"), 272 }) 273 if res.Err == nil { 274 t.Fatalf("Expected error, got none") 275 } 276 }