util_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. Copyright 2017 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "strings"
  16. "testing"
  17. "k8s.io/client-go/util/flowcontrol"
  18. )
  19. func TestRegisterMetricAndTrackRateLimiterUsage(t *testing.T) {
  20. testCases := []struct {
  21. ownerName string
  22. rateLimiter flowcontrol.RateLimiter
  23. err string
  24. }{
  25. {
  26. ownerName: "owner_name",
  27. rateLimiter: flowcontrol.NewTokenBucketRateLimiter(1, 1),
  28. err: "",
  29. },
  30. {
  31. ownerName: "owner_name",
  32. rateLimiter: flowcontrol.NewTokenBucketRateLimiter(1, 1),
  33. err: "already registered",
  34. },
  35. {
  36. ownerName: "invalid-owner-name",
  37. rateLimiter: flowcontrol.NewTokenBucketRateLimiter(1, 1),
  38. err: "error registering rate limiter usage metric",
  39. },
  40. }
  41. for i, tc := range testCases {
  42. e := RegisterMetricAndTrackRateLimiterUsage(tc.ownerName, tc.rateLimiter)
  43. if e != nil {
  44. if tc.err == "" {
  45. t.Errorf("[%d] unexpected error: %v", i, e)
  46. } else if !strings.Contains(e.Error(), tc.err) {
  47. t.Errorf("[%d] expected an error containing %q: %v", i, tc.err, e)
  48. }
  49. }
  50. }
  51. }