github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/elb/v2/loadbalancers/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	th "github.com/huaweicloud/golangsdk/testhelper"
     9  	"github.com/huaweicloud/golangsdk/testhelper/client"
    10  
    11  	"github.com/huaweicloud/golangsdk/openstack/elb/v2/loadbalancers"
    12  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/listeners"
    13  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/monitors"
    14  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/pools"
    15  )
    16  
    17  // LoadbalancersListBody contains the canned body of a loadbalancer list response.
    18  const LoadbalancersListBody = `
    19  {
    20  	"loadbalancers":[
    21  	         {
    22  			"id": "c331058c-6a40-4144-948e-b9fb1df9db4b",
    23  			"tenant_id": "54030507-44f7-473c-9342-b4d14a95f692",
    24  			"name": "web_lb",
    25  			"description": "lb config for the web tier",
    26  			"vip_subnet_id": "8a49c438-848f-467b-9655-ea1548708154",
    27  			"vip_address": "10.30.176.47",
    28  			"vip_port_id": "2a22e552-a347-44fd-b530-1f2b1b2a6735",
    29  			"flavor": "small",
    30  			"provider": "haproxy",
    31  			"admin_state_up": true,
    32  			"provisioning_status": "ACTIVE",
    33  			"operating_status": "ONLINE"
    34  		},
    35  		{
    36  			"id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
    37  			"tenant_id": "54030507-44f7-473c-9342-b4d14a95f692",
    38  			"name": "db_lb",
    39  			"description": "lb config for the db tier",
    40  			"vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    41  			"vip_address": "10.30.176.48",
    42  			"vip_port_id": "2bf413c8-41a9-4477-b505-333d5cbe8b55",
    43  			"flavor": "medium",
    44  			"provider": "haproxy",
    45  			"admin_state_up": true,
    46  			"provisioning_status": "PENDING_CREATE",
    47  			"operating_status": "OFFLINE"
    48  		}
    49  	]
    50  }
    51  `
    52  
    53  // SingleLoadbalancerBody is the canned body of a Get request on an existing loadbalancer.
    54  const SingleLoadbalancerBody = `
    55  {
    56  	"loadbalancer": {
    57  		"id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
    58  		"tenant_id": "54030507-44f7-473c-9342-b4d14a95f692",
    59  		"name": "db_lb",
    60  		"description": "lb config for the db tier",
    61  		"vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    62  		"vip_address": "10.30.176.48",
    63  		"vip_port_id": "2bf413c8-41a9-4477-b505-333d5cbe8b55",
    64  		"flavor": "medium",
    65  		"provider": "haproxy",
    66  		"admin_state_up": true,
    67  		"provisioning_status": "PENDING_CREATE",
    68  		"operating_status": "OFFLINE"
    69  	}
    70  }
    71  `
    72  
    73  // PostUpdateLoadbalancerBody is the canned response body of a Update request on an existing loadbalancer.
    74  const PostUpdateLoadbalancerBody = `
    75  {
    76  	"loadbalancer": {
    77  		"id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
    78  		"tenant_id": "54030507-44f7-473c-9342-b4d14a95f692",
    79  		"name": "NewLoadbalancerName",
    80  		"description": "lb config for the db tier",
    81  		"vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    82  		"vip_address": "10.30.176.48",
    83  		"vip_port_id": "2bf413c8-41a9-4477-b505-333d5cbe8b55",
    84  		"flavor": "medium",
    85  		"provider": "haproxy",
    86  		"admin_state_up": true,
    87  		"provisioning_status": "PENDING_CREATE",
    88  		"operating_status": "OFFLINE"
    89  	}
    90  }
    91  `
    92  
    93  // SingleLoadbalancerBody is the canned body of a Get request on an existing loadbalancer.
    94  const LoadbalancerStatuesesTree = `
    95  {
    96  	"statuses" : {
    97  		"loadbalancer": {
    98  			"id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
    99  			"name": "db_lb",
   100  			"provisioning_status": "PENDING_UPDATE",
   101  			"operating_status": "ACTIVE",
   102  			"listeners": [{
   103  				"id": "db902c0c-d5ff-4753-b465-668ad9656918",
   104  				"name": "db",
   105  				"provisioning_status": "PENDING_UPDATE",
   106  				"pools": [{
   107  					"id": "fad389a3-9a4a-4762-a365-8c7038508b5d",
   108  					"name": "db",
   109  					"provisioning_status": "PENDING_UPDATE",
   110  					"healthmonitor": {
   111  						"id": "67306cda-815d-4354-9fe4-59e09da9c3c5",
   112  						"type":"PING",
   113  						"provisioning_status": "PENDING_UPDATE"
   114  					},
   115  					"members":[{
   116  						"id": "2a280670-c202-4b0b-a562-34077415aabf",
   117  						"name": "db",
   118  						"address": "10.0.2.11",
   119  						"protocol_port": 80,
   120  						"provisioning_status": "PENDING_UPDATE"
   121  					}]
   122  				}]
   123  			}]
   124  		}
   125  	}
   126  }
   127  `
   128  
   129  var (
   130  	LoadbalancerWeb = loadbalancers.LoadBalancer{
   131  		ID:                 "c331058c-6a40-4144-948e-b9fb1df9db4b",
   132  		TenantID:           "54030507-44f7-473c-9342-b4d14a95f692",
   133  		Name:               "web_lb",
   134  		Description:        "lb config for the web tier",
   135  		VipSubnetID:        "8a49c438-848f-467b-9655-ea1548708154",
   136  		VipAddress:         "10.30.176.47",
   137  		VipPortID:          "2a22e552-a347-44fd-b530-1f2b1b2a6735",
   138  		Flavor:             "small",
   139  		Provider:           "haproxy",
   140  		AdminStateUp:       true,
   141  		ProvisioningStatus: "ACTIVE",
   142  		OperatingStatus:    "ONLINE",
   143  	}
   144  	LoadbalancerDb = loadbalancers.LoadBalancer{
   145  		ID:                 "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
   146  		TenantID:           "54030507-44f7-473c-9342-b4d14a95f692",
   147  		Name:               "db_lb",
   148  		Description:        "lb config for the db tier",
   149  		VipSubnetID:        "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
   150  		VipAddress:         "10.30.176.48",
   151  		VipPortID:          "2bf413c8-41a9-4477-b505-333d5cbe8b55",
   152  		Flavor:             "medium",
   153  		Provider:           "haproxy",
   154  		AdminStateUp:       true,
   155  		ProvisioningStatus: "PENDING_CREATE",
   156  		OperatingStatus:    "OFFLINE",
   157  	}
   158  	LoadbalancerUpdated = loadbalancers.LoadBalancer{
   159  		ID:                 "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
   160  		TenantID:           "54030507-44f7-473c-9342-b4d14a95f692",
   161  		Name:               "NewLoadbalancerName",
   162  		Description:        "lb config for the db tier",
   163  		VipSubnetID:        "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
   164  		VipAddress:         "10.30.176.48",
   165  		VipPortID:          "2bf413c8-41a9-4477-b505-333d5cbe8b55",
   166  		Flavor:             "medium",
   167  		Provider:           "haproxy",
   168  		AdminStateUp:       true,
   169  		ProvisioningStatus: "PENDING_CREATE",
   170  		OperatingStatus:    "OFFLINE",
   171  	}
   172  	LoadbalancerStatusesTree = loadbalancers.LoadBalancer{
   173  		ID:                 "36e08a3e-a78f-4b40-a229-1e7e23eee1ab",
   174  		Name:               "db_lb",
   175  		ProvisioningStatus: "PENDING_UPDATE",
   176  		OperatingStatus:    "ACTIVE",
   177  		Listeners: []listeners.Listener{{
   178  			ID:                 "db902c0c-d5ff-4753-b465-668ad9656918",
   179  			Name:               "db",
   180  			ProvisioningStatus: "PENDING_UPDATE",
   181  			Pools: []pools.Pool{{
   182  				ID:                 "fad389a3-9a4a-4762-a365-8c7038508b5d",
   183  				Name:               "db",
   184  				ProvisioningStatus: "PENDING_UPDATE",
   185  				Monitor: monitors.Monitor{
   186  					ID:                 "67306cda-815d-4354-9fe4-59e09da9c3c5",
   187  					Type:               "PING",
   188  					ProvisioningStatus: "PENDING_UPDATE",
   189  				},
   190  				Members: []pools.Member{{
   191  					ID:                 "2a280670-c202-4b0b-a562-34077415aabf",
   192  					Name:               "db",
   193  					Address:            "10.0.2.11",
   194  					ProtocolPort:       80,
   195  					ProvisioningStatus: "PENDING_UPDATE",
   196  				}},
   197  			}},
   198  		}},
   199  	}
   200  )
   201  
   202  // HandleLoadbalancerListSuccessfully sets up the test server to respond to a loadbalancer List request.
   203  func HandleLoadbalancerListSuccessfully(t *testing.T) {
   204  	th.Mux.HandleFunc("/v2/54030507-44f7-473c-9342-b4d14a95f692/elb/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
   205  		th.TestMethod(t, r, "GET")
   206  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   207  
   208  		w.Header().Add("Content-Type", "application/json")
   209  		r.ParseForm()
   210  		marker := r.Form.Get("marker")
   211  		switch marker {
   212  		case "":
   213  			fmt.Fprintf(w, LoadbalancersListBody)
   214  		case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab":
   215  			fmt.Fprintf(w, `{ "loadbalancers": [] }`)
   216  		default:
   217  			t.Fatalf("/v2.0/lbaas/loadbalancers invoked with unexpected marker=[%s]", marker)
   218  		}
   219  	})
   220  }
   221  
   222  // HandleLoadbalancerCreationSuccessfully sets up the test server to respond to a loadbalancer creation request
   223  // with a given response.
   224  func HandleLoadbalancerCreationSuccessfully(t *testing.T, response string) {
   225  	th.Mux.HandleFunc("/v2/54030507-44f7-473c-9342-b4d14a95f692/elb/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
   226  		th.TestMethod(t, r, "POST")
   227  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   228  		th.TestJSONRequest(t, r, `{
   229  			"loadbalancer": {
   230  				"name": "db_lb",
   231  				"vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
   232  				"vip_address": "10.30.176.48",
   233  				"flavor": "medium",
   234  				"provider": "haproxy",
   235  				"admin_state_up": true
   236  			}
   237  		}`)
   238  
   239  		w.WriteHeader(http.StatusAccepted)
   240  		w.Header().Add("Content-Type", "application/json")
   241  		fmt.Fprintf(w, response)
   242  	})
   243  }
   244  
   245  // HandleLoadbalancerGetSuccessfully sets up the test server to respond to a loadbalancer Get request.
   246  func HandleLoadbalancerGetSuccessfully(t *testing.T) {
   247  	th.Mux.HandleFunc("/v2/54030507-44f7-473c-9342-b4d14a95f692/elb/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab", func(w http.ResponseWriter, r *http.Request) {
   248  		th.TestMethod(t, r, "GET")
   249  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   250  		th.TestHeader(t, r, "Accept", "application/json")
   251  
   252  		fmt.Fprintf(w, SingleLoadbalancerBody)
   253  	})
   254  }
   255  
   256  // HandleLoadbalancerGetStatusesTree sets up the test server to respond to a loadbalancer Get statuses tree request.
   257  func HandleLoadbalancerGetStatusesTree(t *testing.T) {
   258  	th.Mux.HandleFunc("/v2/54030507-44f7-473c-9342-b4d14a95f692/elb/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab/statuses", func(w http.ResponseWriter, r *http.Request) {
   259  		th.TestMethod(t, r, "GET")
   260  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   261  		th.TestHeader(t, r, "Accept", "application/json")
   262  
   263  		fmt.Fprintf(w, LoadbalancerStatuesesTree)
   264  	})
   265  }
   266  
   267  // HandleLoadbalancerDeletionSuccessfully sets up the test server to respond to a loadbalancer deletion request.
   268  func HandleLoadbalancerDeletionSuccessfully(t *testing.T) {
   269  	th.Mux.HandleFunc("/v2/54030507-44f7-473c-9342-b4d14a95f692/elb/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab", func(w http.ResponseWriter, r *http.Request) {
   270  		th.TestMethod(t, r, "DELETE")
   271  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   272  
   273  		w.WriteHeader(http.StatusNoContent)
   274  	})
   275  }
   276  
   277  // HandleLoadbalancerUpdateSuccessfully sets up the test server to respond to a loadbalancer Update request.
   278  func HandleLoadbalancerUpdateSuccessfully(t *testing.T) {
   279  	th.Mux.HandleFunc("/v2/54030507-44f7-473c-9342-b4d14a95f692/elb/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab", func(w http.ResponseWriter, r *http.Request) {
   280  		th.TestMethod(t, r, "PUT")
   281  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   282  		th.TestHeader(t, r, "Accept", "application/json")
   283  		th.TestHeader(t, r, "Content-Type", "application/json")
   284  		th.TestJSONRequest(t, r, `{
   285  			"loadbalancer": {
   286  				"name": "NewLoadbalancerName"
   287  			}
   288  		}`)
   289  
   290  		fmt.Fprintf(w, PostUpdateLoadbalancerBody)
   291  	})
   292  }