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