create_job_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "strings"
  16. "testing"
  17. apps "k8s.io/api/apps/v1"
  18. batchv1 "k8s.io/api/batch/v1"
  19. batchv1beta1 "k8s.io/api/batch/v1beta1"
  20. corev1 "k8s.io/api/core/v1"
  21. apiequality "k8s.io/apimachinery/pkg/api/equality"
  22. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  23. )
  24. func TestCreateJobValidation(t *testing.T) {
  25. tests := map[string]struct {
  26. image string
  27. command []string
  28. from string
  29. expected string
  30. }{
  31. "empty flags": {
  32. expected: "--image or --from must be specified",
  33. },
  34. "both image and from specified": {
  35. image: "my-image",
  36. from: "cronjob/xyz",
  37. expected: "--image or --from must be specified",
  38. },
  39. "from and command specified": {
  40. from: "cronjob/xyz",
  41. command: []string{"test", "command"},
  42. expected: "cannot specify --from and command",
  43. },
  44. }
  45. for name, tc := range tests {
  46. t.Run(name, func(t *testing.T) {
  47. o := &CreateJobOptions{
  48. Image: tc.image,
  49. From: tc.from,
  50. Command: tc.command,
  51. }
  52. err := o.Validate()
  53. if err != nil && !strings.Contains(err.Error(), tc.expected) {
  54. t.Errorf("unexpected error: %v", err)
  55. }
  56. })
  57. }
  58. }
  59. func TestCreateJob(t *testing.T) {
  60. jobName := "test-job"
  61. tests := map[string]struct {
  62. image string
  63. command []string
  64. expected *batchv1.Job
  65. }{
  66. "just image": {
  67. image: "busybox",
  68. expected: &batchv1.Job{
  69. TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
  70. ObjectMeta: metav1.ObjectMeta{
  71. Name: jobName,
  72. },
  73. Spec: batchv1.JobSpec{
  74. Template: corev1.PodTemplateSpec{
  75. Spec: corev1.PodSpec{
  76. Containers: []corev1.Container{
  77. {
  78. Name: jobName,
  79. Image: "busybox",
  80. },
  81. },
  82. RestartPolicy: corev1.RestartPolicyNever,
  83. },
  84. },
  85. },
  86. },
  87. },
  88. "image and command": {
  89. image: "busybox",
  90. command: []string{"date"},
  91. expected: &batchv1.Job{
  92. TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
  93. ObjectMeta: metav1.ObjectMeta{
  94. Name: jobName,
  95. },
  96. Spec: batchv1.JobSpec{
  97. Template: corev1.PodTemplateSpec{
  98. Spec: corev1.PodSpec{
  99. Containers: []corev1.Container{
  100. {
  101. Name: jobName,
  102. Image: "busybox",
  103. Command: []string{"date"},
  104. },
  105. },
  106. RestartPolicy: corev1.RestartPolicyNever,
  107. },
  108. },
  109. },
  110. },
  111. },
  112. }
  113. for name, tc := range tests {
  114. t.Run(name, func(t *testing.T) {
  115. o := &CreateJobOptions{
  116. Name: jobName,
  117. Image: tc.image,
  118. Command: tc.command,
  119. }
  120. job := o.createJob()
  121. if !apiequality.Semantic.DeepEqual(job, tc.expected) {
  122. t.Errorf("expected:\n%#v\ngot:\n%#v", tc.expected, job)
  123. }
  124. })
  125. }
  126. }
  127. func TestCreateJobFromCronJob(t *testing.T) {
  128. jobName := "test-job"
  129. cronJob := &batchv1beta1.CronJob{
  130. Spec: batchv1beta1.CronJobSpec{
  131. JobTemplate: batchv1beta1.JobTemplateSpec{
  132. Spec: batchv1.JobSpec{
  133. Template: corev1.PodTemplateSpec{
  134. Spec: corev1.PodSpec{
  135. Containers: []corev1.Container{
  136. {Image: "test-image"},
  137. },
  138. RestartPolicy: corev1.RestartPolicyNever,
  139. },
  140. },
  141. },
  142. },
  143. },
  144. }
  145. tests := map[string]struct {
  146. from *batchv1beta1.CronJob
  147. expected *batchv1.Job
  148. }{
  149. "from CronJob": {
  150. from: cronJob,
  151. expected: &batchv1.Job{
  152. TypeMeta: metav1.TypeMeta{APIVersion: batchv1.SchemeGroupVersion.String(), Kind: "Job"},
  153. ObjectMeta: metav1.ObjectMeta{
  154. Name: jobName,
  155. Annotations: map[string]string{"cronjob.kubernetes.io/instantiate": "manual"},
  156. OwnerReferences: []metav1.OwnerReference{*metav1.NewControllerRef(cronJob, apps.SchemeGroupVersion.WithKind("CronJob"))},
  157. },
  158. Spec: batchv1.JobSpec{
  159. Template: corev1.PodTemplateSpec{
  160. Spec: corev1.PodSpec{
  161. Containers: []corev1.Container{
  162. {Image: "test-image"},
  163. },
  164. RestartPolicy: corev1.RestartPolicyNever,
  165. },
  166. },
  167. },
  168. },
  169. },
  170. }
  171. for name, tc := range tests {
  172. t.Run(name, func(t *testing.T) {
  173. o := &CreateJobOptions{
  174. Name: jobName,
  175. }
  176. job := o.createJobFromCronJob(tc.from)
  177. if !apiequality.Semantic.DeepEqual(job, tc.expected) {
  178. t.Errorf("expected:\n%#v\ngot:\n%#v", tc.expected, job)
  179. }
  180. })
  181. }
  182. }