@@ -29,6 +29,7 @@ import (
2929 k8sErrors "k8s.io/apimachinery/pkg/api/errors"
3030 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131 "k8s.io/apimachinery/pkg/types"
32+ "sigs.k8s.io/controller-runtime/pkg/client"
3233
3334 placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
3435)
@@ -126,14 +127,43 @@ func expectDeniedByVAP(err error) {
126127 ), "Error should indicate policy violation" )
127128}
128129
129- var _ = Describe ("ValidatingAdmissionPolicy for Managed Resources" , Label ("managedresource" ), Ordered , func () {
130- It ("The VAP and its binding should exist" , func () {
131- var vap admissionregistrationv1.ValidatingAdmissionPolicy
132- Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapName }, & vap )).Should (Succeed (), "ValidatingAdmissionPolicy should be installed" )
130+ func checkVAPAndBindingExistence (client client.Client ) {
131+ By (fmt .Sprintf ("Checking existence of ValidatingAdmissionPolicy: %s" , vapName ))
132+ var vap admissionregistrationv1.ValidatingAdmissionPolicy
133+ Expect (client .Get (ctx , types.NamespacedName {Name : vapName }, & vap )).To (Succeed (),
134+ fmt .Sprintf ("ValidatingAdmissionPolicy %s should exist" , vapName ))
133135
134- var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
135- Expect (sysMastersClient .Get (ctx , types.NamespacedName {Name : vapName }, & vapBinding )).Should (Succeed (), "ValidatingAdmissionPolicyBinding should be installed" )
136- })
136+ By (fmt .Sprintf ("Checking existence of ValidatingAdmissionPolicyBinding: %s" , vapName ))
137+ var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
138+ Expect (client .Get (ctx , types.NamespacedName {Name : vapName }, & vapBinding )).To (Succeed (),
139+ fmt .Sprintf ("ValidatingAdmissionPolicyBinding %s should exist" , vapName ))
140+
141+ By ("Verifying VAP has validations configured" )
142+ Expect (vap .Spec .Validations ).ToNot (BeEmpty (), "VAP should have validation rules" )
143+
144+ By ("Verifying VAP binding references the correct policy" )
145+ Expect (vapBinding .Spec .PolicyName ).To (Equal (vapName ),
146+ fmt .Sprintf ("VAP binding should reference policy %s" , vapName ))
147+
148+ By ("Verifying VAP binding has validation actions configured" )
149+ Expect (vapBinding .Spec .ValidationActions ).ToNot (BeEmpty (), "VAP binding should have validation actions" )
150+ }
151+
152+ func checkVAPAndBindingAbsence (client client.Client ) {
153+ By (fmt .Sprintf ("Checking absence of ValidatingAdmissionPolicy: %s" , vapName ))
154+ var vap admissionregistrationv1.ValidatingAdmissionPolicy
155+ err := client .Get (ctx , types.NamespacedName {Name : vapName }, & vap )
156+ Expect (k8sErrors .IsNotFound (err )).To (BeTrue (),
157+ fmt .Sprintf ("ValidatingAdmissionPolicy %s should not exist" , vapName ))
158+
159+ By (fmt .Sprintf ("Checking absence of ValidatingAdmissionPolicyBinding: %s" , vapName ))
160+ var vapBinding admissionregistrationv1.ValidatingAdmissionPolicyBinding
161+ err = client .Get (ctx , types.NamespacedName {Name : vapName }, & vapBinding )
162+ Expect (k8sErrors .IsNotFound (err )).To (BeTrue (),
163+ fmt .Sprintf ("ValidatingAdmissionPolicyBinding %s should not exist" , vapName ))
164+ }
165+
166+ var _ = Describe ("ValidatingAdmissionPolicy for Managed Resources" , Label ("managedresource" ), Ordered , func () {
137167
138168 It ("should allow operations on unmanaged namespace for non-system:masters user" , func () {
139169 unmanagedNS := createUnmanagedNamespace ("test-unmanaged-ns" )
0 commit comments