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