github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas/pools/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 9 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas/pools" 10 "github.com/huaweicloud/golangsdk/pagination" 11 th "github.com/huaweicloud/golangsdk/testhelper" 12 ) 13 14 func TestList(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/v2.0/lb/pools", func(w http.ResponseWriter, r *http.Request) { 19 th.TestMethod(t, r, "GET") 20 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 21 22 w.Header().Add("Content-Type", "application/json") 23 w.WriteHeader(http.StatusOK) 24 25 fmt.Fprintf(w, ` 26 { 27 "pools":[ 28 { 29 "status":"ACTIVE", 30 "lb_method":"ROUND_ROBIN", 31 "protocol":"HTTP", 32 "description":"", 33 "health_monitors":[ 34 "466c8345-28d8-4f84-a246-e04380b0461d", 35 "5d4b5228-33b0-4e60-b225-9b727c1a20e7" 36 ], 37 "members":[ 38 "701b531b-111a-4f21-ad85-4795b7b12af6", 39 "beb53b4d-230b-4abd-8118-575b8fa006ef" 40 ], 41 "status_description": null, 42 "id":"72741b06-df4d-4715-b142-276b6bce75ab", 43 "vip_id":"4ec89087-d057-4e2c-911f-60a3b47ee304", 44 "name":"app_pool", 45 "admin_state_up":true, 46 "subnet_id":"8032909d-47a1-4715-90af-5153ffe39861", 47 "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea", 48 "health_monitors_status": [], 49 "provider": "haproxy" 50 } 51 ] 52 } 53 `) 54 }) 55 56 count := 0 57 58 pools.List(fake.ServiceClient(), pools.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 59 count++ 60 actual, err := pools.ExtractPools(page) 61 if err != nil { 62 t.Errorf("Failed to extract pools: %v", err) 63 return false, err 64 } 65 66 expected := []pools.Pool{ 67 { 68 Status: "ACTIVE", 69 LBMethod: "ROUND_ROBIN", 70 Protocol: "HTTP", 71 Description: "", 72 MonitorIDs: []string{ 73 "466c8345-28d8-4f84-a246-e04380b0461d", 74 "5d4b5228-33b0-4e60-b225-9b727c1a20e7", 75 }, 76 SubnetID: "8032909d-47a1-4715-90af-5153ffe39861", 77 TenantID: "83657cfcdfe44cd5920adaf26c48ceea", 78 AdminStateUp: true, 79 Name: "app_pool", 80 MemberIDs: []string{ 81 "701b531b-111a-4f21-ad85-4795b7b12af6", 82 "beb53b4d-230b-4abd-8118-575b8fa006ef", 83 }, 84 ID: "72741b06-df4d-4715-b142-276b6bce75ab", 85 VIPID: "4ec89087-d057-4e2c-911f-60a3b47ee304", 86 Provider: "haproxy", 87 }, 88 } 89 90 th.CheckDeepEquals(t, expected, actual) 91 92 return true, nil 93 }) 94 95 if count != 1 { 96 t.Errorf("Expected 1 page, got %d", count) 97 } 98 } 99 100 func TestCreate(t *testing.T) { 101 th.SetupHTTP() 102 defer th.TeardownHTTP() 103 104 th.Mux.HandleFunc("/v2.0/lb/pools", func(w http.ResponseWriter, r *http.Request) { 105 th.TestMethod(t, r, "POST") 106 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 107 th.TestHeader(t, r, "Content-Type", "application/json") 108 th.TestHeader(t, r, "Accept", "application/json") 109 th.TestJSONRequest(t, r, ` 110 { 111 "pool": { 112 "lb_method": "ROUND_ROBIN", 113 "protocol": "HTTP", 114 "name": "Example pool", 115 "subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9", 116 "tenant_id": "2ffc6e22aae24e4795f87155d24c896f", 117 "provider": "haproxy" 118 } 119 } 120 `) 121 122 w.Header().Add("Content-Type", "application/json") 123 w.WriteHeader(http.StatusCreated) 124 125 fmt.Fprintf(w, ` 126 { 127 "pool": { 128 "status": "PENDING_CREATE", 129 "lb_method": "ROUND_ROBIN", 130 "protocol": "HTTP", 131 "description": "", 132 "health_monitors": [], 133 "members": [], 134 "status_description": null, 135 "id": "69055154-f603-4a28-8951-7cc2d9e54a9a", 136 "vip_id": null, 137 "name": "Example pool", 138 "admin_state_up": true, 139 "subnet_id": "1981f108-3c48-48d2-b908-30f7d28532c9", 140 "tenant_id": "2ffc6e22aae24e4795f87155d24c896f", 141 "health_monitors_status": [], 142 "provider": "haproxy" 143 } 144 } 145 `) 146 }) 147 148 options := pools.CreateOpts{ 149 LBMethod: pools.LBMethodRoundRobin, 150 Protocol: "HTTP", 151 Name: "Example pool", 152 SubnetID: "1981f108-3c48-48d2-b908-30f7d28532c9", 153 TenantID: "2ffc6e22aae24e4795f87155d24c896f", 154 Provider: "haproxy", 155 } 156 p, err := pools.Create(fake.ServiceClient(), options).Extract() 157 th.AssertNoErr(t, err) 158 159 th.AssertEquals(t, "PENDING_CREATE", p.Status) 160 th.AssertEquals(t, "ROUND_ROBIN", p.LBMethod) 161 th.AssertEquals(t, "HTTP", p.Protocol) 162 th.AssertEquals(t, "", p.Description) 163 th.AssertDeepEquals(t, []string{}, p.MonitorIDs) 164 th.AssertDeepEquals(t, []string{}, p.MemberIDs) 165 th.AssertEquals(t, "69055154-f603-4a28-8951-7cc2d9e54a9a", p.ID) 166 th.AssertEquals(t, "Example pool", p.Name) 167 th.AssertEquals(t, "1981f108-3c48-48d2-b908-30f7d28532c9", p.SubnetID) 168 th.AssertEquals(t, "2ffc6e22aae24e4795f87155d24c896f", p.TenantID) 169 th.AssertEquals(t, "haproxy", p.Provider) 170 } 171 172 func TestGet(t *testing.T) { 173 th.SetupHTTP() 174 defer th.TeardownHTTP() 175 176 th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) { 177 th.TestMethod(t, r, "GET") 178 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 179 180 w.Header().Add("Content-Type", "application/json") 181 w.WriteHeader(http.StatusOK) 182 183 fmt.Fprintf(w, ` 184 { 185 "pool":{ 186 "id":"332abe93-f488-41ba-870b-2ac66be7f853", 187 "tenant_id":"19eaa775-cf5d-49bc-902e-2f85f668d995", 188 "name":"Example pool", 189 "description":"", 190 "protocol":"tcp", 191 "lb_algorithm":"ROUND_ROBIN", 192 "session_persistence":{ 193 }, 194 "healthmonitor_id":null, 195 "members":[ 196 ], 197 "admin_state_up":true, 198 "status":"ACTIVE" 199 } 200 } 201 `) 202 }) 203 204 n, err := pools.Get(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853").Extract() 205 th.AssertNoErr(t, err) 206 207 th.AssertEquals(t, n.ID, "332abe93-f488-41ba-870b-2ac66be7f853") 208 } 209 210 func TestUpdate(t *testing.T) { 211 th.SetupHTTP() 212 defer th.TeardownHTTP() 213 214 th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) { 215 th.TestMethod(t, r, "PUT") 216 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 217 th.TestHeader(t, r, "Content-Type", "application/json") 218 th.TestHeader(t, r, "Accept", "application/json") 219 th.TestJSONRequest(t, r, ` 220 { 221 "pool":{ 222 "name":"SuperPool", 223 "lb_method": "LEAST_CONNECTIONS" 224 } 225 } 226 `) 227 228 w.Header().Add("Content-Type", "application/json") 229 w.WriteHeader(http.StatusOK) 230 231 fmt.Fprintf(w, ` 232 { 233 "pool":{ 234 "status":"PENDING_UPDATE", 235 "lb_method":"LEAST_CONNECTIONS", 236 "protocol":"TCP", 237 "description":"", 238 "health_monitors":[ 239 240 ], 241 "subnet_id":"8032909d-47a1-4715-90af-5153ffe39861", 242 "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea", 243 "admin_state_up":true, 244 "name":"SuperPool", 245 "members":[ 246 247 ], 248 "id":"61b1f87a-7a21-4ad3-9dda-7f81d249944f", 249 "vip_id":null 250 } 251 } 252 `) 253 }) 254 255 options := pools.UpdateOpts{Name: "SuperPool", LBMethod: pools.LBMethodLeastConnections} 256 257 n, err := pools.Update(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", options).Extract() 258 th.AssertNoErr(t, err) 259 260 th.AssertEquals(t, "SuperPool", n.Name) 261 th.AssertDeepEquals(t, "LEAST_CONNECTIONS", n.LBMethod) 262 } 263 264 func TestDelete(t *testing.T) { 265 th.SetupHTTP() 266 defer th.TeardownHTTP() 267 268 th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853", func(w http.ResponseWriter, r *http.Request) { 269 th.TestMethod(t, r, "DELETE") 270 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 271 w.WriteHeader(http.StatusNoContent) 272 }) 273 274 res := pools.Delete(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853") 275 th.AssertNoErr(t, res.Err) 276 } 277 278 func TestAssociateHealthMonitor(t *testing.T) { 279 th.SetupHTTP() 280 defer th.TeardownHTTP() 281 282 th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853/health_monitors", func(w http.ResponseWriter, r *http.Request) { 283 th.TestMethod(t, r, "POST") 284 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 285 th.TestHeader(t, r, "Content-Type", "application/json") 286 th.TestHeader(t, r, "Accept", "application/json") 287 th.TestJSONRequest(t, r, ` 288 { 289 "health_monitor":{ 290 "id":"b624decf-d5d3-4c66-9a3d-f047e7786181" 291 } 292 } 293 `) 294 295 w.Header().Add("Content-Type", "application/json") 296 w.WriteHeader(http.StatusCreated) 297 fmt.Fprintf(w, `{}`) 298 }) 299 300 _, err := pools.AssociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181").Extract() 301 th.AssertNoErr(t, err) 302 } 303 304 func TestDisassociateHealthMonitor(t *testing.T) { 305 th.SetupHTTP() 306 defer th.TeardownHTTP() 307 308 th.Mux.HandleFunc("/v2.0/lb/pools/332abe93-f488-41ba-870b-2ac66be7f853/health_monitors/b624decf-d5d3-4c66-9a3d-f047e7786181", func(w http.ResponseWriter, r *http.Request) { 309 th.TestMethod(t, r, "DELETE") 310 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 311 w.WriteHeader(http.StatusNoContent) 312 }) 313 314 res := pools.DisassociateMonitor(fake.ServiceClient(), "332abe93-f488-41ba-870b-2ac66be7f853", "b624decf-d5d3-4c66-9a3d-f047e7786181") 315 th.AssertNoErr(t, res.Err) 316 }