github.com/gophercloud/gophercloud@v1.11.0/openstack/networking/v2/extensions/qos/rules/testing/requests_test.go (about) 1 package testing 2 3 import ( 4 "fmt" 5 "net/http" 6 "testing" 7 8 fake "github.com/gophercloud/gophercloud/openstack/networking/v2/common" 9 "github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/qos/rules" 10 "github.com/gophercloud/gophercloud/pagination" 11 th "github.com/gophercloud/gophercloud/testhelper" 12 ) 13 14 func TestListBandwidthLimitRule(t *testing.T) { 15 th.SetupHTTP() 16 defer th.TeardownHTTP() 17 18 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules", 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, BandwidthLimitRulesListResult) 26 }) 27 28 count := 0 29 30 err := rules.ListBandwidthLimitRules( 31 fake.ServiceClient(), 32 "501005fa-3b56-4061-aaca-3f24995112e1", 33 rules.BandwidthLimitRulesListOpts{}, 34 ).EachPage(func(page pagination.Page) (bool, error) { 35 count++ 36 actual, err := rules.ExtractBandwidthLimitRules(page) 37 if err != nil { 38 t.Errorf("Failed to extract bandwith limit rules: %v", err) 39 return false, nil 40 } 41 42 expected := []rules.BandwidthLimitRule{ 43 { 44 ID: "30a57f4a-336b-4382-8275-d708babd2241", 45 MaxKBps: 3000, 46 MaxBurstKBps: 300, 47 Direction: "egress", 48 }, 49 } 50 51 th.CheckDeepEquals(t, expected, actual) 52 53 return true, nil 54 }) 55 th.AssertNoErr(t, err) 56 57 if count != 1 { 58 t.Errorf("Expected 1 page, got %d", count) 59 } 60 } 61 62 func TestGetBandwidthLimitRule(t *testing.T) { 63 th.SetupHTTP() 64 defer th.TeardownHTTP() 65 66 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 67 th.TestMethod(t, r, "GET") 68 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 69 70 w.Header().Add("Content-Type", "application/json") 71 w.WriteHeader(http.StatusOK) 72 73 fmt.Fprintf(w, BandwidthLimitRulesGetResult) 74 }) 75 76 r, err := rules.GetBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241").ExtractBandwidthLimitRule() 77 th.AssertNoErr(t, err) 78 79 th.AssertEquals(t, r.ID, "30a57f4a-336b-4382-8275-d708babd2241") 80 th.AssertEquals(t, r.Direction, "egress") 81 th.AssertEquals(t, r.MaxBurstKBps, 300) 82 th.AssertEquals(t, r.MaxKBps, 3000) 83 } 84 85 func TestCreateBandwidthLimitRule(t *testing.T) { 86 th.SetupHTTP() 87 defer th.TeardownHTTP() 88 89 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules", func(w http.ResponseWriter, r *http.Request) { 90 th.TestMethod(t, r, "POST") 91 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 92 th.TestHeader(t, r, "Content-Type", "application/json") 93 th.TestHeader(t, r, "Accept", "application/json") 94 th.TestJSONRequest(t, r, BandwidthLimitRulesCreateRequest) 95 96 w.Header().Add("Content-Type", "application/json") 97 w.WriteHeader(http.StatusCreated) 98 99 fmt.Fprintf(w, BandwidthLimitRulesCreateResult) 100 }) 101 102 opts := rules.CreateBandwidthLimitRuleOpts{ 103 MaxKBps: 2000, 104 MaxBurstKBps: 200, 105 } 106 r, err := rules.CreateBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractBandwidthLimitRule() 107 th.AssertNoErr(t, err) 108 109 th.AssertEquals(t, 200, r.MaxBurstKBps) 110 th.AssertEquals(t, 2000, r.MaxKBps) 111 } 112 113 func TestUpdateBandwidthLimitRule(t *testing.T) { 114 th.SetupHTTP() 115 defer th.TeardownHTTP() 116 117 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 118 th.TestMethod(t, r, "PUT") 119 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 120 th.TestHeader(t, r, "Content-Type", "application/json") 121 th.TestHeader(t, r, "Accept", "application/json") 122 th.TestJSONRequest(t, r, BandwidthLimitRulesUpdateRequest) 123 124 w.Header().Add("Content-Type", "application/json") 125 w.WriteHeader(http.StatusOK) 126 127 fmt.Fprintf(w, BandwidthLimitRulesUpdateResult) 128 }) 129 130 maxKBps := 500 131 maxBurstKBps := 0 132 opts := rules.UpdateBandwidthLimitRuleOpts{ 133 MaxKBps: &maxKBps, 134 MaxBurstKBps: &maxBurstKBps, 135 } 136 r, err := rules.UpdateBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractBandwidthLimitRule() 137 th.AssertNoErr(t, err) 138 139 th.AssertEquals(t, 0, r.MaxBurstKBps) 140 th.AssertEquals(t, 500, r.MaxKBps) 141 } 142 143 func TestDeleteBandwidthLimitRule(t *testing.T) { 144 th.SetupHTTP() 145 defer th.TeardownHTTP() 146 147 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/bandwidth_limit_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 148 th.TestMethod(t, r, "DELETE") 149 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 150 w.WriteHeader(http.StatusNoContent) 151 }) 152 153 res := rules.DeleteBandwidthLimitRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241") 154 th.AssertNoErr(t, res.Err) 155 } 156 157 func TestListDSCPMarkingRule(t *testing.T) { 158 th.SetupHTTP() 159 defer th.TeardownHTTP() 160 161 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules", func(w http.ResponseWriter, r *http.Request) { 162 th.TestMethod(t, r, "GET") 163 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 164 165 w.Header().Add("Content-Type", "application/json") 166 w.WriteHeader(http.StatusOK) 167 168 fmt.Fprintf(w, DSCPMarkingRulesListResult) 169 }) 170 171 count := 0 172 173 err := rules.ListDSCPMarkingRules( 174 fake.ServiceClient(), 175 "501005fa-3b56-4061-aaca-3f24995112e1", 176 rules.DSCPMarkingRulesListOpts{}, 177 ).EachPage(func(page pagination.Page) (bool, error) { 178 count++ 179 actual, err := rules.ExtractDSCPMarkingRules(page) 180 if err != nil { 181 t.Errorf("Failed to extract DSCP marking rules: %v", err) 182 return false, nil 183 } 184 185 expected := []rules.DSCPMarkingRule{ 186 { 187 ID: "30a57f4a-336b-4382-8275-d708babd2241", 188 DSCPMark: 20, 189 }, 190 } 191 192 th.CheckDeepEquals(t, expected, actual) 193 194 return true, nil 195 }) 196 th.AssertNoErr(t, err) 197 198 if count != 1 { 199 t.Errorf("Expected 1 page, got %d", count) 200 } 201 } 202 203 func TestGetDSCPMarkingRule(t *testing.T) { 204 th.SetupHTTP() 205 defer th.TeardownHTTP() 206 207 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 208 th.TestMethod(t, r, "GET") 209 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 210 211 w.Header().Add("Content-Type", "application/json") 212 w.WriteHeader(http.StatusOK) 213 214 fmt.Fprintf(w, DSCPMarkingRuleGetResult) 215 }) 216 217 r, err := rules.GetDSCPMarkingRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241").ExtractDSCPMarkingRule() 218 th.AssertNoErr(t, err) 219 220 th.AssertEquals(t, r.ID, "30a57f4a-336b-4382-8275-d708babd2241") 221 th.AssertEquals(t, 26, r.DSCPMark) 222 } 223 224 func TestCreateDSCPMarkingRule(t *testing.T) { 225 th.SetupHTTP() 226 defer th.TeardownHTTP() 227 228 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules", func(w http.ResponseWriter, r *http.Request) { 229 th.TestMethod(t, r, "POST") 230 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 231 th.TestHeader(t, r, "Content-Type", "application/json") 232 th.TestHeader(t, r, "Accept", "application/json") 233 th.TestJSONRequest(t, r, DSCPMarkingRuleCreateRequest) 234 235 w.Header().Add("Content-Type", "application/json") 236 w.WriteHeader(http.StatusCreated) 237 238 fmt.Fprintf(w, DSCPMarkingRuleCreateResult) 239 }) 240 241 opts := rules.CreateDSCPMarkingRuleOpts{ 242 DSCPMark: 20, 243 } 244 r, err := rules.CreateDSCPMarkingRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractDSCPMarkingRule() 245 th.AssertNoErr(t, err) 246 247 th.AssertEquals(t, "30a57f4a-336b-4382-8275-d708babd2241", r.ID) 248 th.AssertEquals(t, 20, r.DSCPMark) 249 } 250 251 func TestUpdateDSCPMarkingRule(t *testing.T) { 252 th.SetupHTTP() 253 defer th.TeardownHTTP() 254 255 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 256 th.TestMethod(t, r, "PUT") 257 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 258 th.TestHeader(t, r, "Content-Type", "application/json") 259 th.TestHeader(t, r, "Accept", "application/json") 260 th.TestJSONRequest(t, r, DSCPMarkingRuleUpdateRequest) 261 262 w.Header().Add("Content-Type", "application/json") 263 w.WriteHeader(http.StatusOK) 264 265 fmt.Fprintf(w, DSCPMarkingRuleUpdateResult) 266 }) 267 268 dscpMark := 26 269 opts := rules.UpdateDSCPMarkingRuleOpts{ 270 DSCPMark: &dscpMark, 271 } 272 r, err := rules.UpdateDSCPMarkingRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractDSCPMarkingRule() 273 th.AssertNoErr(t, err) 274 275 th.AssertEquals(t, "30a57f4a-336b-4382-8275-d708babd2241", r.ID) 276 th.AssertEquals(t, 26, r.DSCPMark) 277 } 278 279 func TestDeleteDSCPMarkingRule(t *testing.T) { 280 th.SetupHTTP() 281 defer th.TeardownHTTP() 282 283 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/dscp_marking_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 284 th.TestMethod(t, r, "DELETE") 285 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 286 w.WriteHeader(http.StatusNoContent) 287 }) 288 289 res := rules.DeleteDSCPMarkingRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241") 290 th.AssertNoErr(t, res.Err) 291 } 292 293 func TestListMinimumBandwidthRule(t *testing.T) { 294 th.SetupHTTP() 295 defer th.TeardownHTTP() 296 297 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules", func(w http.ResponseWriter, r *http.Request) { 298 th.TestMethod(t, r, "GET") 299 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 300 301 w.Header().Add("Content-Type", "application/json") 302 w.WriteHeader(http.StatusOK) 303 304 fmt.Fprintf(w, MinimumBandwidthRulesListResult) 305 }) 306 307 count := 0 308 309 err := rules.ListMinimumBandwidthRules( 310 fake.ServiceClient(), 311 "501005fa-3b56-4061-aaca-3f24995112e1", 312 rules.MinimumBandwidthRulesListOpts{}, 313 ).EachPage(func(page pagination.Page) (bool, error) { 314 count++ 315 actual, err := rules.ExtractMinimumBandwidthRules(page) 316 if err != nil { 317 t.Errorf("Failed to extract minimum bandwith rules: %v", err) 318 return false, nil 319 } 320 321 expected := []rules.MinimumBandwidthRule{ 322 { 323 ID: "30a57f4a-336b-4382-8275-d708babd2241", 324 Direction: "egress", 325 MinKBps: 3000, 326 }, 327 } 328 329 th.CheckDeepEquals(t, expected, actual) 330 331 return true, nil 332 }) 333 th.AssertNoErr(t, err) 334 335 if count != 1 { 336 t.Errorf("Expected 1 page, got %d", count) 337 } 338 } 339 340 func TestGetMinimumBandwidthRule(t *testing.T) { 341 th.SetupHTTP() 342 defer th.TeardownHTTP() 343 344 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 345 th.TestMethod(t, r, "GET") 346 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 347 348 w.Header().Add("Content-Type", "application/json") 349 w.WriteHeader(http.StatusOK) 350 351 fmt.Fprintf(w, MinimumBandwidthRulesGetResult) 352 }) 353 354 r, err := rules.GetMinimumBandwidthRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241").ExtractMinimumBandwidthRule() 355 th.AssertNoErr(t, err) 356 357 th.AssertEquals(t, r.ID, "30a57f4a-336b-4382-8275-d708babd2241") 358 th.AssertEquals(t, r.Direction, "egress") 359 th.AssertEquals(t, r.MinKBps, 3000) 360 } 361 362 func TestCreateMinimumBandwidthRule(t *testing.T) { 363 th.SetupHTTP() 364 defer th.TeardownHTTP() 365 366 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules", func(w http.ResponseWriter, r *http.Request) { 367 th.TestMethod(t, r, "POST") 368 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 369 th.TestHeader(t, r, "Content-Type", "application/json") 370 th.TestHeader(t, r, "Accept", "application/json") 371 th.TestJSONRequest(t, r, MinimumBandwidthRulesCreateRequest) 372 373 w.Header().Add("Content-Type", "application/json") 374 w.WriteHeader(http.StatusCreated) 375 376 fmt.Fprintf(w, MinimumBandwidthRulesCreateResult) 377 }) 378 379 opts := rules.CreateMinimumBandwidthRuleOpts{ 380 MinKBps: 2000, 381 } 382 r, err := rules.CreateMinimumBandwidthRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", opts).ExtractMinimumBandwidthRule() 383 th.AssertNoErr(t, err) 384 385 th.AssertEquals(t, 2000, r.MinKBps) 386 } 387 388 func TestUpdateMinimumBandwidthRule(t *testing.T) { 389 th.SetupHTTP() 390 defer th.TeardownHTTP() 391 392 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 393 th.TestMethod(t, r, "PUT") 394 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 395 th.TestHeader(t, r, "Content-Type", "application/json") 396 th.TestHeader(t, r, "Accept", "application/json") 397 th.TestJSONRequest(t, r, MinimumBandwidthRulesUpdateRequest) 398 399 w.Header().Add("Content-Type", "application/json") 400 w.WriteHeader(http.StatusOK) 401 402 fmt.Fprintf(w, MinimumBandwidthRulesUpdateResult) 403 }) 404 405 minKBps := 500 406 opts := rules.UpdateMinimumBandwidthRuleOpts{ 407 MinKBps: &minKBps, 408 } 409 r, err := rules.UpdateMinimumBandwidthRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241", opts).ExtractMinimumBandwidthRule() 410 th.AssertNoErr(t, err) 411 412 th.AssertEquals(t, 500, r.MinKBps) 413 } 414 415 func TestDeleteMinimumBandwidthRule(t *testing.T) { 416 th.SetupHTTP() 417 defer th.TeardownHTTP() 418 419 th.Mux.HandleFunc("/v2.0/qos/policies/501005fa-3b56-4061-aaca-3f24995112e1/minimum_bandwidth_rules/30a57f4a-336b-4382-8275-d708babd2241", func(w http.ResponseWriter, r *http.Request) { 420 th.TestMethod(t, r, "DELETE") 421 th.TestHeader(t, r, "X-Auth-Token", fake.TokenID) 422 w.WriteHeader(http.StatusNoContent) 423 }) 424 425 res := rules.DeleteMinimumBandwidthRule(fake.ServiceClient(), "501005fa-3b56-4061-aaca-3f24995112e1", "30a57f4a-336b-4382-8275-d708babd2241") 426 th.AssertNoErr(t, res.Err) 427 }