defaults_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. batchv1 "k8s.io/api/batch/v1"
  18. "k8s.io/api/core/v1"
  19. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  20. "k8s.io/apimachinery/pkg/runtime"
  21. "k8s.io/kubernetes/pkg/api/legacyscheme"
  22. _ "k8s.io/kubernetes/pkg/apis/batch/install"
  23. . "k8s.io/kubernetes/pkg/apis/batch/v1"
  24. _ "k8s.io/kubernetes/pkg/apis/core/install"
  25. utilpointer "k8s.io/utils/pointer"
  26. )
  27. func TestSetDefaultJob(t *testing.T) {
  28. defaultLabels := map[string]string{"default": "default"}
  29. tests := map[string]struct {
  30. original *batchv1.Job
  31. expected *batchv1.Job
  32. expectLabels bool
  33. }{
  34. "All unspecified -> sets all to default values": {
  35. original: &batchv1.Job{
  36. Spec: batchv1.JobSpec{
  37. Template: v1.PodTemplateSpec{
  38. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  39. },
  40. },
  41. },
  42. expected: &batchv1.Job{
  43. Spec: batchv1.JobSpec{
  44. Completions: utilpointer.Int32Ptr(1),
  45. Parallelism: utilpointer.Int32Ptr(1),
  46. BackoffLimit: utilpointer.Int32Ptr(6),
  47. },
  48. },
  49. expectLabels: true,
  50. },
  51. "All unspecified -> all integers are defaulted and no default labels": {
  52. original: &batchv1.Job{
  53. ObjectMeta: metav1.ObjectMeta{
  54. Labels: map[string]string{"mylabel": "myvalue"},
  55. },
  56. Spec: batchv1.JobSpec{
  57. Template: v1.PodTemplateSpec{
  58. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  59. },
  60. },
  61. },
  62. expected: &batchv1.Job{
  63. Spec: batchv1.JobSpec{
  64. Completions: utilpointer.Int32Ptr(1),
  65. Parallelism: utilpointer.Int32Ptr(1),
  66. BackoffLimit: utilpointer.Int32Ptr(6),
  67. },
  68. },
  69. },
  70. "WQ: Parallelism explicitly 0 and completions unset -> BackoffLimit is defaulted": {
  71. original: &batchv1.Job{
  72. Spec: batchv1.JobSpec{
  73. Parallelism: utilpointer.Int32Ptr(0),
  74. Template: v1.PodTemplateSpec{
  75. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  76. },
  77. },
  78. },
  79. expected: &batchv1.Job{
  80. Spec: batchv1.JobSpec{
  81. Parallelism: utilpointer.Int32Ptr(0),
  82. BackoffLimit: utilpointer.Int32Ptr(6),
  83. },
  84. },
  85. expectLabels: true,
  86. },
  87. "WQ: Parallelism explicitly 2 and completions unset -> BackoffLimit is defaulted": {
  88. original: &batchv1.Job{
  89. Spec: batchv1.JobSpec{
  90. Parallelism: utilpointer.Int32Ptr(2),
  91. Template: v1.PodTemplateSpec{
  92. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  93. },
  94. },
  95. },
  96. expected: &batchv1.Job{
  97. Spec: batchv1.JobSpec{
  98. Parallelism: utilpointer.Int32Ptr(2),
  99. BackoffLimit: utilpointer.Int32Ptr(6),
  100. },
  101. },
  102. expectLabels: true,
  103. },
  104. "Completions explicitly 2 and others unset -> parallelism and BackoffLimit are defaulted": {
  105. original: &batchv1.Job{
  106. Spec: batchv1.JobSpec{
  107. Completions: utilpointer.Int32Ptr(2),
  108. Template: v1.PodTemplateSpec{
  109. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  110. },
  111. },
  112. },
  113. expected: &batchv1.Job{
  114. Spec: batchv1.JobSpec{
  115. Completions: utilpointer.Int32Ptr(2),
  116. Parallelism: utilpointer.Int32Ptr(1),
  117. BackoffLimit: utilpointer.Int32Ptr(6),
  118. },
  119. },
  120. expectLabels: true,
  121. },
  122. "BackoffLimit explicitly 5 and others unset -> parallelism and completions are defaulted": {
  123. original: &batchv1.Job{
  124. Spec: batchv1.JobSpec{
  125. BackoffLimit: utilpointer.Int32Ptr(5),
  126. Template: v1.PodTemplateSpec{
  127. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  128. },
  129. },
  130. },
  131. expected: &batchv1.Job{
  132. Spec: batchv1.JobSpec{
  133. Completions: utilpointer.Int32Ptr(1),
  134. Parallelism: utilpointer.Int32Ptr(1),
  135. BackoffLimit: utilpointer.Int32Ptr(5),
  136. },
  137. },
  138. expectLabels: true,
  139. },
  140. "All set -> no change": {
  141. original: &batchv1.Job{
  142. Spec: batchv1.JobSpec{
  143. Completions: utilpointer.Int32Ptr(8),
  144. Parallelism: utilpointer.Int32Ptr(9),
  145. BackoffLimit: utilpointer.Int32Ptr(10),
  146. Template: v1.PodTemplateSpec{
  147. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  148. },
  149. },
  150. },
  151. expected: &batchv1.Job{
  152. Spec: batchv1.JobSpec{
  153. Completions: utilpointer.Int32Ptr(8),
  154. Parallelism: utilpointer.Int32Ptr(9),
  155. BackoffLimit: utilpointer.Int32Ptr(10),
  156. Template: v1.PodTemplateSpec{
  157. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  158. },
  159. },
  160. },
  161. expectLabels: true,
  162. },
  163. "All set, flipped -> no change": {
  164. original: &batchv1.Job{
  165. Spec: batchv1.JobSpec{
  166. Completions: utilpointer.Int32Ptr(11),
  167. Parallelism: utilpointer.Int32Ptr(10),
  168. BackoffLimit: utilpointer.Int32Ptr(9),
  169. Template: v1.PodTemplateSpec{
  170. ObjectMeta: metav1.ObjectMeta{Labels: defaultLabels},
  171. },
  172. },
  173. },
  174. expected: &batchv1.Job{
  175. Spec: batchv1.JobSpec{
  176. Completions: utilpointer.Int32Ptr(11),
  177. Parallelism: utilpointer.Int32Ptr(10),
  178. BackoffLimit: utilpointer.Int32Ptr(9),
  179. },
  180. },
  181. expectLabels: true,
  182. },
  183. }
  184. for name, test := range tests {
  185. original := test.original
  186. expected := test.expected
  187. obj2 := roundTrip(t, runtime.Object(original))
  188. actual, ok := obj2.(*batchv1.Job)
  189. if !ok {
  190. t.Errorf("%s: unexpected object: %v", name, actual)
  191. t.FailNow()
  192. }
  193. validateDefaultInt32(t, name, "Completions", actual.Spec.Completions, expected.Spec.Completions)
  194. validateDefaultInt32(t, name, "Parallelism", actual.Spec.Parallelism, expected.Spec.Parallelism)
  195. validateDefaultInt32(t, name, "BackoffLimit", actual.Spec.BackoffLimit, expected.Spec.BackoffLimit)
  196. if test.expectLabels != reflect.DeepEqual(actual.Labels, actual.Spec.Template.Labels) {
  197. if test.expectLabels {
  198. t.Errorf("%s: expected: %v, got: %v", name, actual.Spec.Template.Labels, actual.Labels)
  199. } else {
  200. t.Errorf("%s: unexpected equality: %v", name, actual.Labels)
  201. }
  202. }
  203. }
  204. }
  205. func validateDefaultInt32(t *testing.T, name string, field string, actual *int32, expected *int32) {
  206. if (actual == nil) != (expected == nil) {
  207. t.Errorf("%s: got different *%s than expected: %v %v", name, field, actual, expected)
  208. }
  209. if actual != nil && expected != nil {
  210. if *actual != *expected {
  211. t.Errorf("%s: got different %s than expected: %d %d", name, field, *actual, *expected)
  212. }
  213. }
  214. }
  215. func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
  216. data, err := runtime.Encode(legacyscheme.Codecs.LegacyCodec(SchemeGroupVersion), obj)
  217. if err != nil {
  218. t.Errorf("%v\n %#v", err, obj)
  219. return nil
  220. }
  221. obj2, err := runtime.Decode(legacyscheme.Codecs.UniversalDecoder(), data)
  222. if err != nil {
  223. t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
  224. return nil
  225. }
  226. obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
  227. err = legacyscheme.Scheme.Convert(obj2, obj3, nil)
  228. if err != nil {
  229. t.Errorf("%v\nSource: %#v", err, obj2)
  230. return nil
  231. }
  232. return obj3
  233. }