@@ -4,10 +4,13 @@ import (
44 "context"
55 "testing"
66
7+ "encoding/json"
8+
79 "github.com/stretchr/testify/assert"
810 admissionv1 "k8s.io/api/admission/v1"
911 "k8s.io/apimachinery/pkg/runtime"
1012 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
13+ v1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1"
1114)
1215
1316func TestValidatingHandler_Basic (t * testing.T ) {
@@ -30,3 +33,42 @@ func TestValidatingHandler_Basic(t *testing.T) {
3033 assert .False (t , resp .Allowed )
3134 assert .Contains (t , resp .Result .Message , "metadata.name is required" )
3235}
36+
37+ func TestValidatingHandler_Dataset (t * testing.T ) {
38+ h := NewValidatingHandler ()
39+ // prepare decoder with scheme so that typed decoding works
40+ scheme := runtime .NewScheme ()
41+ err := v1alpha1 .AddToScheme (scheme )
42+ if err != nil {
43+ t .Fatalf ("failed to add v1alpha1 to scheme: %v" , err )
44+ }
45+ dec := admission .NewDecoder (scheme )
46+ h .InjectDecoder (dec )
47+
48+ // Dataset missing mounts and runtimes -> denied
49+ ds := & v1alpha1.Dataset {}
50+ raw , _ := json .Marshal (ds )
51+ req := admission.Request {
52+ AdmissionRequest : admissionv1.AdmissionRequest {
53+ Object : runtime.RawExtension {Raw : raw },
54+ },
55+ }
56+ resp := h .Handle (context .Background (), req )
57+ assert .False (t , resp .Allowed )
58+
59+ // Dataset with a valid mount -> allowed
60+ ds = & v1alpha1.Dataset {}
61+ ds .Spec .Mounts = []v1alpha1.Mount {{MountPoint : "/data" }}
62+ raw , _ = json .Marshal (ds )
63+ req .AdmissionRequest .Object .Raw = raw
64+ resp = h .Handle (context .Background (), req )
65+ assert .True (t , resp .Allowed )
66+
67+ // Dataset with invalid mount (too short) -> denied
68+ ds = & v1alpha1.Dataset {}
69+ ds .Spec .Mounts = []v1alpha1.Mount {{MountPoint : "abc" }}
70+ raw , _ = json .Marshal (ds )
71+ req .AdmissionRequest .Object .Raw = raw
72+ resp = h .Handle (context .Background (), req )
73+ assert .False (t , resp .Allowed )
74+ }
0 commit comments