github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/lbaas_v2/loadbalancers/testing/fixtures_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/listeners" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/loadbalancers" 10 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/monitors" 11 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/lbaas_v2/pools" 12 th "github.com/gophercloud/gophercloud/testhelper" 13 "github.com/gophercloud/gophercloud/testhelper/client" 14 ) 15 16 // LoadbalancersListBody contains the canned body of a loadbalancer list response. 17 const LoadbalancersListBody = ` 18 { 19 "loadbalancers":[ 20 { 21 "id": "c331058c-6a40-4144-948e-b9fb1df9db4b", 22 "tenant_id": "54030507-44f7-473c-9342-b4d14a95f692", 23 "name": "web_lb", 24 "description": "lb config for the web tier", 25 "vip_subnet_id": "8a49c438-848f-467b-9655-ea1548708154", 26 "vip_address": "10.30.176.47", 27 "vip_port_id": "2a22e552-a347-44fd-b530-1f2b1b2a6735", 28 "flavor_id": "60df399a-ee85-11e9-81b4-2a2ae2dbcce4", 29 "provider": "haproxy", 30 "admin_state_up": true, 31 "provisioning_status": "ACTIVE", 32 "operating_status": "ONLINE" 33 }, 34 { 35 "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 36 "tenant_id": "54030507-44f7-473c-9342-b4d14a95f692", 37 "name": "db_lb", 38 "description": "lb config for the db tier", 39 "vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086", 40 "vip_address": "10.30.176.48", 41 "vip_port_id": "2bf413c8-41a9-4477-b505-333d5cbe8b55", 42 "flavor_id": "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4", 43 "provider": "haproxy", 44 "admin_state_up": true, 45 "provisioning_status": "PENDING_CREATE", 46 "operating_status": "OFFLINE" 47 } 48 ] 49 } 50 ` 51 52 // SingleLoadbalancerBody is the canned body of a Get request on an existing loadbalancer. 53 const SingleLoadbalancerBody = ` 54 { 55 "loadbalancer": { 56 "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 57 "tenant_id": "54030507-44f7-473c-9342-b4d14a95f692", 58 "name": "db_lb", 59 "description": "lb config for the db tier", 60 "vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086", 61 "vip_address": "10.30.176.48", 62 "vip_port_id": "2bf413c8-41a9-4477-b505-333d5cbe8b55", 63 "flavor_id": "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4", 64 "provider": "haproxy", 65 "admin_state_up": true, 66 "provisioning_status": "PENDING_CREATE", 67 "operating_status": "OFFLINE" 68 } 69 } 70 ` 71 72 // PostUpdateLoadbalancerBody is the canned response body of a Update request on an existing loadbalancer. 73 const PostUpdateLoadbalancerBody = ` 74 { 75 "loadbalancer": { 76 "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 77 "tenant_id": "54030507-44f7-473c-9342-b4d14a95f692", 78 "name": "NewLoadbalancerName", 79 "description": "lb config for the db tier", 80 "vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086", 81 "vip_address": "10.30.176.48", 82 "vip_port_id": "2bf413c8-41a9-4477-b505-333d5cbe8b55", 83 "flavor_id": "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4", 84 "provider": "haproxy", 85 "admin_state_up": true, 86 "provisioning_status": "PENDING_CREATE", 87 "operating_status": "OFFLINE" 88 } 89 } 90 ` 91 92 // GetLoadbalancerStatusesBody is the canned request body of a Get request on loadbalancer's status. 93 const GetLoadbalancerStatusesBody = ` 94 { 95 "statuses" : { 96 "loadbalancer": { 97 "id": "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 98 "name": "db_lb", 99 "provisioning_status": "PENDING_UPDATE", 100 "operating_status": "ACTIVE", 101 "listeners": [{ 102 "id": "db902c0c-d5ff-4753-b465-668ad9656918", 103 "name": "db", 104 "provisioning_status": "ACTIVE", 105 "pools": [{ 106 "id": "fad389a3-9a4a-4762-a365-8c7038508b5d", 107 "name": "db", 108 "provisioning_status": "ACTIVE", 109 "healthmonitor": { 110 "id": "67306cda-815d-4354-9fe4-59e09da9c3c5", 111 "type":"PING", 112 "provisioning_status": "ACTIVE" 113 }, 114 "members":[{ 115 "id": "2a280670-c202-4b0b-a562-34077415aabf", 116 "name": "db", 117 "address": "10.0.2.11", 118 "protocol_port": 80, 119 "provisioning_status": "ACTIVE" 120 }] 121 }] 122 }] 123 } 124 } 125 } 126 ` 127 128 // LoadbalancerStatsTree is the canned request body of a Get request on loadbalancer's statistics. 129 const GetLoadbalancerStatsBody = ` 130 { 131 "stats": { 132 "active_connections": 0, 133 "bytes_in": 9532, 134 "bytes_out": 22033, 135 "request_errors": 46, 136 "total_connections": 112 137 } 138 } 139 ` 140 141 var ( 142 LoadbalancerWeb = loadbalancers.LoadBalancer{ 143 ID: "c331058c-6a40-4144-948e-b9fb1df9db4b", 144 TenantID: "54030507-44f7-473c-9342-b4d14a95f692", 145 Name: "web_lb", 146 Description: "lb config for the web tier", 147 VipSubnetID: "8a49c438-848f-467b-9655-ea1548708154", 148 VipAddress: "10.30.176.47", 149 VipPortID: "2a22e552-a347-44fd-b530-1f2b1b2a6735", 150 FlavorID: "60df399a-ee85-11e9-81b4-2a2ae2dbcce4", 151 Provider: "haproxy", 152 AdminStateUp: true, 153 ProvisioningStatus: "ACTIVE", 154 OperatingStatus: "ONLINE", 155 } 156 LoadbalancerDb = loadbalancers.LoadBalancer{ 157 ID: "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 158 TenantID: "54030507-44f7-473c-9342-b4d14a95f692", 159 Name: "db_lb", 160 Description: "lb config for the db tier", 161 VipSubnetID: "9cedb85d-0759-4898-8a4b-fa5a5ea10086", 162 VipAddress: "10.30.176.48", 163 VipPortID: "2bf413c8-41a9-4477-b505-333d5cbe8b55", 164 FlavorID: "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4", 165 Provider: "haproxy", 166 AdminStateUp: true, 167 ProvisioningStatus: "PENDING_CREATE", 168 OperatingStatus: "OFFLINE", 169 } 170 LoadbalancerUpdated = loadbalancers.LoadBalancer{ 171 ID: "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 172 TenantID: "54030507-44f7-473c-9342-b4d14a95f692", 173 Name: "NewLoadbalancerName", 174 Description: "lb config for the db tier", 175 VipSubnetID: "9cedb85d-0759-4898-8a4b-fa5a5ea10086", 176 VipAddress: "10.30.176.48", 177 VipPortID: "2bf413c8-41a9-4477-b505-333d5cbe8b55", 178 FlavorID: "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4", 179 Provider: "haproxy", 180 AdminStateUp: true, 181 ProvisioningStatus: "PENDING_CREATE", 182 OperatingStatus: "OFFLINE", 183 } 184 LoadbalancerStatusesTree = loadbalancers.StatusTree{ 185 Loadbalancer: &loadbalancers.LoadBalancer{ 186 ID: "36e08a3e-a78f-4b40-a229-1e7e23eee1ab", 187 Name: "db_lb", 188 ProvisioningStatus: "PENDING_UPDATE", 189 OperatingStatus: "ACTIVE", 190 Listeners: []listeners.Listener{{ 191 ID: "db902c0c-d5ff-4753-b465-668ad9656918", 192 Name: "db", 193 ProvisioningStatus: "ACTIVE", 194 Pools: []pools.Pool{{ 195 ID: "fad389a3-9a4a-4762-a365-8c7038508b5d", 196 Name: "db", 197 ProvisioningStatus: "ACTIVE", 198 Monitor: monitors.Monitor{ 199 ID: "67306cda-815d-4354-9fe4-59e09da9c3c5", 200 Type: "PING", 201 ProvisioningStatus: "ACTIVE", 202 }, 203 Members: []pools.Member{{ 204 ID: "2a280670-c202-4b0b-a562-34077415aabf", 205 Name: "db", 206 Address: "10.0.2.11", 207 ProtocolPort: 80, 208 ProvisioningStatus: "ACTIVE", 209 }}, 210 }}, 211 }}, 212 }, 213 } 214 LoadbalancerStatsTree = loadbalancers.Stats{ 215 ActiveConnections: 0, 216 BytesIn: 9532, 217 BytesOut: 22033, 218 RequestErrors: 46, 219 TotalConnections: 112, 220 } 221 ) 222 223 // HandleLoadbalancerListSuccessfully sets up the test server to respond to a loadbalancer List request. 224 func HandleLoadbalancerListSuccessfully(t *testing.T) { 225 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers", func(w http.ResponseWriter, r *http.Request) { 226 th.TestMethod(t, r, "GET") 227 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 228 229 w.Header().Add("Content-Type", "application/json") 230 r.ParseForm() 231 marker := r.Form.Get("marker") 232 switch marker { 233 case "": 234 fmt.Fprintf(w, LoadbalancersListBody) 235 case "45e08a3e-a78f-4b40-a229-1e7e23eee1ab": 236 fmt.Fprintf(w, `{ "loadbalancers": [] }`) 237 default: 238 t.Fatalf("/v2.0/lbaas/loadbalancers invoked with unexpected marker=[%s]", marker) 239 } 240 }) 241 } 242 243 // HandleLoadbalancerCreationSuccessfully sets up the test server to respond to a loadbalancer creation request 244 // with a given response. 245 func HandleLoadbalancerCreationSuccessfully(t *testing.T, response string) { 246 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers", func(w http.ResponseWriter, r *http.Request) { 247 th.TestMethod(t, r, "POST") 248 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 249 th.TestJSONRequest(t, r, `{ 250 "loadbalancer": { 251 "name": "db_lb", 252 "vip_subnet_id": "9cedb85d-0759-4898-8a4b-fa5a5ea10086", 253 "vip_address": "10.30.176.48", 254 "flavor_id": "bba40eb2-ee8c-11e9-81b4-2a2ae2dbcce4", 255 "provider": "haproxy", 256 "admin_state_up": true 257 } 258 }`) 259 260 w.WriteHeader(http.StatusAccepted) 261 w.Header().Add("Content-Type", "application/json") 262 fmt.Fprintf(w, response) 263 }) 264 } 265 266 // HandleLoadbalancerGetSuccessfully sets up the test server to respond to a loadbalancer Get request. 267 func HandleLoadbalancerGetSuccessfully(t *testing.T) { 268 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab", func(w http.ResponseWriter, r *http.Request) { 269 th.TestMethod(t, r, "GET") 270 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 271 th.TestHeader(t, r, "Accept", "application/json") 272 273 fmt.Fprintf(w, SingleLoadbalancerBody) 274 }) 275 } 276 277 // HandleLoadbalancerGetStatusesTree sets up the test server to respond to a loadbalancer Get statuses tree request. 278 func HandleLoadbalancerGetStatusesTree(t *testing.T) { 279 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab/statuses", func(w http.ResponseWriter, r *http.Request) { 280 th.TestMethod(t, r, "GET") 281 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 282 th.TestHeader(t, r, "Accept", "application/json") 283 284 fmt.Fprintf(w, GetLoadbalancerStatusesBody) 285 }) 286 } 287 288 // HandleLoadbalancerDeletionSuccessfully sets up the test server to respond to a loadbalancer deletion request. 289 func HandleLoadbalancerDeletionSuccessfully(t *testing.T) { 290 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab", func(w http.ResponseWriter, r *http.Request) { 291 th.TestMethod(t, r, "DELETE") 292 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 293 294 w.WriteHeader(http.StatusNoContent) 295 }) 296 } 297 298 // HandleLoadbalancerUpdateSuccessfully sets up the test server to respond to a loadbalancer Update request. 299 func HandleLoadbalancerUpdateSuccessfully(t *testing.T) { 300 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab", func(w http.ResponseWriter, r *http.Request) { 301 th.TestMethod(t, r, "PUT") 302 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 303 th.TestHeader(t, r, "Accept", "application/json") 304 th.TestHeader(t, r, "Content-Type", "application/json") 305 th.TestJSONRequest(t, r, `{ 306 "loadbalancer": { 307 "name": "NewLoadbalancerName" 308 } 309 }`) 310 311 fmt.Fprintf(w, PostUpdateLoadbalancerBody) 312 }) 313 } 314 315 // HandleLoadbalancerGetStatsTree sets up the test server to respond to a loadbalancer Get stats tree request. 316 func HandleLoadbalancerGetStatsTree(t *testing.T) { 317 th.Mux.HandleFunc("/v2.0/lbaas/loadbalancers/36e08a3e-a78f-4b40-a229-1e7e23eee1ab/stats", func(w http.ResponseWriter, r *http.Request) { 318 th.TestMethod(t, r, "GET") 319 th.TestHeader(t, r, "X-Auth-Token", client.TokenID) 320 th.TestHeader(t, r, "Accept", "application/json") 321 322 fmt.Fprintf(w, GetLoadbalancerStatsBody) 323 }) 324 }