github.com/gophercloud/gophercloud@v1.11.0/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/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  	"github.com/gophercloud/gophercloud/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  		r.ParseForm()
   143  		marker := r.Form.Get("marker")
   144  		switch marker {
   145  		case "":
   146  			fmt.Fprintf(w, PoolsListBody)
   147  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   148  			fmt.Fprintf(w, `{ "pools": [] }`)
   149  		default:
   150  			t.Fatalf("/v2.0/lbaas/pools invoked with unexpected marker=[%s]", marker)
   151  		}
   152  	})
   153  }
   154  
   155  // HandlePoolCreationSuccessfully sets up the test server to respond to a pool creation request
   156  // with a given response.
   157  func HandlePoolCreationSuccessfully(t *testing.T, response string) {
   158  	th.Mux.HandleFunc("/v2.0/lbaas/pools", func(w http.ResponseWriter, r *http.Request) {
   159  		th.TestMethod(t, r, "POST")
   160  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   161  		th.TestJSONRequest(t, r, `{
   162  			"pool": {
   163  			        "lb_algorithm": "ROUND_ROBIN",
   164  			        "protocol": "HTTP",
   165  			        "name": "Example pool",
   166  			        "project_id": "2ffc6e22aae24e4795f87155d24c896f",
   167  			        "loadbalancer_id": "79e05663-7f03-45d2-a092-8b94062f22ab"
   168  			}
   169  		}`)
   170  
   171  		w.WriteHeader(http.StatusAccepted)
   172  		w.Header().Add("Content-Type", "application/json")
   173  		fmt.Fprintf(w, response)
   174  	})
   175  }
   176  
   177  // HandlePoolGetSuccessfully sets up the test server to respond to a pool Get request.
   178  func HandlePoolGetSuccessfully(t *testing.T) {
   179  	th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
   180  		th.TestMethod(t, r, "GET")
   181  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   182  		th.TestHeader(t, r, "Accept", "application/json")
   183  
   184  		fmt.Fprintf(w, SinglePoolBody)
   185  	})
   186  }
   187  
   188  // HandlePoolDeletionSuccessfully sets up the test server to respond to a pool deletion request.
   189  func HandlePoolDeletionSuccessfully(t *testing.T) {
   190  	th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
   191  		th.TestMethod(t, r, "DELETE")
   192  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   193  
   194  		w.WriteHeader(http.StatusNoContent)
   195  	})
   196  }
   197  
   198  // HandlePoolUpdateSuccessfully sets up the test server to respond to a pool Update request.
   199  func HandlePoolUpdateSuccessfully(t *testing.T) {
   200  	th.Mux.HandleFunc("/v2.0/lbaas/pools/c3741b06-df4d-4715-b142-276b6bce75ab", func(w http.ResponseWriter, r *http.Request) {
   201  		th.TestMethod(t, r, "PUT")
   202  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   203  		th.TestHeader(t, r, "Accept", "application/json")
   204  		th.TestHeader(t, r, "Content-Type", "application/json")
   205  		th.TestJSONRequest(t, r, `{
   206  			"pool": {
   207  				"name": "NewPoolName",
   208                                  "lb_algorithm": "LEAST_CONNECTIONS"
   209  			}
   210  		}`)
   211  
   212  		fmt.Fprintf(w, PostUpdatePoolBody)
   213  	})
   214  }
   215  
   216  // MembersListBody contains the canned body of a member list response.
   217  const MembersListBody = `
   218  {
   219  	"members":[
   220  		{
   221  			"id": "2a280670-c202-4b0b-a562-34077415aabf",
   222  			"address": "10.0.2.10",
   223  			"weight": 5,
   224  			"name": "web",
   225  			"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   226  			"project_id": "2ffc6e22aae24e4795f87155d24c896f",
   227  			"admin_state_up":true,
   228  			"protocol_port": 80
   229  		},
   230  		{
   231  			"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   232  			"address": "10.0.2.11",
   233  			"weight": 10,
   234  			"name": "db",
   235  			"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   236  			"project_id": "2ffc6e22aae24e4795f87155d24c896f",
   237  			"admin_state_up":false,
   238  			"protocol_port": 80,
   239  			"provisioning_status": "ACTIVE",
   240  			"created_at": "2018-08-23T20:05:21",
   241  			"updated_at": "2018-08-23T21:22:53",
   242  			"operating_status": "ONLINE",
   243  			"backup": false,
   244  			"monitor_address": "192.168.1.111",
   245  			"monitor_port": 80
   246  		}
   247  	]
   248  }
   249  `
   250  
   251  // SingleMemberBody is the canned body of a Get request on an existing member.
   252  const SingleMemberBody = `
   253  {
   254  	"member": {
   255  		"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   256  		"address": "10.0.2.11",
   257  		"weight": 10,
   258  		"name": "db",
   259  		"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   260  		"project_id": "2ffc6e22aae24e4795f87155d24c896f",
   261  		"admin_state_up":false,
   262  		"protocol_port": 80,
   263  		"provisioning_status": "ACTIVE",
   264  		"created_at": "2018-08-23T20:05:21",
   265  		"updated_at": "2018-08-23T21:22:53",
   266  		"operating_status": "ONLINE",
   267  		"backup": false,
   268  		"monitor_address": "192.168.1.111",
   269  		"monitor_port": 80
   270  	}
   271  }
   272  `
   273  
   274  // PostUpdateMemberBody is the canned response body of a Update request on an existing member.
   275  const PostUpdateMemberBody = `
   276  {
   277  	"member": {
   278  		"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   279  		"address": "10.0.2.11",
   280  		"weight": 10,
   281  		"name": "db",
   282  		"subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   283  		"project_id": "2ffc6e22aae24e4795f87155d24c896f",
   284  		"admin_state_up":false,
   285  		"protocol_port": 80
   286  	}
   287  }
   288  `
   289  
   290  var (
   291  	MemberWeb = pools.Member{
   292  		SubnetID:     "1981f108-3c48-48d2-b908-30f7d28532c9",
   293  		ProjectID:    "2ffc6e22aae24e4795f87155d24c896f",
   294  		AdminStateUp: true,
   295  		Name:         "web",
   296  		ID:           "2a280670-c202-4b0b-a562-34077415aabf",
   297  		Address:      "10.0.2.10",
   298  		Weight:       5,
   299  		ProtocolPort: 80,
   300  	}
   301  	MemberDb = pools.Member{
   302  		SubnetID:           "1981f108-3c48-48d2-b908-30f7d28532c9",
   303  		ProjectID:          "2ffc6e22aae24e4795f87155d24c896f",
   304  		AdminStateUp:       false,
   305  		Name:               "db",
   306  		ID:                 "fad389a3-9a4a-4762-a365-8c7038508b5d",
   307  		Address:            "10.0.2.11",
   308  		Weight:             10,
   309  		ProtocolPort:       80,
   310  		ProvisioningStatus: "ACTIVE",
   311  		CreatedAt:          time.Date(2018, 8, 23, 20, 05, 21, 0, time.UTC),
   312  		UpdatedAt:          time.Date(2018, 8, 23, 21, 22, 53, 0, time.UTC),
   313  		OperatingStatus:    "ONLINE",
   314  		Backup:             false,
   315  		MonitorAddress:     "192.168.1.111",
   316  		MonitorPort:        80,
   317  	}
   318  	MemberUpdated = pools.Member{
   319  		SubnetID:     "1981f108-3c48-48d2-b908-30f7d28532c9",
   320  		ProjectID:    "2ffc6e22aae24e4795f87155d24c896f",
   321  		AdminStateUp: false,
   322  		Name:         "db",
   323  		ID:           "fad389a3-9a4a-4762-a365-8c7038508b5d",
   324  		Address:      "10.0.2.11",
   325  		Weight:       10,
   326  		ProtocolPort: 80,
   327  	}
   328  )
   329  
   330  // HandleMemberListSuccessfully sets up the test server to respond to a member List request.
   331  func HandleMemberListSuccessfully(t *testing.T) {
   332  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
   333  		th.TestMethod(t, r, "GET")
   334  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   335  
   336  		w.Header().Add("Content-Type", "application/json")
   337  		r.ParseForm()
   338  		marker := r.Form.Get("marker")
   339  		switch marker {
   340  		case "":
   341  			fmt.Fprintf(w, MembersListBody)
   342  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   343  			fmt.Fprintf(w, `{ "members": [] }`)
   344  		default:
   345  			t.Fatalf("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members invoked with unexpected marker=[%s]", marker)
   346  		}
   347  	})
   348  }
   349  
   350  // HandleMemberCreationSuccessfully sets up the test server to respond to a member creation request
   351  // with a given response.
   352  func HandleMemberCreationSuccessfully(t *testing.T, response string) {
   353  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
   354  		th.TestMethod(t, r, "POST")
   355  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   356  		th.TestJSONRequest(t, r, `{
   357  			"member": {
   358  			        "address": "10.0.2.11",
   359  			        "weight": 10,
   360  			        "name": "db",
   361  			        "subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9",
   362  			        "project_id": "2ffc6e22aae24e4795f87155d24c896f",
   363  			        "protocol_port": 80
   364  			}
   365  		}`)
   366  
   367  		w.WriteHeader(http.StatusAccepted)
   368  		w.Header().Add("Content-Type", "application/json")
   369  		fmt.Fprintf(w, response)
   370  	})
   371  }
   372  
   373  // HandleMemberGetSuccessfully sets up the test server to respond to a member Get request.
   374  func HandleMemberGetSuccessfully(t *testing.T) {
   375  	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) {
   376  		th.TestMethod(t, r, "GET")
   377  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   378  		th.TestHeader(t, r, "Accept", "application/json")
   379  
   380  		fmt.Fprintf(w, SingleMemberBody)
   381  	})
   382  }
   383  
   384  // HandleMemberDeletionSuccessfully sets up the test server to respond to a member deletion request.
   385  func HandleMemberDeletionSuccessfully(t *testing.T) {
   386  	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) {
   387  		th.TestMethod(t, r, "DELETE")
   388  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   389  
   390  		w.WriteHeader(http.StatusNoContent)
   391  	})
   392  }
   393  
   394  // HandleMemberUpdateSuccessfully sets up the test server to respond to a member Update request.
   395  func HandleMemberUpdateSuccessfully(t *testing.T) {
   396  	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) {
   397  		th.TestMethod(t, r, "PUT")
   398  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   399  		th.TestHeader(t, r, "Accept", "application/json")
   400  		th.TestHeader(t, r, "Content-Type", "application/json")
   401  		th.TestJSONRequest(t, r, `{
   402  			"member": {
   403  				"name": "newMemberName",
   404                                  "weight": 4
   405  			}
   406  		}`)
   407  
   408  		fmt.Fprintf(w, PostUpdateMemberBody)
   409  	})
   410  }
   411  
   412  // HandleMembersUpdateSuccessfully sets up the test server to respond to a batch member Update request.
   413  func HandleMembersUpdateSuccessfully(t *testing.T) {
   414  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
   415  		th.TestMethod(t, r, "PUT")
   416  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   417  		th.TestHeader(t, r, "Accept", "application/json")
   418  		th.TestHeader(t, r, "Content-Type", "application/json")
   419  		th.TestJSONRequest(t, r, `{
   420  			"members": [
   421  				{
   422  					"name": "web-server-1",
   423  					"weight": 20,
   424  					"subnet_id": "bbb35f84-35cc-4b2f-84c2-a6a29bba68aa",
   425  					"address": "192.0.2.16",
   426  					"protocol_port": 80
   427  				},
   428  				{
   429  					"name": "web-server-2",
   430  					"weight": 10,
   431  					"subnet_id": "bbb35f84-35cc-4b2f-84c2-a6a29bba68aa",
   432  					"address": "192.0.2.17",
   433  					"protocol_port": 80
   434  				}
   435  			]
   436  		}`)
   437  
   438  		w.WriteHeader(http.StatusAccepted)
   439  	})
   440  }
   441  
   442  // HandleEmptyMembersUpdateSuccessfully sets up the test server to respond to an empty batch member Update request.
   443  func HandleEmptyMembersUpdateSuccessfully(t *testing.T) {
   444  	th.Mux.HandleFunc("/v2.0/lbaas/pools/332abe93-f488-41ba-870b-2ac66be7f853/members", func(w http.ResponseWriter, r *http.Request) {
   445  		th.TestMethod(t, r, "PUT")
   446  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   447  		th.TestHeader(t, r, "Accept", "application/json")
   448  		th.TestHeader(t, r, "Content-Type", "application/json")
   449  		th.TestJSONRequest(t, r, `{
   450  			"members": []
   451  		}`)
   452  
   453  		w.WriteHeader(http.StatusAccepted)
   454  	})
   455  }