-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathimplement_test.go
More file actions
211 lines (180 loc) · 7.15 KB
/
implement_test.go
File metadata and controls
211 lines (180 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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(_ context.Context) error { return nil }
func (m *mockEngine) DeleteVolume(_ context.Context) 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)
})
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)
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{}))
})
})
})