Skip to content

Commit 374202a

Browse files
committed
test(fluidapp): deduplicate remaining sonar literals
Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 4176ca7 commit 374202a

2 files changed

Lines changed: 107 additions & 77 deletions

File tree

pkg/controllers/v1alpha1/fluidapp/dataflowaffinity/dataflowaffinity_controller_test.go

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ func newTestScheme() *runtime.Scheme {
4343

4444
var _ = Describe("DataOpJobReconciler", func() {
4545
const (
46-
controllerUIDKey = "controller-uid"
47-
testNodeName = "node01"
48-
testPodName = "test-pod"
46+
controllerUIDKey = "controller-uid"
47+
testNodeName = "node01"
48+
testPodName = "test-pod"
49+
testJobName = "test-job"
50+
customLabelKey = "custom-label"
51+
testControllerUID = "test-uid"
4952
)
5053

5154
var testScheme *runtime.Scheme
@@ -115,8 +118,6 @@ var _ = Describe("DataOpJobReconciler", func() {
115118

116119
Context("when job is a valid fluid job without affinity annotation", func() {
117120
It("injects the dataflow affinity annotation and returns no requeue", func() {
118-
const testJobName = "test-job"
119-
120121
job := &batchv1.Job{
121122
ObjectMeta: metav1.ObjectMeta{
122123
Name: testJobName,
@@ -211,13 +212,13 @@ var _ = Describe("DataOpJobReconciler", func() {
211212
common.K8sNodeNameLabelKey: testNodeName,
212213
common.K8sRegionLabelKey: "region01",
213214
common.K8sZoneLabelKey: "zone01",
214-
"custom-label": "custom-value",
215+
customLabelKey: "custom-value",
215216
}
216217
exposedLabelNames := []string{
217218
common.K8sNodeNameLabelKey,
218219
common.K8sRegionLabelKey,
219220
common.K8sZoneLabelKey,
220-
"custom-label",
221+
customLabelKey,
221222
}
222223

223224
fillCustomizedNodeAffinity(annotationsToInject, nodeLabels, exposedLabelNames)
@@ -226,7 +227,7 @@ var _ = Describe("DataOpJobReconciler", func() {
226227
Expect(annotationsToInject[common.AnnotationDataFlowCustomizedAffinityPrefix+common.K8sNodeNameLabelKey]).To(Equal(testNodeName))
227228
Expect(annotationsToInject[common.AnnotationDataFlowCustomizedAffinityPrefix+common.K8sRegionLabelKey]).To(Equal("region01"))
228229
Expect(annotationsToInject[common.AnnotationDataFlowCustomizedAffinityPrefix+common.K8sZoneLabelKey]).To(Equal("zone01"))
229-
Expect(annotationsToInject[common.AnnotationDataFlowCustomizedAffinityPrefix+"custom-label"]).To(Equal("custom-value"))
230+
Expect(annotationsToInject[common.AnnotationDataFlowCustomizedAffinityPrefix+customLabelKey]).To(Equal("custom-value"))
230231
})
231232

232233
It("skips non-existent labels", func() {
@@ -266,7 +267,7 @@ var _ = Describe("DataOpJobReconciler", func() {
266267
It("injects node labels as annotations onto the job", func() {
267268
job := &batchv1.Job{
268269
ObjectMeta: metav1.ObjectMeta{
269-
Name: "test-job",
270+
Name: testJobName,
270271
Labels: map[string]string{
271272
common.LabelAnnotationManagedBy: common.Fluid,
272273
},
@@ -376,15 +377,15 @@ var _ = Describe("DataOpJobReconciler", func() {
376377
It("returns an error", func() {
377378
job := &batchv1.Job{
378379
ObjectMeta: metav1.ObjectMeta{
379-
Name: "test-job",
380+
Name: testJobName,
380381
Labels: map[string]string{
381382
common.LabelAnnotationManagedBy: common.Fluid,
382383
},
383384
},
384385
Spec: batchv1.JobSpec{
385386
Selector: &metav1.LabelSelector{
386387
MatchLabels: map[string]string{
387-
controllerUIDKey: "test-uid",
388+
controllerUIDKey: testControllerUID,
388389
},
389390
},
390391
},
@@ -393,7 +394,7 @@ var _ = Describe("DataOpJobReconciler", func() {
393394
ObjectMeta: metav1.ObjectMeta{
394395
Name: testPodName,
395396
Labels: map[string]string{
396-
controllerUIDKey: "test-uid",
397+
controllerUIDKey: testControllerUID,
397398
},
398399
},
399400
Spec: v1.PodSpec{NodeName: ""},
@@ -413,7 +414,7 @@ var _ = Describe("DataOpJobReconciler", func() {
413414
It("creates the annotations map before injecting labels", func() {
414415
job := &batchv1.Job{
415416
ObjectMeta: metav1.ObjectMeta{
416-
Name: "test-job",
417+
Name: testJobName,
417418
Labels: map[string]string{
418419
common.LabelAnnotationManagedBy: common.Fluid,
419420
},
@@ -422,7 +423,7 @@ var _ = Describe("DataOpJobReconciler", func() {
422423
Spec: batchv1.JobSpec{
423424
Selector: &metav1.LabelSelector{
424425
MatchLabels: map[string]string{
425-
controllerUIDKey: "test-uid",
426+
controllerUIDKey: testControllerUID,
426427
},
427428
},
428429
},
@@ -431,7 +432,7 @@ var _ = Describe("DataOpJobReconciler", func() {
431432
ObjectMeta: metav1.ObjectMeta{
432433
Name: testPodName,
433434
Labels: map[string]string{
434-
controllerUIDKey: "test-uid",
435+
controllerUIDKey: testControllerUID,
435436
},
436437
},
437438
Spec: v1.PodSpec{NodeName: testNodeName},

pkg/controllers/v1alpha1/fluidapp/implement_test.go

Lines changed: 91 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ import (
3333

3434
var _ = Describe("FluidAppReconcilerImplement", func() {
3535
const (
36-
expectedJuiceFSMountCmd = "/mnt/jfs/juicefs-fuse"
37-
fuseMountPath = "/mnt/fuse"
36+
expectedJuiceFSMountCmd = "/mnt/jfs/juicefs-fuse"
37+
fuseMountPath = "/mnt/fuse"
38+
juicefsFuseMount = "juicefs-fuse-mount"
39+
juicefsMountPath = "/mnt/jfs"
40+
shouldSucceedWithoutErrs = "should succeed without errors"
3841
)
3942

4043
var patches *gomonkey.Patches
@@ -65,75 +68,101 @@ var _ = Describe("FluidAppReconcilerImplement", func() {
6568
Expect(i.umountFuseSidecars(pod)).To(Succeed())
6669
})
6770

68-
It("uses the container prestop command when present", func() {
69-
patches = gomonkey.ApplyFunc(kubeclient.ExecCommandInContainerWithContext, func(_ context.Context, podName, containerName, namespace string, cmd []string) (string, string, error) {
70-
Expect(podName).To(Equal("test"))
71-
Expect(containerName).To(Equal(common.FuseContainerName + "-0"))
72-
Expect(namespace).To(BeEmpty())
73-
Expect(cmd).To(Equal([]string{"umount"}))
74-
return "", "", nil
75-
})
76-
77-
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
78-
pod := &corev1.Pod{
79-
ObjectMeta: metav1.ObjectMeta{Name: "test"},
80-
Spec: corev1.PodSpec{Containers: []corev1.Container{{
81-
Name: common.FuseContainerName + "-0",
82-
Lifecycle: &corev1.Lifecycle{PreStop: &corev1.LifecycleHandler{
83-
Exec: &corev1.ExecAction{Command: []string{"umount"}},
84-
}},
85-
}}},
86-
}
71+
Context("when fuse container has no mount path", func() {
72+
It(shouldSucceedWithoutErrs, func() {
73+
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
74+
pod := &corev1.Pod{
75+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
76+
Spec: corev1.PodSpec{
77+
Containers: []corev1.Container{{Name: common.FuseContainerName + "-0"}},
78+
},
79+
}
8780

88-
Expect(i.umountFuseSidecars(pod)).To(Succeed())
81+
Expect(i.umountFuseSidecars(pod)).To(Succeed())
82+
})
8983
})
9084

91-
It("derives the mount path when the fuse sidecar has no prestop", func() {
92-
patches = gomonkey.ApplyFunc(kubeclient.ExecCommandInContainerWithContext, func(_ context.Context, _, _, _ string, cmd []string) (string, string, error) {
93-
Expect(cmd).To(Equal([]string{"umount", expectedJuiceFSMountCmd}))
94-
return "", "", nil
85+
Context("when fuse container has prestop hook", func() {
86+
It(shouldSucceedWithoutErrs, func() {
87+
patches = gomonkey.ApplyFunc(kubeclient.ExecCommandInContainerWithContext, func(_ context.Context, podName, containerName, namespace string, cmd []string) (string, string, error) {
88+
Expect(podName).To(Equal("test"))
89+
Expect(containerName).To(Equal(common.FuseContainerName + "-0"))
90+
Expect(namespace).To(BeEmpty())
91+
Expect(cmd).To(Equal([]string{"umount"}))
92+
return "", "", nil
93+
})
94+
95+
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
96+
pod := &corev1.Pod{
97+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
98+
Spec: corev1.PodSpec{Containers: []corev1.Container{{
99+
Name: common.FuseContainerName + "-0",
100+
Lifecycle: &corev1.Lifecycle{PreStop: &corev1.LifecycleHandler{
101+
Exec: &corev1.ExecAction{Command: []string{"umount"}},
102+
}},
103+
}}},
104+
}
105+
106+
Expect(i.umountFuseSidecars(pod)).To(Succeed())
95107
})
96-
97-
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
98-
pod := &corev1.Pod{
99-
ObjectMeta: metav1.ObjectMeta{Name: "test"},
100-
Spec: corev1.PodSpec{Containers: []corev1.Container{{
101-
Name: common.FuseContainerName + "-0",
102-
VolumeMounts: []corev1.VolumeMount{{
103-
Name: "juicefs-fuse-mount",
104-
MountPath: "/mnt/jfs",
105-
}},
106-
}}},
107-
}
108-
109-
Expect(i.umountFuseSidecars(pod)).To(Succeed())
110108
})
111109

112-
It("unmounts each fuse sidecar container", func() {
113-
containerNames := []string{}
114-
patches = gomonkey.ApplyFunc((*FluidAppReconcilerImplement).umountFuseSidecar, func(_ *FluidAppReconcilerImplement, _ *corev1.Pod, fuseContainer corev1.Container) error {
115-
containerNames = append(containerNames, fuseContainer.Name)
116-
return nil
110+
Context("when fuse container has mount path", func() {
111+
It("should succeed and umount the path", func() {
112+
patches = gomonkey.ApplyFunc(kubeclient.ExecCommandInContainerWithContext, func(_ context.Context, _, _, _ string, cmd []string) (string, string, error) {
113+
Expect(cmd).To(Equal([]string{"umount", expectedJuiceFSMountCmd}))
114+
return "", "", nil
115+
})
116+
117+
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
118+
pod := &corev1.Pod{
119+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
120+
Spec: corev1.PodSpec{Containers: []corev1.Container{{
121+
Name: common.FuseContainerName + "-0",
122+
VolumeMounts: []corev1.VolumeMount{{
123+
Name: juicefsFuseMount,
124+
MountPath: juicefsMountPath,
125+
}},
126+
}}},
127+
}
128+
129+
Expect(i.umountFuseSidecars(pod)).To(Succeed())
117130
})
131+
})
118132

119-
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
120-
pod := &corev1.Pod{
121-
ObjectMeta: metav1.ObjectMeta{Name: "test"},
122-
Spec: corev1.PodSpec{Containers: []corev1.Container{
123-
{
124-
Name: common.FuseContainerName + "-0",
125-
VolumeMounts: []corev1.VolumeMount{{Name: "juicefs-fuse-mount", MountPath: "/mnt/jfs"}},
126-
},
127-
{
128-
Name: common.FuseContainerName + "-1",
129-
VolumeMounts: []corev1.VolumeMount{{Name: "juicefs-fuse-mount", MountPath: "/mnt/jfs"}},
130-
},
131-
}},
132-
}
133+
Context("when pod has multiple fuse sidecars", func() {
134+
It("unmounts each fuse sidecar container", func() {
135+
containerNames := []string{}
136+
patches = gomonkey.ApplyFunc((*FluidAppReconcilerImplement).umountFuseSidecar, func(_ *FluidAppReconcilerImplement, _ *corev1.Pod, fuseContainer corev1.Container) error {
137+
containerNames = append(containerNames, fuseContainer.Name)
138+
return nil
139+
})
140+
141+
i := &FluidAppReconcilerImplement{Log: fake.NullLogger()}
142+
pod := &corev1.Pod{
143+
ObjectMeta: metav1.ObjectMeta{Name: "test"},
144+
Spec: corev1.PodSpec{Containers: []corev1.Container{
145+
{
146+
Name: common.FuseContainerName + "-0",
147+
VolumeMounts: []corev1.VolumeMount{{
148+
Name: juicefsFuseMount,
149+
MountPath: juicefsMountPath,
150+
}},
151+
},
152+
{
153+
Name: common.FuseContainerName + "-1",
154+
VolumeMounts: []corev1.VolumeMount{{
155+
Name: juicefsFuseMount,
156+
MountPath: juicefsMountPath,
157+
}},
158+
},
159+
}},
160+
}
133161

134-
Expect(i.umountFuseSidecars(pod)).To(Succeed())
135-
Expect(containerNames).To(ConsistOf(common.FuseContainerName+"-0", common.FuseContainerName+"-1"))
136-
Expect(containerNames).To(HaveLen(2))
162+
Expect(i.umountFuseSidecars(pod)).To(Succeed())
163+
Expect(containerNames).To(ConsistOf(common.FuseContainerName+"-0", common.FuseContainerName+"-1"))
164+
Expect(containerNames).To(HaveLen(2))
165+
})
137166
})
138167
})
139168

0 commit comments

Comments
 (0)