blob: 32af5c30d2e35884e268666eb3869fb7424fa2eb [file] [log] [blame]
Jamie Hannafordfba65af2014-11-03 10:32:37 +01001package lbs
Jamie Hannaford186d4e22014-10-31 12:26:11 +01002
3import (
4 "testing"
5
Jamie Hannafordcfe2f282014-11-07 15:11:21 +01006 "github.com/rackspace/gophercloud"
Jamie Hannaford186d4e22014-10-31 12:26:11 +01007 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford89ebfc22014-11-03 16:27:47 +01008 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +01009 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle"
Jamie Hannaford1c817312014-11-04 10:56:58 +010011 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010012 th "github.com/rackspace/gophercloud/testhelper"
13 "github.com/rackspace/gophercloud/testhelper/client"
14)
15
Jamie Hannaford07c06962014-10-31 16:42:03 +010016const (
17 id1 = 12345
18 id2 = 67890
19)
20
Jamie Hannaford186d4e22014-10-31 12:26:11 +010021func 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 Hannaford46336282014-11-04 14:48:20 +010040 Algorithm: "RANDOM",
Jamie Hannaford186d4e22014-10-31 12:26:11 +010041 Status: ACTIVE,
42 NodeCount: 3,
Jamie Hannaford1c817312014-11-04 10:56:58 +010043 VIPs: []vips.VIP{
44 vips.VIP{
Jamie Hannaford186d4e22014-10-31 12:26:11 +010045 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 Hannaforde09b6822014-10-31 15:33:57 +010064
65func 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 Hannaford1c817312014-11-04 10:56:58 +010075 VIPs: []vips.VIP{
76 vips.VIP{ID: 2341},
77 vips.VIP{ID: 900001},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010078 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +010079 Nodes: []nodes.Node{
80 nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010081 },
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 Hannaford46336282014-11-04 14:48:20 +010093 Algorithm: "RANDOM",
Jamie Hannaforde09b6822014-10-31 15:33:57 +010094 Status: BUILD,
95 Timeout: 30,
96 Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"},
Jamie Hannaford89ebfc22014-11-03 16:27:47 +010097 Nodes: []nodes.Node{
98 nodes.Node{
Jamie Hannaforde09b6822014-10-31 15:33:57 +010099 Address: "10.1.1.1",
100 ID: 653,
101 Port: 80,
102 Status: "ONLINE",
103 Condition: "ENABLED",
104 Weight: 1,
105 },
106 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100107 VIPs: []vips.VIP{
108 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100109 ID: 39,
110 Address: "206.10.10.210",
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100111 Type: vips.PUBLIC,
112 Version: vips.IPV4,
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100113 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100114 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100115 ID: 900001,
116 Address: "2001:4801:79f1:0002:711b:be4c:0000:0021",
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100117 Type: vips.PUBLIC,
118 Version: vips.IPV6,
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100119 },
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 Hannaford1c260332014-10-31 15:57:22 +0100128
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100129func TestBulkDelete(t *testing.T) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100130 th.SetupHTTP()
131 defer th.TeardownHTTP()
132
Jamie Hannaford07c06962014-10-31 16:42:03 +0100133 ids := []int{id1, id2}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100134
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100135 mockBatchDeleteLBResponse(t, ids)
136
137 err := BulkDelete(client.ServiceClient(), ids).ExtractErr()
138 th.AssertNoErr(t, err)
139}
140
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100141func TestDelete(t *testing.T) {
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100142 th.SetupHTTP()
143 defer th.TeardownHTTP()
144
Jamie Hannaford07c06962014-10-31 16:42:03 +0100145 mockDeleteLBResponse(t, id1)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100146
Jamie Hannaford07c06962014-10-31 16:42:03 +0100147 err := Delete(client.ServiceClient(), id1).ExtractErr()
Jamie Hannaford1c260332014-10-31 15:57:22 +0100148 th.AssertNoErr(t, err)
149}
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100150
151func TestGet(t *testing.T) {
Jamie Hannaford07c06962014-10-31 16:42:03 +0100152 th.SetupHTTP()
153 defer th.TeardownHTTP()
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100154
Jamie Hannaford07c06962014-10-31 16:42:03 +0100155 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 Hannaford46336282014-11-04 14:48:20 +0100164 Algorithm: "RANDOM",
Jamie Hannaford07c06962014-10-31 16:42:03 +0100165 Status: ACTIVE,
166 Timeout: 30,
167 ConnectionLogging: ConnectionLogging{Enabled: true},
Jamie Hannaford1c817312014-11-04 10:56:58 +0100168 VIPs: []vips.VIP{
169 vips.VIP{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100170 ID: 1000,
171 Address: "206.10.10.210",
172 Type: "PUBLIC",
173 Version: "IPV4",
174 },
175 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100176 Nodes: []nodes.Node{
177 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100178 Address: "10.1.1.1",
179 ID: 1041,
180 Port: 80,
181 Status: "ONLINE",
182 Condition: "ENABLED",
183 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100184 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100185 Address: "10.1.1.2",
186 ID: 1411,
187 Port: 80,
188 Status: "ONLINE",
189 Condition: "ENABLED",
190 },
191 },
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100192 SessionPersistence: sessions.SessionPersistence{Type: "HTTP_COOKIE"},
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100193 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 Hannaford07c06962014-10-31 16:42:03 +0100197 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 Hannafordfcd22592014-10-31 16:15:34 +0100206}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100207
208func 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 Hannafordcfe2f282014-11-07 15:11:21 +0100217 HalfClosed: gophercloud.Enabled,
Jamie Hannaford46336282014-11-04 14:48:20 +0100218 Algorithm: "RANDOM",
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100219 Port: 8080,
220 Timeout: 100,
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100221 HTTPSRedirect: gophercloud.Disabled,
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100222 }
223
224 err := Update(client.ServiceClient(), id1, opts).ExtractErr()
225 th.AssertNoErr(t, err)
226}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100227
228func 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 Hannaford46336282014-11-04 14:48:20 +0100259
260func 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 Hannafordd78bb352014-11-07 16:36:09 +0100289
290func 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 Hannafordda45b422014-11-10 11:00:38 +0100300
Jamie Hannaford20b75882014-11-10 13:39:51 +0100301func TestEnablingLogging(t *testing.T) {
302 th.SetupHTTP()
303 defer th.TeardownHTTP()
304
305 mockEnableLoggingResponse(t, id1)
306
307 err := EnableLogging(client.ServiceClient(), id1).ExtractErr()
308 th.AssertNoErr(t, err)
309}
310
311func TestDisablingLogging(t *testing.T) {
312 th.SetupHTTP()
313 defer th.TeardownHTTP()
314
315 mockDisableLoggingResponse(t, id1)
316
317 err := DisableLogging(client.ServiceClient(), id1).ExtractErr()
318 th.AssertNoErr(t, err)
319}
320
Jamie Hannafordda45b422014-11-10 11:00:38 +0100321func TestGetErrorPage(t *testing.T) {
322 th.SetupHTTP()
323 defer th.TeardownHTTP()
324
325 mockGetErrorPageResponse(t, id1)
326
327 content, err := GetErrorPage(client.ServiceClient(), id1).Extract()
328 th.AssertNoErr(t, err)
329
330 expected := &ErrorPage{Content: "<html>DEFAULT ERROR PAGE</html>"}
331 th.AssertDeepEquals(t, expected, content)
332}
333
334func TestSetErrorPage(t *testing.T) {
335 th.SetupHTTP()
336 defer th.TeardownHTTP()
337
338 mockSetErrorPageResponse(t, id1)
339
340 html := "<html>New error page</html>"
341 content, err := SetErrorPage(client.ServiceClient(), id1, html).Extract()
342 th.AssertNoErr(t, err)
343
344 expected := &ErrorPage{Content: html}
345 th.AssertDeepEquals(t, expected, content)
346}
347
348func TestDeleteErrorPage(t *testing.T) {
349 th.SetupHTTP()
350 defer th.TeardownHTTP()
351
352 mockDeleteErrorPageResponse(t, id1)
353
354 err := DeleteErrorPage(client.ServiceClient(), id1).ExtractErr()
355 th.AssertNoErr(t, err)
356}
Jamie Hannaford3da65282014-11-10 11:36:16 +0100357
358func TestGetStats(t *testing.T) {
359 th.SetupHTTP()
360 defer th.TeardownHTTP()
361
362 mockGetStatsResponse(t, id1)
363
364 content, err := GetStats(client.ServiceClient(), id1).Extract()
365 th.AssertNoErr(t, err)
366
367 expected := &Stats{
368 ConnectTimeout: 10,
369 ConnectError: 20,
370 ConnectFailure: 30,
371 DataTimedOut: 40,
372 KeepAliveTimedOut: 50,
373 MaxConnections: 60,
374 CurrentConnections: 40,
375 SSLConnectTimeout: 10,
376 SSLConnectError: 20,
377 SSLConnectFailure: 30,
378 SSLDataTimedOut: 40,
379 SSLKeepAliveTimedOut: 50,
380 SSLMaxConnections: 60,
381 SSLCurrentConnections: 40,
382 }
383 th.AssertDeepEquals(t, expected, content)
384}
Jamie Hannaford20b75882014-11-10 13:39:51 +0100385
386func TestIsCached(t *testing.T) {
387 th.SetupHTTP()
388 defer th.TeardownHTTP()
389
390 mockGetCachingResponse(t, id1)
391
392 res, err := IsContentCached(client.ServiceClient(), id1)
393 th.AssertNoErr(t, err)
394 th.AssertEquals(t, true, res)
395}
396
397func TestEnablingCaching(t *testing.T) {
398 th.SetupHTTP()
399 defer th.TeardownHTTP()
400
401 mockEnableCachingResponse(t, id1)
402
403 err := EnableCaching(client.ServiceClient(), id1).ExtractErr()
404 th.AssertNoErr(t, err)
405}
406
407func TestDisablingCaching(t *testing.T) {
408 th.SetupHTTP()
409 defer th.TeardownHTTP()
410
411 mockDisableCachingResponse(t, id1)
412
413 err := DisableCaching(client.ServiceClient(), id1).ExtractErr()
414 th.AssertNoErr(t, err)
415}