fixtures.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. package testing
  2. import (
  3. "fmt"
  4. "net/http"
  5. "testing"
  6. "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/loadbalancer/v2/l7policies"
  7. th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
  8. "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper/client"
  9. )
  10. // SingleL7PolicyBody is the canned body of a Get request on an existing l7policy.
  11. const SingleL7PolicyBody = `
  12. {
  13. "l7policy": {
  14. "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
  15. "description": "",
  16. "admin_state_up": true,
  17. "redirect_pool_id": null,
  18. "redirect_url": "http://www.example.com",
  19. "action": "REDIRECT_TO_URL",
  20. "position": 1,
  21. "project_id": "e3cd678b11784734bc366148aa37580e",
  22. "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
  23. "name": "redirect-example.com",
  24. "rules": []
  25. }
  26. }
  27. `
  28. var (
  29. L7PolicyToURL = l7policies.L7Policy{
  30. ID: "8a1412f0-4c32-4257-8b07-af4770b604fd",
  31. Name: "redirect-example.com",
  32. ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d",
  33. Action: "REDIRECT_TO_URL",
  34. Position: 1,
  35. Description: "",
  36. ProjectID: "e3cd678b11784734bc366148aa37580e",
  37. RedirectPoolID: "",
  38. RedirectURL: "http://www.example.com",
  39. AdminStateUp: true,
  40. Rules: []l7policies.Rule{},
  41. }
  42. L7PolicyToPool = l7policies.L7Policy{
  43. ID: "964f4ba4-f6cd-405c-bebd-639460af7231",
  44. Name: "redirect-pool",
  45. ListenerID: "be3138a3-5cf7-4513-a4c2-bb137e668bab",
  46. Action: "REDIRECT_TO_POOL",
  47. Position: 1,
  48. Description: "",
  49. ProjectID: "c1f7910086964990847dc6c8b128f63c",
  50. RedirectPoolID: "bac433c6-5bea-4311-80da-bd1cd90fbd25",
  51. RedirectURL: "",
  52. AdminStateUp: true,
  53. Rules: []l7policies.Rule{},
  54. }
  55. L7PolicyUpdated = l7policies.L7Policy{
  56. ID: "8a1412f0-4c32-4257-8b07-af4770b604fd",
  57. Name: "NewL7PolicyName",
  58. ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d",
  59. Action: "REDIRECT_TO_URL",
  60. Position: 1,
  61. Description: "Redirect requests to example.com",
  62. ProjectID: "e3cd678b11784734bc366148aa37580e",
  63. RedirectPoolID: "",
  64. RedirectURL: "http://www.new-example.com",
  65. AdminStateUp: true,
  66. Rules: []l7policies.Rule{},
  67. }
  68. RulePath = l7policies.Rule{
  69. ID: "16621dbb-a736-4888-a57a-3ecd53df784c",
  70. RuleType: "PATH",
  71. CompareType: "REGEX",
  72. Value: "/images*",
  73. ProjectID: "e3cd678b11784734bc366148aa37580e",
  74. Key: "",
  75. Invert: false,
  76. AdminStateUp: true,
  77. }
  78. RuleHostName = l7policies.Rule{
  79. ID: "d24521a0-df84-4468-861a-a531af116d1e",
  80. RuleType: "HOST_NAME",
  81. CompareType: "EQUAL_TO",
  82. Value: "www.example.com",
  83. ProjectID: "e3cd678b11784734bc366148aa37580e",
  84. Key: "",
  85. Invert: false,
  86. AdminStateUp: true,
  87. }
  88. RuleUpdated = l7policies.Rule{
  89. ID: "16621dbb-a736-4888-a57a-3ecd53df784c",
  90. RuleType: "PATH",
  91. CompareType: "REGEX",
  92. Value: "/images/special*",
  93. ProjectID: "e3cd678b11784734bc366148aa37580e",
  94. Key: "",
  95. Invert: false,
  96. AdminStateUp: true,
  97. }
  98. )
  99. // HandleL7PolicyCreationSuccessfully sets up the test server to respond to a l7policy creation request
  100. // with a given response.
  101. func HandleL7PolicyCreationSuccessfully(t *testing.T, response string) {
  102. th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) {
  103. th.TestMethod(t, r, "POST")
  104. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  105. th.TestJSONRequest(t, r, `{
  106. "l7policy": {
  107. "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
  108. "redirect_url": "http://www.example.com",
  109. "name": "redirect-example.com",
  110. "action": "REDIRECT_TO_URL"
  111. }
  112. }`)
  113. w.WriteHeader(http.StatusAccepted)
  114. w.Header().Add("Content-Type", "application/json")
  115. fmt.Fprintf(w, response)
  116. })
  117. }
  118. // L7PoliciesListBody contains the canned body of a l7policy list response.
  119. const L7PoliciesListBody = `
  120. {
  121. "l7policies": [
  122. {
  123. "redirect_pool_id": null,
  124. "description": "",
  125. "admin_state_up": true,
  126. "rules": [],
  127. "project_id": "e3cd678b11784734bc366148aa37580e",
  128. "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
  129. "redirect_url": "http://www.example.com",
  130. "action": "REDIRECT_TO_URL",
  131. "position": 1,
  132. "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
  133. "name": "redirect-example.com"
  134. },
  135. {
  136. "redirect_pool_id": "bac433c6-5bea-4311-80da-bd1cd90fbd25",
  137. "description": "",
  138. "admin_state_up": true,
  139. "rules": [],
  140. "project_id": "c1f7910086964990847dc6c8b128f63c",
  141. "listener_id": "be3138a3-5cf7-4513-a4c2-bb137e668bab",
  142. "action": "REDIRECT_TO_POOL",
  143. "position": 1,
  144. "id": "964f4ba4-f6cd-405c-bebd-639460af7231",
  145. "name": "redirect-pool"
  146. }
  147. ]
  148. }
  149. `
  150. // PostUpdateL7PolicyBody is the canned response body of a Update request on an existing l7policy.
  151. const PostUpdateL7PolicyBody = `
  152. {
  153. "l7policy": {
  154. "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
  155. "description": "Redirect requests to example.com",
  156. "admin_state_up": true,
  157. "redirect_pool_id": null,
  158. "redirect_url": "http://www.new-example.com",
  159. "action": "REDIRECT_TO_URL",
  160. "position": 1,
  161. "project_id": "e3cd678b11784734bc366148aa37580e",
  162. "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
  163. "name": "NewL7PolicyName",
  164. "rules": []
  165. }
  166. }
  167. `
  168. // HandleL7PolicyListSuccessfully sets up the test server to respond to a l7policy List request.
  169. func HandleL7PolicyListSuccessfully(t *testing.T) {
  170. th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) {
  171. th.TestMethod(t, r, "GET")
  172. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  173. w.Header().Add("Content-Type", "application/json")
  174. r.ParseForm()
  175. marker := r.Form.Get("marker")
  176. switch marker {
  177. case "":
  178. fmt.Fprintf(w, L7PoliciesListBody)
  179. case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
  180. fmt.Fprintf(w, `{ "l7policies": [] }`)
  181. default:
  182. t.Fatalf("/v2.0/lbaas/l7policies invoked with unexpected marker=[%s]", marker)
  183. }
  184. })
  185. }
  186. // HandleL7PolicyGetSuccessfully sets up the test server to respond to a l7policy Get request.
  187. func HandleL7PolicyGetSuccessfully(t *testing.T) {
  188. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
  189. th.TestMethod(t, r, "GET")
  190. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  191. th.TestHeader(t, r, "Accept", "application/json")
  192. fmt.Fprintf(w, SingleL7PolicyBody)
  193. })
  194. }
  195. // HandleL7PolicyDeletionSuccessfully sets up the test server to respond to a l7policy deletion request.
  196. func HandleL7PolicyDeletionSuccessfully(t *testing.T) {
  197. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
  198. th.TestMethod(t, r, "DELETE")
  199. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  200. w.WriteHeader(http.StatusNoContent)
  201. })
  202. }
  203. // HandleL7PolicyUpdateSuccessfully sets up the test server to respond to a l7policy Update request.
  204. func HandleL7PolicyUpdateSuccessfully(t *testing.T) {
  205. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
  206. th.TestMethod(t, r, "PUT")
  207. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  208. th.TestHeader(t, r, "Accept", "application/json")
  209. th.TestHeader(t, r, "Content-Type", "application/json")
  210. th.TestJSONRequest(t, r, `{
  211. "l7policy": {
  212. "name": "NewL7PolicyName",
  213. "action": "REDIRECT_TO_URL",
  214. "redirect_url": "http://www.new-example.com"
  215. }
  216. }`)
  217. fmt.Fprintf(w, PostUpdateL7PolicyBody)
  218. })
  219. }
  220. // SingleRuleBody is the canned body of a Get request on an existing rule.
  221. const SingleRuleBody = `
  222. {
  223. "rule": {
  224. "compare_type": "REGEX",
  225. "invert": false,
  226. "admin_state_up": true,
  227. "value": "/images*",
  228. "key": null,
  229. "project_id": "e3cd678b11784734bc366148aa37580e",
  230. "type": "PATH",
  231. "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
  232. }
  233. }
  234. `
  235. // HandleRuleCreationSuccessfully sets up the test server to respond to a rule creation request
  236. // with a given response.
  237. func HandleRuleCreationSuccessfully(t *testing.T, response string) {
  238. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) {
  239. th.TestMethod(t, r, "POST")
  240. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  241. th.TestJSONRequest(t, r, `{
  242. "rule": {
  243. "compare_type": "REGEX",
  244. "type": "PATH",
  245. "value": "/images*"
  246. }
  247. }`)
  248. w.WriteHeader(http.StatusAccepted)
  249. w.Header().Add("Content-Type", "application/json")
  250. fmt.Fprintf(w, response)
  251. })
  252. }
  253. // RulesListBody contains the canned body of a rule list response.
  254. const RulesListBody = `
  255. {
  256. "rules":[
  257. {
  258. "compare_type": "REGEX",
  259. "invert": false,
  260. "admin_state_up": true,
  261. "value": "/images*",
  262. "key": null,
  263. "project_id": "e3cd678b11784734bc366148aa37580e",
  264. "type": "PATH",
  265. "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
  266. },
  267. {
  268. "compare_type": "EQUAL_TO",
  269. "invert": false,
  270. "admin_state_up": true,
  271. "value": "www.example.com",
  272. "key": null,
  273. "project_id": "e3cd678b11784734bc366148aa37580e",
  274. "type": "HOST_NAME",
  275. "id": "d24521a0-df84-4468-861a-a531af116d1e"
  276. }
  277. ]
  278. }
  279. `
  280. // HandleRuleListSuccessfully sets up the test server to respond to a rule List request.
  281. func HandleRuleListSuccessfully(t *testing.T) {
  282. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) {
  283. th.TestMethod(t, r, "GET")
  284. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  285. w.Header().Add("Content-Type", "application/json")
  286. r.ParseForm()
  287. marker := r.Form.Get("marker")
  288. switch marker {
  289. case "":
  290. fmt.Fprintf(w, RulesListBody)
  291. case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
  292. fmt.Fprintf(w, `{ "rules": [] }`)
  293. default:
  294. t.Fatalf("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules invoked with unexpected marker=[%s]", marker)
  295. }
  296. })
  297. }
  298. // HandleRuleGetSuccessfully sets up the test server to respond to a rule Get request.
  299. func HandleRuleGetSuccessfully(t *testing.T) {
  300. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
  301. th.TestMethod(t, r, "GET")
  302. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  303. th.TestHeader(t, r, "Accept", "application/json")
  304. fmt.Fprintf(w, SingleRuleBody)
  305. })
  306. }
  307. // HandleRuleDeletionSuccessfully sets up the test server to respond to a rule deletion request.
  308. func HandleRuleDeletionSuccessfully(t *testing.T) {
  309. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
  310. th.TestMethod(t, r, "DELETE")
  311. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  312. w.WriteHeader(http.StatusNoContent)
  313. })
  314. }
  315. // PostUpdateRuleBody is the canned response body of a Update request on an existing rule.
  316. const PostUpdateRuleBody = `
  317. {
  318. "rule": {
  319. "compare_type": "REGEX",
  320. "invert": false,
  321. "admin_state_up": true,
  322. "value": "/images/special*",
  323. "key": null,
  324. "project_id": "e3cd678b11784734bc366148aa37580e",
  325. "type": "PATH",
  326. "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
  327. }
  328. }
  329. `
  330. // HandleRuleUpdateSuccessfully sets up the test server to respond to a rule Update request.
  331. func HandleRuleUpdateSuccessfully(t *testing.T) {
  332. th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules/16621dbb-a736-4888-a57a-3ecd53df784c", func(w http.ResponseWriter, r *http.Request) {
  333. th.TestMethod(t, r, "PUT")
  334. th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
  335. th.TestHeader(t, r, "Accept", "application/json")
  336. th.TestHeader(t, r, "Content-Type", "application/json")
  337. th.TestJSONRequest(t, r, `{
  338. "rule": {
  339. "compare_type": "REGEX",
  340. "type": "PATH",
  341. "value": "/images/special*"
  342. }
  343. }`)
  344. fmt.Fprintf(w, PostUpdateRuleBody)
  345. })
  346. }