Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 1 | package nodes |
| 2 | |
| 3 | import ( |
| 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 13 | func _rootURL(lbID int) string { |
| 14 | return "/loadbalancers/" + strconv.Itoa(lbID) + "/nodes" |
| 15 | } |
| 16 | |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 17 | func _nodeURL(lbID, nodeID int) string { |
| 18 | return _rootURL(lbID) + "/" + strconv.Itoa(nodeID) |
| 19 | } |
| 20 | |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 21 | func mockListResponse(t *testing.T, lbID int) { |
| 22 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 23 | 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 Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 33 | "id": 410, |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 34 | "address": "10.1.1.1", |
| 35 | "port": 80, |
| 36 | "condition": "ENABLED", |
| 37 | "status": "ONLINE", |
| 38 | "weight": 3, |
| 39 | "type": "PRIMARY" |
| 40 | }, |
| 41 | { |
Jamie Hannaford | 3cfa00a | 2014-11-03 11:16:35 +0100 | [diff] [blame] | 42 | "id": 411, |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 43 | "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 Hannaford | ed8b89a | 2014-11-03 12:24:19 +0100 | [diff] [blame] | 56 | func mockCreateResponse(t *testing.T, lbID int) { |
| 57 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 58 | 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 Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 81 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 82 | |
| 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 Hannaford | 16bebfc | 2014-11-03 12:52:30 +0100 | [diff] [blame] | 110 | func mockBatchDeleteResponse(t *testing.T, lbID int, ids []int) { |
| 111 | th.Mux.HandleFunc(_rootURL(lbID), func(w http.ResponseWriter, r *http.Request) { |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 112 | 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 Hannaford | 9f4870f | 2014-11-03 14:03:16 +0100 | [diff] [blame] | 126 | func mockDeleteResponse(t *testing.T, lbID, nodeID int) { |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 127 | 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] | 128 | th.TestMethod(t, r, "DELETE") |
| 129 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
Jamie Hannaford | 2592f1f | 2014-11-06 11:10:38 +0100 | [diff] [blame] | 130 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 131 | }) |
| 132 | } |
| 133 | |
Jamie Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 134 | func mockGetResponse(t *testing.T, lbID, nodeID int) { |
| 135 | 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] | 136 | 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 Hannaford | 51175a0 | 2014-11-03 13:29:44 +0100 | [diff] [blame] | 158 | func mockUpdateResponse(t *testing.T, lbID, nodeID int) { |
| 159 | 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] | 160 | 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 Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 166 | "condition": "DRAINING", |
| 167 | "weight": 10, |
| 168 | "type": "SECONDARY" |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | `) |
| 172 | |
Jamie Hannaford | 00222d7 | 2014-11-03 13:58:52 +0100 | [diff] [blame] | 173 | w.WriteHeader(http.StatusAccepted) |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 174 | }) |
| 175 | } |
| 176 | |
Jamie Hannaford | 703527e | 2014-11-05 12:38:15 +0100 | [diff] [blame] | 177 | func mockListEventsResponse(t *testing.T, lbID int) { |
| 178 | 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] | 179 | th.TestMethod(t, r, "GET") |
| 180 | th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) |
| 181 | |
| 182 | w.Header().Add("Content-Type", "application/json") |
| 183 | w.WriteHeader(http.StatusOK) |
| 184 | |
| 185 | fmt.Fprintf(w, ` |
| 186 | { |
| 187 | "nodeServiceEvents": [ |
| 188 | { |
| 189 | "detailedMessage": "Node is ok", |
| 190 | "nodeId": 373, |
| 191 | "id": 7, |
| 192 | "type": "UPDATE_NODE", |
| 193 | "description": "Node '373' status changed to 'ONLINE' for load balancer '323'", |
| 194 | "category": "UPDATE", |
| 195 | "severity": "INFO", |
| 196 | "relativeUri": "/406271/loadbalancers/323/nodes/373/events", |
| 197 | "accountId": 406271, |
| 198 | "loadbalancerId": 323, |
| 199 | "title": "Node Status Updated", |
| 200 | "author": "Rackspace Cloud", |
| 201 | "created": "10-30-2012 10:18:23" |
Jamie Hannaford | 2a8031e | 2014-11-03 10:31:20 +0100 | [diff] [blame] | 202 | } |
| 203 | ] |
| 204 | } |
| 205 | `) |
| 206 | }) |
| 207 | } |