Jamie Hannaford | fba65af | 2014-11-03 10:32:37 +0100 | [diff] [blame] | 1 | package lbs |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 6 | "github.com/rackspace/gophercloud" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes" |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 9 | "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions" |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 10 | "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle" |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 11 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 12 | th "github.com/rackspace/gophercloud/testhelper" |
| 13 | "github.com/rackspace/gophercloud/testhelper/client" |
| 14 | ) |
| 15 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 16 | const ( |
| 17 | id1 = 12345 |
| 18 | id2 = 67890 |
| 19 | ) |
| 20 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 21 | func TestList(t *testing.T) { |
| 22 | th.SetupHTTP() |
| 23 | defer th.TeardownHTTP() |
| 24 | |
| 25 | mockListLBResponse(t) |
| 26 | |
| 27 | count := 0 |
| 28 | |
| 29 | err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 30 | count++ |
| 31 | actual, err := ExtractLBs(page) |
| 32 | th.AssertNoErr(t, err) |
| 33 | |
| 34 | expected := []LoadBalancer{ |
| 35 | LoadBalancer{ |
| 36 | Name: "lb-site1", |
| 37 | ID: 71, |
| 38 | Protocol: "HTTP", |
| 39 | Port: 80, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 40 | Algorithm: "RANDOM", |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 41 | Status: ACTIVE, |
| 42 | NodeCount: 3, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 43 | VIPs: []vips.VIP{ |
| 44 | vips.VIP{ |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 45 | ID: 403, |
| 46 | Address: "206.55.130.1", |
| 47 | Type: "PUBLIC", |
| 48 | Version: "IPV4", |
| 49 | }, |
| 50 | }, |
| 51 | Created: Datetime{Time: "2010-11-30T03:23:42Z"}, |
| 52 | Updated: Datetime{Time: "2010-11-30T03:23:44Z"}, |
| 53 | }, |
| 54 | } |
| 55 | |
| 56 | th.CheckDeepEquals(t, expected, actual) |
| 57 | |
| 58 | return true, nil |
| 59 | }) |
| 60 | |
| 61 | th.AssertNoErr(t, err) |
| 62 | th.AssertEquals(t, 1, count) |
| 63 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 64 | |
| 65 | func TestCreate(t *testing.T) { |
| 66 | th.SetupHTTP() |
| 67 | defer th.TeardownHTTP() |
| 68 | |
| 69 | mockCreateLBResponse(t) |
| 70 | |
| 71 | opts := CreateOpts{ |
| 72 | Name: "a-new-loadbalancer", |
| 73 | Port: 80, |
| 74 | Protocol: "HTTP", |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 75 | VIPs: []vips.VIP{ |
| 76 | vips.VIP{ID: 2341}, |
| 77 | vips.VIP{ID: 900001}, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 78 | }, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 79 | Nodes: []nodes.Node{ |
| 80 | nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"}, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 81 | }, |
| 82 | } |
| 83 | |
| 84 | lb, err := Create(client.ServiceClient(), opts).Extract() |
| 85 | th.AssertNoErr(t, err) |
| 86 | |
| 87 | expected := &LoadBalancer{ |
| 88 | Name: "a-new-loadbalancer", |
| 89 | ID: 144, |
| 90 | Protocol: "HTTP", |
| 91 | HalfClosed: false, |
| 92 | Port: 83, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 93 | Algorithm: "RANDOM", |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 94 | Status: BUILD, |
| 95 | Timeout: 30, |
| 96 | Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"}, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 97 | Nodes: []nodes.Node{ |
| 98 | nodes.Node{ |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 99 | Address: "10.1.1.1", |
| 100 | ID: 653, |
| 101 | Port: 80, |
| 102 | Status: "ONLINE", |
| 103 | Condition: "ENABLED", |
| 104 | Weight: 1, |
| 105 | }, |
| 106 | }, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 107 | VIPs: []vips.VIP{ |
| 108 | vips.VIP{ |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 109 | ID: 39, |
| 110 | Address: "206.10.10.210", |
Jamie Hannaford | d56375d | 2014-11-05 12:38:04 +0100 | [diff] [blame] | 111 | Type: vips.PUBLIC, |
| 112 | Version: vips.IPV4, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 113 | }, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 114 | vips.VIP{ |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 115 | ID: 900001, |
| 116 | Address: "2001:4801:79f1:0002:711b:be4c:0000:0021", |
Jamie Hannaford | d56375d | 2014-11-05 12:38:04 +0100 | [diff] [blame] | 117 | Type: vips.PUBLIC, |
| 118 | Version: vips.IPV6, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 119 | }, |
| 120 | }, |
| 121 | Created: Datetime{Time: "2011-04-13T14:18:07Z"}, |
| 122 | Updated: Datetime{Time: "2011-04-13T14:18:07Z"}, |
| 123 | ConnectionLogging: ConnectionLogging{Enabled: false}, |
| 124 | } |
| 125 | |
| 126 | th.AssertDeepEquals(t, expected, lb) |
| 127 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 128 | |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 129 | func TestBulkDelete(t *testing.T) { |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 130 | th.SetupHTTP() |
| 131 | defer th.TeardownHTTP() |
| 132 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 133 | ids := []int{id1, id2} |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 134 | |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 135 | mockBatchDeleteLBResponse(t, ids) |
| 136 | |
| 137 | err := BulkDelete(client.ServiceClient(), ids).ExtractErr() |
| 138 | th.AssertNoErr(t, err) |
| 139 | } |
| 140 | |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 141 | func TestDelete(t *testing.T) { |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 142 | th.SetupHTTP() |
| 143 | defer th.TeardownHTTP() |
| 144 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 145 | mockDeleteLBResponse(t, id1) |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 146 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 147 | err := Delete(client.ServiceClient(), id1).ExtractErr() |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 148 | th.AssertNoErr(t, err) |
| 149 | } |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 150 | |
| 151 | func TestGet(t *testing.T) { |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 152 | th.SetupHTTP() |
| 153 | defer th.TeardownHTTP() |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 154 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 155 | mockGetLBResponse(t, id1) |
| 156 | |
| 157 | lb, err := Get(client.ServiceClient(), id1).Extract() |
| 158 | |
| 159 | expected := &LoadBalancer{ |
| 160 | Name: "sample-loadbalancer", |
| 161 | ID: 2000, |
| 162 | Protocol: "HTTP", |
| 163 | Port: 80, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 164 | Algorithm: "RANDOM", |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 165 | Status: ACTIVE, |
| 166 | Timeout: 30, |
| 167 | ConnectionLogging: ConnectionLogging{Enabled: true}, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 168 | VIPs: []vips.VIP{ |
| 169 | vips.VIP{ |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 170 | ID: 1000, |
| 171 | Address: "206.10.10.210", |
| 172 | Type: "PUBLIC", |
| 173 | Version: "IPV4", |
| 174 | }, |
| 175 | }, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 176 | Nodes: []nodes.Node{ |
| 177 | nodes.Node{ |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 178 | Address: "10.1.1.1", |
| 179 | ID: 1041, |
| 180 | Port: 80, |
| 181 | Status: "ONLINE", |
| 182 | Condition: "ENABLED", |
| 183 | }, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 184 | nodes.Node{ |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 185 | Address: "10.1.1.2", |
| 186 | ID: 1411, |
| 187 | Port: 80, |
| 188 | Status: "ONLINE", |
| 189 | Condition: "ENABLED", |
| 190 | }, |
| 191 | }, |
Jamie Hannaford | 6bc93aa | 2014-11-06 12:37:52 +0100 | [diff] [blame] | 192 | SessionPersistence: sessions.SessionPersistence{Type: "HTTP_COOKIE"}, |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 193 | ConnectionThrottle: throttle.ConnectionThrottle{MaxConnections: 100}, |
| 194 | Cluster: Cluster{Name: "c1.dfw1"}, |
| 195 | Created: Datetime{Time: "2010-11-30T03:23:42Z"}, |
| 196 | Updated: Datetime{Time: "2010-11-30T03:23:44Z"}, |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 197 | SourceAddrs: SourceAddrs{ |
| 198 | IPv4Public: "10.12.99.28", |
| 199 | IPv4Private: "10.0.0.0", |
| 200 | IPv6Public: "2001:4801:79f1:1::1/64", |
| 201 | }, |
| 202 | } |
| 203 | |
| 204 | th.AssertDeepEquals(t, expected, lb) |
| 205 | th.AssertNoErr(t, err) |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 206 | } |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 207 | |
| 208 | func TestUpdate(t *testing.T) { |
| 209 | th.SetupHTTP() |
| 210 | defer th.TeardownHTTP() |
| 211 | |
| 212 | mockUpdateLBResponse(t, id1) |
| 213 | |
| 214 | opts := UpdateOpts{ |
| 215 | Name: "a-new-loadbalancer", |
| 216 | Protocol: "TCP", |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 217 | HalfClosed: gophercloud.Enabled, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 218 | Algorithm: "RANDOM", |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 219 | Port: 8080, |
| 220 | Timeout: 100, |
Jamie Hannaford | cfe2f28 | 2014-11-07 15:11:21 +0100 | [diff] [blame] | 221 | HTTPSRedirect: gophercloud.Disabled, |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | err := Update(client.ServiceClient(), id1, opts).ExtractErr() |
| 225 | th.AssertNoErr(t, err) |
| 226 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 227 | |
| 228 | func TestListProtocols(t *testing.T) { |
| 229 | th.SetupHTTP() |
| 230 | defer th.TeardownHTTP() |
| 231 | |
| 232 | mockListProtocolsResponse(t) |
| 233 | |
| 234 | count := 0 |
| 235 | |
| 236 | err := ListProtocols(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 237 | count++ |
| 238 | actual, err := ExtractProtocols(page) |
| 239 | th.AssertNoErr(t, err) |
| 240 | |
| 241 | expected := []Protocol{ |
| 242 | Protocol{Name: "DNS_TCP", Port: 53}, |
| 243 | Protocol{Name: "DNS_UDP", Port: 53}, |
| 244 | Protocol{Name: "FTP", Port: 21}, |
| 245 | Protocol{Name: "HTTP", Port: 80}, |
| 246 | Protocol{Name: "HTTPS", Port: 443}, |
| 247 | Protocol{Name: "IMAPS", Port: 993}, |
| 248 | Protocol{Name: "IMAPv4", Port: 143}, |
| 249 | } |
| 250 | |
| 251 | th.CheckDeepEquals(t, expected[0:7], actual) |
| 252 | |
| 253 | return true, nil |
| 254 | }) |
| 255 | |
| 256 | th.AssertNoErr(t, err) |
| 257 | th.AssertEquals(t, 1, count) |
| 258 | } |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 259 | |
| 260 | func TestListAlgorithms(t *testing.T) { |
| 261 | th.SetupHTTP() |
| 262 | defer th.TeardownHTTP() |
| 263 | |
| 264 | mockListAlgorithmsResponse(t) |
| 265 | |
| 266 | count := 0 |
| 267 | |
| 268 | err := ListAlgorithms(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 269 | count++ |
| 270 | actual, err := ExtractAlgorithms(page) |
| 271 | th.AssertNoErr(t, err) |
| 272 | |
| 273 | expected := []Algorithm{ |
| 274 | Algorithm{Name: "LEAST_CONNECTIONS"}, |
| 275 | Algorithm{Name: "RANDOM"}, |
| 276 | Algorithm{Name: "ROUND_ROBIN"}, |
| 277 | Algorithm{Name: "WEIGHTED_LEAST_CONNECTIONS"}, |
| 278 | Algorithm{Name: "WEIGHTED_ROUND_ROBIN"}, |
| 279 | } |
| 280 | |
| 281 | th.CheckDeepEquals(t, expected, actual) |
| 282 | |
| 283 | return true, nil |
| 284 | }) |
| 285 | |
| 286 | th.AssertNoErr(t, err) |
| 287 | th.AssertEquals(t, 1, count) |
| 288 | } |
Jamie Hannaford | d78bb35 | 2014-11-07 16:36:09 +0100 | [diff] [blame] | 289 | |
| 290 | func TestIsLoggingEnabled(t *testing.T) { |
| 291 | th.SetupHTTP() |
| 292 | defer th.TeardownHTTP() |
| 293 | |
| 294 | mockGetLoggingResponse(t, id1) |
| 295 | |
| 296 | res, err := IsLoggingEnabled(client.ServiceClient(), id1) |
| 297 | th.AssertNoErr(t, err) |
| 298 | th.AssertEquals(t, true, res) |
| 299 | } |
Jamie Hannaford | da45b42 | 2014-11-10 11:00:38 +0100 | [diff] [blame] | 300 | |
| 301 | func TestGetErrorPage(t *testing.T) { |
| 302 | th.SetupHTTP() |
| 303 | defer th.TeardownHTTP() |
| 304 | |
| 305 | mockGetErrorPageResponse(t, id1) |
| 306 | |
| 307 | content, err := GetErrorPage(client.ServiceClient(), id1).Extract() |
| 308 | th.AssertNoErr(t, err) |
| 309 | |
| 310 | expected := &ErrorPage{Content: "<html>DEFAULT ERROR PAGE</html>"} |
| 311 | th.AssertDeepEquals(t, expected, content) |
| 312 | } |
| 313 | |
| 314 | func TestSetErrorPage(t *testing.T) { |
| 315 | th.SetupHTTP() |
| 316 | defer th.TeardownHTTP() |
| 317 | |
| 318 | mockSetErrorPageResponse(t, id1) |
| 319 | |
| 320 | html := "<html>New error page</html>" |
| 321 | content, err := SetErrorPage(client.ServiceClient(), id1, html).Extract() |
| 322 | th.AssertNoErr(t, err) |
| 323 | |
| 324 | expected := &ErrorPage{Content: html} |
| 325 | th.AssertDeepEquals(t, expected, content) |
| 326 | } |
| 327 | |
| 328 | func TestDeleteErrorPage(t *testing.T) { |
| 329 | th.SetupHTTP() |
| 330 | defer th.TeardownHTTP() |
| 331 | |
| 332 | mockDeleteErrorPageResponse(t, id1) |
| 333 | |
| 334 | err := DeleteErrorPage(client.ServiceClient(), id1).ExtractErr() |
| 335 | th.AssertNoErr(t, err) |
| 336 | } |
Jamie Hannaford | 3da6528 | 2014-11-10 11:36:16 +0100 | [diff] [blame] | 337 | |
| 338 | func TestGetStats(t *testing.T) { |
| 339 | th.SetupHTTP() |
| 340 | defer th.TeardownHTTP() |
| 341 | |
| 342 | mockGetStatsResponse(t, id1) |
| 343 | |
| 344 | content, err := GetStats(client.ServiceClient(), id1).Extract() |
| 345 | th.AssertNoErr(t, err) |
| 346 | |
| 347 | expected := &Stats{ |
| 348 | ConnectTimeout: 10, |
| 349 | ConnectError: 20, |
| 350 | ConnectFailure: 30, |
| 351 | DataTimedOut: 40, |
| 352 | KeepAliveTimedOut: 50, |
| 353 | MaxConnections: 60, |
| 354 | CurrentConnections: 40, |
| 355 | SSLConnectTimeout: 10, |
| 356 | SSLConnectError: 20, |
| 357 | SSLConnectFailure: 30, |
| 358 | SSLDataTimedOut: 40, |
| 359 | SSLKeepAliveTimedOut: 50, |
| 360 | SSLMaxConnections: 60, |
| 361 | SSLCurrentConnections: 40, |
| 362 | } |
| 363 | th.AssertDeepEquals(t, expected, content) |
| 364 | } |