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