github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/monitors/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/monitors"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  // HealthmonitorsListBody contains the canned body of a healthmonitor list response.
    14  const HealthmonitorsListBody = `
    15  {
    16  	"healthmonitors":[
    17  		{
    18  			"admin_state_up":true,
    19  			"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    20  			"delay":10,
    21  			"name":"web",
    22  			"max_retries":1,
    23  			"timeout":1,
    24  			"type":"PING",
    25  			"pools": [{"id": "84f1b61f-58c4-45bf-a8a9-2dafb9e5214d"}],
    26  			"id":"466c8345-28d8-4f84-a246-e04380b0461d"
    27  		},
    28  		{
    29  			"admin_state_up":true,
    30  			"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    31  			"delay":5,
    32  			"name":"db",
    33  			"expected_codes":"200",
    34  			"max_retries":2,
    35  			"http_method":"GET",
    36  			"timeout":2,
    37  			"url_path":"/",
    38  			"type":"HTTP",
    39  			"pools": [{"id": "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}],
    40  			"id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7"
    41  		}
    42  	]
    43  }
    44  `
    45  
    46  // SingleHealthmonitorBody is the canned body of a Get request on an existing healthmonitor.
    47  const SingleHealthmonitorBody = `
    48  {
    49  	"healthmonitor": {
    50  		"admin_state_up":true,
    51  		"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    52  		"delay":5,
    53  		"name":"db",
    54  		"expected_codes":"200",
    55  		"max_retries":2,
    56  		"http_method":"GET",
    57  		"timeout":2,
    58  		"url_path":"/",
    59  		"type":"HTTP",
    60  		"pools": [{"id": "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}],
    61  		"id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7"
    62  	}
    63  }
    64  `
    65  
    66  // PostUpdateHealthmonitorBody is the canned response body of a Update request on an existing healthmonitor.
    67  const PostUpdateHealthmonitorBody = `
    68  {
    69  	"healthmonitor": {
    70  		"admin_state_up":true,
    71  		"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    72  		"delay":3,
    73  		"name":"NewHealthmonitorName",
    74  		"expected_codes":"301",
    75  		"max_retries":10,
    76  		"http_method":"GET",
    77  		"timeout":20,
    78  		"url_path":"/another_check",
    79  		"type":"HTTP",
    80  		"pools": [{"id": "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}],
    81  		"id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7"
    82  	}
    83  }
    84  `
    85  
    86  var (
    87  	HealthmonitorWeb = monitors.Monitor{
    88  		AdminStateUp: true,
    89  		Name:         "web",
    90  		TenantID:     "83657cfcdfe44cd5920adaf26c48ceea",
    91  		Delay:        10,
    92  		MaxRetries:   1,
    93  		Timeout:      1,
    94  		Type:         "PING",
    95  		ID:           "466c8345-28d8-4f84-a246-e04380b0461d",
    96  		Pools:        []monitors.PoolID{{ID: "84f1b61f-58c4-45bf-a8a9-2dafb9e5214d"}},
    97  	}
    98  	HealthmonitorDb = monitors.Monitor{
    99  		AdminStateUp:  true,
   100  		Name:          "db",
   101  		TenantID:      "83657cfcdfe44cd5920adaf26c48ceea",
   102  		Delay:         5,
   103  		ExpectedCodes: "200",
   104  		MaxRetries:    2,
   105  		Timeout:       2,
   106  		URLPath:       "/",
   107  		Type:          "HTTP",
   108  		HTTPMethod:    "GET",
   109  		ID:            "5d4b5228-33b0-4e60-b225-9b727c1a20e7",
   110  		Pools:         []monitors.PoolID{{ID: "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}},
   111  	}
   112  	HealthmonitorUpdated = monitors.Monitor{
   113  		AdminStateUp:  true,
   114  		Name:          "NewHealthmonitorName",
   115  		TenantID:      "83657cfcdfe44cd5920adaf26c48ceea",
   116  		Delay:         3,
   117  		ExpectedCodes: "301",
   118  		MaxRetries:    10,
   119  		Timeout:       20,
   120  		URLPath:       "/another_check",
   121  		Type:          "HTTP",
   122  		HTTPMethod:    "GET",
   123  		ID:            "5d4b5228-33b0-4e60-b225-9b727c1a20e7",
   124  		Pools:         []monitors.PoolID{{ID: "d459f7d8-c6ee-439d-8713-d3fc08aeed8d"}},
   125  	}
   126  )
   127  
   128  // HandleHealthmonitorListSuccessfully sets up the test server to respond to a healthmonitor List request.
   129  func HandleHealthmonitorListSuccessfully(t *testing.T) {
   130  	th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors", func(w http.ResponseWriter, r *http.Request) {
   131  		th.TestMethod(t, r, "GET")
   132  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   133  
   134  		w.Header().Add("Content-Type", "application/json")
   135  		r.ParseForm()
   136  		marker := r.Form.Get("marker")
   137  		switch marker {
   138  		case "":
   139  			fmt.Fprintf(w, HealthmonitorsListBody)
   140  		case "556c8345-28d8-4f84-a246-e04380b0461d":
   141  			fmt.Fprintf(w, `{ "healthmonitors": [] }`)
   142  		default:
   143  			t.Fatalf("/v2.0/lbaas/healthmonitors invoked with unexpected marker=[%s]", marker)
   144  		}
   145  	})
   146  }
   147  
   148  // HandleHealthmonitorCreationSuccessfully sets up the test server to respond to a healthmonitor creation request
   149  // with a given response.
   150  func HandleHealthmonitorCreationSuccessfully(t *testing.T, response string) {
   151  	th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors", func(w http.ResponseWriter, r *http.Request) {
   152  		th.TestMethod(t, r, "POST")
   153  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   154  		th.TestJSONRequest(t, r, `{
   155  			"healthmonitor": {
   156  				"type":"HTTP",
   157  				"pool_id":"84f1b61f-58c4-45bf-a8a9-2dafb9e5214d",
   158  				"tenant_id":"453105b9-1754-413f-aab1-55f1af620750",
   159  				"delay":20,
   160  				"name":"db",
   161  				"timeout":10,
   162  				"max_retries":5,
   163  				"url_path":"/check",
   164  				"expected_codes":"200-299"
   165  			}
   166  		}`)
   167  
   168  		w.WriteHeader(http.StatusAccepted)
   169  		w.Header().Add("Content-Type", "application/json")
   170  		fmt.Fprintf(w, response)
   171  	})
   172  }
   173  
   174  // HandleHealthmonitorGetSuccessfully sets up the test server to respond to a healthmonitor Get request.
   175  func HandleHealthmonitorGetSuccessfully(t *testing.T) {
   176  	th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors/5d4b5228-33b0-4e60-b225-9b727c1a20e7", func(w http.ResponseWriter, r *http.Request) {
   177  		th.TestMethod(t, r, "GET")
   178  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   179  		th.TestHeader(t, r, "Accept", "application/json")
   180  
   181  		fmt.Fprintf(w, SingleHealthmonitorBody)
   182  	})
   183  }
   184  
   185  // HandleHealthmonitorDeletionSuccessfully sets up the test server to respond to a healthmonitor deletion request.
   186  func HandleHealthmonitorDeletionSuccessfully(t *testing.T) {
   187  	th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors/5d4b5228-33b0-4e60-b225-9b727c1a20e7", func(w http.ResponseWriter, r *http.Request) {
   188  		th.TestMethod(t, r, "DELETE")
   189  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   190  
   191  		w.WriteHeader(http.StatusNoContent)
   192  	})
   193  }
   194  
   195  // HandleHealthmonitorUpdateSuccessfully sets up the test server to respond to a healthmonitor Update request.
   196  func HandleHealthmonitorUpdateSuccessfully(t *testing.T) {
   197  	th.Mux.HandleFunc("/v2.0/lbaas/healthmonitors/5d4b5228-33b0-4e60-b225-9b727c1a20e7", func(w http.ResponseWriter, r *http.Request) {
   198  		th.TestMethod(t, r, "PUT")
   199  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   200  		th.TestHeader(t, r, "Accept", "application/json")
   201  		th.TestHeader(t, r, "Content-Type", "application/json")
   202  		th.TestJSONRequest(t, r, `{
   203  			"healthmonitor": {
   204  				"name": "NewHealthmonitorName",
   205  				"delay": 3,
   206  				"timeout": 20,
   207  				"max_retries": 10,
   208  				"url_path": "/another_check",
   209  				"expected_codes": "301"
   210  			}
   211  		}`)
   212  
   213  		fmt.Fprintf(w, PostUpdateHealthmonitorBody)
   214  	})
   215  }