blob: 5178eee71d364a80435f46606955a44262d02146 [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")
84 w.WriteHeader(http.StatusOK)
85
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": {
212 "minConnections": 10,
213 "maxConnections": 100,
214 "maxConnectionRate": 50,
215 "rateInterval": 60
216 },
217 "cluster": {
218 "name": "c1.dfw1"
219 },
220 "created": {
221 "time": "2010-11-30T03:23:42Z"
222 },
223 "updated": {
224 "time": "2010-11-30T03:23:44Z"
225 },
226 "sourceAddresses": {
227 "ipv6Public": "2001:4801:79f1:1::1/64",
228 "ipv4Servicenet": "10.0.0.0",
229 "ipv4Public": "10.12.99.28"
230 }
231 }
232}
233 `)
234 })
235}
Jamie Hannaford76fcc832014-10-31 16:56:50 +0100236
237func mockUpdateLBResponse(t *testing.T, id int) {
238 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
239 th.TestMethod(t, r, "PUT")
240 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
241
242 th.TestJSONRequest(t, r, `
243{
244 "loadBalancer": {
245 "name": "a-new-loadbalancer",
246 "protocol": "TCP",
247 "halfClosed": true,
248 "algorithm": "RANDOM",
249 "port": 8080,
250 "timeout": 100,
251 "httpsRedirect": false
252 }
253}
254 `)
255
256 w.WriteHeader(http.StatusOK)
257 })
258}