blob: a8ec19e07c3e156e390e108c0d0496d16ed4c96c [file] [log] [blame]
Jamie Hannafordfba65af2014-11-03 10:32:37 +01001package lbs
Jamie Hannaford186d4e22014-10-31 12:26:11 +01002
3import (
4 "testing"
Jamie Hannaford4d398ff2014-11-14 11:03:00 +01005 "time"
Jamie Hannaford186d4e22014-10-31 12:26:11 +01006
Jamie Hannafordcfe2f282014-11-07 15:11:21 +01007 "github.com/rackspace/gophercloud"
Jamie Hannaford186d4e22014-10-31 12:26:11 +01008 "github.com/rackspace/gophercloud/pagination"
Jamie Hannaford89ebfc22014-11-03 16:27:47 +01009 "github.com/rackspace/gophercloud/rackspace/lb/v1/nodes"
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +010010 "github.com/rackspace/gophercloud/rackspace/lb/v1/sessions"
Jamie Hannafordcfe2f282014-11-07 15:11:21 +010011 "github.com/rackspace/gophercloud/rackspace/lb/v1/throttle"
Jamie Hannaford1c817312014-11-04 10:56:58 +010012 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +010013 th "github.com/rackspace/gophercloud/testhelper"
14 "github.com/rackspace/gophercloud/testhelper/client"
15)
16
Jamie Hannaford07c06962014-10-31 16:42:03 +010017const (
18 id1 = 12345
19 id2 = 67890
Jamie Hannaford4d398ff2014-11-14 11:03:00 +010020 ts1 = "2010-11-30T03:23:42Z"
21 ts2 = "2010-11-30T03:23:44Z"
Jamie Hannaford07c06962014-10-31 16:42:03 +010022)
23
Jamie Hannaford4d398ff2014-11-14 11:03:00 +010024func toTime(t *testing.T, str string) time.Time {
25 ts, err := time.Parse(time.RFC3339, str)
26 if err != nil {
27 t.Fatalf("Could not parse time: %s", err.Error())
28 }
29 return ts
30}
31
Jamie Hannaford186d4e22014-10-31 12:26:11 +010032func TestList(t *testing.T) {
33 th.SetupHTTP()
34 defer th.TeardownHTTP()
35
36 mockListLBResponse(t)
37
38 count := 0
39
40 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
41 count++
42 actual, err := ExtractLBs(page)
43 th.AssertNoErr(t, err)
44
45 expected := []LoadBalancer{
46 LoadBalancer{
47 Name: "lb-site1",
48 ID: 71,
49 Protocol: "HTTP",
50 Port: 80,
Jamie Hannaford46336282014-11-04 14:48:20 +010051 Algorithm: "RANDOM",
Jamie Hannaford186d4e22014-10-31 12:26:11 +010052 Status: ACTIVE,
53 NodeCount: 3,
Jamie Hannaford1c817312014-11-04 10:56:58 +010054 VIPs: []vips.VIP{
55 vips.VIP{
Jamie Hannaford186d4e22014-10-31 12:26:11 +010056 ID: 403,
57 Address: "206.55.130.1",
58 Type: "PUBLIC",
59 Version: "IPV4",
60 },
61 },
Jamie Hannaford4d398ff2014-11-14 11:03:00 +010062 Created: Datetime{Time: toTime(t, ts1)},
63 Updated: Datetime{Time: toTime(t, ts2)},
64 },
65 LoadBalancer{
66 ID: 72,
67 Name: "lb-site2",
68 Created: Datetime{Time: toTime(t, "2011-11-30T03:23:42Z")},
69 Updated: Datetime{Time: toTime(t, "2011-11-30T03:23:44Z")},
70 },
71 LoadBalancer{
72 ID: 73,
73 Name: "lb-site3",
74 Created: Datetime{Time: toTime(t, "2012-11-30T03:23:42Z")},
75 Updated: Datetime{Time: toTime(t, "2012-11-30T03:23:44Z")},
Jamie Hannaford186d4e22014-10-31 12:26:11 +010076 },
77 }
78
79 th.CheckDeepEquals(t, expected, actual)
80
81 return true, nil
82 })
83
84 th.AssertNoErr(t, err)
85 th.AssertEquals(t, 1, count)
86}
Jamie Hannaforde09b6822014-10-31 15:33:57 +010087
88func TestCreate(t *testing.T) {
89 th.SetupHTTP()
90 defer th.TeardownHTTP()
91
92 mockCreateLBResponse(t)
93
94 opts := CreateOpts{
95 Name: "a-new-loadbalancer",
96 Port: 80,
97 Protocol: "HTTP",
Jamie Hannaford1c817312014-11-04 10:56:58 +010098 VIPs: []vips.VIP{
99 vips.VIP{ID: 2341},
100 vips.VIP{ID: 900001},
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100101 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100102 Nodes: []nodes.Node{
103 nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"},
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100104 },
105 }
106
107 lb, err := Create(client.ServiceClient(), opts).Extract()
108 th.AssertNoErr(t, err)
109
110 expected := &LoadBalancer{
111 Name: "a-new-loadbalancer",
112 ID: 144,
113 Protocol: "HTTP",
114 HalfClosed: false,
115 Port: 83,
Jamie Hannaford46336282014-11-04 14:48:20 +0100116 Algorithm: "RANDOM",
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100117 Status: BUILD,
118 Timeout: 30,
119 Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"},
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100120 Nodes: []nodes.Node{
121 nodes.Node{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100122 Address: "10.1.1.1",
123 ID: 653,
124 Port: 80,
125 Status: "ONLINE",
126 Condition: "ENABLED",
127 Weight: 1,
128 },
129 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100130 VIPs: []vips.VIP{
131 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100132 ID: 39,
133 Address: "206.10.10.210",
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100134 Type: vips.PUBLIC,
135 Version: vips.IPV4,
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100136 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100137 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100138 ID: 900001,
139 Address: "2001:4801:79f1:0002:711b:be4c:0000:0021",
Jamie Hannafordd56375d2014-11-05 12:38:04 +0100140 Type: vips.PUBLIC,
141 Version: vips.IPV6,
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100142 },
143 },
Jamie Hannaford4d398ff2014-11-14 11:03:00 +0100144 Created: Datetime{Time: toTime(t, ts1)},
145 Updated: Datetime{Time: toTime(t, ts2)},
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100146 ConnectionLogging: ConnectionLogging{Enabled: false},
147 }
148
149 th.AssertDeepEquals(t, expected, lb)
150}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100151
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100152func TestBulkDelete(t *testing.T) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100153 th.SetupHTTP()
154 defer th.TeardownHTTP()
155
Jamie Hannaford07c06962014-10-31 16:42:03 +0100156 ids := []int{id1, id2}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100157
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100158 mockBatchDeleteLBResponse(t, ids)
159
160 err := BulkDelete(client.ServiceClient(), ids).ExtractErr()
161 th.AssertNoErr(t, err)
162}
163
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100164func TestDelete(t *testing.T) {
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100165 th.SetupHTTP()
166 defer th.TeardownHTTP()
167
Jamie Hannaford07c06962014-10-31 16:42:03 +0100168 mockDeleteLBResponse(t, id1)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100169
Jamie Hannaford07c06962014-10-31 16:42:03 +0100170 err := Delete(client.ServiceClient(), id1).ExtractErr()
Jamie Hannaford1c260332014-10-31 15:57:22 +0100171 th.AssertNoErr(t, err)
172}
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100173
174func TestGet(t *testing.T) {
Jamie Hannaford07c06962014-10-31 16:42:03 +0100175 th.SetupHTTP()
176 defer th.TeardownHTTP()
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100177
Jamie Hannaford07c06962014-10-31 16:42:03 +0100178 mockGetLBResponse(t, id1)
179
180 lb, err := Get(client.ServiceClient(), id1).Extract()
181
182 expected := &LoadBalancer{
183 Name: "sample-loadbalancer",
184 ID: 2000,
185 Protocol: "HTTP",
186 Port: 80,
Jamie Hannaford46336282014-11-04 14:48:20 +0100187 Algorithm: "RANDOM",
Jamie Hannaford07c06962014-10-31 16:42:03 +0100188 Status: ACTIVE,
189 Timeout: 30,
190 ConnectionLogging: ConnectionLogging{Enabled: true},
Jamie Hannaford1c817312014-11-04 10:56:58 +0100191 VIPs: []vips.VIP{
192 vips.VIP{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100193 ID: 1000,
194 Address: "206.10.10.210",
195 Type: "PUBLIC",
196 Version: "IPV4",
197 },
198 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100199 Nodes: []nodes.Node{
200 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100201 Address: "10.1.1.1",
202 ID: 1041,
203 Port: 80,
204 Status: "ONLINE",
205 Condition: "ENABLED",
206 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100207 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100208 Address: "10.1.1.2",
209 ID: 1411,
210 Port: 80,
211 Status: "ONLINE",
212 Condition: "ENABLED",
213 },
214 },
Jamie Hannaford6bc93aa2014-11-06 12:37:52 +0100215 SessionPersistence: sessions.SessionPersistence{Type: "HTTP_COOKIE"},
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100216 ConnectionThrottle: throttle.ConnectionThrottle{MaxConnections: 100},
217 Cluster: Cluster{Name: "c1.dfw1"},
Jamie Hannaford4d398ff2014-11-14 11:03:00 +0100218 Created: Datetime{Time: toTime(t, ts1)},
219 Updated: Datetime{Time: toTime(t, ts2)},
Jamie Hannaford07c06962014-10-31 16:42:03 +0100220 SourceAddrs: SourceAddrs{
221 IPv4Public: "10.12.99.28",
222 IPv4Private: "10.0.0.0",
223 IPv6Public: "2001:4801:79f1:1::1/64",
224 },
225 }
226
227 th.AssertDeepEquals(t, expected, lb)
228 th.AssertNoErr(t, err)
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100229}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100230
231func TestUpdate(t *testing.T) {
232 th.SetupHTTP()
233 defer th.TeardownHTTP()
234
235 mockUpdateLBResponse(t, id1)
236
237 opts := UpdateOpts{
238 Name: "a-new-loadbalancer",
239 Protocol: "TCP",
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100240 HalfClosed: gophercloud.Enabled,
Jamie Hannaford46336282014-11-04 14:48:20 +0100241 Algorithm: "RANDOM",
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100242 Port: 8080,
243 Timeout: 100,
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100244 HTTPSRedirect: gophercloud.Disabled,
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100245 }
246
247 err := Update(client.ServiceClient(), id1, opts).ExtractErr()
248 th.AssertNoErr(t, err)
249}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100250
251func TestListProtocols(t *testing.T) {
252 th.SetupHTTP()
253 defer th.TeardownHTTP()
254
255 mockListProtocolsResponse(t)
256
257 count := 0
258
259 err := ListProtocols(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
260 count++
261 actual, err := ExtractProtocols(page)
262 th.AssertNoErr(t, err)
263
264 expected := []Protocol{
265 Protocol{Name: "DNS_TCP", Port: 53},
266 Protocol{Name: "DNS_UDP", Port: 53},
267 Protocol{Name: "FTP", Port: 21},
268 Protocol{Name: "HTTP", Port: 80},
269 Protocol{Name: "HTTPS", Port: 443},
270 Protocol{Name: "IMAPS", Port: 993},
271 Protocol{Name: "IMAPv4", Port: 143},
272 }
273
274 th.CheckDeepEquals(t, expected[0:7], actual)
275
276 return true, nil
277 })
278
279 th.AssertNoErr(t, err)
280 th.AssertEquals(t, 1, count)
281}
Jamie Hannaford46336282014-11-04 14:48:20 +0100282
283func TestListAlgorithms(t *testing.T) {
284 th.SetupHTTP()
285 defer th.TeardownHTTP()
286
287 mockListAlgorithmsResponse(t)
288
289 count := 0
290
291 err := ListAlgorithms(client.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
292 count++
293 actual, err := ExtractAlgorithms(page)
294 th.AssertNoErr(t, err)
295
296 expected := []Algorithm{
297 Algorithm{Name: "LEAST_CONNECTIONS"},
298 Algorithm{Name: "RANDOM"},
299 Algorithm{Name: "ROUND_ROBIN"},
300 Algorithm{Name: "WEIGHTED_LEAST_CONNECTIONS"},
301 Algorithm{Name: "WEIGHTED_ROUND_ROBIN"},
302 }
303
304 th.CheckDeepEquals(t, expected, actual)
305
306 return true, nil
307 })
308
309 th.AssertNoErr(t, err)
310 th.AssertEquals(t, 1, count)
311}
Jamie Hannafordd78bb352014-11-07 16:36:09 +0100312
313func TestIsLoggingEnabled(t *testing.T) {
314 th.SetupHTTP()
315 defer th.TeardownHTTP()
316
317 mockGetLoggingResponse(t, id1)
318
319 res, err := IsLoggingEnabled(client.ServiceClient(), id1)
320 th.AssertNoErr(t, err)
321 th.AssertEquals(t, true, res)
322}
Jamie Hannafordda45b422014-11-10 11:00:38 +0100323
Jamie Hannaford20b75882014-11-10 13:39:51 +0100324func TestEnablingLogging(t *testing.T) {
325 th.SetupHTTP()
326 defer th.TeardownHTTP()
327
328 mockEnableLoggingResponse(t, id1)
329
330 err := EnableLogging(client.ServiceClient(), id1).ExtractErr()
331 th.AssertNoErr(t, err)
332}
333
334func TestDisablingLogging(t *testing.T) {
335 th.SetupHTTP()
336 defer th.TeardownHTTP()
337
338 mockDisableLoggingResponse(t, id1)
339
340 err := DisableLogging(client.ServiceClient(), id1).ExtractErr()
341 th.AssertNoErr(t, err)
342}
343
Jamie Hannafordda45b422014-11-10 11:00:38 +0100344func TestGetErrorPage(t *testing.T) {
345 th.SetupHTTP()
346 defer th.TeardownHTTP()
347
348 mockGetErrorPageResponse(t, id1)
349
350 content, err := GetErrorPage(client.ServiceClient(), id1).Extract()
351 th.AssertNoErr(t, err)
352
353 expected := &ErrorPage{Content: "<html>DEFAULT ERROR PAGE</html>"}
354 th.AssertDeepEquals(t, expected, content)
355}
356
357func TestSetErrorPage(t *testing.T) {
358 th.SetupHTTP()
359 defer th.TeardownHTTP()
360
361 mockSetErrorPageResponse(t, id1)
362
363 html := "<html>New error page</html>"
364 content, err := SetErrorPage(client.ServiceClient(), id1, html).Extract()
365 th.AssertNoErr(t, err)
366
367 expected := &ErrorPage{Content: html}
368 th.AssertDeepEquals(t, expected, content)
369}
370
371func TestDeleteErrorPage(t *testing.T) {
372 th.SetupHTTP()
373 defer th.TeardownHTTP()
374
375 mockDeleteErrorPageResponse(t, id1)
376
377 err := DeleteErrorPage(client.ServiceClient(), id1).ExtractErr()
378 th.AssertNoErr(t, err)
379}
Jamie Hannaford3da65282014-11-10 11:36:16 +0100380
381func TestGetStats(t *testing.T) {
382 th.SetupHTTP()
383 defer th.TeardownHTTP()
384
385 mockGetStatsResponse(t, id1)
386
387 content, err := GetStats(client.ServiceClient(), id1).Extract()
388 th.AssertNoErr(t, err)
389
390 expected := &Stats{
391 ConnectTimeout: 10,
392 ConnectError: 20,
393 ConnectFailure: 30,
394 DataTimedOut: 40,
395 KeepAliveTimedOut: 50,
396 MaxConnections: 60,
397 CurrentConnections: 40,
398 SSLConnectTimeout: 10,
399 SSLConnectError: 20,
400 SSLConnectFailure: 30,
401 SSLDataTimedOut: 40,
402 SSLKeepAliveTimedOut: 50,
403 SSLMaxConnections: 60,
404 SSLCurrentConnections: 40,
405 }
406 th.AssertDeepEquals(t, expected, content)
407}
Jamie Hannaford20b75882014-11-10 13:39:51 +0100408
409func TestIsCached(t *testing.T) {
410 th.SetupHTTP()
411 defer th.TeardownHTTP()
412
413 mockGetCachingResponse(t, id1)
414
415 res, err := IsContentCached(client.ServiceClient(), id1)
416 th.AssertNoErr(t, err)
417 th.AssertEquals(t, true, res)
418}
419
420func TestEnablingCaching(t *testing.T) {
421 th.SetupHTTP()
422 defer th.TeardownHTTP()
423
424 mockEnableCachingResponse(t, id1)
425
426 err := EnableCaching(client.ServiceClient(), id1).ExtractErr()
427 th.AssertNoErr(t, err)
428}
429
430func TestDisablingCaching(t *testing.T) {
431 th.SetupHTTP()
432 defer th.TeardownHTTP()
433
434 mockDisableCachingResponse(t, id1)
435
436 err := DisableCaching(client.ServiceClient(), id1).ExtractErr()
437 th.AssertNoErr(t, err)
438}