github.com/gophercloud/gophercloud@v1.11.0/openstack/loadbalancer/v2/loadbalancers/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/l7policies"
     7  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/listeners"
     8  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/monitors"
     9  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/pools"
    10  
    11  	"github.com/gophercloud/gophercloud"
    12  	"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
    13  	fake "github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/testhelper"
    14  	"github.com/gophercloud/gophercloud/pagination"
    15  	th "github.com/gophercloud/gophercloud/testhelper"
    16  )
    17  
    18  func TestListLoadbalancers(t *testing.T) {
    19  	th.SetupHTTP()
    20  	defer th.TeardownHTTP()
    21  	HandleLoadbalancerListSuccessfully(t)
    22  
    23  	pages := 0
    24  	err := loadbalancers.List(fake.ServiceClient(), loadbalancers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    25  		pages++
    26  
    27  		actual, err := loadbalancers.ExtractLoadBalancers(page)
    28  		if err != nil {
    29  			return false, err
    30  		}
    31  
    32  		if len(actual) != 2 {
    33  			t.Fatalf("Expected 2 loadbalancers, got %d", len(actual))
    34  		}
    35  		th.CheckDeepEquals(t, LoadbalancerWeb, actual[0])
    36  		th.CheckDeepEquals(t, LoadbalancerDb, actual[1])
    37  
    38  		return true, nil
    39  	})
    40  
    41  	th.AssertNoErr(t, err)
    42  
    43  	if pages != 1 {
    44  		t.Errorf("Expected 1 page, saw %d", pages)
    45  	}
    46  }
    47  
    48  func TestListAllLoadbalancers(t *testing.T) {
    49  	th.SetupHTTP()
    50  	defer th.TeardownHTTP()
    51  	HandleLoadbalancerListSuccessfully(t)
    52  
    53  	allPages, err := loadbalancers.List(fake.ServiceClient(), loadbalancers.ListOpts{}).AllPages()
    54  	th.AssertNoErr(t, err)
    55  	actual, err := loadbalancers.ExtractLoadBalancers(allPages)
    56  	th.AssertNoErr(t, err)
    57  	th.CheckDeepEquals(t, LoadbalancerWeb, actual[0])
    58  	th.CheckDeepEquals(t, LoadbalancerDb, actual[1])
    59  }
    60  
    61  func TestCreateLoadbalancer(t *testing.T) {
    62  	th.SetupHTTP()
    63  	defer th.TeardownHTTP()
    64  	HandleLoadbalancerCreationSuccessfully(t, SingleLoadbalancerBody)
    65  
    66  	actual, err := loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{
    67  		Name:         "db_lb",
    68  		AdminStateUp: gophercloud.Enabled,
    69  		VipPortID:    "2bf413c8-41a9-4477-b505-333d5cbe8b55",
    70  		VipSubnetID:  "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    71  		VipAddress:   "10.30.176.48",
    72  		FlavorID:     "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4",
    73  		Provider:     "haproxy",
    74  		Tags:         []string{"test", "stage"},
    75  	}).Extract()
    76  	th.AssertNoErr(t, err)
    77  
    78  	th.CheckDeepEquals(t, LoadbalancerDb, *actual)
    79  }
    80  
    81  func TestCreateFullyPopulatedLoadbalancer(t *testing.T) {
    82  	th.SetupHTTP()
    83  	defer th.TeardownHTTP()
    84  	HandleFullyPopulatedLoadbalancerCreationSuccessfully(t, PostFullyPopulatedLoadbalancerBody)
    85  
    86  	actual, err := loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{
    87  		Name:         "db_lb",
    88  		AdminStateUp: gophercloud.Enabled,
    89  		VipPortID:    "2bf413c8-41a9-4477-b505-333d5cbe8b55",
    90  		VipSubnetID:  "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    91  		VipAddress:   "10.30.176.48",
    92  		FlavorID:     "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4",
    93  		Provider:     "octavia",
    94  		Tags:         []string{"test", "stage"},
    95  		Listeners: []listeners.CreateOpts{{
    96  			Protocol:     "HTTP",
    97  			ProtocolPort: 8080,
    98  			Name:         "redirect_listener",
    99  			L7Policies: []l7policies.CreateOpts{{
   100  				Name:        "redirect-example.com",
   101  				Action:      l7policies.ActionRedirectToURL,
   102  				RedirectURL: "http://www.example.com",
   103  				Rules: []l7policies.CreateRuleOpts{{
   104  					RuleType:    l7policies.TypePath,
   105  					CompareType: l7policies.CompareTypeRegex,
   106  					Value:       "/images*",
   107  				}},
   108  			}},
   109  			DefaultPool: &pools.CreateOpts{
   110  				LBMethod: pools.LBMethodRoundRobin,
   111  				Protocol: "HTTP",
   112  				Name:     "Example pool",
   113  				Members: []pools.BatchUpdateMemberOpts{{
   114  					Address:      "192.0.2.51",
   115  					ProtocolPort: 80,
   116  				}, {
   117  					Address:      "192.0.2.52",
   118  					ProtocolPort: 80,
   119  				}},
   120  				Monitor: &monitors.CreateOpts{
   121  					Name:           "db",
   122  					Type:           "HTTP",
   123  					Delay:          3,
   124  					Timeout:        1,
   125  					MaxRetries:     2,
   126  					MaxRetriesDown: 3,
   127  					URLPath:        "/index.html",
   128  					HTTPMethod:     "GET",
   129  					ExpectedCodes:  "200",
   130  				},
   131  			},
   132  		}},
   133  	}).Extract()
   134  	th.AssertNoErr(t, err)
   135  
   136  	th.CheckDeepEquals(t, FullyPopulatedLoadBalancerDb, *actual)
   137  }
   138  
   139  func TestGetLoadbalancer(t *testing.T) {
   140  	th.SetupHTTP()
   141  	defer th.TeardownHTTP()
   142  	HandleLoadbalancerGetSuccessfully(t)
   143  
   144  	client := fake.ServiceClient()
   145  	actual, err := loadbalancers.Get(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").Extract()
   146  	if err != nil {
   147  		t.Fatalf("Unexpected Get error: %v", err)
   148  	}
   149  
   150  	th.CheckDeepEquals(t, LoadbalancerDb, *actual)
   151  }
   152  
   153  func TestGetLoadbalancerStatusesTree(t *testing.T) {
   154  	th.SetupHTTP()
   155  	defer th.TeardownHTTP()
   156  	HandleLoadbalancerGetStatusesTree(t)
   157  
   158  	client := fake.ServiceClient()
   159  	actual, err := loadbalancers.GetStatuses(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").Extract()
   160  	if err != nil {
   161  		t.Fatalf("Unexpected Get error: %v", err)
   162  	}
   163  
   164  	th.CheckDeepEquals(t, LoadbalancerStatusesTree, *actual)
   165  }
   166  
   167  func TestDeleteLoadbalancer(t *testing.T) {
   168  	th.SetupHTTP()
   169  	defer th.TeardownHTTP()
   170  	HandleLoadbalancerDeletionSuccessfully(t)
   171  
   172  	res := loadbalancers.Delete(fake.ServiceClient(), "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", nil)
   173  	th.AssertNoErr(t, res.Err)
   174  }
   175  
   176  func TestUpdateLoadbalancer(t *testing.T) {
   177  	th.SetupHTTP()
   178  	defer th.TeardownHTTP()
   179  	HandleLoadbalancerUpdateSuccessfully(t)
   180  
   181  	client := fake.ServiceClient()
   182  	name := "NewLoadbalancerName"
   183  	tags := []string{"test"}
   184  	actual, err := loadbalancers.Update(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", loadbalancers.UpdateOpts{
   185  		Name: &name,
   186  		Tags: &tags,
   187  	}).Extract()
   188  	if err != nil {
   189  		t.Fatalf("Unexpected Update error: %v", err)
   190  	}
   191  
   192  	th.CheckDeepEquals(t, LoadbalancerUpdated, *actual)
   193  }
   194  
   195  func TestCascadingDeleteLoadbalancer(t *testing.T) {
   196  	th.SetupHTTP()
   197  	defer th.TeardownHTTP()
   198  	HandleLoadbalancerDeletionSuccessfully(t)
   199  
   200  	sc := fake.ServiceClient()
   201  	deleteOpts := loadbalancers.DeleteOpts{
   202  		Cascade: true,
   203  	}
   204  
   205  	query, err := deleteOpts.ToLoadBalancerDeleteQuery()
   206  	th.AssertNoErr(t, err)
   207  	th.AssertEquals(t, query, "?cascade=true")
   208  
   209  	err = loadbalancers.Delete(sc, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", deleteOpts).ExtractErr()
   210  	th.AssertNoErr(t, err)
   211  }
   212  
   213  func TestGetLoadbalancerStatsTree(t *testing.T) {
   214  	th.SetupHTTP()
   215  	defer th.TeardownHTTP()
   216  	HandleLoadbalancerGetStatsTree(t)
   217  
   218  	client := fake.ServiceClient()
   219  	actual, err := loadbalancers.GetStats(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").Extract()
   220  	if err != nil {
   221  		t.Fatalf("Unexpected Get error: %v", err)
   222  	}
   223  
   224  	th.CheckDeepEquals(t, LoadbalancerStatsTree, *actual)
   225  }
   226  
   227  func TestFailoverLoadbalancer(t *testing.T) {
   228  	th.SetupHTTP()
   229  	defer th.TeardownHTTP()
   230  	HandleLoadbalancerFailoverSuccessfully(t)
   231  
   232  	res := loadbalancers.Failover(fake.ServiceClient(), "36e08a3e-a78f-4b40-a229-1e7e23eee1ab")
   233  	th.AssertNoErr(t, res.Err)
   234  }