github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas_v2/loadbalancers/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  	fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common"
     8  	"github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas_v2/loadbalancers"
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  )
    12  
    13  func TestListLoadbalancers(t *testing.T) {
    14  	th.SetupHTTP()
    15  	defer th.TeardownHTTP()
    16  	HandleLoadbalancerListSuccessfully(t)
    17  
    18  	pages := 0
    19  	err := loadbalancers.List(fake.ServiceClient(), loadbalancers.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
    20  		pages++
    21  
    22  		actual, err := loadbalancers.ExtractLoadBalancers(page)
    23  		if err != nil {
    24  			return false, err
    25  		}
    26  
    27  		if len(actual) != 2 {
    28  			t.Fatalf("Expected 2 loadbalancers, got %d", len(actual))
    29  		}
    30  		th.CheckDeepEquals(t, LoadbalancerWeb, actual[0])
    31  		th.CheckDeepEquals(t, LoadbalancerDb, actual[1])
    32  
    33  		return true, nil
    34  	})
    35  
    36  	th.AssertNoErr(t, err)
    37  
    38  	if pages != 1 {
    39  		t.Errorf("Expected 1 page, saw %d", pages)
    40  	}
    41  }
    42  
    43  func TestListAllLoadbalancers(t *testing.T) {
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  	HandleLoadbalancerListSuccessfully(t)
    47  
    48  	allPages, err := loadbalancers.List(fake.ServiceClient(), loadbalancers.ListOpts{}).AllPages()
    49  	th.AssertNoErr(t, err)
    50  	actual, err := loadbalancers.ExtractLoadBalancers(allPages)
    51  	th.AssertNoErr(t, err)
    52  	th.CheckDeepEquals(t, LoadbalancerWeb, actual[0])
    53  	th.CheckDeepEquals(t, LoadbalancerDb, actual[1])
    54  }
    55  
    56  func TestCreateLoadbalancer(t *testing.T) {
    57  	th.SetupHTTP()
    58  	defer th.TeardownHTTP()
    59  	HandleLoadbalancerCreationSuccessfully(t, SingleLoadbalancerBody)
    60  
    61  	actual, err := loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{
    62  		Name:         "db_lb",
    63  		AdminStateUp: golangsdk.Enabled,
    64  		VipSubnetID:  "9cedb85d-0759-4898-8a4b-fa5a5ea10086",
    65  		VipAddress:   "10.30.176.48",
    66  		Flavor:       "medium",
    67  		Provider:     "haproxy",
    68  	}).Extract()
    69  	th.AssertNoErr(t, err)
    70  
    71  	th.CheckDeepEquals(t, LoadbalancerDb, *actual)
    72  }
    73  
    74  func TestRequiredCreateOpts(t *testing.T) {
    75  	res := loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{})
    76  	if res.Err == nil {
    77  		t.Fatalf("Expected error, got none")
    78  	}
    79  	res = loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{Name: "foo"})
    80  	if res.Err == nil {
    81  		t.Fatalf("Expected error, got none")
    82  	}
    83  	res = loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{Name: "foo", Description: "bar"})
    84  	if res.Err == nil {
    85  		t.Fatalf("Expected error, got none")
    86  	}
    87  	res = loadbalancers.Create(fake.ServiceClient(), loadbalancers.CreateOpts{Name: "foo", Description: "bar", VipAddress: "bar"})
    88  	if res.Err == nil {
    89  		t.Fatalf("Expected error, got none")
    90  	}
    91  }
    92  
    93  func TestGetLoadbalancer(t *testing.T) {
    94  	th.SetupHTTP()
    95  	defer th.TeardownHTTP()
    96  	HandleLoadbalancerGetSuccessfully(t)
    97  
    98  	client := fake.ServiceClient()
    99  	actual, err := loadbalancers.Get(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").Extract()
   100  	if err != nil {
   101  		t.Fatalf("Unexpected Get error: %v", err)
   102  	}
   103  
   104  	th.CheckDeepEquals(t, LoadbalancerDb, *actual)
   105  }
   106  
   107  func TestGetLoadbalancerStatusesTree(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  	HandleLoadbalancerGetStatusesTree(t)
   111  
   112  	client := fake.ServiceClient()
   113  	actual, err := loadbalancers.GetStatuses(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").Extract()
   114  	if err != nil {
   115  		t.Fatalf("Unexpected Get error: %v", err)
   116  	}
   117  
   118  	th.CheckDeepEquals(t, LoadbalancerStatusesTree, *(actual.Loadbalancer))
   119  }
   120  
   121  func TestDeleteLoadbalancer(t *testing.T) {
   122  	th.SetupHTTP()
   123  	defer th.TeardownHTTP()
   124  	HandleLoadbalancerDeletionSuccessfully(t)
   125  
   126  	res := loadbalancers.Delete(fake.ServiceClient(), "36e08a3e-a78f-4b40-a229-1e7e23eee1ab")
   127  	th.AssertNoErr(t, res.Err)
   128  }
   129  
   130  func TestUpdateLoadbalancer(t *testing.T) {
   131  	th.SetupHTTP()
   132  	defer th.TeardownHTTP()
   133  	HandleLoadbalancerUpdateSuccessfully(t)
   134  
   135  	client := fake.ServiceClient()
   136  	actual, err := loadbalancers.Update(client, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", loadbalancers.UpdateOpts{
   137  		Name: "NewLoadbalancerName",
   138  	}).Extract()
   139  	if err != nil {
   140  		t.Fatalf("Unexpected Update error: %v", err)
   141  	}
   142  
   143  	th.CheckDeepEquals(t, LoadbalancerUpdated, *actual)
   144  }
   145  
   146  func TestCascadingDeleteLoadbalancer(t *testing.T) {
   147  	th.SetupHTTP()
   148  	defer th.TeardownHTTP()
   149  	HandleLoadbalancerDeletionSuccessfully(t)
   150  
   151  	sc := fake.ServiceClient()
   152  	sc.Type = "network"
   153  	err := loadbalancers.CascadingDelete(sc, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").ExtractErr()
   154  	if err == nil {
   155  		t.Fatalf("expected error running CascadingDelete with Neutron service client but didn't get one")
   156  	}
   157  
   158  	sc.Type = "load-balancer"
   159  	err = loadbalancers.CascadingDelete(sc, "36e08a3e-a78f-4b40-a229-1e7e23eee1ab").ExtractErr()
   160  	th.AssertNoErr(t, err)
   161  }