requests_test.go 8.0 KB

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