utilization_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. Copyright 2018 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. "fmt"
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. )
  19. type resourceUtilizationRatioTestCase struct {
  20. metrics PodMetricsInfo
  21. requests map[string]int64
  22. targetUtilization int32
  23. expectedUtilizationRatio float64
  24. expectedCurrentUtilization int32
  25. expectedRawAverageValue int64
  26. expectedErr error
  27. }
  28. func (tc *resourceUtilizationRatioTestCase) runTest(t *testing.T) {
  29. actualUtilizationRatio, actualCurrentUtilization, actualRawAverageValue, actualErr := GetResourceUtilizationRatio(tc.metrics, tc.requests, tc.targetUtilization)
  30. if tc.expectedErr != nil {
  31. assert.Error(t, actualErr, "there should be an error getting the utilization ratio")
  32. assert.Contains(t, fmt.Sprintf("%v", actualErr), fmt.Sprintf("%v", tc.expectedErr), "the error message should be as expected")
  33. return
  34. }
  35. assert.NoError(t, actualErr, "there should be no error retrieving the utilization ratio")
  36. assert.Equal(t, tc.expectedUtilizationRatio, actualUtilizationRatio, "the utilization ratios should be as expected")
  37. assert.Equal(t, tc.expectedCurrentUtilization, actualCurrentUtilization, "the current utilization should be as expected")
  38. assert.Equal(t, tc.expectedRawAverageValue, actualRawAverageValue, "the raw average value should be as expected")
  39. }
  40. type metricUtilizationRatioTestCase struct {
  41. metrics PodMetricsInfo
  42. targetUtilization int64
  43. expectedUtilizationRatio float64
  44. expectedCurrentUtilization int64
  45. }
  46. func (tc *metricUtilizationRatioTestCase) runTest(t *testing.T) {
  47. actualUtilizationRatio, actualCurrentUtilization := GetMetricUtilizationRatio(tc.metrics, tc.targetUtilization)
  48. assert.Equal(t, tc.expectedUtilizationRatio, actualUtilizationRatio, "the utilization ratios should be as expected")
  49. assert.Equal(t, tc.expectedCurrentUtilization, actualCurrentUtilization, "the current utilization should be as expected")
  50. }
  51. func TestGetResourceUtilizationRatioBaseCase(t *testing.T) {
  52. tc := resourceUtilizationRatioTestCase{
  53. metrics: PodMetricsInfo{
  54. "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76},
  55. },
  56. requests: map[string]int64{
  57. "test-pod-0": 100, "test-pod-1": 100,
  58. },
  59. targetUtilization: 50,
  60. expectedUtilizationRatio: 1.26,
  61. expectedCurrentUtilization: 63,
  62. expectedRawAverageValue: 63,
  63. expectedErr: nil,
  64. }
  65. tc.runTest(t)
  66. }
  67. func TestGetResourceUtilizationRatioIgnorePodsWithNoRequest(t *testing.T) {
  68. tc := resourceUtilizationRatioTestCase{
  69. metrics: PodMetricsInfo{
  70. "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76}, "test-pod-no-request": {Value: 100},
  71. },
  72. requests: map[string]int64{
  73. "test-pod-0": 100, "test-pod-1": 100,
  74. },
  75. targetUtilization: 50,
  76. expectedUtilizationRatio: 1.26,
  77. expectedCurrentUtilization: 63,
  78. expectedRawAverageValue: 63,
  79. expectedErr: nil,
  80. }
  81. tc.runTest(t)
  82. }
  83. func TestGetResourceUtilizationRatioExtraRequest(t *testing.T) {
  84. tc := resourceUtilizationRatioTestCase{
  85. metrics: PodMetricsInfo{
  86. "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76},
  87. },
  88. requests: map[string]int64{
  89. "test-pod-0": 100, "test-pod-1": 100, "test-pod-extra-request": 500,
  90. },
  91. targetUtilization: 50,
  92. expectedUtilizationRatio: 1.26,
  93. expectedCurrentUtilization: 63,
  94. expectedRawAverageValue: 63,
  95. expectedErr: nil,
  96. }
  97. tc.runTest(t)
  98. }
  99. func TestGetResourceUtilizationRatioNoRequests(t *testing.T) {
  100. tc := resourceUtilizationRatioTestCase{
  101. metrics: PodMetricsInfo{
  102. "test-pod-0": {Value: 50}, "test-pod-1": {Value: 76},
  103. },
  104. requests: map[string]int64{},
  105. targetUtilization: 50,
  106. expectedUtilizationRatio: 0,
  107. expectedCurrentUtilization: 0,
  108. expectedRawAverageValue: 0,
  109. expectedErr: fmt.Errorf("no metrics returned matched known pods"),
  110. }
  111. tc.runTest(t)
  112. }
  113. func TestGetMetricUtilizationRatioBaseCase(t *testing.T) {
  114. tc := metricUtilizationRatioTestCase{
  115. metrics: PodMetricsInfo{
  116. "test-pod-0": {Value: 5000}, "test-pod-1": {Value: 10000},
  117. },
  118. targetUtilization: 10000,
  119. expectedUtilizationRatio: .75,
  120. expectedCurrentUtilization: 7500,
  121. }
  122. tc.runTest(t)
  123. }