Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,56 +16,63 @@ 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"
)

func TestMutate(t *testing.T) {
var _ = Describe("MountPropagationInjector Plugin", func() {
var (
client client.Client
cl client.Client
pod *corev1.Pod
plugin api.MutatingHandler
err error
)

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())
}
BeforeEach(func() {
cl = nil
plugin, err = NewPlugin(cl, "")
})

runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
if err != nil {
t.Errorf("fail to create the runtimeInfo with error %v", err)
}
It("should create plugin without error", func() {
Expect(err).NotTo(HaveOccurred())
Expect(plugin.GetName()).To(Equal(Name))
})

pod = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
}
Context("Mutate method", func() {
var runtimeInfo base.RuntimeInfoInterface

shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
if err != nil {
t.Errorf("fail to mutate pod with error %v", err)
}
BeforeEach(func() {
runtimeInfo, err = base.BuildRuntimeInfo("test", "fluid", "alluxio")
Expect(err).NotTo(HaveOccurred())
pod = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
}
})
Comment on lines +49 to +58
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The err variable from the outer scope is being overwritten in this BeforeEach block. This can lead to confusing test failures. If NewPlugin in the outer BeforeEach were to fail, the error would be masked here, and subsequent tests would likely panic instead of failing with a clear message. It's better to use a local variable for the error from BuildRuntimeInfo and to check the error from the outer scope at the beginning of this block to ensure the plugin was created successfully.

        BeforeEach(func() {
            Expect(err).NotTo(HaveOccurred())
            var e error
            runtimeInfo, e = base.BuildRuntimeInfo("test", "fluid", "alluxio")
            Expect(e).NotTo(HaveOccurred())
            pod = &corev1.Pod{
                ObjectMeta: metav1.ObjectMeta{
                    Name:      "test",
                    Namespace: "test",
                },
            }
        })


if shouldStop {
t.Errorf("expect shouldStop as false, but got %v", shouldStop)
}
It("should mutate pod with valid runtimeInfo and not stop", func() {
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
Expect(err).NotTo(HaveOccurred())
Expect(shouldStop).To(BeFalse())
})

_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
if err != nil {
t.Errorf("fail to mutate pod with error %v", err)
}
It("should mutate pod with empty runtimeInfos", func() {
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
Expect(err).NotTo(HaveOccurred())
Expect(shouldStop).To(BeFalse())
})

_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
if err == nil {
t.Errorf("expect error is not nil")
}
}
It("should return error when runtimeInfo is nil", func() {
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
Expect(err).To(HaveOccurred())
Expect(shouldStop).To(BeTrue())
})
})
})
Original file line number Diff line number Diff line change
@@ -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")
}
Loading