github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/pools/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/pools"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  // PoolsListBody contains the canned body of a pool list response.
    14  const PoolsListBody = `
    15  {
    16  	"pools":[
    17  	         {
    18  			"lb_algorithm":"ROUND_ROBIN",
    19  			"protocol":"HTTP",
    20  			"description":"",
    21  			"healthmonitor_id": "466c8345-28d8-4f84-a246-e04380b0461d",
    22  			"members":[{"id": "53306cda-815d-4354-9fe4-59e09da9c3c5"}],
    23  			"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
    24  			"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
    25  			"id":"72741b06-df4d-4715-b142-276b6bce75ab",
    26  			"name":"web",
    27  			"admin_state_up":true,
    28  			"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    29  			"provider": "haproxy"
    30  		},
    31  		{
    32  			"lb_algorithm":"LEAST_CONNECTION",
    33  			"protocol":"HTTP",
    34  			"description":"",
    35  			"healthmonitor_id": "5f6c8345-28d8-4f84-a246-e04380b0461d",
    36  			"members":[{"id": "67306cda-815d-4354-9fe4-59e09da9c3c5"}],
    37  			"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
    38  			"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
    39  			"id":"c3741b06-df4d-4715-b142-276b6bce75ab",
    40  			"name":"db",
    41  			"admin_state_up":true,
    42  			"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    43  			"provider": "haproxy"
    44  		}
    45  	]
    46  }
    47  `
    48  
    49  // SinglePoolBody is the canned body of a Get request on an existing pool.
    50  const SinglePoolBody = `
    51  {
    52  	"pool": {
    53  		"lb_algorithm":"LEAST_CONNECTION",
    54  		"protocol":"HTTP",
    55  		"description":"",
    56  		"healthmonitor_id": "5f6c8345-28d8-4f84-a246-e04380b0461d",
    57  		"members":[{"id": "67306cda-815d-4354-9fe4-59e09da9c3c5"}],
    58  		"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
    59  		"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
    60  		"id":"c3741b06-df4d-4715-b142-276b6bce75ab",
    61  		"name":"db",
    62  		"admin_state_up":true,
    63  		"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    64  		"provider": "haproxy"
    65  	}
    66  }
    67  `
    68  
    69  // PostUpdatePoolBody is the canned response body of a Update request on an existing pool.
    70  const PostUpdatePoolBody = `
    71  {
    72  	"pool": {
    73  		"lb_algorithm":"LEAST_CONNECTION",
    74  		"protocol":"HTTP",
    75  		"description":"",
    76  		"healthmonitor_id": "5f6c8345-28d8-4f84-a246-e04380b0461d",
    77  		"members":[{"id": "67306cda-815d-4354-9fe4-59e09da9c3c5"}],
    78  		"listeners":[{"id": "2a280670-c202-4b0b-a562-34077415aabf"}],
    79  		"loadbalancers":[{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}],
    80  		"id":"c3741b06-df4d-4715-b142-276b6bce75ab",
    81  		"name":"db",
    82  		"admin_state_up":true,
    83  		"tenant_id":"83657cfcdfe44cd5920adaf26c48ceea",
    84  		"provider": "haproxy"
    85  	}
    86  }
    87  `
    88  
    89  var (
    90  	PoolWeb = pools.Pool{
    91  		LBMethod:      "ROUND_ROBIN",
    92  		Protocol:      "HTTP",
    93  		Description:   "",
    94  		MonitorID:     "466c8345-28d8-4f84-a246-e04380b0461d",
    95  		TenantID:      "83657cfcdfe44cd5920adaf26c48ceea",
    96  		AdminStateUp:  true,
    97  		Name:          "web",
    98  		Members:       []pools.Member{{ID: "53306cda-815d-4354-9fe4-59e09da9c3c5"}},
    99  		ID:            "72741b06-df4d-4715-b142-276b6bce75ab",
   100  		Loadbalancers: []pools.LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}},
   101  		Listeners:     []pools.ListenerID{{ID: "2a280670-c202-4b0b-a562-34077415aabf"}},
   102  		Provider:      "haproxy",
   103  	}
   104  	PoolDb = pools.Pool{
   105  		LBMethod:      "LEAST_CONNECTION",
   106  		Protocol:      "HTTP",
   107  		Description:   "",
   108  		MonitorID:     "5f6c8345-28d8-4f84-a246-e04380b0461d",
   109  		TenantID:      "83657cfcdfe44cd5920adaf26c48ceea",
   110  		AdminStateUp:  true,
   111  		Name:          "db",
   112  		Members:       []pools.Member{{ID: "67306cda-815d-4354-9fe4-59e09da9c3c5"}},
   113  		ID:            "c3741b06-df4d-4715-b142-276b6bce75ab",
   114  		Loadbalancers: []pools.LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}},
   115  		Listeners:     []pools.ListenerID{{ID: "2a280670-c202-4b0b-a562-34077415aabf"}},
   116  		Provider:      "haproxy",
   117  	}
   118  	PoolUpdated = pools.Pool{
   119  		LBMethod:      "LEAST_CONNECTION",
   120  		Protocol:      "HTTP",
   121  		Description:   "",
   122  		MonitorID:     "5f6c8345-28d8-4f84-a246-e04380b0461d",
   123  		TenantID:      "83657cfcdfe44cd5920adaf26c48ceea",
   124  		AdminStateUp:  true,
   125  		Name:          "db",
   126  		Members:       []pools.Member{{ID: "67306cda-815d-4354-9fe4-59e09da9c3c5"}},
   127  		ID:            "c3741b06-df4d-4715-b142-276b6bce75ab",
   128  		Loadbalancers: []pools.LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}},
   129  		Listeners:     []pools.ListenerID{{ID: "2a280670-c202-4b0b-a562-34077415aabf"}},
   130  		Provider:      "haproxy",
   131  	}
   132  )
   133  
   134  // HandlePoolListSuccessfully sets up the test server to respond to a pool List request.
   135  func HandlePoolListSuccessfully(t *testing.T) {
   136  	th.Mux.HandleFunc("/v2.0/lbaas/pools", func(w http.ResponseWriter, r *http.Request) {
   137  		th.TestMethod(t, r, "GET")
   138  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   139  
   140  		w.Header().Add("Content-Type", "application/json")
   141  		r.ParseForm()
   142  		marker := r.Form.Get("marker")
   143  		switch marker {
   144  		case "":
   145  			fmt.Fprintf(w, PoolsListBody)
   146  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   147  			fmt.Fprintf(w, `{ "pools": [] }`)
   148  		default:
   149  			t.Fatalf("/v2.0/lbaas/pools invoked with unexpected marker=[%s]", marker)
   150  		}
   151  	})
   152  }
   153  
   154  // HandlePoolCreationSuccessfully sets up the test server to respond to a pool creation request
   155  // with a given response.
   156  func HandlePoolCreationSuccessfully(t *testing.T, response string) {
   157  	th.Mux.HandleFunc("/v2.0/lbaas/pools", func(w http.ResponseWriter, r *http.Request) {
   158  		th.TestMethod(t, r, "POST")
   159  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   160  		th.TestJSONRequest(t, r, `{
   161  			"pool": {
   162  			        "lb_algorithm": "ROUND_ROBIN",
   163  			        "protocol": "HTTP",
   164  			        "name": "Example pool",
   165  			        "tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
   166  			        "loadbalancer_id": "79e05663-7f03-45d2-a092-8b94062f22ab"
   167  			}
   168  		}`)
   169  
   170  		w.WriteHeader(http.StatusAccepted)
   171  		w.Header().Add("Content-Type", "application/json")
   172  		fmt.Fprintf(w, response)
   173  	})
   174  }
   175  
   176  // HandlePoolGetSuccessfully sets up the test server to respond to a pool Get request.
   177  func HandlePoolGetSuccessfully(t *testing.T) {
   178  	th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
   179  		th.TestMethod(t, r, "GET")
   180  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   181  		th.TestHeader(t, r, "Accept", "application/json")
   182  
   183  		fmt.Fprintf(w, SinglePoolBody)
   184  	})
   185  }
   186  
   187  // HandlePoolDeletionSuccessfully sets up the test server to respond to a pool deletion request.
   188  func HandlePoolDeletionSuccessfully(t *testing.T) {
   189  	th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
   190  		th.TestMethod(t, r, "DELETE")
   191  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   192  
   193  		w.WriteHeader(http.StatusNoContent)
   194  	})
   195  }
   196  
   197  // HandlePoolUpdateSuccessfully sets up the test server to respond to a pool Update request.
   198  func HandlePoolUpdateSuccessfully(t *testing.T) {
   199  	th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
   200  		th.TestMethod(t, r, "PUT")
   201  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   202  		th.TestHeader(t, r, "Accept", "application/json")
   203  		th.TestHeader(t, r, "Content-Type", "application/json")
   204  		th.TestJSONRequest(t, r, `{
   205  			"pool": {
   206  				"name": "NewPoolName",
   207                                  "lb_algorithm": "LEAST_CONNECTIONS"
   208  			}
   209  		}`)
   210  
   211  		fmt.Fprintf(w, PostUpdatePoolBody)
   212  	})
   213  }
   214  
   215  // MembersListBody contains the canned body of a member list response.
   216  const MembersListBody = `
   217  {
   218  	"members":[
   219  		{
   220  			"id": "2a280670-c202-4b0b-a562-34077415aabf",
   221  			"address": "10.0.2.10",
   222  			"weight": 5,
   223  			"name": "web",
   224  			"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   225  			"tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
   226  			"admin_state_up":true,
   227  			"protocol_port": 80
   228  		},
   229  		{
   230  			"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   231  			"address": "10.0.2.11",
   232  			"weight": 10,
   233  			"name": "db",
   234  			"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   235  			"tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
   236  			"admin_state_up":false,
   237  			"protocol_port": 80
   238  		}
   239  	]
   240  }
   241  `
   242  
   243  // SingleMemberBody is the canned body of a Get request on an existing member.
   244  const SingleMemberBody = `
   245  {
   246  	"member": {
   247  		"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   248  		"address": "10.0.2.11",
   249  		"weight": 10,
   250  		"name": "db",
   251  		"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   252  		"tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
   253  		"admin_state_up":false,
   254  		"protocol_port": 80
   255  	}
   256  }
   257  `
   258  
   259  // PostUpdateMemberBody is the canned response body of a Update request on an existing member.
   260  const PostUpdateMemberBody = `
   261  {
   262  	"member": {
   263  		"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   264  		"address": "10.0.2.11",
   265  		"weight": 10,
   266  		"name": "db",
   267  		"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   268  		"tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
   269  		"admin_state_up":false,
   270  		"protocol_port": 80
   271  	}
   272  }
   273  `
   274  
   275  var (
   276  	MemberWeb = pools.Member{
   277  		SubnetID:     "1981f108-3c48-48d2-b908-30f7d28532c9",
   278  		TenantID:     "2ffc6e22aae24e4795f87155d24c896f",
   279  		AdminStateUp: true,
   280  		Name:         "web",
   281  		ID:           "2a280670-c202-4b0b-a562-34077415aabf",
   282  		Address:      "10.0.2.10",
   283  		Weight:       5,
   284  		ProtocolPort: 80,
   285  	}
   286  	MemberDb = pools.Member{
   287  		SubnetID:     "1981f108-3c48-48d2-b908-30f7d28532c9",
   288  		TenantID:     "2ffc6e22aae24e4795f87155d24c896f",
   289  		AdminStateUp: false,
   290  		Name:         "db",
   291  		ID:           "fad389a3-9a4a-4762-a365-8c7038508b5d",
   292  		Address:      "10.0.2.11",
   293  		Weight:       10,
   294  		ProtocolPort: 80,
   295  	}
   296  	MemberUpdated = pools.Member{
   297  		SubnetID:     "1981f108-3c48-48d2-b908-30f7d28532c9",
   298  		TenantID:     "2ffc6e22aae24e4795f87155d24c896f",
   299  		AdminStateUp: false,
   300  		Name:         "db",
   301  		ID:           "fad389a3-9a4a-4762-a365-8c7038508b5d",
   302  		Address:      "10.0.2.11",
   303  		Weight:       10,
   304  		ProtocolPort: 80,
   305  	}
   306  )
   307  
   308  // HandleMemberListSuccessfully sets up the test server to respond to a member List request.
   309  func HandleMemberListSuccessfully(t *testing.T) {
   310  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
   311  		th.TestMethod(t, r, "GET")
   312  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   313  
   314  		w.Header().Add("Content-Type", "application/json")
   315  		r.ParseForm()
   316  		marker := r.Form.Get("marker")
   317  		switch marker {
   318  		case "":
   319  			fmt.Fprintf(w, MembersListBody)
   320  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   321  			fmt.Fprintf(w, `{ "members": [] }`)
   322  		default:
   323  			t.Fatalf("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members invoked with unexpected marker=[%s]", marker)
   324  		}
   325  	})
   326  }
   327  
   328  // HandleMemberCreationSuccessfully sets up the test server to respond to a member creation request
   329  // with a given response.
   330  func HandleMemberCreationSuccessfully(t *testing.T, response string) {
   331  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
   332  		th.TestMethod(t, r, "POST")
   333  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   334  		th.TestJSONRequest(t, r, `{
   335  			"member": {
   336  			        "address": "10.0.2.11",
   337  			        "weight": 10,
   338  			        "name": "db",
   339  			        "subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   340  			        "tenant_id": "2ffc6e22aae24e4795f87155d24c896f",
   341  			        "protocol_port": 80
   342  			}
   343  		}`)
   344  
   345  		w.WriteHeader(http.StatusAccepted)
   346  		w.Header().Add("Content-Type", "application/json")
   347  		fmt.Fprintf(w, response)
   348  	})
   349  }
   350  
   351  // HandleMemberGetSuccessfully sets up the test server to respond to a member Get request.
   352  func HandleMemberGetSuccessfully(t *testing.T) {
   353  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members/2a280670-c202-4b0b-a562-34077415aabf", func(w http.ResponseWriter, r *http.Request) {
   354  		th.TestMethod(t, r, "GET")
   355  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   356  		th.TestHeader(t, r, "Accept", "application/json")
   357  
   358  		fmt.Fprintf(w, SingleMemberBody)
   359  	})
   360  }
   361  
   362  // HandleMemberDeletionSuccessfully sets up the test server to respond to a member deletion request.
   363  func HandleMemberDeletionSuccessfully(t *testing.T) {
   364  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members/2a280670-c202-4b0b-a562-34077415aabf", func(w http.ResponseWriter, r *http.Request) {
   365  		th.TestMethod(t, r, "DELETE")
   366  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   367  
   368  		w.WriteHeader(http.StatusNoContent)
   369  	})
   370  }
   371  
   372  // HandleMemberUpdateSuccessfully sets up the test server to respond to a member Update request.
   373  func HandleMemberUpdateSuccessfully(t *testing.T) {
   374  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members/2a280670-c202-4b0b-a562-34077415aabf", func(w http.ResponseWriter, r *http.Request) {
   375  		th.TestMethod(t, r, "PUT")
   376  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   377  		th.TestHeader(t, r, "Accept", "application/json")
   378  		th.TestHeader(t, r, "Content-Type", "application/json")
   379  		th.TestJSONRequest(t, r, `{
   380  			"member": {
   381  				"name": "newMemberName",
   382                                  "weight": 4
   383  			}
   384  		}`)
   385  
   386  		fmt.Fprintf(w, PostUpdateMemberBody)
   387  	})
   388  }