Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions pkg/utils/kubeclient/cronjob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
batchv1 "k8s.io/api/batch/v1"
batchv1beta1 "k8s.io/api/batch/v1beta1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -64,6 +66,7 @@ var _ = Describe("GetCronJobStatus", func() {
testCronJobs = append(testCronJobs, cj.DeepCopy())
}

_ = batchv1beta1.AddToScheme(testScheme)
client = fake.NewFakeClientWithScheme(testScheme, testCronJobs...)

// Apply gomonkey patch
Expand Down Expand Up @@ -106,4 +109,61 @@ var _ = Describe("GetCronJobStatus", func() {
Expect(got).To(BeNil())
})
})

Context("when batchv1 CronJob is not supported and batchv1beta1 CronJob exists", func() {
It("should return converted status from batchv1beta1", func() {
key := types.NamespacedName{
Namespace: namespace,
Name: "test-beta",
}
betaCronJob := &batchv1beta1.CronJob{
ObjectMeta: metav1.ObjectMeta{
Name: "test-beta",
Namespace: namespace,
},
Status: batchv1beta1.CronJobStatus{
Active: []corev1.ObjectReference{
{Name: "pod-0"},
},
LastScheduleTime: &testDate,
LastSuccessfulTime: &testDate,
},
}
betaClient := fake.NewFakeClientWithScheme(testScheme, betaCronJob.DeepCopy())

patch.Reset()
patch = gomonkey.ApplyFunc(compatibility.IsBatchV1CronJobSupported, func() bool {
return false
})

got, err := GetCronJobStatus(betaClient, key)

Expect(err).NotTo(HaveOccurred())
Expect(got).NotTo(BeNil())
Expect(got.LastScheduleTime).To(Equal(&testDate))
Expect(got.LastSuccessfulTime).To(Equal(&testDate))
Expect(got.Active).To(HaveLen(1))
Expect(got.Active[0].Name).To(Equal("pod-0"))
})
})

Context("when batchv1 CronJob is not supported and batchv1beta1 CronJob does not exist", func() {
It("should return an error", func() {
key := types.NamespacedName{
Namespace: namespace,
Name: "test-beta-notexist",
}
emptyClient := fake.NewFakeClientWithScheme(testScheme)

patch.Reset()
patch = gomonkey.ApplyFunc(compatibility.IsBatchV1CronJobSupported, func() bool {
return false
})

got, err := GetCronJobStatus(emptyClient, key)

Expect(err).To(HaveOccurred())
Expect(got).To(BeNil())
})
})
})