defaults_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2016 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 v1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. autoscalingv1 "k8s.io/api/autoscaling/v1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. _ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
  21. . "k8s.io/kubernetes/pkg/apis/autoscaling/v1"
  22. _ "k8s.io/kubernetes/pkg/apis/core/install"
  23. utilpointer "k8s.io/utils/pointer"
  24. )
  25. func TestSetDefaultHPA(t *testing.T) {
  26. tests := []struct {
  27. hpa autoscalingv1.HorizontalPodAutoscaler
  28. expectReplicas int32
  29. test string
  30. }{
  31. {
  32. hpa: autoscalingv1.HorizontalPodAutoscaler{},
  33. expectReplicas: 1,
  34. test: "unspecified min replicas, use the default value",
  35. },
  36. {
  37. hpa: autoscalingv1.HorizontalPodAutoscaler{
  38. Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
  39. MinReplicas: utilpointer.Int32Ptr(3),
  40. },
  41. },
  42. expectReplicas: 3,
  43. test: "set min replicas to 3",
  44. },
  45. }
  46. for _, test := range tests {
  47. hpa := &test.hpa
  48. obj2 := roundTrip(t, runtime.Object(hpa))
  49. hpa2, ok := obj2.(*autoscalingv1.HorizontalPodAutoscaler)
  50. if !ok {
  51. t.Fatalf("unexpected object: %v", obj2)
  52. }
  53. if hpa2.Spec.MinReplicas == nil {
  54. t.Errorf("unexpected nil MinReplicas")
  55. } else if test.expectReplicas != *hpa2.Spec.MinReplicas {
  56. t.Errorf("expected: %d MinReplicas, got: %d", test.expectReplicas, *hpa2.Spec.MinReplicas)
  57. }
  58. }
  59. }
  60. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  61. data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  62. if err != nil {
  63. t.Errorf("%v\n %#v", err, obj)
  64. return nil
  65. }
  66. obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  67. if err != nil {
  68. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  69. return nil
  70. }
  71. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  72. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  73. if err != nil {
  74. t.Errorf("%v\nSource: %#v", err, obj2)
  75. return nil
  76. }
  77. return obj3
  78. }