@@ -448,7 +448,7 @@ func TestDeleteValidatingConfiguration(t *testing.T) {
448448 }
449449}
450450
451- func TestApplyValidatingAdmissionPolicyConfiguration (t * testing.T ) {
451+ func TestApplyValidatingAdmissionPolicy (t * testing.T ) {
452452 defaultPolicy := & admissionregistrationv1beta1.ValidatingAdmissionPolicy {}
453453 defaultPolicy .SetName ("test" )
454454 createEvent := "ValidatingAdmissionPolicyCreated"
@@ -562,6 +562,138 @@ func TestApplyValidatingAdmissionPolicyConfiguration(t *testing.T) {
562562 }
563563}
564564
565+ func TestDeleteValidatingAdmissionPolicy (t * testing.T ) {
566+ defaultPolicy := & admissionregistrationv1.ValidatingAdmissionPolicy {}
567+ defaultPolicy .SetName ("test" )
568+ deleteEvent := "ValidatingAdmissionPolicyDeleted"
569+
570+ tests := []struct {
571+ name string
572+ expectModified bool
573+ existing func () * admissionregistrationv1.ValidatingAdmissionPolicy
574+ input func () * admissionregistrationv1.ValidatingAdmissionPolicy
575+ checkUpdated func (* admissionregistrationv1.ValidatingAdmissionPolicy ) error
576+ expectedEvents []string
577+ }{
578+ {
579+ name : "Should delete policy if it exists" ,
580+ expectModified : true ,
581+ input : func () * admissionregistrationv1.ValidatingAdmissionPolicy {
582+ policy := defaultPolicy .DeepCopy ()
583+ return policy
584+ },
585+ existing : func () * admissionregistrationv1.ValidatingAdmissionPolicy {
586+ policy := defaultPolicy .DeepCopy ()
587+ return policy
588+ },
589+ expectedEvents : []string {deleteEvent },
590+ },
591+ {
592+ name : "Should do nothing if policy does not exist" ,
593+ expectModified : false ,
594+ input : func () * admissionregistrationv1.ValidatingAdmissionPolicy {
595+ policy := defaultPolicy .DeepCopy ()
596+ return policy
597+ },
598+ expectedEvents : []string {},
599+ },
600+ }
601+
602+ for _ , test := range tests {
603+ t .Run (test .name , func (t * testing.T ) {
604+
605+ existingHooks := []runtime.Object {}
606+ if test .existing != nil {
607+ existingHooks = append (existingHooks , test .existing ())
608+ }
609+ client := fake .NewSimpleClientset (existingHooks ... )
610+ recorder := events .NewInMemoryRecorder ("test" , clocktesting .NewFakePassiveClock (time .Now ()))
611+
612+ testApply := func (expectModify bool ) {
613+ updatedHook , modified , err := DeleteValidatingAdmissionPolicyV1 (
614+ context .TODO (),
615+ client .AdmissionregistrationV1 (),
616+ recorder , test .input ())
617+ if err != nil {
618+ t .Fatal (err )
619+ }
620+ if expectModify != modified {
621+ t .Errorf ("expected modified to be equal %v, got %v: %#v" , expectModify , modified , updatedHook )
622+ }
623+ }
624+
625+ testApply (test .expectModified )
626+ assertEvents (t , test .name , test .expectedEvents , recorder .Events ())
627+ })
628+ }
629+ }
630+
631+ func TestDeleteValidatingAdmissionPolicyBinding (t * testing.T ) {
632+ defaultPolicyBinding := & admissionregistrationv1.ValidatingAdmissionPolicyBinding {}
633+ defaultPolicyBinding .SetName ("test" )
634+ deleteEvent := "ValidatingAdmissionPolicyBindingDeleted"
635+
636+ tests := []struct {
637+ name string
638+ expectModified bool
639+ existing func () * admissionregistrationv1.ValidatingAdmissionPolicyBinding
640+ input func () * admissionregistrationv1.ValidatingAdmissionPolicyBinding
641+ checkUpdated func (* admissionregistrationv1.ValidatingAdmissionPolicyBinding ) error
642+ expectedEvents []string
643+ }{
644+ {
645+ name : "Should delete policy binding if it exists" ,
646+ expectModified : true ,
647+ input : func () * admissionregistrationv1.ValidatingAdmissionPolicyBinding {
648+ policyBinding := defaultPolicyBinding .DeepCopy ()
649+ return policyBinding
650+ },
651+ existing : func () * admissionregistrationv1.ValidatingAdmissionPolicyBinding {
652+ policyBinding := defaultPolicyBinding .DeepCopy ()
653+ return policyBinding
654+ },
655+ expectedEvents : []string {deleteEvent },
656+ },
657+ {
658+ name : "Should do nothing if policy binding does not exist" ,
659+ expectModified : false ,
660+ input : func () * admissionregistrationv1.ValidatingAdmissionPolicyBinding {
661+ policyBinding := defaultPolicyBinding .DeepCopy ()
662+ return policyBinding
663+ },
664+ expectedEvents : []string {},
665+ },
666+ }
667+
668+ for _ , test := range tests {
669+ t .Run (test .name , func (t * testing.T ) {
670+
671+ existingHooks := []runtime.Object {}
672+ if test .existing != nil {
673+ existingHooks = append (existingHooks , test .existing ())
674+ }
675+ client := fake .NewSimpleClientset (existingHooks ... )
676+ recorder := events .NewInMemoryRecorder ("test" , clocktesting .NewFakePassiveClock (time .Now ()))
677+
678+ testApply := func (expectModify bool ) {
679+ updatedHook , modified , err := DeleteValidatingAdmissionPolicyBindingV1 (
680+ context .TODO (),
681+ client .AdmissionregistrationV1 (),
682+ recorder , test .input ())
683+ if err != nil {
684+ t .Fatal (err )
685+ }
686+ if expectModify != modified {
687+ t .Errorf ("expected modified to be equal %v, got %v: %#v" , expectModify , modified , updatedHook )
688+ }
689+ }
690+
691+ testApply (test .expectModified )
692+ assertEvents (t , test .name , test .expectedEvents , recorder .Events ())
693+ })
694+ }
695+ }
696+
565697func assertEvents (t * testing.T , testCase string , expectedReasons []string , events []* corev1.Event ) {
566698 if len (expectedReasons ) != len (events ) {
567699 t .Errorf (
0 commit comments