blob: 0aea54103630a3257954b4651ab267f7e540ed70 [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
Jamie Hannaford16bebfc2014-11-03 12:52:30 +0100110func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) {
111 th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100112 th.TestMethod(t, r, "DELETE")
113 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
114
115 r.ParseForm()
116
117 for k, v := range ids {
118 fids := r.Form["id"]
119 th.AssertEquals(t, strconv.Itoa(v), fids[k])
120 }
121
122 w.WriteHeader(http.StatusAccepted)
123 })
124}
125
Jamie Hannaford9f4870f2014-11-03 14:03:16 +0100126func mockDeleteResponse(t *testing.T, lbID, nodeID int) {
Jamie Hannaford51175a02014-11-03 13:29:44 +0100127 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100128 th.TestMethod(t, r, "DELETE")
129 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
Jamie Hannaford2592f1f2014-11-06 11:10:38 +0100130 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100131 })
132}
133
Jamie Hannaford51175a02014-11-03 13:29:44 +0100134func mockGetResponse(t *testing.T, lbID, nodeID int) {
135 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100136 th.TestMethod(t, r, "GET")
137 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
138
139 w.Header().Add("Content-Type", "application/json")
140 w.WriteHeader(http.StatusOK)
141
142 fmt.Fprintf(w, `
143{
144 "node": {
145 "id": 410,
146 "address": "10.1.1.1",
147 "port": 80,
148 "condition": "ENABLED",
149 "status": "ONLINE",
150 "weight": 12,
151 "type": "PRIMARY"
152 }
153}
154 `)
155 })
156}
157
Jamie Hannaford51175a02014-11-03 13:29:44 +0100158func mockUpdateResponse(t *testing.T, lbID, nodeID int) {
159 th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100160 th.TestMethod(t, r, "PUT")
161 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
162
163 th.TestJSONRequest(t, r, `
164{
165 "node": {
Jamie Hannaford00222d72014-11-03 13:58:52 +0100166 "address": "1.2.3.4",
167 "condition": "DRAINING",
168 "weight": 10,
169 "type": "SECONDARY"
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100170 }
171}
172 `)
173
Jamie Hannaford00222d72014-11-03 13:58:52 +0100174 w.WriteHeader(http.StatusAccepted)
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100175 })
176}
177
Jamie Hannaford703527e2014-11-05 12:38:15 +0100178func mockListEventsResponse(t *testing.T, lbID int) {
179 th.Mux.HandleFunc(_rootURL(lbID)+"/events", func(w http.ResponseWriter, r *http.Request) {
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100180 th.TestMethod(t, r, "GET")
181 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
182
183 w.Header().Add("Content-Type", "application/json")
184 w.WriteHeader(http.StatusOK)
185
186 fmt.Fprintf(w, `
187{
188 "nodeServiceEvents": [
189 {
190 "detailedMessage": "Node is ok",
191 "nodeId": 373,
192 "id": 7,
193 "type": "UPDATE_NODE",
194 "description": "Node '373' status changed to 'ONLINE' for load balancer '323'",
195 "category": "UPDATE",
196 "severity": "INFO",
197 "relativeUri": "/406271/loadbalancers/323/nodes/373/events",
198 "accountId": 406271,
199 "loadbalancerId": 323,
200 "title": "Node Status Updated",
201 "author": "Rackspace Cloud",
202 "created": "10-30-2012 10:18:23"
Jamie Hannaford2a8031e2014-11-03 10:31:20 +0100203 }
204 ]
205}
206`)
207 })
208}