blob: 033cd7f840c5498a36fdcad6ab5aac5401b04e47 [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 Hannaford1c817312014-11-04 10:56:58 +01008 "github.com/rackspace/gophercloud/rackspace/lb/v1/vips"
Jamie Hannaford186d4e22014-10-31 12:26:11 +01009 th "github.com/rackspace/gophercloud/testhelper"
10 "github.com/rackspace/gophercloud/testhelper/client"
11)
12
Jamie Hannaford07c06962014-10-31 16:42:03 +010013const (
14 id1 = 12345
15 id2 = 67890
16)
17
Jamie Hannaford186d4e22014-10-31 12:26:11 +010018func 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,
37 Algorithm: RAND,
38 Status: ACTIVE,
39 NodeCount: 3,
Jamie Hannaford1c817312014-11-04 10:56:58 +010040 VIPs: []vips.VIP{
41 vips.VIP{
Jamie Hannaford186d4e22014-10-31 12:26:11 +010042 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 Hannaforde09b6822014-10-31 15:33:57 +010061
62func 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 Hannaford1c817312014-11-04 10:56:58 +010072 VIPs: []vips.VIP{
73 vips.VIP{ID: 2341},
74 vips.VIP{ID: 900001},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010075 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +010076 Nodes: []nodes.Node{
77 nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010078 },
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,
90 Algorithm: RAND,
91 Status: BUILD,
92 Timeout: 30,
93 Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"},
Jamie Hannaford89ebfc22014-11-03 16:27:47 +010094 Nodes: []nodes.Node{
95 nodes.Node{
Jamie Hannaforde09b6822014-10-31 15:33:57 +010096 Address: "10.1.1.1",
97 ID: 653,
98 Port: 80,
99 Status: "ONLINE",
100 Condition: "ENABLED",
101 Weight: 1,
102 },
103 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100104 VIPs: []vips.VIP{
105 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100106 ID: 39,
107 Address: "206.10.10.210",
108 Type: "PUBLIC",
109 Version: "IPV4",
110 },
Jamie Hannaford1c817312014-11-04 10:56:58 +0100111 vips.VIP{
Jamie Hannaforde09b6822014-10-31 15:33:57 +0100112 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 Hannaford1c260332014-10-31 15:57:22 +0100125
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100126func TestBulkDelete(t *testing.T) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100127 th.SetupHTTP()
128 defer th.TeardownHTTP()
129
Jamie Hannaford07c06962014-10-31 16:42:03 +0100130 ids := []int{id1, id2}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100131
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100132 mockBatchDeleteLBResponse(t, ids)
133
134 err := BulkDelete(client.ServiceClient(), ids).ExtractErr()
135 th.AssertNoErr(t, err)
136}
137
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100138func TestDelete(t *testing.T) {
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100139 th.SetupHTTP()
140 defer th.TeardownHTTP()
141
Jamie Hannaford07c06962014-10-31 16:42:03 +0100142 mockDeleteLBResponse(t, id1)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100143
Jamie Hannaford07c06962014-10-31 16:42:03 +0100144 err := Delete(client.ServiceClient(), id1).ExtractErr()
Jamie Hannaford1c260332014-10-31 15:57:22 +0100145 th.AssertNoErr(t, err)
146}
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100147
148func TestGet(t *testing.T) {
Jamie Hannaford07c06962014-10-31 16:42:03 +0100149 th.SetupHTTP()
150 defer th.TeardownHTTP()
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100151
Jamie Hannaford07c06962014-10-31 16:42:03 +0100152 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,
161 Algorithm: RAND,
162 Status: ACTIVE,
163 Timeout: 30,
164 ConnectionLogging: ConnectionLogging{Enabled: true},
Jamie Hannaford1c817312014-11-04 10:56:58 +0100165 VIPs: []vips.VIP{
166 vips.VIP{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100167 ID: 1000,
168 Address: "206.10.10.210",
169 Type: "PUBLIC",
170 Version: "IPV4",
171 },
172 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100173 Nodes: []nodes.Node{
174 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100175 Address: "10.1.1.1",
176 ID: 1041,
177 Port: 80,
178 Status: "ONLINE",
179 Condition: "ENABLED",
180 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100181 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100182 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 Hannafordfcd22592014-10-31 16:15:34 +0100208}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100209
210func 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,
220 Algorithm: RAND,
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 Hannaford4ab9aea2014-11-04 14:38:06 +0100229
230func 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}