github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/l7policies/testing/fixtures.go (about)

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