autoscale_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 versioned
  14. import (
  15. "reflect"
  16. "testing"
  17. autoscalingv1 "k8s.io/api/autoscaling/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. utilpointer "k8s.io/utils/pointer"
  20. )
  21. func TestHPAGenerate(t *testing.T) {
  22. tests := []struct {
  23. name string
  24. HPAName string
  25. scaleRefKind string
  26. scaleRefName string
  27. scaleRefAPIVersion string
  28. minReplicas int32
  29. maxReplicas int32
  30. CPUPercent int32
  31. expected *autoscalingv1.HorizontalPodAutoscaler
  32. expectErr bool
  33. }{
  34. {
  35. name: "valid case",
  36. HPAName: "foo",
  37. minReplicas: 1,
  38. maxReplicas: 10,
  39. CPUPercent: 80,
  40. scaleRefKind: "kind",
  41. scaleRefName: "name",
  42. scaleRefAPIVersion: "apiVersion",
  43. expected: &autoscalingv1.HorizontalPodAutoscaler{
  44. ObjectMeta: metav1.ObjectMeta{
  45. Name: "foo",
  46. },
  47. Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
  48. TargetCPUUtilizationPercentage: utilpointer.Int32Ptr(80),
  49. ScaleTargetRef: autoscalingv1.CrossVersionObjectReference{
  50. Kind: "kind",
  51. Name: "name",
  52. APIVersion: "apiVersion",
  53. },
  54. MaxReplicas: int32(10),
  55. MinReplicas: utilpointer.Int32Ptr(1),
  56. },
  57. },
  58. expectErr: false,
  59. },
  60. {
  61. name: "'name' is a required parameter",
  62. scaleRefKind: "kind",
  63. scaleRefName: "name",
  64. scaleRefAPIVersion: "apiVersion",
  65. expectErr: true,
  66. },
  67. {
  68. name: "'max' is a required parameter",
  69. HPAName: "foo",
  70. scaleRefKind: "kind",
  71. scaleRefName: "name",
  72. scaleRefAPIVersion: "apiVersion",
  73. expectErr: true,
  74. },
  75. {
  76. name: "'max' must be greater than or equal to 'min'",
  77. HPAName: "foo",
  78. minReplicas: 10,
  79. maxReplicas: 1,
  80. scaleRefKind: "kind",
  81. scaleRefName: "name",
  82. scaleRefAPIVersion: "apiVersion",
  83. expectErr: true,
  84. },
  85. {
  86. name: "'max' must be at least 1",
  87. HPAName: "foo",
  88. minReplicas: 1,
  89. maxReplicas: -10,
  90. scaleRefKind: "kind",
  91. scaleRefName: "name",
  92. scaleRefAPIVersion: "apiVersion",
  93. expectErr: true,
  94. },
  95. }
  96. for _, tt := range tests {
  97. t.Run(tt.name, func(t *testing.T) {
  98. generator := HorizontalPodAutoscalerGeneratorV1{
  99. Name: tt.HPAName,
  100. ScaleRefKind: tt.scaleRefKind,
  101. ScaleRefName: tt.scaleRefName,
  102. ScaleRefAPIVersion: tt.scaleRefAPIVersion,
  103. MinReplicas: tt.minReplicas,
  104. MaxReplicas: tt.maxReplicas,
  105. CPUPercent: tt.CPUPercent,
  106. }
  107. obj, err := generator.StructuredGenerate()
  108. if tt.expectErr && err != nil {
  109. return
  110. }
  111. if !tt.expectErr && err != nil {
  112. t.Errorf("[%s] unexpected error: %v", tt.name, err)
  113. }
  114. if tt.expectErr && err == nil {
  115. t.Errorf("[%s] expect error, got nil", tt.name)
  116. }
  117. if !reflect.DeepEqual(obj.(*autoscalingv1.HorizontalPodAutoscaler), tt.expected) {
  118. t.Errorf("[%s] want:\n%#v\ngot:\n%#v", tt.name, tt.expected, obj.(*autoscalingv1.HorizontalPodAutoscaler))
  119. }
  120. })
  121. }
  122. }