@@ -16,56 +16,63 @@ limitations under the License.
1616package mountpropagationinjector
1717
1818import (
19- "testing"
20-
2119 "github.com/fluid-cloudnative/fluid/pkg/ddc/base"
20+ "github.com/fluid-cloudnative/fluid/pkg/webhook/plugins/api"
21+ . "github.com/onsi/ginkgo/v2"
22+ . "github.com/onsi/gomega"
2223 corev1 "k8s.io/api/core/v1"
2324 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425 "sigs.k8s.io/controller-runtime/pkg/client"
2526)
2627
27- func TestMutate ( t * testing. T ) {
28+ var _ = Describe ( "MountPropagationInjector Plugin" , func ( ) {
2829 var (
29- client client.Client
30+ cl client.Client
3031 pod * corev1.Pod
32+ plugin api.MutatingHandler
33+ err error
3134 )
3235
33- plugin , err := NewPlugin (client , "" )
34- if err != nil {
35- t .Error ("new plugin occurs error" , err )
36- }
37- if plugin .GetName () != Name {
38- t .Errorf ("GetName expect %v, got %v" , Name , plugin .GetName ())
39- }
36+ BeforeEach (func () {
37+ cl = nil
38+ plugin , err = NewPlugin (cl , "" )
39+ })
4040
41- runtimeInfo , err := base . BuildRuntimeInfo ( "test" , "fluid" , "alluxio" )
42- if err != nil {
43- t . Errorf ( "fail to create the runtimeInfo with error %v" , err )
44- }
41+ It ( "should create plugin without error" , func () {
42+ Expect ( err ). NotTo ( HaveOccurred ())
43+ Expect ( plugin . GetName ()). To ( Equal ( Name ) )
44+ })
4545
46- pod = & corev1.Pod {
47- ObjectMeta : metav1.ObjectMeta {
48- Name : "test" ,
49- Namespace : "test" ,
50- },
51- }
46+ Context ("Mutate method" , func () {
47+ var runtimeInfo base.RuntimeInfoInterface
5248
53- shouldStop , err := plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {"test" : runtimeInfo })
54- if err != nil {
55- t .Errorf ("fail to mutate pod with error %v" , err )
56- }
49+ BeforeEach (func () {
50+ runtimeInfo , err = base .BuildRuntimeInfo ("test" , "fluid" , "alluxio" )
51+ Expect (err ).NotTo (HaveOccurred ())
52+ pod = & corev1.Pod {
53+ ObjectMeta : metav1.ObjectMeta {
54+ Name : "test" ,
55+ Namespace : "test" ,
56+ },
57+ }
58+ })
5759
58- if shouldStop {
59- t .Errorf ("expect shouldStop as false, but got %v" , shouldStop )
60- }
60+ It ("should mutate pod with valid runtimeInfo and not stop" , func () {
61+ shouldStop , err := plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {"test" : runtimeInfo })
62+ Expect (err ).NotTo (HaveOccurred ())
63+ Expect (shouldStop ).To (BeFalse ())
64+ })
6165
62- _ , err = plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {})
63- if err != nil {
64- t .Errorf ("fail to mutate pod with error %v" , err )
65- }
66+ It ("should mutate pod with empty runtimeInfos" , func () {
67+ shouldStop , err := plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {})
68+ Expect (err ).NotTo (HaveOccurred ())
69+ Expect (shouldStop ).To (BeFalse ())
70+ })
6671
67- _ , err = plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {"test" : nil })
68- if err == nil {
69- t .Errorf ("expect error is not nil" )
70- }
71- }
72+ It ("should return error when runtimeInfo is nil" , func () {
73+ shouldStop , err := plugin .Mutate (pod , map [string ]base.RuntimeInfoInterface {"test" : nil })
74+ Expect (err ).To (HaveOccurred ())
75+ Expect (shouldStop ).To (BeTrue ())
76+ })
77+ })
78+ })
0 commit comments