Skip to content

Commit 242a0bd

Browse files
authored
test(jindo): migrate controller tests to Ginkgo/Gomega (#5688)
* test(jindo): migrate controller tests to Ginkgo/Gomega Migrate pkg/controllers/v1alpha1/jindo/ unit tests to Ginkgo v2/Gomega. Fix broken suite bootstrap (Ginkgo v1 Unknown Decorator error) and add full coverage for implement.go and jindoruntime_controller.go (86.0%). 13/13 specs pass. Signed-off-by: Harsh <harshmastic@gmail.com> * test(jindo): assert AddToScheme errors and use defer for mutex unlock - Replace silent _ = datav1alpha1.AddToScheme(s) with Expect(datav1alpha1.AddToScheme(s)).NotTo(HaveOccurred()) in all It blocks across controller_test.go and implement_test.go so scheme-registration failures surface immediately rather than producing misleading test behaviour. - Change r.mutex.Lock()/Expect/r.mutex.Unlock() sequence in RemoveEngine test to use defer r.mutex.Unlock() so the lock is always released even when an Expect assertion fails. Signed-off-by: Harsh <harshmastic@gmail.com> * test(jindo): tighten APIVersion assertion to exact Equal match Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent fcee91f commit 242a0bd

3 files changed

Lines changed: 438 additions & 50 deletions

File tree

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
/*
2+
Copyright 2026 The Fluid Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package jindo
18+
19+
import (
20+
"context"
21+
22+
. "github.com/onsi/ginkgo/v2"
23+
. "github.com/onsi/gomega"
24+
25+
datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
26+
"github.com/fluid-cloudnative/fluid/pkg/utils/fake"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/apimachinery/pkg/types"
30+
"k8s.io/client-go/tools/record"
31+
ctrl "sigs.k8s.io/controller-runtime"
32+
)
33+
34+
var _ = Describe("JindoRuntime Controller", func() {
35+
Describe("NewRuntimeReconciler", func() {
36+
It("should create a RuntimeReconciler with non-nil fields", func() {
37+
s := runtime.NewScheme()
38+
Expect(datav1alpha1.AddToScheme(s)).NotTo(HaveOccurred())
39+
mockClient := fake.NewFakeClientWithScheme(s)
40+
recorder := record.NewFakeRecorder(16)
41+
42+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
43+
44+
Expect(r).NotTo(BeNil())
45+
Expect(r.Scheme).NotTo(BeNil())
46+
Expect(r.engines).NotTo(BeNil())
47+
Expect(r.mutex).NotTo(BeNil())
48+
Expect(r.RuntimeReconciler).NotTo(BeNil())
49+
})
50+
})
51+
52+
Describe("ControllerName", func() {
53+
It("should return the correct controller name", func() {
54+
s := runtime.NewScheme()
55+
mockClient := fake.NewFakeClientWithScheme(s)
56+
recorder := record.NewFakeRecorder(16)
57+
58+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
59+
60+
Expect(r.ControllerName()).To(Equal(controllerName))
61+
})
62+
})
63+
64+
Describe("ManagedResource", func() {
65+
It("should return a JindoRuntime object with correct TypeMeta", func() {
66+
s := runtime.NewScheme()
67+
mockClient := fake.NewFakeClientWithScheme(s)
68+
recorder := record.NewFakeRecorder(16)
69+
70+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
71+
obj := r.ManagedResource()
72+
73+
Expect(obj).NotTo(BeNil())
74+
jr, ok := obj.(*datav1alpha1.JindoRuntime)
75+
Expect(ok).To(BeTrue())
76+
Expect(jr.Kind).To(Equal(datav1alpha1.JindoRuntimeKind))
77+
Expect(jr.APIVersion).To(Equal(datav1alpha1.GroupVersion.Group + "/" + datav1alpha1.GroupVersion.Version))
78+
})
79+
80+
It("should populate APIVersion and Kind correctly", func() {
81+
s := runtime.NewScheme()
82+
mockClient := fake.NewFakeClientWithScheme(s)
83+
recorder := record.NewFakeRecorder(16)
84+
85+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
86+
obj := r.ManagedResource()
87+
88+
jr, ok := obj.(*datav1alpha1.JindoRuntime)
89+
Expect(ok).To(BeTrue())
90+
Expect(jr.TypeMeta).To(Equal(metav1.TypeMeta{
91+
Kind: datav1alpha1.JindoRuntimeKind,
92+
APIVersion: datav1alpha1.GroupVersion.Group + "/" + datav1alpha1.GroupVersion.Version,
93+
}))
94+
})
95+
})
96+
97+
Describe("Reconcile", func() {
98+
It("should return empty result when runtime is not found", func() {
99+
s := runtime.NewScheme()
100+
Expect(datav1alpha1.AddToScheme(s)).NotTo(HaveOccurred())
101+
mockClient := fake.NewFakeClientWithScheme(s)
102+
recorder := record.NewFakeRecorder(16)
103+
104+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
105+
106+
req := ctrl.Request{
107+
NamespacedName: types.NamespacedName{
108+
Name: "nonexistent-runtime",
109+
Namespace: "default",
110+
},
111+
}
112+
113+
result, err := r.Reconcile(context.TODO(), req)
114+
Expect(err).NotTo(HaveOccurred())
115+
Expect(result).To(Equal(ctrl.Result{}))
116+
})
117+
118+
It("should requeue with error when runtime name is invalid DNS label", func() {
119+
s := runtime.NewScheme()
120+
Expect(datav1alpha1.AddToScheme(s)).NotTo(HaveOccurred())
121+
122+
// Runtime with invalid DNS name (starts with number) triggers validation
123+
invalidName := "20-hbase"
124+
jindoRuntime := &datav1alpha1.JindoRuntime{
125+
ObjectMeta: metav1.ObjectMeta{
126+
Name: invalidName,
127+
Namespace: "default",
128+
},
129+
}
130+
dataset := &datav1alpha1.Dataset{
131+
ObjectMeta: metav1.ObjectMeta{
132+
Name: invalidName,
133+
Namespace: "default",
134+
},
135+
}
136+
137+
mockClient := fake.NewFakeClientWithScheme(s, jindoRuntime, dataset)
138+
recorder := record.NewFakeRecorder(16)
139+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
140+
141+
req := ctrl.Request{
142+
NamespacedName: types.NamespacedName{
143+
Name: invalidName,
144+
Namespace: "default",
145+
},
146+
}
147+
148+
_, err := r.Reconcile(context.TODO(), req)
149+
// Invalid runtime name causes an error path
150+
Expect(err).To(HaveOccurred())
151+
})
152+
})
153+
})

0 commit comments

Comments
 (0)