|
| 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 thinruntime |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "sync" |
| 23 | + |
| 24 | + "github.com/agiledragon/gomonkey/v2" |
| 25 | + . "github.com/onsi/ginkgo/v2" |
| 26 | + . "github.com/onsi/gomega" |
| 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 | + datav1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" |
| 34 | + "github.com/fluid-cloudnative/fluid/pkg/controllers" |
| 35 | + "github.com/fluid-cloudnative/fluid/pkg/dataoperation" |
| 36 | + "github.com/fluid-cloudnative/fluid/pkg/ddc" |
| 37 | + "github.com/fluid-cloudnative/fluid/pkg/ddc/base" |
| 38 | + cruntime "github.com/fluid-cloudnative/fluid/pkg/runtime" |
| 39 | + "github.com/fluid-cloudnative/fluid/pkg/utils/fake" |
| 40 | +) |
| 41 | + |
| 42 | +// mockEngine is a minimal no-op implementation of base.Engine used in tests only. |
| 43 | +type mockEngine struct{} |
| 44 | + |
| 45 | +func (m *mockEngine) ID() string { return "mock" } |
| 46 | +func (m *mockEngine) Shutdown() error { return nil } |
| 47 | +func (m *mockEngine) Setup(_ cruntime.ReconcileRequestContext) (bool, error) { return true, nil } |
| 48 | +func (m *mockEngine) CreateVolume() error { return nil } |
| 49 | +func (m *mockEngine) DeleteVolume() error { return nil } |
| 50 | +func (m *mockEngine) Sync(_ cruntime.ReconcileRequestContext) error { return nil } |
| 51 | +func (m *mockEngine) Validate(_ cruntime.ReconcileRequestContext) error { return nil } |
| 52 | +func (m *mockEngine) Operate(_ cruntime.ReconcileRequestContext, _ *datav1alpha1.OperationStatus, _ dataoperation.OperationInterface) (ctrl.Result, error) { |
| 53 | + return ctrl.Result{}, nil |
| 54 | +} |
| 55 | + |
| 56 | +// newTestThinRuntimeReconciler builds a ThinRuntimeReconciler seeded with the |
| 57 | +// given scheme and runtime objects. Pass nil scheme to get a default one. |
| 58 | +func newTestThinRuntimeReconciler(s *runtime.Scheme, objs ...runtime.Object) *ThinRuntimeReconciler { |
| 59 | + if s == nil { |
| 60 | + s = runtime.NewScheme() |
| 61 | + _ = datav1alpha1.AddToScheme(s) |
| 62 | + } |
| 63 | + fakeClient := fake.NewFakeClientWithScheme(s, objs...) |
| 64 | + log := ctrl.Log.WithName("thinruntime-test") |
| 65 | + recorder := record.NewFakeRecorder(10) |
| 66 | + r := &ThinRuntimeReconciler{ |
| 67 | + Scheme: s, |
| 68 | + mutex: &sync.Mutex{}, |
| 69 | + engines: map[string]base.Engine{}, |
| 70 | + } |
| 71 | + r.RuntimeReconciler = controllers.NewRuntimeReconciler(r, fakeClient, log, recorder) |
| 72 | + return r |
| 73 | +} |
| 74 | + |
| 75 | +var _ = Describe("ThinRuntimeReconciler Implement", func() { |
| 76 | + |
| 77 | + Describe("getRuntime", func() { |
| 78 | + var r *ThinRuntimeReconciler |
| 79 | + |
| 80 | + BeforeEach(func() { |
| 81 | + testRuntime := &datav1alpha1.ThinRuntime{ |
| 82 | + ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "default"}, |
| 83 | + } |
| 84 | + s := runtime.NewScheme() |
| 85 | + _ = datav1alpha1.AddToScheme(s) |
| 86 | + r = newTestThinRuntimeReconciler(s, testRuntime) |
| 87 | + }) |
| 88 | + |
| 89 | + It("should return the runtime when it exists in the cluster", func() { |
| 90 | + ctx := cruntime.ReconcileRequestContext{ |
| 91 | + Context: context.TODO(), |
| 92 | + NamespacedName: types.NamespacedName{Name: "test", Namespace: "default"}, |
| 93 | + } |
| 94 | + result, err := r.getRuntime(ctx) |
| 95 | + Expect(err).NotTo(HaveOccurred()) |
| 96 | + Expect(result).NotTo(BeNil()) |
| 97 | + Expect(result.Name).To(Equal("test")) |
| 98 | + Expect(result.Namespace).To(Equal("default")) |
| 99 | + }) |
| 100 | + |
| 101 | + It("should return an error when the runtime does not exist", func() { |
| 102 | + ctx := cruntime.ReconcileRequestContext{ |
| 103 | + Context: context.TODO(), |
| 104 | + NamespacedName: types.NamespacedName{Name: "nonexistent", Namespace: "default"}, |
| 105 | + } |
| 106 | + result, err := r.getRuntime(ctx) |
| 107 | + Expect(err).To(HaveOccurred()) |
| 108 | + Expect(result).To(BeNil()) |
| 109 | + }) |
| 110 | + }) |
| 111 | + |
| 112 | + Describe("GetOrCreateEngine", func() { |
| 113 | + var r *ThinRuntimeReconciler |
| 114 | + |
| 115 | + BeforeEach(func() { |
| 116 | + r = newTestThinRuntimeReconciler(nil) |
| 117 | + }) |
| 118 | + |
| 119 | + It("should propagate engine creation errors", func() { |
| 120 | + patches := gomonkey.ApplyFunc(ddc.CreateEngine, |
| 121 | + func(_ string, _ cruntime.ReconcileRequestContext) (base.Engine, error) { |
| 122 | + return nil, fmt.Errorf("engine creation failed") |
| 123 | + }) |
| 124 | + defer patches.Reset() |
| 125 | + |
| 126 | + ctx := cruntime.ReconcileRequestContext{ |
| 127 | + Context: context.TODO(), |
| 128 | + NamespacedName: types.NamespacedName{Name: "fail", Namespace: "default"}, |
| 129 | + } |
| 130 | + engine, err := r.GetOrCreateEngine(ctx) |
| 131 | + Expect(err).To(HaveOccurred()) |
| 132 | + Expect(err.Error()).To(ContainSubstring("engine creation failed")) |
| 133 | + Expect(engine).To(BeNil()) |
| 134 | + }) |
| 135 | + |
| 136 | + It("should create engine on first call and return cached engine on second call", func() { |
| 137 | + mock := &mockEngine{} |
| 138 | + callCount := 0 |
| 139 | + patches := gomonkey.ApplyFunc(ddc.CreateEngine, |
| 140 | + func(_ string, _ cruntime.ReconcileRequestContext) (base.Engine, error) { |
| 141 | + callCount++ |
| 142 | + return mock, nil |
| 143 | + }) |
| 144 | + defer patches.Reset() |
| 145 | + |
| 146 | + ctx := cruntime.ReconcileRequestContext{ |
| 147 | + Context: context.TODO(), |
| 148 | + NamespacedName: types.NamespacedName{Name: "cached", Namespace: "default"}, |
| 149 | + } |
| 150 | + |
| 151 | + // First call: engine is created and stored. |
| 152 | + engine1, err := r.GetOrCreateEngine(ctx) |
| 153 | + Expect(err).NotTo(HaveOccurred()) |
| 154 | + Expect(engine1).To(Equal(base.Engine(mock))) |
| 155 | + Expect(callCount).To(Equal(1)) |
| 156 | + |
| 157 | + // Second call: engine should be retrieved from the cache without re-creation. |
| 158 | + engine2, err := r.GetOrCreateEngine(ctx) |
| 159 | + Expect(err).NotTo(HaveOccurred()) |
| 160 | + Expect(engine2).To(Equal(base.Engine(mock))) |
| 161 | + Expect(callCount).To(Equal(1), "CreateEngine must not be called a second time") |
| 162 | + }) |
| 163 | + }) |
| 164 | + |
| 165 | + Describe("RemoveEngine", func() { |
| 166 | + var r *ThinRuntimeReconciler |
| 167 | + |
| 168 | + BeforeEach(func() { |
| 169 | + r = newTestThinRuntimeReconciler(nil) |
| 170 | + }) |
| 171 | + |
| 172 | + It("should remove a cached engine by namespaced name", func() { |
| 173 | + id := ddc.GenerateEngineID(types.NamespacedName{Name: "test", Namespace: "default"}) |
| 174 | + r.engines[id] = &mockEngine{} |
| 175 | + |
| 176 | + ctx := cruntime.ReconcileRequestContext{ |
| 177 | + Context: context.TODO(), |
| 178 | + NamespacedName: types.NamespacedName{Name: "test", Namespace: "default"}, |
| 179 | + } |
| 180 | + r.RemoveEngine(ctx) |
| 181 | + |
| 182 | + _, found := r.engines[id] |
| 183 | + Expect(found).To(BeFalse()) |
| 184 | + }) |
| 185 | + |
| 186 | + It("should not panic when removing a non-existent engine", func() { |
| 187 | + ctx := cruntime.ReconcileRequestContext{ |
| 188 | + Context: context.TODO(), |
| 189 | + NamespacedName: types.NamespacedName{Name: "ghost", Namespace: "default"}, |
| 190 | + } |
| 191 | + Expect(func() { r.RemoveEngine(ctx) }).NotTo(Panic()) |
| 192 | + }) |
| 193 | + }) |
| 194 | + |
| 195 | + Describe("Reconcile", func() { |
| 196 | + It("should return no error when the runtime is not found", func() { |
| 197 | + // The fake client has no ThinRuntime objects, so getRuntime will |
| 198 | + // return a NotFound error, which Reconcile should swallow gracefully. |
| 199 | + s := runtime.NewScheme() |
| 200 | + _ = datav1alpha1.AddToScheme(s) |
| 201 | + r := newTestThinRuntimeReconciler(s) |
| 202 | + |
| 203 | + req := ctrl.Request{ |
| 204 | + NamespacedName: types.NamespacedName{Name: "missing", Namespace: "default"}, |
| 205 | + } |
| 206 | + result, err := r.Reconcile(context.TODO(), req) |
| 207 | + Expect(err).NotTo(HaveOccurred()) |
| 208 | + Expect(result).To(Equal(ctrl.Result{})) |
| 209 | + }) |
| 210 | + }) |
| 211 | +}) |
0 commit comments