123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- package testing
- import (
- "fmt"
- "net/http"
- "testing"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/openstack/loadbalancer/v2/l7policies"
- th "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper"
- "devel.mephi.ru/iacherepanov/openstack-gophercloud/testhelper/client"
- )
- // SingleL7PolicyBody is the canned body of a Get request on an existing l7policy.
- const SingleL7PolicyBody = `
- {
- "l7policy": {
- "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
- "description": "",
- "admin_state_up": true,
- "redirect_pool_id": null,
- "redirect_url": "http://www.example.com",
- "action": "REDIRECT_TO_URL",
- "position": 1,
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
- "name": "redirect-example.com",
- "rules": []
- }
- }
- `
- var (
- L7PolicyToURL = l7policies.L7Policy{
- ID: "8a1412f0-4c32-4257-8b07-af4770b604fd",
- Name: "redirect-example.com",
- ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d",
- Action: "REDIRECT_TO_URL",
- Position: 1,
- Description: "",
- ProjectID: "e3cd678b11784734bc366148aa37580e",
- RedirectPoolID: "",
- RedirectURL: "http://www.example.com",
- AdminStateUp: true,
- Rules: []l7policies.Rule{},
- }
- L7PolicyToPool = l7policies.L7Policy{
- ID: "964f4ba4-f6cd-405c-bebd-639460af7231",
- Name: "redirect-pool",
- ListenerID: "be3138a3-5cf7-4513-a4c2-bb137e668bab",
- Action: "REDIRECT_TO_POOL",
- Position: 1,
- Description: "",
- ProjectID: "c1f7910086964990847dc6c8b128f63c",
- RedirectPoolID: "bac433c6-5bea-4311-80da-bd1cd90fbd25",
- RedirectURL: "",
- AdminStateUp: true,
- Rules: []l7policies.Rule{},
- }
- L7PolicyUpdated = l7policies.L7Policy{
- ID: "8a1412f0-4c32-4257-8b07-af4770b604fd",
- Name: "NewL7PolicyName",
- ListenerID: "023f2e34-7806-443b-bfae-16c324569a3d",
- Action: "REDIRECT_TO_URL",
- Position: 1,
- Description: "Redirect requests to example.com",
- ProjectID: "e3cd678b11784734bc366148aa37580e",
- RedirectPoolID: "",
- RedirectURL: "http://www.new-example.com",
- AdminStateUp: true,
- Rules: []l7policies.Rule{},
- }
- RulePath = l7policies.Rule{
- ID: "16621dbb-a736-4888-a57a-3ecd53df784c",
- RuleType: "PATH",
- CompareType: "REGEX",
- Value: "/images*",
- ProjectID: "e3cd678b11784734bc366148aa37580e",
- Key: "",
- Invert: false,
- AdminStateUp: true,
- }
- RuleHostName = l7policies.Rule{
- ID: "d24521a0-df84-4468-861a-a531af116d1e",
- RuleType: "HOST_NAME",
- CompareType: "EQUAL_TO",
- Value: "www.example.com",
- ProjectID: "e3cd678b11784734bc366148aa37580e",
- Key: "",
- Invert: false,
- AdminStateUp: true,
- }
- RuleUpdated = l7policies.Rule{
- ID: "16621dbb-a736-4888-a57a-3ecd53df784c",
- RuleType: "PATH",
- CompareType: "REGEX",
- Value: "/images/special*",
- ProjectID: "e3cd678b11784734bc366148aa37580e",
- Key: "",
- Invert: false,
- AdminStateUp: true,
- }
- )
- // HandleL7PolicyCreationSuccessfully sets up the test server to respond to a l7policy creation request
- // with a given response.
- func HandleL7PolicyCreationSuccessfully(t *testing.T, response string) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- th.TestJSONRequest(t, r, `{
- "l7policy": {
- "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
- "redirect_url": "http://www.example.com",
- "name": "redirect-example.com",
- "action": "REDIRECT_TO_URL"
- }
- }`)
- w.WriteHeader(http.StatusAccepted)
- w.Header().Add("Content-Type", "application/json")
- fmt.Fprintf(w, response)
- })
- }
- // L7PoliciesListBody contains the canned body of a l7policy list response.
- const L7PoliciesListBody = `
- {
- "l7policies": [
- {
- "redirect_pool_id": null,
- "description": "",
- "admin_state_up": true,
- "rules": [],
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
- "redirect_url": "http://www.example.com",
- "action": "REDIRECT_TO_URL",
- "position": 1,
- "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
- "name": "redirect-example.com"
- },
- {
- "redirect_pool_id": "bac433c6-5bea-4311-80da-bd1cd90fbd25",
- "description": "",
- "admin_state_up": true,
- "rules": [],
- "project_id": "c1f7910086964990847dc6c8b128f63c",
- "listener_id": "be3138a3-5cf7-4513-a4c2-bb137e668bab",
- "action": "REDIRECT_TO_POOL",
- "position": 1,
- "id": "964f4ba4-f6cd-405c-bebd-639460af7231",
- "name": "redirect-pool"
- }
- ]
- }
- `
- // PostUpdateL7PolicyBody is the canned response body of a Update request on an existing l7policy.
- const PostUpdateL7PolicyBody = `
- {
- "l7policy": {
- "listener_id": "023f2e34-7806-443b-bfae-16c324569a3d",
- "description": "Redirect requests to example.com",
- "admin_state_up": true,
- "redirect_pool_id": null,
- "redirect_url": "http://www.new-example.com",
- "action": "REDIRECT_TO_URL",
- "position": 1,
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "id": "8a1412f0-4c32-4257-8b07-af4770b604fd",
- "name": "NewL7PolicyName",
- "rules": []
- }
- }
- `
- // HandleL7PolicyListSuccessfully sets up the test server to respond to a l7policy List request.
- func HandleL7PolicyListSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- w.Header().Add("Content-Type", "application/json")
- r.ParseForm()
- marker := r.Form.Get("marker")
- switch marker {
- case "":
- fmt.Fprintf(w, L7PoliciesListBody)
- case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
- fmt.Fprintf(w, `{ "l7policies": [] }`)
- default:
- t.Fatalf("/v2.0/lbaas/l7policies invoked with unexpected marker=[%s]", marker)
- }
- })
- }
- // HandleL7PolicyGetSuccessfully sets up the test server to respond to a l7policy Get request.
- func HandleL7PolicyGetSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- fmt.Fprintf(w, SingleL7PolicyBody)
- })
- }
- // HandleL7PolicyDeletionSuccessfully sets up the test server to respond to a l7policy deletion request.
- func HandleL7PolicyDeletionSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- w.WriteHeader(http.StatusNoContent)
- })
- }
- // HandleL7PolicyUpdateSuccessfully sets up the test server to respond to a l7policy Update request.
- func HandleL7PolicyUpdateSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestJSONRequest(t, r, `{
- "l7policy": {
- "name": "NewL7PolicyName",
- "action": "REDIRECT_TO_URL",
- "redirect_url": "http://www.new-example.com"
- }
- }`)
- fmt.Fprintf(w, PostUpdateL7PolicyBody)
- })
- }
- // SingleRuleBody is the canned body of a Get request on an existing rule.
- const SingleRuleBody = `
- {
- "rule": {
- "compare_type": "REGEX",
- "invert": false,
- "admin_state_up": true,
- "value": "/images*",
- "key": null,
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "type": "PATH",
- "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
- }
- }
- `
- // HandleRuleCreationSuccessfully sets up the test server to respond to a rule creation request
- // with a given response.
- func HandleRuleCreationSuccessfully(t *testing.T, response string) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "POST")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- th.TestJSONRequest(t, r, `{
- "rule": {
- "compare_type": "REGEX",
- "type": "PATH",
- "value": "/images*"
- }
- }`)
- w.WriteHeader(http.StatusAccepted)
- w.Header().Add("Content-Type", "application/json")
- fmt.Fprintf(w, response)
- })
- }
- // RulesListBody contains the canned body of a rule list response.
- const RulesListBody = `
- {
- "rules":[
- {
- "compare_type": "REGEX",
- "invert": false,
- "admin_state_up": true,
- "value": "/images*",
- "key": null,
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "type": "PATH",
- "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
- },
- {
- "compare_type": "EQUAL_TO",
- "invert": false,
- "admin_state_up": true,
- "value": "www.example.com",
- "key": null,
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "type": "HOST_NAME",
- "id": "d24521a0-df84-4468-861a-a531af116d1e"
- }
- ]
- }
- `
- // HandleRuleListSuccessfully sets up the test server to respond to a rule List request.
- func HandleRuleListSuccessfully(t *testing.T) {
- th.Mux.HandleFunc("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules", func(w http.ResponseWriter, r *http.Request) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- w.Header().Add("Content-Type", "application/json")
- r.ParseForm()
- marker := r.Form.Get("marker")
- switch marker {
- case "":
- fmt.Fprintf(w, RulesListBody)
- case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
- fmt.Fprintf(w, `{ "rules": [] }`)
- default:
- t.Fatalf("/v2.0/lbaas/l7policies/8a1412f0-4c32-4257-8b07-af4770b604fd/rules invoked with unexpected marker=[%s]", marker)
- }
- })
- }
- // HandleRuleGetSuccessfully sets up the test server to respond to a rule Get request.
- func HandleRuleGetSuccessfully(t *testing.T) {
- 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) {
- th.TestMethod(t, r, "GET")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- fmt.Fprintf(w, SingleRuleBody)
- })
- }
- // HandleRuleDeletionSuccessfully sets up the test server to respond to a rule deletion request.
- func HandleRuleDeletionSuccessfully(t *testing.T) {
- 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) {
- th.TestMethod(t, r, "DELETE")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- w.WriteHeader(http.StatusNoContent)
- })
- }
- // PostUpdateRuleBody is the canned response body of a Update request on an existing rule.
- const PostUpdateRuleBody = `
- {
- "rule": {
- "compare_type": "REGEX",
- "invert": false,
- "admin_state_up": true,
- "value": "/images/special*",
- "key": null,
- "project_id": "e3cd678b11784734bc366148aa37580e",
- "type": "PATH",
- "id": "16621dbb-a736-4888-a57a-3ecd53df784c"
- }
- }
- `
- // HandleRuleUpdateSuccessfully sets up the test server to respond to a rule Update request.
- func HandleRuleUpdateSuccessfully(t *testing.T) {
- 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) {
- th.TestMethod(t, r, "PUT")
- th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
- th.TestHeader(t, r, "Accept", "application/json")
- th.TestHeader(t, r, "Content-Type", "application/json")
- th.TestJSONRequest(t, r, `{
- "rule": {
- "compare_type": "REGEX",
- "type": "PATH",
- "value": "/images/special*"
- }
- }`)
- fmt.Fprintf(w, PostUpdateRuleBody)
- })
- }
|