blob: 8c912ae5d085d88c9a23ab95bb458cbdcf760216 [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"
7 th "github.com/rackspace/gophercloud/testhelper"
8 "github.com/rackspace/gophercloud/testhelper/client"
9)
10
Jamie Hannaford07c06962014-10-31 16:42:03 +010011const (
12 id1 = 12345
13 id2 = 67890
14)
15
Jamie Hannaford186d4e22014-10-31 12:26:11 +010016func TestList(t *testing.T) {
17 th.SetupHTTP()
18 defer th.TeardownHTTP()
19
20 mockListLBResponse(t)
21
22 count := 0
23
24 err := List(client.ServiceClient(), ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
25 count++
26 actual, err := ExtractLBs(page)
27 th.AssertNoErr(t, err)
28
29 expected := []LoadBalancer{
30 LoadBalancer{
31 Name: "lb-site1",
32 ID: 71,
33 Protocol: "HTTP",
34 Port: 80,
35 Algorithm: RAND,
36 Status: ACTIVE,
37 NodeCount: 3,
38 VIPs: []VIP{
39 VIP{
40 ID: 403,
41 Address: "206.55.130.1",
42 Type: "PUBLIC",
43 Version: "IPV4",
44 },
45 },
46 Created: Datetime{Time: "2010-11-30T03:23:42Z"},
47 Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
48 },
49 }
50
51 th.CheckDeepEquals(t, expected, actual)
52
53 return true, nil
54 })
55
56 th.AssertNoErr(t, err)
57 th.AssertEquals(t, 1, count)
58}
Jamie Hannaforde09b6822014-10-31 15:33:57 +010059
60func TestCreate(t *testing.T) {
61 th.SetupHTTP()
62 defer th.TeardownHTTP()
63
64 mockCreateLBResponse(t)
65
66 opts := CreateOpts{
67 Name: "a-new-loadbalancer",
68 Port: 80,
69 Protocol: "HTTP",
70 VIPs: []VIP{
71 VIP{ID: 2341},
72 VIP{ID: 900001},
73 },
74 Nodes: []Node{
75 Node{Address: "10.1.1.1", Port: 80, Condition: "ENABLED"},
76 },
77 }
78
79 lb, err := Create(client.ServiceClient(), opts).Extract()
80 th.AssertNoErr(t, err)
81
82 expected := &LoadBalancer{
83 Name: "a-new-loadbalancer",
84 ID: 144,
85 Protocol: "HTTP",
86 HalfClosed: false,
87 Port: 83,
88 Algorithm: RAND,
89 Status: BUILD,
90 Timeout: 30,
91 Cluster: Cluster{Name: "ztm-n01.staging1.lbaas.rackspace.net"},
92 Nodes: []Node{
93 Node{
94 Address: "10.1.1.1",
95 ID: 653,
96 Port: 80,
97 Status: "ONLINE",
98 Condition: "ENABLED",
99 Weight: 1,
100 },
101 },
102 VIPs: []VIP{
103 VIP{
104 ID: 39,
105 Address: "206.10.10.210",
106 Type: "PUBLIC",
107 Version: "IPV4",
108 },
109 VIP{
110 ID: 900001,
111 Address: "2001:4801:79f1:0002:711b:be4c:0000:0021",
112 Type: "PUBLIC",
113 Version: "IPV6",
114 },
115 },
116 Created: Datetime{Time: "2011-04-13T14:18:07Z"},
117 Updated: Datetime{Time: "2011-04-13T14:18:07Z"},
118 ConnectionLogging: ConnectionLogging{Enabled: false},
119 }
120
121 th.AssertDeepEquals(t, expected, lb)
122}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100123
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100124func TestBulkDelete(t *testing.T) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100125 th.SetupHTTP()
126 defer th.TeardownHTTP()
127
Jamie Hannaford07c06962014-10-31 16:42:03 +0100128 ids := []int{id1, id2}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100129
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100130 mockBatchDeleteLBResponse(t, ids)
131
132 err := BulkDelete(client.ServiceClient(), ids).ExtractErr()
133 th.AssertNoErr(t, err)
134}
135
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100136func TestDelete(t *testing.T) {
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100137 th.SetupHTTP()
138 defer th.TeardownHTTP()
139
Jamie Hannaford07c06962014-10-31 16:42:03 +0100140 mockDeleteLBResponse(t, id1)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100141
Jamie Hannaford07c06962014-10-31 16:42:03 +0100142 err := Delete(client.ServiceClient(), id1).ExtractErr()
Jamie Hannaford1c260332014-10-31 15:57:22 +0100143 th.AssertNoErr(t, err)
144}
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100145
146func TestGet(t *testing.T) {
Jamie Hannaford07c06962014-10-31 16:42:03 +0100147 th.SetupHTTP()
148 defer th.TeardownHTTP()
Jamie Hannafordfcd22592014-10-31 16:15:34 +0100149
Jamie Hannaford07c06962014-10-31 16:42:03 +0100150 mockGetLBResponse(t, id1)
151
152 lb, err := Get(client.ServiceClient(), id1).Extract()
153
154 expected := &LoadBalancer{
155 Name: "sample-loadbalancer",
156 ID: 2000,
157 Protocol: "HTTP",
158 Port: 80,
159 Algorithm: RAND,
160 Status: ACTIVE,
161 Timeout: 30,
162 ConnectionLogging: ConnectionLogging{Enabled: true},
163 VIPs: []VIP{
164 VIP{
165 ID: 1000,
166 Address: "206.10.10.210",
167 Type: "PUBLIC",
168 Version: "IPV4",
169 },
170 },
171 Nodes: []Node{
172 Node{
173 Address: "10.1.1.1",
174 ID: 1041,
175 Port: 80,
176 Status: "ONLINE",
177 Condition: "ENABLED",
178 },
179 Node{
180 Address: "10.1.1.2",
181 ID: 1411,
182 Port: 80,
183 Status: "ONLINE",
184 Condition: "ENABLED",
185 },
186 },
187 SessionPersistence: SessionPersistence{Type: "HTTP_COOKIE"},
188 ConnectionThrottle: ConnectionThrottle{
189 MinConns: 10,
190 MaxConns: 100,
191 MaxConnRate: 50,
192 RateInterval: 60,
193 },
194 Cluster: Cluster{Name: "c1.dfw1"},
195 Created: Datetime{Time: "2010-11-30T03:23:42Z"},
196 Updated: Datetime{Time: "2010-11-30T03:23:44Z"},
197 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",
217 HalfClosed: Enabled,
218 Algorithm: RAND,
219 Port: 8080,
220 Timeout: 100,
221 HTTPSRedirect: Disabled,
222 }
223
224 err := Update(client.ServiceClient(), id1, opts).ExtractErr()
225 th.AssertNoErr(t, err)
226}