defaults_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 v2beta1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. autoscalingv2beta1 "k8s.io/api/autoscaling/v2beta1"
  18. "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. "k8s.io/kubernetes/pkg/apis/autoscaling"
  23. _ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
  24. . "k8s.io/kubernetes/pkg/apis/autoscaling/v2beta1"
  25. _ "k8s.io/kubernetes/pkg/apis/core/install"
  26. utilpointer "k8s.io/utils/pointer"
  27. )
  28. func TestSetDefaultHPA(t *testing.T) {
  29. utilizationDefaultVal := int32(autoscaling.DefaultCPUUtilization)
  30. defaultReplicas := utilpointer.Int32Ptr(1)
  31. defaultTemplate := []autoscalingv2beta1.MetricSpec{
  32. {
  33. Type: autoscalingv2beta1.ResourceMetricSourceType,
  34. Resource: &autoscalingv2beta1.ResourceMetricSource{
  35. Name: v1.ResourceCPU,
  36. TargetAverageUtilization: &utilizationDefaultVal,
  37. },
  38. },
  39. }
  40. tests := []struct {
  41. original *autoscalingv2beta1.HorizontalPodAutoscaler
  42. expected *autoscalingv2beta1.HorizontalPodAutoscaler
  43. }{
  44. { // MinReplicas default value
  45. original: &autoscalingv2beta1.HorizontalPodAutoscaler{
  46. Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
  47. Metrics: defaultTemplate,
  48. },
  49. },
  50. expected: &autoscalingv2beta1.HorizontalPodAutoscaler{
  51. Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
  52. MinReplicas: defaultReplicas,
  53. Metrics: defaultTemplate,
  54. },
  55. },
  56. },
  57. { // MinReplicas update
  58. original: &autoscalingv2beta1.HorizontalPodAutoscaler{
  59. Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
  60. MinReplicas: utilpointer.Int32Ptr(3),
  61. Metrics: defaultTemplate,
  62. },
  63. },
  64. expected: &autoscalingv2beta1.HorizontalPodAutoscaler{
  65. Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
  66. MinReplicas: utilpointer.Int32Ptr(3),
  67. Metrics: defaultTemplate,
  68. },
  69. },
  70. },
  71. { // Metrics default value
  72. original: &autoscalingv2beta1.HorizontalPodAutoscaler{
  73. Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
  74. MinReplicas: defaultReplicas,
  75. },
  76. },
  77. expected: &autoscalingv2beta1.HorizontalPodAutoscaler{
  78. Spec: autoscalingv2beta1.HorizontalPodAutoscalerSpec{
  79. MinReplicas: defaultReplicas,
  80. Metrics: defaultTemplate,
  81. },
  82. },
  83. },
  84. }
  85. for i, test := range tests {
  86. original := test.original
  87. expected := test.expected
  88. obj2 := roundTrip(t, runtime.Object(original))
  89. got, ok := obj2.(*autoscalingv2beta1.HorizontalPodAutoscaler)
  90. if !ok {
  91. t.Fatalf("(%d) unexpected object: %v", i, obj2)
  92. }
  93. if !apiequality.Semantic.DeepEqual(got.Spec, expected.Spec) {
  94. t.Errorf("(%d) got different than expected\ngot:\n\t%+v\nexpected:\n\t%+v", i, got.Spec, expected.Spec)
  95. }
  96. }
  97. }
  98. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  99. data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  100. if err != nil {
  101. t.Errorf("%v\n %#v", err, obj)
  102. return nil
  103. }
  104. obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  105. if err != nil {
  106. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  107. return nil
  108. }
  109. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  110. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  111. if err != nil {
  112. t.Errorf("%v\nSource: %#v", err, obj2)
  113. return nil
  114. }
  115. return obj3
  116. }