|
| 1 | +// +build fixtures |
| 2 | +package listeners |
| 3 | + |
| 4 | +import ( |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + th "github.com/rackspace/gophercloud/testhelper" |
| 10 | + "github.com/rackspace/gophercloud/testhelper/client" |
| 11 | +) |
| 12 | + |
| 13 | +// ListenersListBody contains the canned body of a listeners list response. |
| 14 | +const ListenersListBody = ` |
| 15 | +{ |
| 16 | + "listeners":[ |
| 17 | + { |
| 18 | + "id": "db902c0c-d5ff-4753-b465-668ad9656918", |
| 19 | + "tenant_id": "310df60f-2a10-4ee5-9554-98393092194c", |
| 20 | + "name": "web", |
| 21 | + "description": "listener config for the web tier", |
| 22 | + "loadbalancers": [{"id": "53306cda-815d-4354-9444-59e09da9c3c5"}], |
| 23 | + "protocol": "HTTP", |
| 24 | + "protocol_port": 80, |
| 25 | + "default_pool_id": "fad389a3-9a4a-4762-a365-8c7038508b5d", |
| 26 | + "admin_state_up": true, |
| 27 | + "default_tls_container_ref": "2c433435-20de-4411-84ae-9cc8917def76", |
| 28 | + "sni_container_refs": ["3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"] |
| 29 | + }, |
| 30 | + { |
| 31 | + "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", |
| 32 | + "tenant_id": "310df60f-2a10-4ee5-9554-98393092194c", |
| 33 | + "name": "db", |
| 34 | + "description": "listener config for the db tier", |
| 35 | + "loadbalancers": [{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}], |
| 36 | + "protocol": "TCP", |
| 37 | + "protocol_port": 3306, |
| 38 | + "default_pool_id": "41efe233-7591-43c5-9cf7-923964759f9e", |
| 39 | + "connection_limit": 2000, |
| 40 | + "admin_state_up": true, |
| 41 | + "default_tls_container_ref": "2c433435-20de-4411-84ae-9cc8917def76", |
| 42 | + "sni_container_refs": ["3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"] |
| 43 | + } |
| 44 | + ] |
| 45 | +} |
| 46 | +` |
| 47 | + |
| 48 | +// SingleServerBody is the canned body of a Get request on an existing listener. |
| 49 | +const SingleListenerBody = ` |
| 50 | +{ |
| 51 | + "listener": { |
| 52 | + "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", |
| 53 | + "tenant_id": "310df60f-2a10-4ee5-9554-98393092194c", |
| 54 | + "name": "db", |
| 55 | + "description": "listener config for the db tier", |
| 56 | + "loadbalancers": [{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}], |
| 57 | + "protocol": "TCP", |
| 58 | + "protocol_port": 3306, |
| 59 | + "default_pool_id": "41efe233-7591-43c5-9cf7-923964759f9e", |
| 60 | + "connection_limit": 2000, |
| 61 | + "admin_state_up": true, |
| 62 | + "default_tls_container_ref": "2c433435-20de-4411-84ae-9cc8917def76", |
| 63 | + "sni_container_refs": ["3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"] |
| 64 | + } |
| 65 | +} |
| 66 | +` |
| 67 | + |
| 68 | +// PostUpdateListenerBody is the canned response body of a Update request on an existing listener. |
| 69 | +const PostUpdateListenerBody = ` |
| 70 | +{ |
| 71 | + "listener": { |
| 72 | + "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", |
| 73 | + "tenant_id": "310df60f-2a10-4ee5-9554-98393092194c", |
| 74 | + "name": "NewListenerName", |
| 75 | + "description": "listener config for the db tier", |
| 76 | + "loadbalancers": [{"id": "79e05663-7f03-45d2-a092-8b94062f22ab"}], |
| 77 | + "protocol": "TCP", |
| 78 | + "protocol_port": 3306, |
| 79 | + "default_pool_id": "41efe233-7591-43c5-9cf7-923964759f9e", |
| 80 | + "connection_limit": 1000, |
| 81 | + "admin_state_up": true, |
| 82 | + "default_tls_container_ref": "2c433435-20de-4411-84ae-9cc8917def76", |
| 83 | + "sni_container_refs": ["3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"] |
| 84 | + } |
| 85 | +} |
| 86 | +` |
| 87 | + |
| 88 | +var ( |
| 89 | + ListenerWeb = Listener{ |
| 90 | + ID: "db902c0c-d5ff-4753-b465-668ad9656918", |
| 91 | + TenantID: "310df60f-2a10-4ee5-9554-98393092194c", |
| 92 | + Name: "web", |
| 93 | + Description: "listener config for the web tier", |
| 94 | + Loadbalancers: []LoadBalancerID{{ID: "53306cda-815d-4354-9444-59e09da9c3c5"}}, |
| 95 | + Protocol: "HTTP", |
| 96 | + ProtocolPort: 80, |
| 97 | + DefaultPoolID: "fad389a3-9a4a-4762-a365-8c7038508b5d", |
| 98 | + AdminStateUp: true, |
| 99 | + DefaultTlsContainerRef: "2c433435-20de-4411-84ae-9cc8917def76", |
| 100 | + SniContainerRefs: []string{"3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"}, |
| 101 | + } |
| 102 | + ListenerDb = Listener{ |
| 103 | + ID: "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", |
| 104 | + TenantID: "310df60f-2a10-4ee5-9554-98393092194c", |
| 105 | + Name: "db", |
| 106 | + Description: "listener config for the db tier", |
| 107 | + Loadbalancers: []LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}}, |
| 108 | + Protocol: "TCP", |
| 109 | + ProtocolPort: 3306, |
| 110 | + DefaultPoolID: "41efe233-7591-43c5-9cf7-923964759f9e", |
| 111 | + ConnLimit: 2000, |
| 112 | + AdminStateUp: true, |
| 113 | + DefaultTlsContainerRef: "2c433435-20de-4411-84ae-9cc8917def76", |
| 114 | + SniContainerRefs: []string{"3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"}, |
| 115 | + } |
| 116 | + ListenerUpdated = Listener{ |
| 117 | + ID: "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", |
| 118 | + TenantID: "310df60f-2a10-4ee5-9554-98393092194c", |
| 119 | + Name: "NewListenerName", |
| 120 | + Description: "listener config for the db tier", |
| 121 | + Loadbalancers: []LoadBalancerID{{ID: "79e05663-7f03-45d2-a092-8b94062f22ab"}}, |
| 122 | + Protocol: "TCP", |
| 123 | + ProtocolPort: 3306, |
| 124 | + DefaultPoolID: "41efe233-7591-43c5-9cf7-923964759f9e", |
| 125 | + ConnLimit: 1000, |
| 126 | + AdminStateUp: true, |
| 127 | + DefaultTlsContainerRef: "2c433435-20de-4411-84ae-9cc8917def76", |
| 128 | + SniContainerRefs: []string{"3d328d82-2547-4921-ac2f-61c3b452b5ff", "b3cfd7e3-8c19-455c-8ebb-d78dfd8f7e7d"}, |
| 129 | + } |
| 130 | +) |
| 131 | + |
| 132 | +// HandleListenerListSuccessfully sets up the test server to respond to a listener List request. |
| 133 | +func HandleListenerListSuccessfully(t *testing.T) { |
| 134 | + th.Mux.HandleFunc("/v2.0/lbaas/listeners", func(w http.ResponseWriter, r *http.Request) { |
| 135 | + th.TestMethod(t, r, "GET") |
| 136 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 137 | + |
| 138 | + w.Header().Add("Content-Type", "application/json") |
| 139 | + r.ParseForm() |
| 140 | + marker := r.Form.Get("marker") |
| 141 | + switch marker { |
| 142 | + case "": |
| 143 | + fmt.Fprintf(w, ListenersListBody) |
| 144 | + case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab": |
| 145 | + fmt.Fprintf(w, `{ "listeners": [] }`) |
| 146 | + default: |
| 147 | + t.Fatalf("/v2.0/lbaas/listeners invoked with unexpected marker=[%s]", marker) |
| 148 | + } |
| 149 | + }) |
| 150 | +} |
| 151 | + |
| 152 | +// HandleListenerCreationSuccessfully sets up the test server to respond to a listener creation request |
| 153 | +// with a given response. |
| 154 | +func HandleListenerCreationSuccessfully(t *testing.T, response string) { |
| 155 | + th.Mux.HandleFunc("/v2.0/lbaas/listeners", func(w http.ResponseWriter, r *http.Request) { |
| 156 | + th.TestMethod(t, r, "POST") |
| 157 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 158 | + th.TestJSONRequest(t, r, `{ |
| 159 | + "listener": { |
| 160 | + "loadbalancer_id": "79e05663-7f03-45d2-a092-8b94062f22ab", |
| 161 | + "protocol": "TCP", |
| 162 | + "name": "db", |
| 163 | + "admin_state_up": true, |
| 164 | + "default_tls_container_ref": "2c433435-20de-4411-84ae-9cc8917def76", |
| 165 | + "default_pool_id": "41efe233-7591-43c5-9cf7-923964759f9e", |
| 166 | + "protocol_port": 3306 |
| 167 | + } |
| 168 | + }`) |
| 169 | + |
| 170 | + w.WriteHeader(http.StatusAccepted) |
| 171 | + w.Header().Add("Content-Type", "application/json") |
| 172 | + fmt.Fprintf(w, response) |
| 173 | + }) |
| 174 | +} |
| 175 | + |
| 176 | +// HandleListenerGetSuccessfully sets up the test server to respond to a listener Get request. |
| 177 | +func HandleListenerGetSuccessfully(t *testing.T) { |
| 178 | + th.Mux.HandleFunc("/v2.0/lbaas/listeners/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { |
| 179 | + th.TestMethod(t, r, "GET") |
| 180 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 181 | + th.TestHeader(t, r, "Accept", "application/json") |
| 182 | + |
| 183 | + fmt.Fprintf(w, SingleListenerBody) |
| 184 | + }) |
| 185 | +} |
| 186 | + |
| 187 | +// HandleListenerDeletionSuccessfully sets up the test server to respond to a listener deletion request. |
| 188 | +func HandleListenerDeletionSuccessfully(t *testing.T) { |
| 189 | + th.Mux.HandleFunc("/v2.0/lbaas/listeners/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { |
| 190 | + th.TestMethod(t, r, "DELETE") |
| 191 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 192 | + |
| 193 | + w.WriteHeader(http.StatusNoContent) |
| 194 | + }) |
| 195 | +} |
| 196 | + |
| 197 | +// HandleListenerUpdateSuccessfully sets up the test server to respond to a listener Update request. |
| 198 | +func HandleListenerUpdateSuccessfully(t *testing.T) { |
| 199 | + th.Mux.HandleFunc("/v2.0/lbaas/listeners/4ec89087-d057-4e2c-911f-60a3b47ee304", func(w http.ResponseWriter, r *http.Request) { |
| 200 | + th.TestMethod(t, r, "PUT") |
| 201 | + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) |
| 202 | + th.TestHeader(t, r, "Accept", "application/json") |
| 203 | + th.TestHeader(t, r, "Content-Type", "application/json") |
| 204 | + th.TestJSONRequest(t, r, `{ |
| 205 | + "listener": { |
| 206 | + "name": "NewListenerName", |
| 207 | + "connection_limit": 1001 |
| 208 | + } |
| 209 | + }`) |
| 210 | + |
| 211 | + fmt.Fprintf(w, PostUpdateListenerBody) |
| 212 | + }) |
| 213 | +} |
0 commit comments