Skip to content

Commit 44c3489

Browse files
authored
test(runtime): refactor tests for runtimeEventHandler with Ginkgo (#5598)
Signed-off-by: adity1raut <araut7798@gmail.com>
1 parent 2aab098 commit 44c3489

1 file changed

Lines changed: 94 additions & 94 deletions

File tree

pkg/ctrl/watch/runtime_test.go

Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -17,103 +17,103 @@ limitations under the License.
1717
package watch
1818

1919
import (
20-
"testing"
21-
2220
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
2321
corev1 "k8s.io/api/core/v1"
2422
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2523
"sigs.k8s.io/controller-runtime/pkg/event"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
2627
)
2728

28-
func TestOnCreateFunc(t *testing.T) {
29-
30-
// 1. the Object is RuntimeInterface
31-
createRuntimeEvent := event.CreateEvent{
32-
Object: &datav1alpha1.JindoRuntime{},
33-
}
34-
runtimeEventHandler := &runtimeEventHandler{}
35-
36-
f := runtimeEventHandler.onCreateFunc(&FakeRuntimeReconciler{})
37-
predicate := f(createRuntimeEvent)
38-
39-
if !predicate {
40-
t.Errorf("The event %v should be reconciled, but skip.", createRuntimeEvent)
41-
}
42-
43-
// 2. the Object is not RuntimeInterface
44-
createRuntimeEvent.Object = &corev1.Pod{}
45-
predicate = f(createRuntimeEvent)
46-
if predicate {
47-
t.Errorf("The event %v should ben't reconciled, but pass.", createRuntimeEvent)
48-
}
49-
}
50-
51-
func TestOnUpdateFunc(t *testing.T) {
52-
53-
updateRuntimeEvent := event.UpdateEvent{
54-
ObjectOld: &datav1alpha1.JindoRuntime{
55-
ObjectMeta: metav1.ObjectMeta{
56-
ResourceVersion: "123",
57-
},
58-
},
59-
ObjectNew: &datav1alpha1.JindoRuntime{
60-
ObjectMeta: metav1.ObjectMeta{
61-
ResourceVersion: "456",
62-
},
63-
},
64-
}
65-
runtimeEventHandler := &runtimeEventHandler{}
66-
67-
f := runtimeEventHandler.onUpdateFunc(&FakeRuntimeReconciler{})
68-
predicate := f(updateRuntimeEvent)
69-
70-
// 1. expect the updateEvent is validated
71-
if !predicate {
72-
t.Errorf("The event %v should be reconciled, but skip.", updateRuntimeEvent)
73-
}
74-
75-
// 2. expect the updateEvent is not validated due to the resource version is equal
76-
updateRuntimeEvent.ObjectOld.SetResourceVersion("456")
77-
predicate = f(updateRuntimeEvent)
78-
if predicate {
79-
t.Errorf("The event %v should ben't reconciled, but pass.", updateRuntimeEvent)
80-
}
81-
82-
// 3. expect the updateEvent is not validated due to the object is not kind of runtimeInterface
83-
updateRuntimeEvent.ObjectOld = &corev1.Pod{}
84-
updateRuntimeEvent.ObjectNew = &corev1.Pod{}
85-
predicate = f(updateRuntimeEvent)
86-
if predicate {
87-
t.Errorf("The event %v should ben't reconciled, but pass.", updateRuntimeEvent)
88-
}
89-
90-
// 4. expect the updateEvent is not validate due the old Object is not kind of the runtimeInterface
91-
updateRuntimeEvent.ObjectNew = &datav1alpha1.JindoRuntime{}
92-
predicate = f(updateRuntimeEvent)
93-
if predicate {
94-
t.Errorf("The event %v should ben't reconciled, but pass.", updateRuntimeEvent)
95-
}
96-
}
97-
98-
func TestOnDeleteFunc(t *testing.T) {
99-
100-
// 1. the Object is RuntimeInterface
101-
delRuntimeEvent := event.DeleteEvent{
102-
Object: &datav1alpha1.JindoRuntime{},
103-
}
104-
runtimeEventHandler := &runtimeEventHandler{}
105-
106-
f := runtimeEventHandler.onDeleteFunc(&FakeRuntimeReconciler{})
107-
predicate := f(delRuntimeEvent)
108-
109-
if !predicate {
110-
t.Errorf("The event %v should be reconciled, but skip.", delRuntimeEvent)
111-
}
112-
113-
// 2. the Object is not RuntimeInterface
114-
delRuntimeEvent.Object = &corev1.Pod{}
115-
predicate = f(delRuntimeEvent)
116-
if predicate {
117-
t.Errorf("The event %v should ben't reconciled, but pass.", delRuntimeEvent)
118-
}
119-
}
29+
var _ = Describe("runtimeEventHandler", func() {
30+
var (
31+
handler *runtimeEventHandler
32+
reconciler *FakeRuntimeReconciler
33+
)
34+
35+
BeforeEach(func() {
36+
handler = &runtimeEventHandler{}
37+
reconciler = &FakeRuntimeReconciler{}
38+
})
39+
40+
Describe("onCreateFunc", func() {
41+
It("should reconcile if the object is RuntimeInterface", func() {
42+
createRuntimeEvent := event.CreateEvent{
43+
Object: &datav1alpha1.JindoRuntime{},
44+
}
45+
f := handler.onCreateFunc(reconciler)
46+
Expect(f(createRuntimeEvent)).To(BeTrue())
47+
})
48+
49+
It("should not reconcile if the object is not RuntimeInterface", func() {
50+
createRuntimeEvent := event.CreateEvent{
51+
Object: &corev1.Pod{},
52+
}
53+
f := handler.onCreateFunc(reconciler)
54+
Expect(f(createRuntimeEvent)).To(BeFalse())
55+
})
56+
})
57+
58+
Describe("onUpdateFunc", func() {
59+
var updateRuntimeEvent event.UpdateEvent
60+
61+
BeforeEach(func() {
62+
updateRuntimeEvent = event.UpdateEvent{
63+
ObjectOld: &datav1alpha1.JindoRuntime{
64+
ObjectMeta: metav1.ObjectMeta{
65+
ResourceVersion: "123",
66+
},
67+
},
68+
ObjectNew: &datav1alpha1.JindoRuntime{
69+
ObjectMeta: metav1.ObjectMeta{
70+
ResourceVersion: "456",
71+
},
72+
},
73+
}
74+
})
75+
76+
It("should reconcile if resource versions differ and both are RuntimeInterface", func() {
77+
f := handler.onUpdateFunc(reconciler)
78+
Expect(f(updateRuntimeEvent)).To(BeTrue())
79+
})
80+
81+
It("should not reconcile if resource versions are equal", func() {
82+
updateRuntimeEvent.ObjectOld.(*datav1alpha1.JindoRuntime).SetResourceVersion("456")
83+
f := handler.onUpdateFunc(reconciler)
84+
Expect(f(updateRuntimeEvent)).To(BeFalse())
85+
})
86+
87+
It("should not reconcile if both objects are not RuntimeInterface", func() {
88+
updateRuntimeEvent.ObjectOld = &corev1.Pod{}
89+
updateRuntimeEvent.ObjectNew = &corev1.Pod{}
90+
f := handler.onUpdateFunc(reconciler)
91+
Expect(f(updateRuntimeEvent)).To(BeFalse())
92+
})
93+
94+
It("should not reconcile if only new object is RuntimeInterface", func() {
95+
updateRuntimeEvent.ObjectOld = &corev1.Pod{}
96+
updateRuntimeEvent.ObjectNew = &datav1alpha1.JindoRuntime{}
97+
f := handler.onUpdateFunc(reconciler)
98+
Expect(f(updateRuntimeEvent)).To(BeFalse())
99+
})
100+
})
101+
102+
Describe("onDeleteFunc", func() {
103+
It("should reconcile if the object is RuntimeInterface", func() {
104+
delRuntimeEvent := event.DeleteEvent{
105+
Object: &datav1alpha1.JindoRuntime{},
106+
}
107+
f := handler.onDeleteFunc(reconciler)
108+
Expect(f(delRuntimeEvent)).To(BeTrue())
109+
})
110+
111+
It("should not reconcile if the object is not RuntimeInterface", func() {
112+
delRuntimeEvent := event.DeleteEvent{
113+
Object: &corev1.Pod{},
114+
}
115+
f := handler.onDeleteFunc(reconciler)
116+
Expect(f(delRuntimeEvent)).To(BeFalse())
117+
})
118+
})
119+
})

0 commit comments

Comments
 (0)