blob: 04c2785e4c86207ad6609c73c7e3de6b72f35bc4 [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 Hannaford186d4e22014-10-31 12:26:11 +01008 th "github.com/rackspace/gophercloud/testhelper"
9 "github.com/rackspace/gophercloud/testhelper/client"
10)
11
Jamie Hannaford07c06962014-10-31 16:42:03 +010012const (
13 id1 = 12345
14 id2 = 67890
15)
16
Jamie Hannaford186d4e22014-10-31 12:26:11 +010017func 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 Hannaforde09b6822014-10-31 15:33:57 +010060
61func 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 Hannaford89ebfc22014-11-03 16:27:47 +010075 Nodes: []nodes.Node{
76 nodes.Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"},
Jamie Hannaforde09b6822014-10-31 15:33:57 +010077 },
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 Hannaford89ebfc22014-11-03 16:27:47 +010093 Nodes: []nodes.Node{
94 nodes.Node{
Jamie Hannaforde09b6822014-10-31 15:33:57 +010095 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 Hannaford1c260332014-10-31 15:57:22 +0100124
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100125func TestBulkDelete(t *testing.T) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100126 th.SetupHTTP()
127 defer th.TeardownHTTP()
128
Jamie Hannaford07c06962014-10-31 16:42:03 +0100129 ids := []int{id1, id2}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100130
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100131 mockBatchDeleteLBResponse(t, ids)
132
133 err := BulkDelete(client.ServiceClient(), ids).ExtractErr()
134 th.AssertNoErr(t, err)
135}
136
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100137func TestDelete(t *testing.T) {
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100138 th.SetupHTTP()
139 defer th.TeardownHTTP()
140
Jamie Hannaford07c06962014-10-31 16:42:03 +0100141 mockDeleteLBResponse(t, id1)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100142
Jamie Hannaford07c06962014-10-31 16:42:03 +0100143 err := Delete(client.ServiceClient(), id1).ExtractErr()
Jamie Hannaford1c260332014-10-31 15:57:22 +0100144 th.AssertNoErr(t, err)
145}
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100146
147func TestGet(t *testing.T) {
Jamie Hannaford07c06962014-10-31 16:42:03 +0100148 th.SetupHTTP()
149 defer th.TeardownHTTP()
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100150
Jamie Hannaford07c06962014-10-31 16:42:03 +0100151 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 Hannaford89ebfc22014-11-03 16:27:47 +0100172 Nodes: []nodes.Node{
173 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100174 Address: "10.1.1.1",
175 ID: 1041,
176 Port: 80,
177 Status: "ONLINE",
178 Condition: "ENABLED",
179 },
Jamie Hannaford89ebfc22014-11-03 16:27:47 +0100180 nodes.Node{
Jamie Hannaford07c06962014-10-31 16:42:03 +0100181 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 Hannafordfcd22592014-10-31 16:15:34 +0100207}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100208
209func 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}