defaults_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 v1beta1_test
  14. import (
  15. "reflect"
  16. "testing"
  17. batchv1beta1 "k8s.io/api/batch/v1beta1"
  18. "k8s.io/apimachinery/pkg/runtime"
  19. "k8s.io/kubernetes/pkg/api/legacyscheme"
  20. _ "k8s.io/kubernetes/pkg/apis/batch/install"
  21. . "k8s.io/kubernetes/pkg/apis/batch/v1beta1"
  22. _ "k8s.io/kubernetes/pkg/apis/core/install"
  23. utilpointer "k8s.io/utils/pointer"
  24. )
  25. func TestSetDefaultCronJob(t *testing.T) {
  26. tests := map[string]struct {
  27. original *batchv1beta1.CronJob
  28. expected *batchv1beta1.CronJob
  29. }{
  30. "empty batchv1beta1.CronJob should default batchv1beta1.ConcurrencyPolicy and Suspend": {
  31. original: &batchv1beta1.CronJob{},
  32. expected: &batchv1beta1.CronJob{
  33. Spec: batchv1beta1.CronJobSpec{
  34. ConcurrencyPolicy: batchv1beta1.AllowConcurrent,
  35. Suspend: newBool(false),
  36. SuccessfulJobsHistoryLimit: utilpointer.Int32Ptr(3),
  37. FailedJobsHistoryLimit: utilpointer.Int32Ptr(1),
  38. },
  39. },
  40. },
  41. "set fields should not be defaulted": {
  42. original: &batchv1beta1.CronJob{
  43. Spec: batchv1beta1.CronJobSpec{
  44. ConcurrencyPolicy: batchv1beta1.ForbidConcurrent,
  45. Suspend: newBool(true),
  46. SuccessfulJobsHistoryLimit: utilpointer.Int32Ptr(5),
  47. FailedJobsHistoryLimit: utilpointer.Int32Ptr(5),
  48. },
  49. },
  50. expected: &batchv1beta1.CronJob{
  51. Spec: batchv1beta1.CronJobSpec{
  52. ConcurrencyPolicy: batchv1beta1.ForbidConcurrent,
  53. Suspend: newBool(true),
  54. SuccessfulJobsHistoryLimit: utilpointer.Int32Ptr(5),
  55. FailedJobsHistoryLimit: utilpointer.Int32Ptr(5),
  56. },
  57. },
  58. },
  59. }
  60. for name, test := range tests {
  61. original := test.original
  62. expected := test.expected
  63. obj2 := roundTrip(t, runtime.Object(original))
  64. actual, ok := obj2.(*batchv1beta1.CronJob)
  65. if !ok {
  66. t.Errorf("%s: unexpected object: %v", name, actual)
  67. t.FailNow()
  68. }
  69. if actual.Spec.ConcurrencyPolicy != expected.Spec.ConcurrencyPolicy {
  70. t.Errorf("%s: got different concurrencyPolicy than expected: %v %v", name, actual.Spec.ConcurrencyPolicy, expected.Spec.ConcurrencyPolicy)
  71. }
  72. if *actual.Spec.Suspend != *expected.Spec.Suspend {
  73. t.Errorf("%s: got different suspend than expected: %v %v", name, *actual.Spec.Suspend, *expected.Spec.Suspend)
  74. }
  75. if *actual.Spec.SuccessfulJobsHistoryLimit != *expected.Spec.SuccessfulJobsHistoryLimit {
  76. t.Errorf("%s: got different successfulJobsHistoryLimit than expected: %v %v", name, *actual.Spec.SuccessfulJobsHistoryLimit, *expected.Spec.SuccessfulJobsHistoryLimit)
  77. }
  78. if *actual.Spec.FailedJobsHistoryLimit != *expected.Spec.FailedJobsHistoryLimit {
  79. t.Errorf("%s: got different failedJobsHistoryLimit than expected: %v %v", name, *actual.Spec.FailedJobsHistoryLimit, *expected.Spec.FailedJobsHistoryLimit)
  80. }
  81. }
  82. }
  83. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  84. data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  85. if err != nil {
  86. t.Errorf("%v\n %#v", err, obj)
  87. return nil
  88. }
  89. obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  90. if err != nil {
  91. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  92. return nil
  93. }
  94. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  95. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  96. if err != nil {
  97. t.Errorf("%v\nSource: %#v", err, obj2)
  98. return nil
  99. }
  100. return obj3
  101. }
  102. func newBool(val bool) *bool {
  103. p := new(bool)
  104. *p = val
  105. return p
  106. }