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

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