defaults_test.go 3.0 KB

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