Skip to content

Commit 4fca3ab

Browse files
authored
test(dataset): migrate dataset controller tests to Ginkgo v2 (#5683)
* test(dataset): migrate dataset controller tests to Ginkgo v2 Signed-off-by: Harsh <harshmastic@gmail.com> * test(deploy): harden lazy precheck resolution tests Signed-off-by: Harsh <harshmastic@gmail.com> * test(dataset): tighten deletion assertion Signed-off-by: Harsh <harshmastic@gmail.com> * test: deduplicate controller test literals Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 311c7bd commit 4fca3ab

5 files changed

Lines changed: 670 additions & 14 deletions

File tree

pkg/controllers/deploy/runtime_controllers.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ var (
6363
resolveDefaultPrecheckFuncs = func() map[string]CheckFunc {
6464
return filterOutDisabledRuntimes(defaultPrecheckFuncs)
6565
}
66-
precheckFuncs map[string]CheckFunc
67-
precheckFuncsMu sync.Mutex
66+
cachedPrecheckFuncs map[string]CheckFunc
67+
precheckFuncs map[string]CheckFunc
68+
precheckFuncsMu sync.Mutex
6869
)
6970

7071
func setPrecheckFunc(checks map[string]CheckFunc) {
@@ -82,7 +83,11 @@ func getPrecheckFuncs() map[string]CheckFunc {
8283
return clonePrecheckFuncs(precheckFuncs)
8384
}
8485

85-
return clonePrecheckFuncs(resolveDefaultPrecheckFuncs())
86+
if cachedPrecheckFuncs == nil {
87+
cachedPrecheckFuncs = resolveDefaultPrecheckFuncs()
88+
}
89+
90+
return clonePrecheckFuncs(cachedPrecheckFuncs)
8691
}
8792

8893
func clonePrecheckFuncs(checks map[string]CheckFunc) map[string]CheckFunc {

pkg/controllers/deploy/runtime_controllers_test.go

Lines changed: 89 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,35 @@ import (
4242
"sigs.k8s.io/controller-runtime/pkg/client"
4343
)
4444

45-
const controllerNamespace = common.NamespaceFluidSystem
45+
const (
46+
controllerNamespace = common.NamespaceFluidSystem
47+
customControllerName = "custom-controller"
48+
)
4649

4750
var _ = Describe("runtime controller scaleout", func() {
4851
var originalPodNamespace string
4952
var hadOriginalPodNamespace bool
5053
var originalResolveDefaultPrecheckFuncs func() map[string]CheckFunc
54+
var originalCachedPrecheckFuncs map[string]CheckFunc
55+
var originalPrecheckFuncs map[string]CheckFunc
5156

5257
BeforeEach(func() {
5358
originalPodNamespace, hadOriginalPodNamespace = os.LookupEnv(common.MyPodNamespace)
59+
60+
precheckFuncsMu.Lock()
5461
originalResolveDefaultPrecheckFuncs = resolveDefaultPrecheckFuncs
62+
originalCachedPrecheckFuncs = clonePrecheckFuncs(cachedPrecheckFuncs)
63+
originalPrecheckFuncs = clonePrecheckFuncs(precheckFuncs)
64+
precheckFuncsMu.Unlock()
5565
})
5666

5767
AfterEach(func() {
58-
setPrecheckFunc(nil)
68+
precheckFuncsMu.Lock()
5969
resolveDefaultPrecheckFuncs = originalResolveDefaultPrecheckFuncs
70+
cachedPrecheckFuncs = clonePrecheckFuncs(originalCachedPrecheckFuncs)
71+
precheckFuncs = clonePrecheckFuncs(originalPrecheckFuncs)
72+
precheckFuncsMu.Unlock()
73+
6074
restoreEnv(common.MyPodNamespace, originalPodNamespace, hadOriginalPodNamespace)
6175
})
6276

@@ -164,10 +178,49 @@ var _ = Describe("runtime controller scaleout", func() {
164178
Expect(*stored.Spec.Replicas).To(Equal(int32(1)))
165179
})
166180

167-
It("keeps package-global precheck functions isolated between tests", func() {
181+
It("uses injected precheck funcs without resolving defaults", func() {
182+
fakeClient := newFakeClient(newDeployment(customControllerName, 0, nil))
183+
184+
resolverCalls := 0
185+
precheckCalls := 0
186+
187+
precheckFuncsMu.Lock()
188+
resolveDefaultPrecheckFuncs = func() map[string]CheckFunc {
189+
resolverCalls++
190+
return map[string]CheckFunc{}
191+
}
192+
precheckFuncsMu.Unlock()
193+
194+
setPrecheckFunc(map[string]CheckFunc{
195+
customControllerName: func(client.Client, types.NamespacedName) (bool, error) {
196+
precheckCalls++
197+
return true, nil
198+
},
199+
})
200+
201+
controllerName, scaled, err := ScaleoutRuntimeControllerOnDemand(fakeClient, types.NamespacedName{
202+
Namespace: corev1.NamespaceDefault,
203+
Name: "dataset",
204+
}, fake.NullLogger())
205+
206+
Expect(err).NotTo(HaveOccurred())
207+
Expect(controllerName).To(Equal(customControllerName))
208+
Expect(scaled).To(BeTrue())
209+
Expect(resolverCalls).To(Equal(0))
210+
Expect(precheckCalls).To(Equal(1))
211+
212+
stored := &appsv1.Deployment{}
213+
Expect(fakeClient.Get(context.TODO(), types.NamespacedName{
214+
Namespace: controllerNamespace,
215+
Name: customControllerName,
216+
}, stored)).To(Succeed())
217+
Expect(*stored.Spec.Replicas).To(Equal(int32(1)))
218+
})
219+
220+
It("returns a not found error when an injected controller has no deployment", func() {
168221
fakeClient := newFakeClient(controllerDeployments()...)
169222
setPrecheckFunc(map[string]CheckFunc{
170-
"custom-controller": func(client.Client, types.NamespacedName) (bool, error) {
223+
customControllerName: func(client.Client, types.NamespacedName) (bool, error) {
171224
return true, nil
172225
},
173226
})
@@ -178,7 +231,8 @@ var _ = Describe("runtime controller scaleout", func() {
178231
}, fake.NullLogger())
179232

180233
Expect(err).To(HaveOccurred())
181-
Expect(err).To(MatchError(ContainSubstring("custom-controller")))
234+
Expect(err).To(MatchError(ContainSubstring(customControllerName)))
235+
Expect(err).To(MatchError(ContainSubstring("not found")))
182236
Expect(controllerName).To(BeEmpty())
183237
Expect(scaled).To(BeFalse())
184238
})
@@ -203,7 +257,36 @@ var _ = Describe("runtime controller scaleout", func() {
203257
Expect(getPrecheckFuncs()).To(HaveKey("alluxioruntime-controller"))
204258
})
205259

206-
It("does not pin discovery-filtered defaults into package-global state", func() {
260+
It("resolves defaults lazily and caches the result", func() {
261+
const lazyControllerName = "lazy-controller"
262+
263+
calls := 0
264+
check := func(client.Client, types.NamespacedName) (bool, error) {
265+
return false, nil
266+
}
267+
268+
precheckFuncsMu.Lock()
269+
cachedPrecheckFuncs = nil
270+
precheckFuncs = nil
271+
resolveDefaultPrecheckFuncs = func() map[string]CheckFunc {
272+
calls++
273+
return map[string]CheckFunc{lazyControllerName: check}
274+
}
275+
precheckFuncsMu.Unlock()
276+
277+
checks := getPrecheckFuncs()
278+
Expect(calls).To(Equal(1))
279+
Expect(checks[lazyControllerName]).NotTo(BeNil())
280+
281+
checksAgain := getPrecheckFuncs()
282+
Expect(calls).To(Equal(1))
283+
Expect(checksAgain[lazyControllerName]).NotTo(BeNil())
284+
285+
delete(checks, lazyControllerName)
286+
Expect(getPrecheckFuncs()[lazyControllerName]).NotTo(BeNil())
287+
})
288+
289+
It("does not pin resolved defaults into package-global state", func() {
207290
resolveDefaultPrecheckFuncs = func() map[string]CheckFunc {
208291
return runtimePrecheckFuncs()
209292
}

0 commit comments

Comments
 (0)