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 | |
| 6 | "github.com/rackspace/gophercloud/pagination" |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes" |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 8 | "github.com/rackspace/gophercloud/rackspace/lb/v1/vips" |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 9 | th "github.com/rackspace/gophercloud/testhelper" |
| 10 | "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | ) |
| 12 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 13 | const ( |
| 14 | id1 = 12345 |
| 15 | id2 = 67890 |
| 16 | ) |
| 17 | |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 18 | func TestList(t *testing.T) { |
| 19 | th.SetupHTTP() |
| 20 | defer th.TeardownHTTP() |
| 21 | |
| 22 | mockListLBResponse(t) |
| 23 | |
| 24 | count := 0 |
| 25 | |
| 26 | err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { |
| 27 | count++ |
| 28 | actual, err := ExtractLBs(page) |
| 29 | th.AssertNoErr(t, err) |
| 30 | |
| 31 | expected := []LoadBalancer{ |
| 32 | LoadBalancer{ |
| 33 | Name: "lb-site1", |
| 34 | ID: 71, |
| 35 | Protocol: "HTTP", |
| 36 | Port: 80, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 37 | Algorithm: "RANDOM", |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 38 | Status: ACTIVE, |
| 39 | NodeCount: 3, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 40 | VIPs: []vips.VIP{ |
| 41 | vips.VIP{ |
Jamie Hannaford | 186d4e2 | 2014-10-31 12:26:11 +0100 | [diff] [blame] | 42 | ID: 403, |
| 43 | Address: "206.55.130.1", |
| 44 | Type: "PUBLIC", |
| 45 | Version: "IPV4", |
| 46 | }, |
| 47 | }, |
| 48 | Created: Datetime{Time: "2010-11-30T03:23:42Z"}, |
| 49 | Updated: Datetime{Time: "2010-11-30T03:23:44Z"}, |
| 50 | }, |
| 51 | } |
| 52 | |
| 53 | th.CheckDeepEquals(t, expected, actual) |
| 54 | |
| 55 | return true, nil |
| 56 | }) |
| 57 | |
| 58 | th.AssertNoErr(t, err) |
| 59 | th.AssertEquals(t, 1, count) |
| 60 | } |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 61 | |
| 62 | func TestCreate(t *testing.T) { |
| 63 | th.SetupHTTP() |
| 64 | defer th.TeardownHTTP() |
| 65 | |
| 66 | mockCreateLBResponse(t) |
| 67 | |
| 68 | opts := CreateOpts{ |
| 69 | Name: "a-new-loadbalancer", |
| 70 | Port: 80, |
| 71 | Protocol: "HTTP", |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 72 | VIPs: []vips.VIP{ |
| 73 | vips.VIP{ID: 2341}, |
| 74 | vips.VIP{ID: 900001}, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 75 | }, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 76 | Nodes: []nodes.Node{ |
| 77 | nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"}, |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 78 | }, |
| 79 | } |
| 80 | |
| 81 | lb, err := Create(client.ServiceClient(), opts).Extract() |
| 82 | th.AssertNoErr(t, err) |
| 83 | |
| 84 | expected := &LoadBalancer{ |
| 85 | Name: "a-new-loadbalancer", |
| 86 | ID: 144, |
| 87 | Protocol: "HTTP", |
| 88 | HalfClosed: false, |
| 89 | Port: 83, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 90 | Algorithm: "RANDOM", |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 91 | Status: BUILD, |
| 92 | Timeout: 30, |
| 93 | Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"}, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 94 | Nodes: []nodes.Node{ |
| 95 | nodes.Node{ |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 96 | Address: "10.1.1.1", |
| 97 | ID: 653, |
| 98 | Port: 80, |
| 99 | Status: "ONLINE", |
| 100 | Condition: "ENABLED", |
| 101 | Weight: 1, |
| 102 | }, |
| 103 | }, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 104 | VIPs: []vips.VIP{ |
| 105 | vips.VIP{ |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 106 | ID: 39, |
| 107 | Address: "206.10.10.210", |
| 108 | Type: "PUBLIC", |
| 109 | Version: "IPV4", |
| 110 | }, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 111 | vips.VIP{ |
Jamie Hannaford | e09b682 | 2014-10-31 15:33:57 +0100 | [diff] [blame] | 112 | ID: 900001, |
| 113 | Address: "2001:4801:79f1:0002:711b:be4c:0000:0021", |
| 114 | Type: "PUBLIC", |
| 115 | Version: "IPV6", |
| 116 | }, |
| 117 | }, |
| 118 | Created: Datetime{Time: "2011-04-13T14:18:07Z"}, |
| 119 | Updated: Datetime{Time: "2011-04-13T14:18:07Z"}, |
| 120 | ConnectionLogging: ConnectionLogging{Enabled: false}, |
| 121 | } |
| 122 | |
| 123 | th.AssertDeepEquals(t, expected, lb) |
| 124 | } |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 125 | |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 126 | func TestBulkDelete(t *testing.T) { |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 127 | th.SetupHTTP() |
| 128 | defer th.TeardownHTTP() |
| 129 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 130 | ids := []int{id1, id2} |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 131 | |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 132 | mockBatchDeleteLBResponse(t, ids) |
| 133 | |
| 134 | err := BulkDelete(client.ServiceClient(), ids).ExtractErr() |
| 135 | th.AssertNoErr(t, err) |
| 136 | } |
| 137 | |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 138 | func TestDelete(t *testing.T) { |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 139 | th.SetupHTTP() |
| 140 | defer th.TeardownHTTP() |
| 141 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 142 | mockDeleteLBResponse(t, id1) |
Jamie Hannaford | 5f95e6a | 2014-10-31 16:13:44 +0100 | [diff] [blame] | 143 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 144 | err := Delete(client.ServiceClient(), id1).ExtractErr() |
Jamie Hannaford | 1c26033 | 2014-10-31 15:57:22 +0100 | [diff] [blame] | 145 | th.AssertNoErr(t, err) |
| 146 | } |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 147 | |
| 148 | func TestGet(t *testing.T) { |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 149 | th.SetupHTTP() |
| 150 | defer th.TeardownHTTP() |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 151 | |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 152 | mockGetLBResponse(t, id1) |
| 153 | |
| 154 | lb, err := Get(client.ServiceClient(), id1).Extract() |
| 155 | |
| 156 | expected := &LoadBalancer{ |
| 157 | Name: "sample-loadbalancer", |
| 158 | ID: 2000, |
| 159 | Protocol: "HTTP", |
| 160 | Port: 80, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 161 | Algorithm: "RANDOM", |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 162 | Status: ACTIVE, |
| 163 | Timeout: 30, |
| 164 | ConnectionLogging: ConnectionLogging{Enabled: true}, |
Jamie Hannaford | 1c81731 | 2014-11-04 10:56:58 +0100 | [diff] [blame] | 165 | VIPs: []vips.VIP{ |
| 166 | vips.VIP{ |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 167 | ID: 1000, |
| 168 | Address: "206.10.10.210", |
| 169 | Type: "PUBLIC", |
| 170 | Version: "IPV4", |
| 171 | }, |
| 172 | }, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 173 | Nodes: []nodes.Node{ |
| 174 | nodes.Node{ |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 175 | Address: "10.1.1.1", |
| 176 | ID: 1041, |
| 177 | Port: 80, |
| 178 | Status: "ONLINE", |
| 179 | Condition: "ENABLED", |
| 180 | }, |
Jamie Hannaford | 89ebfc2 | 2014-11-03 16:27:47 +0100 | [diff] [blame] | 181 | nodes.Node{ |
Jamie Hannaford | 07c0696 | 2014-10-31 16:42:03 +0100 | [diff] [blame] | 182 | Address: "10.1.1.2", |
| 183 | ID: 1411, |
| 184 | Port: 80, |
| 185 | Status: "ONLINE", |
| 186 | Condition: "ENABLED", |
| 187 | }, |
| 188 | }, |
| 189 | SessionPersistence: SessionPersistence{Type: "HTTP_COOKIE"}, |
| 190 | ConnectionThrottle: ConnectionThrottle{ |
| 191 | MinConns: 10, |
| 192 | MaxConns: 100, |
| 193 | MaxConnRate: 50, |
| 194 | RateInterval: 60, |
| 195 | }, |
| 196 | Cluster: Cluster{Name: "c1.dfw1"}, |
| 197 | Created: Datetime{Time: "2010-11-30T03:23:42Z"}, |
| 198 | Updated: Datetime{Time: "2010-11-30T03:23:44Z"}, |
| 199 | SourceAddrs: SourceAddrs{ |
| 200 | IPv4Public: "10.12.99.28", |
| 201 | IPv4Private: "10.0.0.0", |
| 202 | IPv6Public: "2001:4801:79f1:1::1/64", |
| 203 | }, |
| 204 | } |
| 205 | |
| 206 | th.AssertDeepEquals(t, expected, lb) |
| 207 | th.AssertNoErr(t, err) |
Jamie Hannaford | fcd2259 | 2014-10-31 16:15:34 +0100 | [diff] [blame] | 208 | } |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 209 | |
| 210 | func TestUpdate(t *testing.T) { |
| 211 | th.SetupHTTP() |
| 212 | defer th.TeardownHTTP() |
| 213 | |
| 214 | mockUpdateLBResponse(t, id1) |
| 215 | |
| 216 | opts := UpdateOpts{ |
| 217 | Name: "a-new-loadbalancer", |
| 218 | Protocol: "TCP", |
| 219 | HalfClosed: Enabled, |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 220 | Algorithm: "RANDOM", |
Jamie Hannaford | 76fcc83 | 2014-10-31 16:56:50 +0100 | [diff] [blame] | 221 | Port: 8080, |
| 222 | Timeout: 100, |
| 223 | HTTPSRedirect: Disabled, |
| 224 | } |
| 225 | |
| 226 | err := Update(client.ServiceClient(), id1, opts).ExtractErr() |
| 227 | th.AssertNoErr(t, err) |
| 228 | } |
Jamie Hannaford | 4ab9aea | 2014-11-04 14:38:06 +0100 | [diff] [blame] | 229 | |
| 230 | func TestListProtocols(t *testing.T) { |
| 231 | th.SetupHTTP() |
| 232 | defer th.TeardownHTTP() |
| 233 | |
| 234 | mockListProtocolsResponse(t) |
| 235 | |
| 236 | count := 0 |
| 237 | |
| 238 | err := ListProtocols(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 239 | count++ |
| 240 | actual, err := ExtractProtocols(page) |
| 241 | th.AssertNoErr(t, err) |
| 242 | |
| 243 | expected := []Protocol{ |
| 244 | Protocol{Name: "DNS_TCP", Port: 53}, |
| 245 | Protocol{Name: "DNS_UDP", Port: 53}, |
| 246 | Protocol{Name: "FTP", Port: 21}, |
| 247 | Protocol{Name: "HTTP", Port: 80}, |
| 248 | Protocol{Name: "HTTPS", Port: 443}, |
| 249 | Protocol{Name: "IMAPS", Port: 993}, |
| 250 | Protocol{Name: "IMAPv4", Port: 143}, |
| 251 | } |
| 252 | |
| 253 | th.CheckDeepEquals(t, expected[0:7], actual) |
| 254 | |
| 255 | return true, nil |
| 256 | }) |
| 257 | |
| 258 | th.AssertNoErr(t, err) |
| 259 | th.AssertEquals(t, 1, count) |
| 260 | } |
Jamie Hannaford | 4633628 | 2014-11-04 14:48:20 +0100 | [diff] [blame] | 261 | |
| 262 | func TestListAlgorithms(t *testing.T) { |
| 263 | th.SetupHTTP() |
| 264 | defer th.TeardownHTTP() |
| 265 | |
| 266 | mockListAlgorithmsResponse(t) |
| 267 | |
| 268 | count := 0 |
| 269 | |
| 270 | err := ListAlgorithms(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) { |
| 271 | count++ |
| 272 | actual, err := ExtractAlgorithms(page) |
| 273 | th.AssertNoErr(t, err) |
| 274 | |
| 275 | expected := []Algorithm{ |
| 276 | Algorithm{Name: "LEAST_CONNECTIONS"}, |
| 277 | Algorithm{Name: "RANDOM"}, |
| 278 | Algorithm{Name: "ROUND_ROBIN"}, |
| 279 | Algorithm{Name: "WEIGHTED_LEAST_CONNECTIONS"}, |
| 280 | Algorithm{Name: "WEIGHTED_ROUND_ROBIN"}, |
| 281 | } |
| 282 | |
| 283 | th.CheckDeepEquals(t, expected, actual) |
| 284 | |
| 285 | return true, nil |
| 286 | }) |
| 287 | |
| 288 | th.AssertNoErr(t, err) |
| 289 | th.AssertEquals(t, 1, count) |
| 290 | } |