Skip to content
Closed
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
111 changes: 69 additions & 42 deletions pkg/webhook/plugins/fusesidecar/fuse_sidecar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,53 +20,80 @@ import (
"testing"

"github.com/fluid-cloudnative/fluid/pkg/ddc/base"
"github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

. "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("FuseSidecar Plugin", func() {
var (
client 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())
}

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 nil")
}
BeforeEach(func() {
s := runtime.NewScheme()
Expect(corev1.AddToScheme(s)).To(Succeed())

c := fake.NewClientBuilder().
WithScheme(s).
Build()

plugin, err = NewPlugin(c, "")
})

It("creates plugin successfully", func() {
Expect(err).NotTo(HaveOccurred())
Expect(plugin.GetName()).To(Equal(Name))
})

Context("when mutating a pod", func() {
var pod *corev1.Pod

BeforeEach(func() {
pod = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "test",
},
}
})

It("does not stop mutation when runtimeInfo is present", func() {
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())
})

It("does not error when runtimeInfos is empty", func() {
_, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
Expect(err).NotTo(HaveOccurred())
})

It("does not error when runtimeInfo is nil", func() {
_, err := plugin.Mutate(
pod,
map[string]base.RuntimeInfoInterface{"test": nil},
)
Expect(err).NotTo(HaveOccurred())
})
})
})

func TestFuseSidecar(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "FuseSidecar Plugin Suite")
}