@@ -20,80 +20,53 @@ import (
2020 "testing"
2121
2222 "github.com/fluid-cloudnative/fluid/pkg/ddc/base"
23- "github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api"
24- "k8s.io/apimachinery/pkg/runtime"
25- "sigs.k8s.io/controller-runtime/pkg/client/fake"
26-
27- . "github.com/onsi/ginkgo/v2"
28- . "github.com/onsi/gomega"
29-
3023 corev1 "k8s.io/api/core/v1"
3124 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+ "sigs.k8s.io/controller-runtime/pkg/client"
3226)
3327
34- var _ = Describe ( "FuseSidecar Plugin" , func ( ) {
28+ func TestMutate ( t * testing. T ) {
3529 var (
36- plugin api. MutatingHandler
37- err error
30+ client client. Client
31+ pod * corev1. Pod
3832 )
3933
40- BeforeEach (func () {
41- s := runtime .NewScheme ()
42- Expect (corev1 .AddToScheme (s )).To (Succeed ())
43-
44- c := fake .NewClientBuilder ().
45- WithScheme (s ).
46- Build ()
47-
48- plugin , err = NewPlugin (c , "" )
49- })
50-
51- It ("creates plugin successfully" , func () {
52- Expect (err ).NotTo (HaveOccurred ())
53- Expect (plugin .GetName ()).To (Equal (Name ))
54- })
55-
56- Context ("when mutating a pod" , func () {
57- var pod * corev1.Pod
58-
59- BeforeEach (func () {
60- pod = & corev1.Pod {
61- ObjectMeta : metav1.ObjectMeta {
62- Name : "test" ,
63- Namespace : "test" ,
64- },
65- }
66- })
67-
68- It ("does not stop mutation when runtimeInfo is present" , func () {
69- runtimeInfo , err := base .BuildRuntimeInfo ("test" , "fluid" , "alluxio" )
70- Expect (err ).NotTo (HaveOccurred ())
71-
72- shouldStop , err := plugin .Mutate (
73- pod ,
74- map [string ]base.RuntimeInfoInterface {"test" : runtimeInfo },
75- )
76-
77- Expect (err ).NotTo (HaveOccurred ())
78- Expect (shouldStop ).To (BeFalse ())
79- })
80-
81- It ("does not error when runtimeInfos is empty" , func () {
82- _ , err := plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {})
83- Expect (err ).NotTo (HaveOccurred ())
84- })
85-
86- It ("does not error when runtimeInfo is nil" , func () {
87- _ , err := plugin .Mutate (
88- pod ,
89- map [string ]base.RuntimeInfoInterface {"test" : nil },
90- )
91- Expect (err ).NotTo (HaveOccurred ())
92- })
93- })
94- })
95-
96- func TestFuseSidecar (t * testing.T ) {
97- RegisterFailHandler (Fail )
98- RunSpecs (t , "FuseSidecar Plugin Suite" )
34+ plugin , err := NewPlugin (client , "" )
35+ if err != nil {
36+ t .Error ("new plugin occurs error" , err )
37+ }
38+ if plugin .GetName () != Name {
39+ t .Errorf ("GetName expect %v, got %v" , Name , plugin .GetName ())
40+ }
41+
42+ runtimeInfo , err := base .BuildRuntimeInfo ("test" , "fluid" , "alluxio" )
43+ if err != nil {
44+ t .Errorf ("fail to create the runtimeInfo with error %v" , err )
45+ }
46+
47+ pod = & corev1.Pod {
48+ ObjectMeta : metav1.ObjectMeta {
49+ Name : "test" ,
50+ Namespace : "test" ,
51+ },
52+ }
53+
54+ shouldStop , err := plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {"test" : runtimeInfo })
55+ if err != nil {
56+ t .Errorf ("fail to mutate pod with error %v" , err )
57+ }
58+
59+ if shouldStop {
60+ t .Errorf ("expect shouldStop as false, but got %v" , shouldStop )
61+ }
62+
63+ _ , err = plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {})
64+ if err != nil {
65+ t .Errorf ("fail to mutate pod with error %v" , err )
66+ }
67+
68+ _ , err = plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {"test" : nil })
69+ if err != nil {
70+ t .Errorf ("expect error is nil" )
71+ }
9972}
0 commit comments