generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(efc): migrate pkg/controllers/v1alpha1/efc to Ginkgo v2 with fake-client unit tests #5698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cheyang
merged 2 commits into
fluid-cloudnative:master
from
hxrshxz:test/efc-ginkgo-v2-tests
Mar 30, 2026
+282
−45
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
pkg/controllers/v1alpha1/efc/efcruntime_controller_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| /* | ||
| Copyright 2026 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package efc | ||
|
|
||
| import ( | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/tools/record" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
| "github.com/fluid-cloudnative/fluid/pkg/utils/fake" | ||
| ) | ||
|
|
||
| var _ = Describe("RuntimeReconciler (EFC)", func() { | ||
|
|
||
| Describe("ControllerName", func() { | ||
| It("should return the constant controller name", func() { | ||
| r := &RuntimeReconciler{} | ||
| Expect(r.ControllerName()).To(Equal("EFCRuntimeController")) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("ManagedResource", func() { | ||
| It("should return an EFCRuntime with correct TypeMeta", func() { | ||
| r := &RuntimeReconciler{} | ||
| obj := r.ManagedResource() | ||
| efcRuntime, ok := obj.(*datav1alpha1.EFCRuntime) | ||
| Expect(ok).To(BeTrue()) | ||
| Expect(efcRuntime.Kind).To(Equal(datav1alpha1.EFCRuntimeKind)) | ||
| Expect(efcRuntime.APIVersion).To(ContainSubstring(datav1alpha1.GroupVersion.Group)) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("NewRuntimeReconciler", func() { | ||
| It("should initialize reconciler with all required fields set", func() { | ||
| s := runtime.NewScheme() | ||
| fakeClient := fake.NewFakeClientWithScheme(s) | ||
| log := ctrl.Log.WithName("test") | ||
| recorder := record.NewFakeRecorder(10) | ||
|
|
||
| r := NewRuntimeReconciler(fakeClient, log, s, recorder) | ||
| Expect(r).NotTo(BeNil()) | ||
| Expect(r.Scheme).To(Equal(s)) | ||
| Expect(r.mutex).NotTo(BeNil()) | ||
| Expect(r.engines).NotTo(BeNil()) | ||
| Expect(r.RuntimeReconciler).NotTo(BeNil()) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| /* | ||
| Copyright 2026 The Fluid Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package efc | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
|
|
||
| "github.com/agiledragon/gomonkey/v2" | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/tools/record" | ||
| ctrl "sigs.k8s.io/controller-runtime" | ||
|
|
||
| datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
| "github.com/fluid-cloudnative/fluid/pkg/controllers" | ||
| "github.com/fluid-cloudnative/fluid/pkg/dataoperation" | ||
| "github.com/fluid-cloudnative/fluid/pkg/ddc" | ||
| "github.com/fluid-cloudnative/fluid/pkg/ddc/base" | ||
| cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime" | ||
| "github.com/fluid-cloudnative/fluid/pkg/utils/fake" | ||
| ) | ||
|
|
||
| // mockEngine is a minimal no-op implementation of base.Engine used in tests only. | ||
| type mockEngine struct{} | ||
|
|
||
| func (m *mockEngine) ID() string { return "mock" } | ||
| func (m *mockEngine) Shutdown() error { return nil } | ||
| func (m *mockEngine) Setup(_ cruntime.ReconcileRequestContext) (bool, error) { return true, nil } | ||
| func (m *mockEngine) CreateVolume() error { return nil } | ||
| func (m *mockEngine) DeleteVolume() error { return nil } | ||
| func (m *mockEngine) Sync(_ cruntime.ReconcileRequestContext) error { return nil } | ||
| func (m *mockEngine) Validate(_ cruntime.ReconcileRequestContext) error { return nil } | ||
| func (m *mockEngine) Operate(_ cruntime.ReconcileRequestContext, _ *datav1alpha1.OperationStatus, _ dataoperation.OperationInterface) (ctrl.Result, error) { | ||
| return ctrl.Result{}, nil | ||
| } | ||
|
|
||
| // newTestEFCReconciler builds a RuntimeReconciler seeded with the | ||
| // given scheme and runtime objects. Pass nil scheme to get a default one. | ||
| func newTestEFCReconciler(s *runtime.Scheme, objs ...runtime.Object) *RuntimeReconciler { | ||
| if s == nil { | ||
| s = runtime.NewScheme() | ||
| _ = datav1alpha1.AddToScheme(s) | ||
| } | ||
| fakeClient := fake.NewFakeClientWithScheme(s, objs...) | ||
| log := ctrl.Log.WithName("efc-test") | ||
| recorder := record.NewFakeRecorder(10) | ||
| r := &RuntimeReconciler{ | ||
| Scheme: s, | ||
| mutex: &sync.Mutex{}, | ||
| engines: map[string]base.Engine{}, | ||
| } | ||
| r.RuntimeReconciler = controllers.NewRuntimeReconciler(r, fakeClient, log, recorder) | ||
| return r | ||
| } | ||
|
|
||
| var _ = Describe("RuntimeReconciler (EFC) Implement", func() { | ||
|
|
||
| Describe("getRuntime", func() { | ||
| var r *RuntimeReconciler | ||
|
|
||
| BeforeEach(func() { | ||
| testRuntime := &datav1alpha1.EFCRuntime{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}, | ||
| } | ||
| s := runtime.NewScheme() | ||
| _ = datav1alpha1.AddToScheme(s) | ||
| r = newTestEFCReconciler(s, testRuntime) | ||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
|
|
||
| It("should return the runtime when it exists in the cluster", func() { | ||
| ctx := cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{Name: "test", Namespace: "default"}, | ||
| } | ||
| result, err := r.getRuntime(ctx) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result).NotTo(BeNil()) | ||
| Expect(result.Name).To(Equal("test")) | ||
| Expect(result.Namespace).To(Equal("default")) | ||
| }) | ||
|
|
||
| It("should return an error when the runtime does not exist", func() { | ||
| ctx := cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{Name: "nonexistent", Namespace: "default"}, | ||
| } | ||
| result, err := r.getRuntime(ctx) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(result).To(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("GetOrCreateEngine", func() { | ||
| var r *RuntimeReconciler | ||
|
|
||
| BeforeEach(func() { | ||
| r = newTestEFCReconciler(nil) | ||
| }) | ||
|
|
||
| It("should propagate engine creation errors", func() { | ||
| patches := gomonkey.ApplyFunc(ddc.CreateEngine, | ||
| func(_ string, _ cruntime.ReconcileRequestContext) (base.Engine, error) { | ||
| return nil, fmt.Errorf("engine creation failed") | ||
| }) | ||
| defer patches.Reset() | ||
|
|
||
| ctx := cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{Name: "fail", Namespace: "default"}, | ||
| } | ||
| engine, err := r.GetOrCreateEngine(ctx) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(err.Error()).To(ContainSubstring("engine creation failed")) | ||
| Expect(engine).To(BeNil()) | ||
| }) | ||
|
|
||
| It("should create engine on first call and return cached engine on second call", func() { | ||
| mock := &mockEngine{} | ||
| callCount := 0 | ||
| patches := gomonkey.ApplyFunc(ddc.CreateEngine, | ||
| func(_ string, _ cruntime.ReconcileRequestContext) (base.Engine, error) { | ||
| callCount++ | ||
| return mock, nil | ||
| }) | ||
| defer patches.Reset() | ||
|
|
||
| ctx := cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{Name: "cached", Namespace: "default"}, | ||
| } | ||
|
|
||
| // First call: engine is created and stored. | ||
| engine1, err := r.GetOrCreateEngine(ctx) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(engine1).To(Equal(base.Engine(mock))) | ||
| Expect(callCount).To(Equal(1)) | ||
|
|
||
| // Second call: engine should be retrieved from the cache without re-creation. | ||
| engine2, err := r.GetOrCreateEngine(ctx) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(engine2).To(Equal(base.Engine(mock))) | ||
| Expect(callCount).To(Equal(1), "CreateEngine must not be called a second time") | ||
| }) | ||
| }) | ||
|
|
||
| Describe("RemoveEngine", func() { | ||
| var r *RuntimeReconciler | ||
|
|
||
| BeforeEach(func() { | ||
| r = newTestEFCReconciler(nil) | ||
| }) | ||
|
|
||
| It("should remove a cached engine by namespaced name", func() { | ||
| id := ddc.GenerateEngineID(types.NamespacedName{Name: "test", Namespace: "default"}) | ||
| r.engines[id] = &mockEngine{} | ||
|
|
||
| ctx := cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{Name: "test", Namespace: "default"}, | ||
| } | ||
| r.RemoveEngine(ctx) | ||
|
|
||
| _, found := r.engines[id] | ||
| Expect(found).To(BeFalse()) | ||
| }) | ||
|
|
||
| It("should not panic when removing a non-existent engine", func() { | ||
| ctx := cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{Name: "ghost", Namespace: "default"}, | ||
| } | ||
| Expect(func() { r.RemoveEngine(ctx) }).NotTo(Panic()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("Reconcile", func() { | ||
| It("should return no error when the runtime is not found", func() { | ||
| // The fake client has no EFCRuntime objects, so getRuntime will | ||
| // return a NotFound error, which Reconcile should swallow gracefully. | ||
| s := runtime.NewScheme() | ||
| _ = datav1alpha1.AddToScheme(s) | ||
| r := newTestEFCReconciler(s) | ||
hxrshxz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| req := ctrl.Request{ | ||
| NamespacedName: types.NamespacedName{Name: "missing", Namespace: "default"}, | ||
| } | ||
| result, err := r.Reconcile(context.Background(), req) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result).To(Equal(ctrl.Result{})) | ||
| }) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.