diff --git a/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_suite_test.go b/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_suite_test.go new file mode 100644 index 00000000000..8f004416aec --- /dev/null +++ b/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_suite_test.go @@ -0,0 +1,13 @@ +package mountpropagationinjector + +import ( + "testing" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +func TestMountPropagationInjector(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "MountPropagationInjector Suite") +} diff --git a/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_test.go b/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_test.go index 41076762760..cc55fab28e9 100644 --- a/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_test.go +++ b/pkg/webhook/plugins/mountpropagationinjector/mount_propagation_injector_test.go @@ -1,10 +1,9 @@ /* - 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 + 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, @@ -12,60 +11,82 @@ 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 mountpropagationinjector import ( - "testing" - "github.com/fluid-cloudnative/fluid/pkg/ddc/base" + "github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client/fake" ) -func TestMutate(t *testing.T) { +var _ = Describe("MountPropagationInjector Plugin", func() { var ( - client client.Client + plugin api.MutatingHandler pod *corev1.Pod ) - plugin, err := NewPlugin(client, "") - if err != nil { - t.Error("new plugin occurs error", err) - } - if plugin.GetName() != Name { - t.Errorf("GetName expect %v, got %v", Name, plugin.GetName()) - } - - runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio") - if err != nil { - t.Errorf("fail to create the runtimeInfo with error %v", err) - } - - pod = &corev1.Pod{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test", - Namespace: "test", - }, - } - - shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo}) - if err != nil { - t.Errorf("fail to mutate pod with error %v", err) - } - - if shouldStop { - t.Errorf("expect shouldStop as false, but got %v", shouldStop) - } - - _, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{}) - if err != nil { - t.Errorf("fail to mutate pod with error %v", err) - } - - _, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil}) - if err == nil { - t.Errorf("expect error is not nil") - } -} + BeforeEach(func() { + scheme := runtime.NewScheme() + Expect(corev1.AddToScheme(scheme)).To(Succeed()) + + c := fake.NewClientBuilder(). + WithScheme(scheme). + Build() + + var err error + plugin, err = NewPlugin(c, "") + Expect(err).NotTo(HaveOccurred()) + + pod = &corev1.Pod{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + } + }) + + It("returns correct plugin name", func() { + Expect(plugin.GetName()).To(Equal(Name)) + }) + + It("injects mount propagation when PVC is mounted", func() { + pod.Spec.Volumes = []corev1.Volume{{ + Name: "data", + VolumeSource: corev1.VolumeSource{ + PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ + ClaimName: "test", + }, + }, + }} + + pod.Spec.Containers = []corev1.Container{{ + Name: "app", + VolumeMounts: []corev1.VolumeMount{{ + Name: "data", + MountPath: "/data", + }}, + }} + + runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio") + Expect(err).NotTo(HaveOccurred()) + + shouldStop, err := plugin.Mutate( + pod, + map[string]base.RuntimeInfoInterface{"test": runtimeInfo}, + ) + + Expect(err).NotTo(HaveOccurred()) + Expect(shouldStop).To(BeFalse()) + + mp := corev1.MountPropagationHostToContainer + Expect(pod.Spec.Containers[0].VolumeMounts[0].MountPropagation). + To(Equal(&mp)) + }) +})