blob: 8899fc5e975ced22a8486cc09cb2d2c8423508a1 [file] [log] [blame]
Jamie Hannaford2a8031e2014-11-03 10:31:20 +01001package nodes
2
3import (
4 "fmt"
5 "net/http"
6 "strconv"
7 "testing"
8
9 th "github.com/rackspace/gophercloud/testhelper"
10 fake "github.com/rackspace/gophercloud/testhelper/client"
11)
12
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010013func _rootURL(lbID int) string {
14 return "/loadbalancers/" + strconv.Itoa(lbID) + "/nodes"
15}
16
Jamie Hannaford51175a02014-11-03 13:29:44 +010017func _nodeURL(lbID, nodeID int) string {
18 return _rootURL(lbID) + "/" + strconv.Itoa(nodeID)
19}
20
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010021func mockListResponse(t *testing.T, lbID int) {
22 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010023 th.TestMethod(t, r, "GET")
24 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
25
26 w.Header().Add("Content-Type", "application/json")
27 w.WriteHeader(http.StatusOK)
28
29 fmt.Fprintf(w, `
30{
31 "nodes": [
32 {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010033 "id": 410,
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010034 "address": "10.1.1.1",
35 "port": 80,
36 "condition": "ENABLED",
37 "status": "ONLINE",
38 "weight": 3,
39 "type": "PRIMARY"
40 },
41 {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010042 "id": 411,
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010043 "address": "10.1.1.2",
44 "port": 80,
45 "condition": "ENABLED",
46 "status": "ONLINE",
47 "weight": 8,
48 "type": "SECONDARY"
49 }
50 ]
51}
52 `)
53 })
54}
55
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010056func mockCreateResponse(t *testing.T, lbID int) {
57 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010058 th.TestMethod(t, r, "POST")
59 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
60
61 th.TestJSONRequest(t, r, `
62{
63 "nodes": [
64 {
65 "address": "10.2.2.3",
66 "port": 80,
67 "condition": "ENABLED",
68 "type": "PRIMARY"
69 },
70 {
71 "address": "10.2.2.4",
72 "port": 81,
73 "condition": "ENABLED",
74 "type": "SECONDARY"
75 }
76 ]
77}
78 `)
79
80 w.Header().Add("Content-Type", "application/json")
Jamie Hannaford2592f1f2014-11-06 11:10:38 +010081 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010082
83 fmt.Fprintf(w, `
84{
85 "nodes": [
86 {
87 "address": "10.2.2.3",
88 "id": 185,
89 "port": 80,
90 "status": "ONLINE",
91 "condition": "ENABLED",
92 "weight": 1,
93 "type": "PRIMARY"
94 },
95 {
96 "address": "10.2.2.4",
97 "id": 186,
98 "port": 81,
99 "status": "ONLINE",
100 "condition": "ENABLED",
101 "weight": 1,
102 "type": "SECONDARY"
103 }
104 ]
105}
106 `)
107 })
108}
109
Ash Wilsonae551432015-06-24 09:40:40 -0400110func mockCreateErrResponse(t *testing.T, lbID int) {
111 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
112 th.TestMethod(t, r, "POST")
113 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
114
115 th.TestJSONRequest(t, r, `
116{
117 "nodes": [
118 {
119 "address": "10.2.2.3",
120 "port": 80,
121 "condition": "ENABLED",
122 "type": "PRIMARY"
123 },
124 {
125 "address": "10.2.2.4",
126 "port": 81,
127 "condition": "ENABLED",
128 "type": "SECONDARY"
129 }
130 ]
131}
132 `)
133
134 w.Header().Add("Content-Type", "application/json")
135 w.WriteHeader(422) // Unprocessable Entity
136
137 fmt.Fprintf(w, `
138{
139 "code": 422,
140 "message": "Load Balancer '%d' has a status of 'PENDING_UPDATE' and is considered immutable."
141}
142 `, lbID)
143 })
144}
145
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100146func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) {
147 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100148 th.TestMethod(t, r, "DELETE")
149 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
150
151 r.ParseForm()
152
153 for k, v := range ids {
154 fids := r.Form["id"]
155 th.AssertEquals(t, strconv.Itoa(v), fids[k])
156 }
157
158 w.WriteHeader(http.StatusAccepted)
159 })
160}
161
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100162func mockDeleteResponse(t *testing.T, lbID, nodeID int) {
Jamie Hannaford51175a02014-11-03 13:29:44 +0100163 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100164 th.TestMethod(t, r, "DELETE")
165 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford2592f1f2014-11-06 11:10:38 +0100166 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100167 })
168}
169
Jamie Hannaford51175a02014-11-03 13:29:44 +0100170func mockGetResponse(t *testing.T, lbID, nodeID int) {
171 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100172 th.TestMethod(t, r, "GET")
173 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
174
175 w.Header().Add("Content-Type", "application/json")
176 w.WriteHeader(http.StatusOK)
177
178 fmt.Fprintf(w, `
179{
180 "node": {
181 "id": 410,
182 "address": "10.1.1.1",
183 "port": 80,
184 "condition": "ENABLED",
185 "status": "ONLINE",
186 "weight": 12,
187 "type": "PRIMARY"
188 }
189}
190 `)
191 })
192}
193
Jamie Hannaford51175a02014-11-03 13:29:44 +0100194func mockUpdateResponse(t *testing.T, lbID, nodeID int) {
195 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100196 th.TestMethod(t, r, "PUT")
197 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
198
199 th.TestJSONRequest(t, r, `
200{
201 "node": {
Jamie Hannaford00222d72014-11-03 13:58:52 +0100202 "condition": "DRAINING",
203 "weight": 10,
204 "type": "SECONDARY"
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100205 }
206}
207 `)
208
Jamie Hannaford00222d72014-11-03 13:58:52 +0100209 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100210 })
211}
212
Jamie Hannaford703527e2014-11-05 12:38:15 +0100213func mockListEventsResponse(t *testing.T, lbID int) {
214 th.Mux.HandleFunc(_rootURL(lbID)+"/events", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100215 th.TestMethod(t, r, "GET")
216 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
217
218 w.Header().Add("Content-Type", "application/json")
219 w.WriteHeader(http.StatusOK)
220
221 fmt.Fprintf(w, `
222{
223 "nodeServiceEvents": [
224 {
225 "detailedMessage": "Node is ok",
226 "nodeId": 373,
227 "id": 7,
228 "type": "UPDATE_NODE",
229 "description": "Node '373' status changed to 'ONLINE' for load balancer '323'",
230 "category": "UPDATE",
231 "severity": "INFO",
232 "relativeUri": "/406271/loadbalancers/323/nodes/373/events",
233 "accountId": 406271,
234 "loadbalancerId": 323,
235 "title": "Node Status Updated",
236 "author": "Rackspace Cloud",
237 "created": "10-30-2012 10:18:23"
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100238 }
239 ]
240}
241`)
242 })
243}