create_cronjob_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2018 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 create
  14. import (
  15. "testing"
  16. batchv1 "k8s.io/api/batch/v1"
  17. batchv1beta1 "k8s.io/api/batch/v1beta1"
  18. corev1 "k8s.io/api/core/v1"
  19. apiequality "k8s.io/apimachinery/pkg/api/equality"
  20. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  21. )
  22. func TestCreateCronJob(t *testing.T) {
  23. cronjobName := "test-job"
  24. tests := map[string]struct {
  25. image string
  26. command []string
  27. schedule string
  28. restart string
  29. expected *batchv1beta1.CronJob
  30. }{
  31. "just image and OnFailure restart policy": {
  32. image: "busybox",
  33. schedule: "0/5 * * * ?",
  34. restart: "OnFailure",
  35. expected: &batchv1beta1.CronJob{
  36. TypeMeta: metav1.TypeMeta{APIVersion: batchv1beta1.SchemeGroupVersion.String(), Kind: "CronJob"},
  37. ObjectMeta: metav1.ObjectMeta{
  38. Name: cronjobName,
  39. },
  40. Spec: batchv1beta1.CronJobSpec{
  41. Schedule: "0/5 * * * ?",
  42. JobTemplate: batchv1beta1.JobTemplateSpec{
  43. ObjectMeta: metav1.ObjectMeta{
  44. Name: cronjobName,
  45. },
  46. Spec: batchv1.JobSpec{
  47. Template: corev1.PodTemplateSpec{
  48. Spec: corev1.PodSpec{
  49. Containers: []corev1.Container{
  50. {
  51. Name: cronjobName,
  52. Image: "busybox",
  53. },
  54. },
  55. RestartPolicy: corev1.RestartPolicyOnFailure,
  56. },
  57. },
  58. },
  59. },
  60. },
  61. },
  62. },
  63. "image, command , schedule and Never restart policy": {
  64. image: "busybox",
  65. command: []string{"date"},
  66. schedule: "0/5 * * * ?",
  67. restart: "Never",
  68. expected: &batchv1beta1.CronJob{
  69. TypeMeta: metav1.TypeMeta{APIVersion: batchv1beta1.SchemeGroupVersion.String(), Kind: "CronJob"},
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: cronjobName,
  72. },
  73. Spec: batchv1beta1.CronJobSpec{
  74. Schedule: "0/5 * * * ?",
  75. JobTemplate: batchv1beta1.JobTemplateSpec{
  76. ObjectMeta: metav1.ObjectMeta{
  77. Name: cronjobName,
  78. },
  79. Spec: batchv1.JobSpec{
  80. Template: corev1.PodTemplateSpec{
  81. Spec: corev1.PodSpec{
  82. Containers: []corev1.Container{
  83. {
  84. Name: cronjobName,
  85. Image: "busybox",
  86. Command: []string{"date"},
  87. },
  88. },
  89. RestartPolicy: corev1.RestartPolicyNever,
  90. },
  91. },
  92. },
  93. },
  94. },
  95. },
  96. },
  97. }
  98. for name, tc := range tests {
  99. t.Run(name, func(t *testing.T) {
  100. o := &CreateCronJobOptions{
  101. Name: cronjobName,
  102. Image: tc.image,
  103. Command: tc.command,
  104. Schedule: tc.schedule,
  105. Restart: tc.restart,
  106. }
  107. cronjob := o.createCronJob()
  108. if !apiequality.Semantic.DeepEqual(cronjob, tc.expected) {
  109. t.Errorf("expected:\n%#v\ngot:\n%#v", tc.expected, cronjob)
  110. }
  111. })
  112. }
  113. }