blob: f0c034ca05b4b150fbb1b33ab5bad3b9d1f9d156 [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
17func mockListResponse(t *testing.T, lbID int) {
18 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010019 th.TestMethod(t, r, "GET")
20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
21
22 w.Header().Add("Content-Type", "application/json")
23 w.WriteHeader(http.StatusOK)
24
25 fmt.Fprintf(w, `
26{
27 "nodes": [
28 {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010029 "id": 410,
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010030 "address": "10.1.1.1",
31 "port": 80,
32 "condition": "ENABLED",
33 "status": "ONLINE",
34 "weight": 3,
35 "type": "PRIMARY"
36 },
37 {
Jamie Hannaford3cfa00a2014-11-03 11:16:35 +010038 "id": 411,
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010039 "address": "10.1.1.2",
40 "port": 80,
41 "condition": "ENABLED",
42 "status": "ONLINE",
43 "weight": 8,
44 "type": "SECONDARY"
45 }
46 ]
47}
48 `)
49 })
50}
51
Jamie Hannaforded8b89a2014-11-03 12:24:19 +010052func mockCreateResponse(t *testing.T, lbID int) {
53 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +010054 th.TestMethod(t, r, "POST")
55 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
56
57 th.TestJSONRequest(t, r, `
58{
59 "nodes": [
60 {
61 "address": "10.2.2.3",
62 "port": 80,
63 "condition": "ENABLED",
64 "type": "PRIMARY"
65 },
66 {
67 "address": "10.2.2.4",
68 "port": 81,
69 "condition": "ENABLED",
70 "type": "SECONDARY"
71 }
72 ]
73}
74 `)
75
76 w.Header().Add("Content-Type", "application/json")
77 w.WriteHeader(http.StatusOK)
78
79 fmt.Fprintf(w, `
80{
81 "nodes": [
82 {
83 "address": "10.2.2.3",
84 "id": 185,
85 "port": 80,
86 "status": "ONLINE",
87 "condition": "ENABLED",
88 "weight": 1,
89 "type": "PRIMARY"
90 },
91 {
92 "address": "10.2.2.4",
93 "id": 186,
94 "port": 81,
95 "status": "ONLINE",
96 "condition": "ENABLED",
97 "weight": 1,
98 "type": "SECONDARY"
99 }
100 ]
101}
102 `)
103 })
104}
105
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100106func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) {
107 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100108 th.TestMethod(t, r, "DELETE")
109 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
110
111 r.ParseForm()
112
113 for k, v := range ids {
114 fids := r.Form["id"]
115 th.AssertEquals(t, strconv.Itoa(v), fids[k])
116 }
117
118 w.WriteHeader(http.StatusAccepted)
119 })
120}
121
122func mockDeleteLBResponse(t *testing.T, id int) {
123 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
124 th.TestMethod(t, r, "DELETE")
125 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
126 w.WriteHeader(http.StatusAccepted)
127 })
128}
129
130func mockGetResponse(t *testing.T, id int) {
131 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
132 th.TestMethod(t, r, "GET")
133 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
134
135 w.Header().Add("Content-Type", "application/json")
136 w.WriteHeader(http.StatusOK)
137
138 fmt.Fprintf(w, `
139{
140 "node": {
141 "id": 410,
142 "address": "10.1.1.1",
143 "port": 80,
144 "condition": "ENABLED",
145 "status": "ONLINE",
146 "weight": 12,
147 "type": "PRIMARY"
148 }
149}
150 `)
151 })
152}
153
154func mockUpdateResponse(t *testing.T, id int) {
155 th.Mux.HandleFunc("/loadbalancers/"+strconv.Itoa(id), func(w http.ResponseWriter, r *http.Request) {
156 th.TestMethod(t, r, "PUT")
157 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
158
159 th.TestJSONRequest(t, r, `
160{
161 "node": {
162 "condition": "ENABLED",
163 "weight": 59
164 }
165}
166 `)
167
168 w.WriteHeader(http.StatusOK)
169 })
170}
171
172func mockListEventsResponse(t *testing.T) {
173 th.Mux.HandleFunc("/loadbalancers", func(w http.ResponseWriter, r *http.Request) {
174 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 "nodeServiceEvents": [
183 {
184 "detailedMessage": "Node is ok",
185 "nodeId": 373,
186 "id": 7,
187 "type": "UPDATE_NODE",
188 "description": "Node '373' status changed to 'ONLINE' for load balancer '323'",
189 "category": "UPDATE",
190 "severity": "INFO",
191 "relativeUri": "/406271/loadbalancers/323/nodes/373/events",
192 "accountId": 406271,
193 "loadbalancerId": 323,
194 "title": "Node Status Updated",
195 "author": "Rackspace Cloud",
196 "created": "10-30-2012 10:18:23"
197 },
198 {
199 "detailedMessage": "Invalid HTTP response received; premature end of headers",
200 "nodeId": 373,
201 "id": 8,
202 "type": "UPDATE_NODE",
203 "description": "Node '373' status changed to 'OFFLINE' for load balancer '323'",
204 "category": "UPDATE",
205 "severity": "INFO",
206 "relativeUri": "/406271/loadbalancers/323/nodes/373/events",
207 "accountId": 406271,
208 "loadbalancerId": 323,
209 "title": "Node Status Updated",
210 "author": "Rackspace Cloud",
211 "created": "10-30-2012 11:22:25"
212 }
213 ]
214}
215`)
216 })
217}