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