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