-
Notifications
You must be signed in to change notification settings - Fork 1.2k
test(goosefs): migrate suite to Ginkgo v2 and add controller tests #5687
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 3 commits into
fluid-cloudnative:master
from
hxrshxz:test/goosefs-controller-ginkgo-migration
Mar 25, 2026
Merged
Changes from all commits
Commits
Show all changes
3 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
277 changes: 277 additions & 0 deletions
277
pkg/controllers/v1alpha1/goosefs/goosefs_runtime_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,277 @@ | ||
| /* | ||
| 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 goosefs | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
|
|
||
| datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
| cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime" | ||
| "github.com/fluid-cloudnative/fluid/pkg/utils/fake" | ||
| 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" | ||
| "sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
| ) | ||
|
|
||
| // buildTestReconcileCtx builds a minimal ReconcileRequestContext for unit tests. | ||
| func buildTestReconcileCtx(req ctrl.Request) cruntime.ReconcileRequestContext { | ||
| return cruntime.ReconcileRequestContext{ | ||
| Context: context.Background(), | ||
| NamespacedName: req.NamespacedName, | ||
| Log: zap.New(zap.UseDevMode(true)), | ||
| } | ||
| } | ||
|
|
||
| const ( | ||
| testRuntimeName = "test-goosefs-runtime" | ||
| testRuntimeNamespace = "default" | ||
| envtestRuntimeName = "envtest-runtime" | ||
| ) | ||
|
|
||
| var _ = Describe("RuntimeReconciler", func() { | ||
| var ( | ||
| testScheme *runtime.Scheme | ||
| ) | ||
|
|
||
| BeforeEach(func() { | ||
| testScheme = runtime.NewScheme() | ||
| Expect(datav1alpha1.AddToScheme(testScheme)).To(Succeed()) | ||
| }) | ||
|
|
||
| Describe("NewRuntimeReconciler", func() { | ||
| It("should create a RuntimeReconciler with non-nil fields", func() { | ||
| fakeClient := fake.NewFakeClientWithScheme(testScheme) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
|
|
||
| Expect(reconciler).NotTo(BeNil()) | ||
| Expect(reconciler.Scheme).NotTo(BeNil()) | ||
| Expect(reconciler.RuntimeReconciler).NotTo(BeNil()) | ||
| Expect(reconciler.engines).NotTo(BeNil()) | ||
| Expect(reconciler.mutex).NotTo(BeNil()) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("ControllerName", func() { | ||
| It("should return the expected controller name", func() { | ||
| fakeClient := fake.NewFakeClientWithScheme(testScheme) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
| Expect(reconciler.ControllerName()).To(Equal(controllerName)) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("Reconcile", func() { | ||
| Context("when the GooseFSRuntime does not exist", func() { | ||
| It("should return empty result and no error (not-found is ignored)", func() { | ||
| fakeClient := fake.NewFakeClientWithScheme(testScheme) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
|
|
||
| req := ctrl.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: "nonexistent-runtime", | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| } | ||
| result, err := reconciler.Reconcile(context.Background(), req) | ||
|
|
||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(result).To(Equal(ctrl.Result{})) | ||
| }) | ||
| }) | ||
|
|
||
| Context("when a GooseFSRuntime exists", func() { | ||
| It("should attempt to reconcile the runtime without crashing", func() { | ||
| runtime := &datav1alpha1.GooseFSRuntime{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| Spec: datav1alpha1.GooseFSRuntimeSpec{}, | ||
| } | ||
|
|
||
| fakeClient := fake.NewFakeClientWithScheme(testScheme, runtime) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
|
|
||
| req := ctrl.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| } | ||
| // Reconcile will call ReconcileInternal which requires a full engine; | ||
| // the result may be an error from engine creation but must not panic. | ||
| _, _ = reconciler.Reconcile(context.Background(), req) | ||
| // Primary assertion: reconciler does not panic and returns | ||
|
hxrshxz marked this conversation as resolved.
hxrshxz marked this conversation as resolved.
hxrshxz marked this conversation as resolved.
|
||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("GetOrCreateEngine", func() { | ||
| It("should return an error when EngineImpl is unset (no engine created)", func() { | ||
| gooseFSRuntime := &datav1alpha1.GooseFSRuntime{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| } | ||
|
|
||
| fakeClient := fake.NewFakeClientWithScheme(testScheme, gooseFSRuntime) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
| Expect(reconciler.engines).To(BeEmpty()) | ||
|
|
||
| ctx := cruntime.ReconcileRequestContext{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| Client: fakeClient, | ||
| Log: log, | ||
| Recorder: fakeRecorder, | ||
| Runtime: gooseFSRuntime, | ||
| // EngineImpl intentionally left empty — simulates unknown impl | ||
| } | ||
|
|
||
| engine, err := reconciler.GetOrCreateEngine(ctx) | ||
| // An empty EngineImpl causes CreateEngine to return an error; no engine stored. | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(engine).To(BeNil()) | ||
| Expect(reconciler.engines).To(BeEmpty()) | ||
| }) | ||
|
hxrshxz marked this conversation as resolved.
|
||
| }) | ||
|
hxrshxz marked this conversation as resolved.
|
||
|
|
||
| Describe("RemoveEngine", func() { | ||
| It("should not panic when removing a non-existent engine", func() { | ||
| fakeClient := fake.NewFakeClientWithScheme(testScheme) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
|
|
||
| ctx := cruntime.ReconcileRequestContext{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| Client: fakeClient, | ||
| Log: log, | ||
| Recorder: fakeRecorder, | ||
| } | ||
|
|
||
| // RemoveEngine on an empty map must not panic. | ||
| Expect(func() { | ||
| reconciler.RemoveEngine(ctx) | ||
| }).NotTo(Panic()) | ||
|
hxrshxz marked this conversation as resolved.
|
||
| Expect(reconciler.engines).To(BeEmpty()) | ||
| }) | ||
| }) | ||
|
hxrshxz marked this conversation as resolved.
|
||
|
|
||
| Describe("getRuntime", func() { | ||
| It("should return error when runtime does not exist", func() { | ||
| fakeClient := fake.NewFakeClientWithScheme(testScheme) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
|
|
||
| req := ctrl.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: "missing", | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| } | ||
| ctx := buildTestReconcileCtx(req) | ||
| rt, err := reconciler.getRuntime(ctx) | ||
| Expect(err).To(HaveOccurred()) | ||
| Expect(rt).To(BeNil()) | ||
| }) | ||
|
|
||
| It("should return the runtime when it exists", func() { | ||
| gooseFSRuntime := &datav1alpha1.GooseFSRuntime{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| Spec: datav1alpha1.GooseFSRuntimeSpec{}, | ||
| } | ||
|
|
||
| fakeClient := fake.NewFakeClientWithScheme(testScheme, gooseFSRuntime) | ||
| fakeRecorder := record.NewFakeRecorder(10) | ||
| log := zap.New(zap.UseDevMode(true)) | ||
|
|
||
| reconciler := NewRuntimeReconciler(fakeClient, log, testScheme, fakeRecorder) | ||
|
|
||
| req := ctrl.Request{ | ||
| NamespacedName: types.NamespacedName{ | ||
| Name: testRuntimeName, | ||
| Namespace: testRuntimeNamespace, | ||
| }, | ||
| } | ||
| ctx := buildTestReconcileCtx(req) | ||
| rt, err := reconciler.getRuntime(ctx) | ||
| Expect(err).NotTo(HaveOccurred()) | ||
| Expect(rt).NotTo(BeNil()) | ||
| Expect(rt.Name).To(Equal(testRuntimeName)) | ||
| Expect(rt.Namespace).To(Equal(testRuntimeNamespace)) | ||
| }) | ||
| }) | ||
|
|
||
| Describe("GooseFSRuntime CRD via envtest", func() { | ||
| It("should create and retrieve a GooseFSRuntime via the k8sClient", func() { | ||
| gooseFSRuntime := &datav1alpha1.GooseFSRuntime{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: envtestRuntimeName, | ||
| Namespace: "default", | ||
| }, | ||
| Spec: datav1alpha1.GooseFSRuntimeSpec{}, | ||
| } | ||
|
|
||
| By("creating the GooseFSRuntime") | ||
| Expect(k8sClient.Create(context.Background(), gooseFSRuntime)).To(Succeed()) | ||
|
|
||
| By("retrieving the GooseFSRuntime") | ||
| var fetched datav1alpha1.GooseFSRuntime | ||
| Expect(k8sClient.Get(context.Background(), types.NamespacedName{ | ||
| Name: envtestRuntimeName, | ||
| Namespace: "default", | ||
| }, &fetched)).To(Succeed()) | ||
| Expect(fetched.Name).To(Equal(envtestRuntimeName)) | ||
|
|
||
| By("deleting the GooseFSRuntime") | ||
| Expect(k8sClient.Delete(context.Background(), gooseFSRuntime)).To(Succeed()) | ||
| }) | ||
| }) | ||
| }) | ||
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.