Keith Byrne | bda4859 | 2016-03-23 11:37:08 +0000 | [diff] [blame] | 1 | // +build fixtures |
| 2 | |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 3 | package nodes |
| 4 | |
| 5 | import ( |
| 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 15 | func _rootURL(lbID int) string { |
| 16 | return "/loadbalancers/" + strconv.Itoa(lbID) + "/nodes" |
| 17 | } |
| 18 | |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 19 | func _nodeURL(lbID, nodeID int) string { |
| 20 | return _rootURL(lbID) + "/" + strconv.Itoa(nodeID) |
| 21 | } |
| 22 | |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 23 | func mockListResponse(t *testing.T, lbID int) { |
| 24 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 25 | 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 35 | "id": 410, |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 36 | "address": "10.1.1.1", |
| 37 | "port": 80, |
| 38 | "condition": "ENABLED", |
| 39 | "status": "ONLINE", |
| 40 | "weight": 3, |
| 41 | "type": "PRIMARY" |
| 42 | }, |
| 43 | { |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 44 | "id": 411, |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 45 | "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 Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 58 | func mockCreateResponse(t *testing.T, lbID int) { |
| 59 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 60 | 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 Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 83 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 84 | |
| 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 Wilson | ae55143 | 2015-06-24 09:40:40 -0400 | [diff] [blame] | 112 | func 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 Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 148 | func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) { |
| 149 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 150 | 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 Hannaford | 9f4870f | 2014-11-03 14:03:16 +0100 | [diff] [blame] | 164 | func mockDeleteResponse(t *testing.T, lbID, nodeID int) { |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 165 | th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 166 | th.TestMethod(t, r, "DELETE") |
| 167 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 168 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 169 | }) |
| 170 | } |
| 171 | |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 172 | func mockGetResponse(t *testing.T, lbID, nodeID int) { |
| 173 | th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 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 | "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 Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 196 | func mockUpdateResponse(t *testing.T, lbID, nodeID int) { |
| 197 | th.Mux.HandleFunc(_nodeURL(lbID, nodeID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 198 | 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 Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 204 | "condition": "DRAINING", |
| 205 | "weight": 10, |
| 206 | "type": "SECONDARY" |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | `) |
| 210 | |
Jamie Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 211 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 212 | }) |
| 213 | } |
| 214 | |
Jamie Hannaford | 703527e | 2014-11-05 12:38:15 +0100 | [diff] [blame] | 215 | func mockListEventsResponse(t *testing.T, lbID int) { |
| 216 | th.Mux.HandleFunc(_rootURL(lbID)+"/events", func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 217 | 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 Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 240 | } |
| 241 | ] |
| 242 | } |
| 243 | `) |
| 244 | }) |
| 245 | } |