github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/lbaas/monitors/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 "github.com/huaweicloud/golangsdk" 9 fake "github.com/huaweicloud/golangsdk/openstack/networking/v2/common" 10 "github.com/huaweicloud/golangsdk/openstack/networking/v2/extensions/lbaas/monitors" 11 "github.com/huaweicloud/golangsdk/pagination" 12 th "github.com/huaweicloud/golangsdk/testhelper" 13 ) 14 15 func TestList(t *testing.T) { 16 th.SetupHTTP() 17 defer th.TeardownHTTP() 18 19 th.Mux.HandleFunc("/v2.0/lb/health_monitors", func(w http.ResponseWriter, r *http.Request) { 20 th.TestMethod(t, r, "GET") 21 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 22 23 w.Header().Add("Content-Type", "application/json") 24 w.WriteHeader(http.StatusOK) 25 26 fmt.Fprintf(w, ` 27 { 28 "health_monitors":[ 29 { 30 "admin_state_up":true, 31 "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea", 32 "delay":10, 33 "max_retries":1, 34 "timeout":1, 35 "type":"PING", 36 "id":"466c8345-28d8-4f84-a246-e04380b0461d" 37 }, 38 { 39 "admin_state_up":true, 40 "tenant_id":"83657cfcdfe44cd5920adaf26c48ceea", 41 "delay":5, 42 "expected_codes":"200", 43 "max_retries":2, 44 "http_method":"GET", 45 "timeout":2, 46 "url_path":"/", 47 "type":"HTTP", 48 "id":"5d4b5228-33b0-4e60-b225-9b727c1a20e7" 49 } 50 ] 51 } 52 `) 53 }) 54 55 count := 0 56 57 monitors.List(fake.ServiceClient(), monitors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) { 58 count++ 59 actual, err := monitors.ExtractMonitors(page) 60 if err != nil { 61 t.Errorf("Failed to extract monitors: %v", err) 62 return false, err 63 } 64 65 expected := []monitors.Monitor{ 66 { 67 AdminStateUp: true, 68 TenantID: "83657cfcdfe44cd5920adaf26c48ceea", 69 Delay: 10, 70 MaxRetries: 1, 71 Timeout: 1, 72 Type: "PING", 73 ID: "466c8345-28d8-4f84-a246-e04380b0461d", 74 }, 75 { 76 AdminStateUp: true, 77 TenantID: "83657cfcdfe44cd5920adaf26c48ceea", 78 Delay: 5, 79 ExpectedCodes: "200", 80 MaxRetries: 2, 81 Timeout: 2, 82 URLPath: "/", 83 Type: "HTTP", 84 HTTPMethod: "GET", 85 ID: "5d4b5228-33b0-4e60-b225-9b727c1a20e7", 86 }, 87 } 88 89 th.CheckDeepEquals(t, expected, actual) 90 91 return true, nil 92 }) 93 94 if count != 1 { 95 t.Errorf("Expected 1 page, got %d", count) 96 } 97 } 98 99 func TestDelayMustBeGreaterOrEqualThanTimeout(t *testing.T) { 100 _, err := monitors.Create(fake.ServiceClient(), monitors.CreateOpts{ 101 Type: "HTTP", 102 Delay: 1, 103 Timeout: 10, 104 MaxRetries: 5, 105 URLPath: "/check", 106 ExpectedCodes: "200-299", 107 }).Extract() 108 109 if err == nil { 110 t.Fatalf("Expected error, got none") 111 } 112 113 _, err = monitors.Update(fake.ServiceClient(), "453105b9-1754-413f-aab1-55f1af620750", monitors.UpdateOpts{ 114 Delay: 1, 115 Timeout: 10, 116 }).Extract() 117 118 if err == nil { 119 t.Fatalf("Expected error, got none") 120 } 121 } 122 123 func TestCreate(t *testing.T) { 124 th.SetupHTTP() 125 defer th.TeardownHTTP() 126 127 th.Mux.HandleFunc("/v2.0/lb/health_monitors", func(w http.ResponseWriter, r *http.Request) { 128 th.TestMethod(t, r, "POST") 129 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 130 th.TestHeader(t, r, "Content-Type", "application/json") 131 th.TestHeader(t, r, "Accept", "application/json") 132 th.TestJSONRequest(t, r, ` 133 { 134 "health_monitor":{ 135 "type":"HTTP", 136 "tenant_id":"453105b9-1754-413f-aab1-55f1af620750", 137 "delay":20, 138 "timeout":10, 139 "max_retries":5, 140 "url_path":"/check", 141 "expected_codes":"200-299" 142 } 143 } 144 `) 145 146 w.Header().Add("Content-Type", "application/json") 147 w.WriteHeader(http.StatusCreated) 148 149 fmt.Fprintf(w, ` 150 { 151 "health_monitor":{ 152 "id":"f3eeab00-8367-4524-b662-55e64d4cacb5", 153 "tenant_id":"453105b9-1754-413f-aab1-55f1af620750", 154 "type":"HTTP", 155 "delay":20, 156 "timeout":10, 157 "max_retries":5, 158 "http_method":"GET", 159 "url_path":"/check", 160 "expected_codes":"200-299", 161 "admin_state_up":true, 162 "status":"ACTIVE" 163 } 164 } 165 `) 166 }) 167 168 _, err := monitors.Create(fake.ServiceClient(), monitors.CreateOpts{ 169 Type: "HTTP", 170 TenantID: "453105b9-1754-413f-aab1-55f1af620750", 171 Delay: 20, 172 Timeout: 10, 173 MaxRetries: 5, 174 URLPath: "/check", 175 ExpectedCodes: "200-299", 176 }).Extract() 177 178 th.AssertNoErr(t, err) 179 } 180 181 func TestRequiredCreateOpts(t *testing.T) { 182 res := monitors.Create(fake.ServiceClient(), monitors.CreateOpts{}) 183 if res.Err == nil { 184 t.Fatalf("Expected error, got none") 185 } 186 res = monitors.Create(fake.ServiceClient(), monitors.CreateOpts{Type: monitors.TypeHTTP}) 187 if res.Err == nil { 188 t.Fatalf("Expected error, got none") 189 } 190 } 191 192 func TestGet(t *testing.T) { 193 th.SetupHTTP() 194 defer th.TeardownHTTP() 195 196 th.Mux.HandleFunc("/v2.0/lb/health_monitors/f3eeab00-8367-4524-b662-55e64d4cacb5", func(w http.ResponseWriter, r *http.Request) { 197 th.TestMethod(t, r, "GET") 198 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 199 200 w.Header().Add("Content-Type", "application/json") 201 w.WriteHeader(http.StatusOK) 202 203 fmt.Fprintf(w, ` 204 { 205 "health_monitor":{ 206 "id":"f3eeab00-8367-4524-b662-55e64d4cacb5", 207 "tenant_id":"453105b9-1754-413f-aab1-55f1af620750", 208 "type":"HTTP", 209 "delay":20, 210 "timeout":10, 211 "max_retries":5, 212 "http_method":"GET", 213 "url_path":"/check", 214 "expected_codes":"200-299", 215 "admin_state_up":true, 216 "status":"ACTIVE" 217 } 218 } 219 `) 220 }) 221 222 hm, err := monitors.Get(fake.ServiceClient(), "f3eeab00-8367-4524-b662-55e64d4cacb5").Extract() 223 th.AssertNoErr(t, err) 224 225 th.AssertEquals(t, "f3eeab00-8367-4524-b662-55e64d4cacb5", hm.ID) 226 th.AssertEquals(t, "453105b9-1754-413f-aab1-55f1af620750", hm.TenantID) 227 th.AssertEquals(t, "HTTP", hm.Type) 228 th.AssertEquals(t, 20, hm.Delay) 229 th.AssertEquals(t, 10, hm.Timeout) 230 th.AssertEquals(t, 5, hm.MaxRetries) 231 th.AssertEquals(t, "GET", hm.HTTPMethod) 232 th.AssertEquals(t, "/check", hm.URLPath) 233 th.AssertEquals(t, "200-299", hm.ExpectedCodes) 234 th.AssertEquals(t, true, hm.AdminStateUp) 235 th.AssertEquals(t, "ACTIVE", hm.Status) 236 } 237 238 func TestUpdate(t *testing.T) { 239 th.SetupHTTP() 240 defer th.TeardownHTTP() 241 242 th.Mux.HandleFunc("/v2.0/lb/health_monitors/b05e44b5-81f9-4551-b474-711a722698f7", func(w http.ResponseWriter, r *http.Request) { 243 th.TestMethod(t, r, "PUT") 244 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 245 th.TestHeader(t, r, "Content-Type", "application/json") 246 th.TestHeader(t, r, "Accept", "application/json") 247 th.TestJSONRequest(t, r, ` 248 { 249 "health_monitor":{ 250 "delay": 30, 251 "timeout": 20, 252 "max_retries": 10, 253 "url_path": "/another_check", 254 "expected_codes": "301", 255 "admin_state_up": true 256 } 257 } 258 `) 259 260 w.Header().Add("Content-Type", "application/json") 261 w.WriteHeader(http.StatusAccepted) 262 263 fmt.Fprintf(w, ` 264 { 265 "health_monitor": { 266 "admin_state_up": true, 267 "tenant_id": "4fd44f30292945e481c7b8a0c8908869", 268 "delay": 30, 269 "max_retries": 10, 270 "http_method": "GET", 271 "timeout": 20, 272 "pools": [ 273 { 274 "status": "PENDING_CREATE", 275 "status_description": null, 276 "pool_id": "6e55751f-6ad4-4e53-b8d4-02e442cd21df" 277 } 278 ], 279 "type": "PING", 280 "id": "b05e44b5-81f9-4551-b474-711a722698f7" 281 } 282 } 283 `) 284 }) 285 286 _, err := monitors.Update(fake.ServiceClient(), "b05e44b5-81f9-4551-b474-711a722698f7", monitors.UpdateOpts{ 287 Delay: 30, 288 Timeout: 20, 289 MaxRetries: 10, 290 URLPath: "/another_check", 291 ExpectedCodes: "301", 292 AdminStateUp: golangsdk.Enabled, 293 }).Extract() 294 295 th.AssertNoErr(t, err) 296 } 297 298 func TestDelete(t *testing.T) { 299 th.SetupHTTP() 300 defer th.TeardownHTTP() 301 302 th.Mux.HandleFunc("/v2.0/lb/health_monitors/b05e44b5-81f9-4551-b474-711a722698f7", func(w http.ResponseWriter, r *http.Request) { 303 th.TestMethod(t, r, "DELETE") 304 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 305 w.WriteHeader(http.StatusNoContent) 306 }) 307 308 res := monitors.Delete(fake.ServiceClient(), "b05e44b5-81f9-4551-b474-711a722698f7") 309 th.AssertNoErr(t, res.Err) 310 }