blob: 350eefaa5c843ed816db9c607cbf308a97285694 [file] [log] [blame]
Keith Byrnebda48592016-03-23 11:37:08 +00001// +build fixtures
2
Jamie Hannaford2a8031e2014-11-03 10:31:20 +01003package nodes
4
5import (
6 "fmt"
7 "net/http"
8 "strconv"
9 "testing"
10
11 th "github.com/rackspace/gophercloud/testhelper"
12 fake "github.com/rackspace/gophercloud/testhelper/client"
13)
14
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010015func _rootURL(lbID int) string {
16 return "/loadbalancers/" + strconv.Itoa(lbID) + "/nodes"
17}
18
Jamie Hannaford51175a02014-11-03 13:29:44 +010019func _nodeURL(lbID, nodeID int) string {
20 return _rootURL(lbID) + "/" + strconv.Itoa(nodeID)
21}
22
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010023func mockListResponse(t *testing.T, lbID int) {
24 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010025 th.TestMethod(t, r, "GET")
26 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
27
28 w.Header().Add("Content-Type", "application/json")
29 w.WriteHeader(http.StatusOK)
30
31 fmt.Fprintf(w, `
32{
33 "nodes": [
34 {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010035 "id": 410,
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010036 "address": "10.1.1.1",
37 "port": 80,
38 "condition": "ENABLED",
39 "status": "ONLINE",
40 "weight": 3,
41 "type": "PRIMARY"
42 },
43 {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010044 "id": 411,
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010045 "address": "10.1.1.2",
46 "port": 80,
47 "condition": "ENABLED",
48 "status": "ONLINE",
49 "weight": 8,
50 "type": "SECONDARY"
51 }
52 ]
53}
54 `)
55 })
56}
57
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010058func mockCreateResponse(t *testing.T, lbID int) {
59 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010060 th.TestMethod(t, r, "POST")
61 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
62
63 th.TestJSONRequest(t, r, `
64{
65 "nodes": [
66 {
67 "address": "10.2.2.3",
68 "port": 80,
69 "condition": "ENABLED",
70 "type": "PRIMARY"
71 },
72 {
73 "address": "10.2.2.4",
74 "port": 81,
75 "condition": "ENABLED",
76 "type": "SECONDARY"
77 }
78 ]
79}
80 `)
81
82 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford2592f1f2014-11-06 11:10:38 +010083 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010084
85 fmt.Fprintf(w, `
86{
87 "nodes": [
88 {
89 "address": "10.2.2.3",
90 "id": 185,
91 "port": 80,
92 "status": "ONLINE",
93 "condition": "ENABLED",
94 "weight": 1,
95 "type": "PRIMARY"
96 },
97 {
98 "address": "10.2.2.4",
99 "id": 186,
100 "port": 81,
101 "status": "ONLINE",
102 "condition": "ENABLED",
103 "weight": 1,
104 "type": "SECONDARY"
105 }
106 ]
107}
108 `)
109 })
110}
111
Ash Wilsonae551432015-06-24 09:40:40 -0400112func mockCreateErrResponse(t *testing.T, lbID int) {
113 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
114 th.TestMethod(t, r, "POST")
115 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
116
117 th.TestJSONRequest(t, r, `
118{
119 "nodes": [
120 {
121 "address": "10.2.2.3",
122 "port": 80,
123 "condition": "ENABLED",
124 "type": "PRIMARY"
125 },
126 {
127 "address": "10.2.2.4",
128 "port": 81,
129 "condition": "ENABLED",
130 "type": "SECONDARY"
131 }
132 ]
133}
134 `)
135
136 w.Header().Add("Content-Type", "application/json")
137 w.WriteHeader(422) // Unprocessable Entity
138
139 fmt.Fprintf(w, `
140{
141 "code": 422,
142 "message": "Load Balancer '%d' has a status of 'PENDING_UPDATE' and is considered immutable."
143}
144 `, lbID)
145 })
146}
147
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100148func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) {
149 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100150 th.TestMethod(t, r, "DELETE")
151 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
152
153 r.ParseForm()
154
155 for k, v := range ids {
156 fids := r.Form["id"]
157 th.AssertEquals(t, strconv.Itoa(v), fids[k])
158 }
159
160 w.WriteHeader(http.StatusAccepted)
161 })
162}
163
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100164func mockDeleteResponse(t *testing.T, lbID, nodeID int) {
Jamie Hannaford51175a02014-11-03 13:29:44 +0100165 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100166 th.TestMethod(t, r, "DELETE")
167 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford2592f1f2014-11-06 11:10:38 +0100168 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100169 })
170}
171
Jamie Hannaford51175a02014-11-03 13:29:44 +0100172func mockGetResponse(t *testing.T, lbID, nodeID int) {
173 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100174 th.TestMethod(t, r, "GET")
175 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
176
177 w.Header().Add("Content-Type", "application/json")
178 w.WriteHeader(http.StatusOK)
179
180 fmt.Fprintf(w, `
181{
182 "node": {
183 "id": 410,
184 "address": "10.1.1.1",
185 "port": 80,
186 "condition": "ENABLED",
187 "status": "ONLINE",
188 "weight": 12,
189 "type": "PRIMARY"
190 }
191}
192 `)
193 })
194}
195
Jamie Hannaford51175a02014-11-03 13:29:44 +0100196func mockUpdateResponse(t *testing.T, lbID, nodeID int) {
197 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100198 th.TestMethod(t, r, "PUT")
199 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
200
201 th.TestJSONRequest(t, r, `
202{
203 "node": {
Jamie Hannaford00222d72014-11-03 13:58:52 +0100204 "condition": "DRAINING",
205 "weight": 10,
206 "type": "SECONDARY"
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100207 }
208}
209 `)
210
Jamie Hannaford00222d72014-11-03 13:58:52 +0100211 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100212 })
213}
214
Jamie Hannaford703527e2014-11-05 12:38:15 +0100215func mockListEventsResponse(t *testing.T, lbID int) {
216 th.Mux.HandleFunc(_rootURL(lbID)+"/events", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100217 th.TestMethod(t, r, "GET")
218 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
219
220 w.Header().Add("Content-Type", "application/json")
221 w.WriteHeader(http.StatusOK)
222
223 fmt.Fprintf(w, `
224{
225 "nodeServiceEvents": [
226 {
227 "detailedMessage": "Node is ok",
228 "nodeId": 373,
229 "id": 7,
230 "type": "UPDATE_NODE",
231 "description": "Node '373' status changed to 'ONLINE' for load balancer '323'",
232 "category": "UPDATE",
233 "severity": "INFO",
234 "relativeUri": "/406271/loadbalancers/323/nodes/373/events",
235 "accountId": 406271,
236 "loadbalancerId": 323,
237 "title": "Node Status Updated",
238 "author": "Rackspace Cloud",
239 "created": "10-30-2012 10:18:23"
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100240 }
241 ]
242}
243`)
244 })
245}