github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/loadbalancer/v2/monitors/testing/fixtures_test.go (about)

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