@@ -17,94 +17,93 @@ limitations under the License.
1717package kubeclient
1818
1919import (
20- "reflect"
21- "testing"
2220 "time"
2321
2422 "github.com/agiledragon/gomonkey/v2"
2523 "github.com/fluid-cloudnative/fluid/pkg/utils/compatibility"
2624 "github.com/fluid-cloudnative/fluid/pkg/utils/fake"
25+ . "github.com/onsi/ginkgo/v2"
26+ . "github.com/onsi/gomega"
2727 batchv1 "k8s.io/api/batch/v1"
2828 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2929 "k8s.io/apimachinery/pkg/runtime"
3030 "k8s.io/apimachinery/pkg/types"
31+ "sigs.k8s.io/controller-runtime/pkg/client"
3132)
3233
33- func TestGetCronJobStatus (t * testing.T ) {
34- nowTime := time .Now ()
35- testDate := metav1 .NewTime (time .Date (nowTime .Year (), nowTime .Month (), nowTime .Day (), nowTime .Hour (), 0 , 0 , 0 , nowTime .Location ()))
34+ var _ = Describe ("GetCronJobStatus" , func () {
35+ var (
36+ nowTime time.Time
37+ testDate metav1.Time
38+ namespace string
39+ testCronJobInputs []* batchv1.CronJob
40+ testCronJobs []runtime.Object
41+ client client.Client
42+ patch * gomonkey.Patches
43+ )
3644
37- namespace := "default"
38- testCronJobInputs := []* batchv1.CronJob {
39- {
40- ObjectMeta : metav1.ObjectMeta {
41- Name : "test1" ,
42- Namespace : namespace ,
43- },
44- Status : batchv1.CronJobStatus {
45- LastScheduleTime : & testDate ,
46- },
47- },
48- }
49-
50- testCronJobs := []runtime.Object {}
51-
52- for _ , cj := range testCronJobInputs {
53- testCronJobs = append (testCronJobs , cj .DeepCopy ())
54- }
55-
56- client := fake .NewFakeClientWithScheme (testScheme , testCronJobs ... )
57-
58- type args struct {
59- key types.NamespacedName
60- }
61- tests := []struct {
62- name string
63- args args
64- want * batchv1.CronJobStatus
65- wantErr bool
66- }{
67- {
68- name : "CronJob exists" ,
69- args : args {
70- key : types.NamespacedName {
71- Namespace : namespace ,
45+ BeforeEach (func () {
46+ nowTime = time .Now ()
47+ testDate = metav1 .NewTime (time .Date (nowTime .Year (), nowTime .Month (), nowTime .Day (), nowTime .Hour (), 0 , 0 , 0 , nowTime .Location ()))
48+ namespace = "default"
49+
50+ testCronJobInputs = []* batchv1.CronJob {
51+ {
52+ ObjectMeta : metav1.ObjectMeta {
7253 Name : "test1" ,
73- },
74- },
75- want : & batchv1.CronJobStatus {
76- LastScheduleTime : & testDate ,
77- },
78- wantErr : false ,
79- },
80- {
81- name : "CronJob exists" ,
82- args : args {
83- key : types.NamespacedName {
8454 Namespace : namespace ,
85- Name : "test-notexist" ,
55+ },
56+ Status : batchv1.CronJobStatus {
57+ LastScheduleTime : & testDate ,
8658 },
8759 },
88- want : nil ,
89- wantErr : true ,
90- },
91- }
60+ }
61+
62+ testCronJobs = []runtime.Object {}
63+ for _ , cj := range testCronJobInputs {
64+ testCronJobs = append (testCronJobs , cj .DeepCopy ())
65+ }
9266
93- patch := gomonkey .ApplyFunc (compatibility .IsBatchV1CronJobSupported , func () bool {
94- return true
67+ client = fake .NewFakeClientWithScheme (testScheme , testCronJobs ... )
68+
69+ // Apply gomonkey patch
70+ patch = gomonkey .ApplyFunc (compatibility .IsBatchV1CronJobSupported , func () bool {
71+ return true
72+ })
9573 })
96- defer patch .Reset ()
97-
98- for _ , tt := range tests {
99- t .Run (tt .name , func (t * testing.T ) {
100- got , err := GetCronJobStatus (client , tt .args .key )
101- if (err != nil ) != tt .wantErr {
102- t .Errorf ("GetCronJobStatus() error = %v, wantErr %v" , err , tt .wantErr )
103- return
74+
75+ AfterEach (func () {
76+ if patch != nil {
77+ patch .Reset ()
78+ }
79+ })
80+
81+ Context ("when CronJob exists" , func () {
82+ It ("should return the CronJob status successfully" , func () {
83+ key := types.NamespacedName {
84+ Namespace : namespace ,
85+ Name : "test1" ,
10486 }
105- if ! reflect .DeepEqual (got , tt .want ) {
106- t .Errorf ("GetCronJobStatus() = %v, want %v" , got , tt .want )
87+
88+ got , err := GetCronJobStatus (client , key )
89+
90+ Expect (err ).NotTo (HaveOccurred ())
91+ Expect (got ).NotTo (BeNil ())
92+ Expect (got .LastScheduleTime ).To (Equal (& testDate ))
93+ })
94+ })
95+
96+ Context ("when CronJob does not exist" , func () {
97+ It ("should return an error" , func () {
98+ key := types.NamespacedName {
99+ Namespace : namespace ,
100+ Name : "test-notexist" ,
107101 }
102+
103+ got , err := GetCronJobStatus (client , key )
104+
105+ Expect (err ).To (HaveOccurred ())
106+ Expect (got ).To (BeNil ())
108107 })
109- }
110- }
108+ })
109+ })
0 commit comments