Skip to content

Commit fcee91f

Browse files
authored
test(vineyard): migrate controller tests to Ginkgo/Gomega (#5699)
* test(vineyard): migrate controller tests to Ginkgo/Gomega Add Ginkgo v2/Gomega test suite for pkg/controllers/v1alpha1/vineyard/ covering getRuntime, GetOrCreateEngine, RemoveEngine, NewRuntimeReconciler, ControllerName, and Reconcile. All 11 specs pass with 92.3% package coverage. Also add VineyardRuntimeKind constant to api/v1alpha1/vineyardruntime_types.go, filling the same gap that exists for all other runtimes (JindoRuntimeKind, EFCRuntimeKind, ThinRuntimeKind, JuiceFSRuntimeKind). Signed-off-by: Harsh <harshmastic@gmail.com> * test(vineyard): address bot review feedback Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 40e9dcb commit fcee91f

4 files changed

Lines changed: 438 additions & 0 deletions

File tree

api/v1alpha1/vineyardruntime_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2222
)
2323

24+
const (
25+
VineyardRuntimeKind = "VineyardRuntime"
26+
)
27+
2428
// VineyardCompTemplateSpec is the common configurations for vineyard components including Master and Worker.
2529
type VineyardCompTemplateSpec struct {
2630
// The replicas of Vineyard component.
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 vineyard
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("VineyardRuntime 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("Reconcile", func() {
65+
It("should return empty result when runtime is not found", func() {
66+
s := runtime.NewScheme()
67+
Expect(datav1alpha1.AddToScheme(s)).NotTo(HaveOccurred())
68+
mockClient := fake.NewFakeClientWithScheme(s)
69+
recorder := record.NewFakeRecorder(16)
70+
71+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
72+
73+
req := ctrl.Request{
74+
NamespacedName: types.NamespacedName{
75+
Name: "nonexistent-runtime",
76+
Namespace: "default",
77+
},
78+
}
79+
80+
result, err := r.Reconcile(context.TODO(), req)
81+
Expect(err).NotTo(HaveOccurred())
82+
Expect(result).To(Equal(ctrl.Result{}))
83+
})
84+
85+
It("should requeue with error when runtime name violates the controller DNS-1035 validation rule", func() {
86+
s := runtime.NewScheme()
87+
Expect(datav1alpha1.AddToScheme(s)).NotTo(HaveOccurred())
88+
89+
// Runtime name starting with a digit fails the controller's DNS-1035 validation.
90+
invalidName := "20-vineyard"
91+
vineyardRuntime := &datav1alpha1.VineyardRuntime{
92+
ObjectMeta: metav1.ObjectMeta{
93+
Name: invalidName,
94+
Namespace: "default",
95+
},
96+
}
97+
dataset := &datav1alpha1.Dataset{
98+
ObjectMeta: metav1.ObjectMeta{
99+
Name: invalidName,
100+
Namespace: "default",
101+
},
102+
}
103+
104+
mockClient := fake.NewFakeClientWithScheme(s, vineyardRuntime, dataset)
105+
recorder := record.NewFakeRecorder(16)
106+
r := NewRuntimeReconciler(mockClient, fake.NullLogger(), s, recorder)
107+
108+
req := ctrl.Request{
109+
NamespacedName: types.NamespacedName{
110+
Name: invalidName,
111+
Namespace: "default",
112+
},
113+
}
114+
115+
_, err := r.Reconcile(context.TODO(), req)
116+
// Invalid runtime name causes an error path
117+
Expect(err).To(HaveOccurred())
118+
})
119+
})
120+
})

0 commit comments

Comments
 (0)