blob: dafbda97067d50dfcce1ba97fc19a09b1545e25d [file] [log] [blame]
Jamie Hannafordfba65af2014-11-03 10:32:37 +01001package lbs
Jamie Hannaford186d4e22014-10-31 12:26:11 +01002
3import (
4 "testing"
5
6 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford89ebfc22014-11-03 16:27:47 +01007 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +01008 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannaford1c817312014-11-04 10:56:58 +01009 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010010 th "github.com/rackspace/gophercloud/testhelper"
11 "github.com/rackspace/gophercloud/testhelper/client"
12)
13
Jamie Hannaford07c06962014-10-31 16:42:03 +010014const (
15 id1 = 12345
16 id2 = 67890
17)
18
Jamie Hannaford186d4e22014-10-31 12:26:11 +010019func TestList(t *testing.T) {
20 th.SetupHTTP()
21 defer th.TeardownHTTP()
22
23 mockListLBResponse(t)
24
25 count := 0
26
27 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
28 count++
29 actual, err := ExtractLBs(page)
30 th.AssertNoErr(t, err)
31
32 expected := []LoadBalancer{
33 LoadBalancer{
34 Name: "lb-site1",
35 ID: 71,
36 Protocol: "HTTP",
37 Port: 80,
Jamie Hannaford46336282014-11-04 14:48:20 +010038 Algorithm: "RANDOM",
Jamie Hannaford186d4e22014-10-31 12:26:11 +010039 Status: ACTIVE,
40 NodeCount: 3,
Jamie Hannaford1c817312014-11-04 10:56:58 +010041 VIPs: []vips.VIP{
42 vips.VIP{
Jamie Hannaford186d4e22014-10-31 12:26:11 +010043 ID: 403,
44 Address: "206.55.130.1",
45 Type: "PUBLIC",
46 Version: "IPV4",
47 },
48 },
49 Created: Datetime{Time: "2010-11-30T03:23:42Z"},
50 Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
51 },
52 }
53
54 th.CheckDeepEquals(t, expected, actual)
55
56 return true, nil
57 })
58
59 th.AssertNoErr(t, err)
60 th.AssertEquals(t, 1, count)
61}
Jamie Hannaforde09b6822014-10-31 15:33:57 +010062
63func TestCreate(t *testing.T) {
64 th.SetupHTTP()
65 defer th.TeardownHTTP()
66
67 mockCreateLBResponse(t)
68
69 opts := CreateOpts{
70 Name: "a-new-loadbalancer",
71 Port: 80,
72 Protocol: "HTTP",
Jamie Hannaford1c817312014-11-04 10:56:58 +010073 VIPs: []vips.VIP{
74 vips.VIP{ID: 2341},
75 vips.VIP{ID: 900001},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010076 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +010077 Nodes: []nodes.Node{
78 nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010079 },
80 }
81
82 lb, err := Create(client.ServiceClient(), opts).Extract()
83 th.AssertNoErr(t, err)
84
85 expected := &LoadBalancer{
86 Name: "a-new-loadbalancer",
87 ID: 144,
88 Protocol: "HTTP",
89 HalfClosed: false,
90 Port: 83,
Jamie Hannaford46336282014-11-04 14:48:20 +010091 Algorithm: "RANDOM",
Jamie Hannaforde09b6822014-10-31 15:33:57 +010092 Status: BUILD,
93 Timeout: 30,
94 Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"},
Jamie Hannaford89ebfc22014-11-03 16:27:47 +010095 Nodes: []nodes.Node{
96 nodes.Node{
Jamie Hannaforde09b6822014-10-31 15:33:57 +010097 Address: "10.1.1.1",
98 ID: 653,
99 Port: 80,
100 Status: "ONLINE",
101 Condition: "ENABLED",
102 Weight: 1,
103 },
104 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100105 VIPs: []vips.VIP{
106 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100107 ID: 39,
108 Address: "206.10.10.210",
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100109 Type: vips.PUBLIC,
110 Version: vips.IPV4,
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100111 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100112 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100113 ID: 900001,
114 Address: "2001:4801:79f1:0002:711b:be4c:0000:0021",
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100115 Type: vips.PUBLIC,
116 Version: vips.IPV6,
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100117 },
118 },
119 Created: Datetime{Time: "2011-04-13T14:18:07Z"},
120 Updated: Datetime{Time: "2011-04-13T14:18:07Z"},
121 ConnectionLogging: ConnectionLogging{Enabled: false},
122 }
123
124 th.AssertDeepEquals(t, expected, lb)
125}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100126
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100127func TestBulkDelete(t *testing.T) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100128 th.SetupHTTP()
129 defer th.TeardownHTTP()
130
Jamie Hannaford07c06962014-10-31 16:42:03 +0100131 ids := []int{id1, id2}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100132
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100133 mockBatchDeleteLBResponse(t, ids)
134
135 err := BulkDelete(client.ServiceClient(), ids).ExtractErr()
136 th.AssertNoErr(t, err)
137}
138
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100139func TestDelete(t *testing.T) {
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100140 th.SetupHTTP()
141 defer th.TeardownHTTP()
142
Jamie Hannaford07c06962014-10-31 16:42:03 +0100143 mockDeleteLBResponse(t, id1)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100144
Jamie Hannaford07c06962014-10-31 16:42:03 +0100145 err := Delete(client.ServiceClient(), id1).ExtractErr()
Jamie Hannaford1c260332014-10-31 15:57:22 +0100146 th.AssertNoErr(t, err)
147}
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100148
149func TestGet(t *testing.T) {
Jamie Hannaford07c06962014-10-31 16:42:03 +0100150 th.SetupHTTP()
151 defer th.TeardownHTTP()
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100152
Jamie Hannaford07c06962014-10-31 16:42:03 +0100153 mockGetLBResponse(t, id1)
154
155 lb, err := Get(client.ServiceClient(), id1).Extract()
156
157 expected := &LoadBalancer{
158 Name: "sample-loadbalancer",
159 ID: 2000,
160 Protocol: "HTTP",
161 Port: 80,
Jamie Hannaford46336282014-11-04 14:48:20 +0100162 Algorithm: "RANDOM",
Jamie Hannaford07c06962014-10-31 16:42:03 +0100163 Status: ACTIVE,
164 Timeout: 30,
165 ConnectionLogging: ConnectionLogging{Enabled: true},
Jamie Hannaford1c817312014-11-04 10:56:58 +0100166 VIPs: []vips.VIP{
167 vips.VIP{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100168 ID: 1000,
169 Address: "206.10.10.210",
170 Type: "PUBLIC",
171 Version: "IPV4",
172 },
173 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100174 Nodes: []nodes.Node{
175 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100176 Address: "10.1.1.1",
177 ID: 1041,
178 Port: 80,
179 Status: "ONLINE",
180 Condition: "ENABLED",
181 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100182 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100183 Address: "10.1.1.2",
184 ID: 1411,
185 Port: 80,
186 Status: "ONLINE",
187 Condition: "ENABLED",
188 },
189 },
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100190 SessionPersistence: sessions.SessionPersistence{Type: "HTTP_COOKIE"},
Jamie Hannaford07c06962014-10-31 16:42:03 +0100191 ConnectionThrottle: ConnectionThrottle{
192 MinConns: 10,
193 MaxConns: 100,
194 MaxConnRate: 50,
195 RateInterval: 60,
196 },
197 Cluster: Cluster{Name: "c1.dfw1"},
198 Created: Datetime{Time: "2010-11-30T03:23:42Z"},
199 Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
200 SourceAddrs: SourceAddrs{
201 IPv4Public: "10.12.99.28",
202 IPv4Private: "10.0.0.0",
203 IPv6Public: "2001:4801:79f1:1::1/64",
204 },
205 }
206
207 th.AssertDeepEquals(t, expected, lb)
208 th.AssertNoErr(t, err)
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100209}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100210
211func TestUpdate(t *testing.T) {
212 th.SetupHTTP()
213 defer th.TeardownHTTP()
214
215 mockUpdateLBResponse(t, id1)
216
217 opts := UpdateOpts{
218 Name: "a-new-loadbalancer",
219 Protocol: "TCP",
220 HalfClosed: Enabled,
Jamie Hannaford46336282014-11-04 14:48:20 +0100221 Algorithm: "RANDOM",
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100222 Port: 8080,
223 Timeout: 100,
224 HTTPSRedirect: Disabled,
225 }
226
227 err := Update(client.ServiceClient(), id1, opts).ExtractErr()
228 th.AssertNoErr(t, err)
229}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100230
231func TestListProtocols(t *testing.T) {
232 th.SetupHTTP()
233 defer th.TeardownHTTP()
234
235 mockListProtocolsResponse(t)
236
237 count := 0
238
239 err := ListProtocols(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
240 count++
241 actual, err := ExtractProtocols(page)
242 th.AssertNoErr(t, err)
243
244 expected := []Protocol{
245 Protocol{Name: "DNS_TCP", Port: 53},
246 Protocol{Name: "DNS_UDP", Port: 53},
247 Protocol{Name: "FTP", Port: 21},
248 Protocol{Name: "HTTP", Port: 80},
249 Protocol{Name: "HTTPS", Port: 443},
250 Protocol{Name: "IMAPS", Port: 993},
251 Protocol{Name: "IMAPv4", Port: 143},
252 }
253
254 th.CheckDeepEquals(t, expected[0:7], actual)
255
256 return true, nil
257 })
258
259 th.AssertNoErr(t, err)
260 th.AssertEquals(t, 1, count)
261}
Jamie Hannaford46336282014-11-04 14:48:20 +0100262
263func TestListAlgorithms(t *testing.T) {
264 th.SetupHTTP()
265 defer th.TeardownHTTP()
266
267 mockListAlgorithmsResponse(t)
268
269 count := 0
270
271 err := ListAlgorithms(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
272 count++
273 actual, err := ExtractAlgorithms(page)
274 th.AssertNoErr(t, err)
275
276 expected := []Algorithm{
277 Algorithm{Name: "LEAST_CONNECTIONS"},
278 Algorithm{Name: "RANDOM"},
279 Algorithm{Name: "ROUND_ROBIN"},
280 Algorithm{Name: "WEIGHTED_LEAST_CONNECTIONS"},
281 Algorithm{Name: "WEIGHTED_ROUND_ROBIN"},
282 }
283
284 th.CheckDeepEquals(t, expected, actual)
285
286 return true, nil
287 })
288
289 th.AssertNoErr(t, err)
290 th.AssertEquals(t, 1, count)
291}