blob: 4c497e45ef972281f79a41e4b0aa46ff3216a11c [file] [log] [blame]
Jamie Hannafordfba65af2014-11-03 10:32:37 +01001package lbs
Jamie Hannaford186d4e22014-10-31 12:26:11 +01002
3import (
4 "fmt"
5 "net/http"
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +01006 "strconv"
Jamie Hannaford186d4e22014-10-31 12:26:11 +01007 "testing"
8
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
13func mockListLBResponse(t *testing.T) {
14 th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
15 th.TestMethod(t, r, "GET")
16 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
17
18 w.Header().Add("Content-Type", "application/json")
19 w.WriteHeader(http.StatusOK)
20
21 fmt.Fprintf(w, `
22{
23 "loadBalancers": [
24 {
25 "name": "lb-site1",
26 "id": 71,
27 "protocol": "HTTP",
28 "port": 80,
29 "algorithm": "RANDOM",
30 "status": "ACTIVE",
31 "nodeCount": 3,
32 "virtualIps": [
33 {
34 "id": 403,
35 "address": "206.55.130.1",
36 "type": "PUBLIC",
37 "ipVersion": "IPV4"
38 }
39 ],
40 "created": {
41 "time": "2010-11-30T03:23:42Z"
42 },
43 "updated": {
44 "time": "2010-11-30T03:23:44Z"
45 }
46 }
47 ]
48}
49 `)
50 })
51}
Jamie Hannaforde09b6822014-10-31 15:33:57 +010052
53func mockCreateLBResponse(t *testing.T) {
54 th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
55 th.TestMethod(t, r, "POST")
56 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
57
58 th.TestJSONRequest(t, r, `
59{
60 "loadBalancer": {
61 "name": "a-new-loadbalancer",
62 "port": 80,
63 "protocol": "HTTP",
64 "virtualIps": [
65 {
66 "id": 2341
67 },
68 {
69 "id": 900001
70 }
71 ],
72 "nodes": [
73 {
74 "address": "10.1.1.1",
75 "port": 80,
76 "condition": "ENABLED"
77 }
78 ]
79 }
80}
81 `)
82
83 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford2592f1f2014-11-06 11:10:38 +010084 w.WriteHeader(http.StatusAccepted)
Jamie Hannaforde09b6822014-10-31 15:33:57 +010085
86 fmt.Fprintf(w, `
87{
88 "loadBalancer": {
89 "name": "a-new-loadbalancer",
90 "id": 144,
91 "protocol": "HTTP",
92 "halfClosed": false,
93 "port": 83,
94 "algorithm": "RANDOM",
95 "status": "BUILD",
96 "timeout": 30,
97 "cluster": {
98 "name": "ztm-n01.staging1.lbaas.rackspace.net"
99 },
100 "nodes": [
101 {
102 "address": "10.1.1.1",
103 "id": 653,
104 "port": 80,
105 "status": "ONLINE",
106 "condition": "ENABLED",
107 "weight": 1
108 }
109 ],
110 "virtualIps": [
111 {
112 "address": "206.10.10.210",
113 "id": 39,
114 "type": "PUBLIC",
115 "ipVersion": "IPV4"
116 },
117 {
118 "address": "2001:4801:79f1:0002:711b:be4c:0000:0021",
119 "id": 900001,
120 "type": "PUBLIC",
121 "ipVersion": "IPV6"
122 }
123 ],
124 "created": {
125 "time": "2011-04-13T14:18:07Z"
126 },
127 "updated": {
128 "time": "2011-04-13T14:18:07Z"
129 },
130 "connectionLogging": {
131 "enabled": false
132 }
133 }
134}
135 `)
136 })
137}
Jamie Hannaford1c260332014-10-31 15:57:22 +0100138
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100139func mockBatchDeleteLBResponse(t *testing.T, ids []int) {
Jamie Hannaford1c260332014-10-31 15:57:22 +0100140 th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
141 th.TestMethod(t, r, "DELETE")
142 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford5f95e6a2014-10-31 16:13:44 +0100143
144 r.ParseForm()
145
146 for k, v := range ids {
147 fids := r.Form["id"]
148 th.AssertEquals(t, strconv.Itoa(v), fids[k])
149 }
150
151 w.WriteHeader(http.StatusAccepted)
152 })
153}
154
155func mockDeleteLBResponse(t *testing.T, id int) {
156 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
157 th.TestMethod(t, r, "DELETE")
158 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford1c260332014-10-31 15:57:22 +0100159 w.WriteHeader(http.StatusAccepted)
160 })
161}
Jamie Hannaford07c06962014-10-31 16:42:03 +0100162
163func mockGetLBResponse(t *testing.T, id int) {
164 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
165 th.TestMethod(t, r, "GET")
166 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
167
168 w.Header().Add("Content-Type", "application/json")
169 w.WriteHeader(http.StatusOK)
170
171 fmt.Fprintf(w, `
172{
173 "loadBalancer": {
174 "id": 2000,
175 "name": "sample-loadbalancer",
176 "protocol": "HTTP",
177 "port": 80,
178 "algorithm": "RANDOM",
179 "status": "ACTIVE",
180 "timeout": 30,
181 "connectionLogging": {
182 "enabled": true
183 },
184 "virtualIps": [
185 {
186 "id": 1000,
187 "address": "206.10.10.210",
188 "type": "PUBLIC",
189 "ipVersion": "IPV4"
190 }
191 ],
192 "nodes": [
193 {
194 "id": 1041,
195 "address": "10.1.1.1",
196 "port": 80,
197 "condition": "ENABLED",
198 "status": "ONLINE"
199 },
200 {
201 "id": 1411,
202 "address": "10.1.1.2",
203 "port": 80,
204 "condition": "ENABLED",
205 "status": "ONLINE"
206 }
207 ],
208 "sessionPersistence": {
209 "persistenceType": "HTTP_COOKIE"
210 },
211 "connectionThrottle": {
Jamie Hannafordcfe2f282014-11-07 15:11:21 +0100212 "maxConnections": 100
Jamie Hannaford07c06962014-10-31 16:42:03 +0100213 },
214 "cluster": {
215 "name": "c1.dfw1"
216 },
217 "created": {
218 "time": "2010-11-30T03:23:42Z"
219 },
220 "updated": {
221 "time": "2010-11-30T03:23:44Z"
222 },
223 "sourceAddresses": {
224 "ipv6Public": "2001:4801:79f1:1::1/64",
225 "ipv4Servicenet": "10.0.0.0",
226 "ipv4Public": "10.12.99.28"
227 }
228 }
229}
230 `)
231 })
232}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100233
234func mockUpdateLBResponse(t *testing.T, id int) {
235 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
236 th.TestMethod(t, r, "PUT")
237 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
238
239 th.TestJSONRequest(t, r, `
240{
241 "loadBalancer": {
242 "name": "a-new-loadbalancer",
243 "protocol": "TCP",
244 "halfClosed": true,
245 "algorithm": "RANDOM",
246 "port": 8080,
247 "timeout": 100,
248 "httpsRedirect": false
249 }
250}
251 `)
252
Jamie Hannaford2592f1f2014-11-06 11:10:38 +0100253 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100254 })
255}
Jamie Hannaford4ab9aea2014-11-04 14:38:06 +0100256
257func mockListProtocolsResponse(t *testing.T) {
258 th.Mux.HandleFunc("/loadbalancers/protocols", func(w http.ResponseWriter, r *http.Request) {
259 th.TestMethod(t, r, "GET")
260 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
261
262 w.Header().Add("Content-Type", "application/json")
263 w.WriteHeader(http.StatusOK)
264
265 fmt.Fprintf(w, `
266{
267 "protocols": [
268 {
269 "name": "DNS_TCP",
270 "port": 53
271 },
272 {
273 "name": "DNS_UDP",
274 "port": 53
275 },
276 {
277 "name": "FTP",
278 "port": 21
279 },
280 {
281 "name": "HTTP",
282 "port": 80
283 },
284 {
285 "name": "HTTPS",
286 "port": 443
287 },
288 {
289 "name": "IMAPS",
290 "port": 993
291 },
292 {
293 "name": "IMAPv4",
294 "port": 143
295 },
296 {
297 "name": "LDAP",
298 "port": 389
299 },
300 {
301 "name": "LDAPS",
302 "port": 636
303 },
304 {
305 "name": "MYSQL",
306 "port": 3306
307 },
308 {
309 "name": "POP3",
310 "port": 110
311 },
312 {
313 "name": "POP3S",
314 "port": 995
315 },
316 {
317 "name": "SMTP",
318 "port": 25
319 },
320 {
321 "name": "TCP",
322 "port": 0
323 },
324 {
325 "name": "TCP_CLIENT_FIRST",
326 "port": 0
327 },
328 {
329 "name": "UDP",
330 "port": 0
331 },
332 {
333 "name": "UDP_STREAM",
334 "port": 0
335 },
336 {
337 "name": "SFTP",
338 "port": 22
339 },
340 {
341 "name": "TCP_STREAM",
342 "port": 0
343 }
344 ]
345}
346 `)
347 })
348}
Jamie Hannaford46336282014-11-04 14:48:20 +0100349
350func mockListAlgorithmsResponse(t *testing.T) {
351 th.Mux.HandleFunc("/loadbalancers/algorithms", func(w http.ResponseWriter, r *http.Request) {
352 th.TestMethod(t, r, "GET")
353 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
354
355 w.Header().Add("Content-Type", "application/json")
356 w.WriteHeader(http.StatusOK)
357
358 fmt.Fprintf(w, `
359{
360 "algorithms": [
361 {
362 "name": "LEAST_CONNECTIONS"
363 },
364 {
365 "name": "RANDOM"
366 },
367 {
368 "name": "ROUND_ROBIN"
369 },
370 {
371 "name": "WEIGHTED_LEAST_CONNECTIONS"
372 },
373 {
374 "name": "WEIGHTED_ROUND_ROBIN"
375 }
376 ]
377}
378 `)
379 })
380}