github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas_v2/l7policies/testing/fixtures_test.go (about)

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