@@ -709,6 +709,109 @@ func TestEvictionPDBStatus(t *testing.T) {
709
709
}
710
710
}
711
711
712
+ func TestAddConditionAndDelete (t * testing.T ) {
713
+ cases := []struct {
714
+ name string
715
+ initialPod bool
716
+ makeDeleteOptions func (* api.Pod ) * metav1.DeleteOptions
717
+ expectErr string
718
+ }{
719
+ {
720
+ name : "simple" ,
721
+ initialPod : true ,
722
+ makeDeleteOptions : func (pod * api.Pod ) * metav1.DeleteOptions { return & metav1.DeleteOptions {} },
723
+ },
724
+ {
725
+ name : "missing" ,
726
+ initialPod : false ,
727
+ makeDeleteOptions : func (pod * api.Pod ) * metav1.DeleteOptions { return & metav1.DeleteOptions {} },
728
+ expectErr : "not found" ,
729
+ },
730
+ {
731
+ name : "valid uid" ,
732
+ initialPod : true ,
733
+ makeDeleteOptions : func (pod * api.Pod ) * metav1.DeleteOptions {
734
+ return & metav1.DeleteOptions {Preconditions : & metav1.Preconditions {UID : & pod .UID }}
735
+ },
736
+ },
737
+ {
738
+ name : "invalid uid" ,
739
+ initialPod : true ,
740
+ makeDeleteOptions : func (pod * api.Pod ) * metav1.DeleteOptions {
741
+ badUID := pod .UID + "1"
742
+ return & metav1.DeleteOptions {Preconditions : & metav1.Preconditions {UID : & badUID }}
743
+ },
744
+ expectErr : "The object might have been deleted and then recreated" ,
745
+ },
746
+ {
747
+ name : "valid resourceVersion" ,
748
+ initialPod : true ,
749
+ makeDeleteOptions : func (pod * api.Pod ) * metav1.DeleteOptions {
750
+ return & metav1.DeleteOptions {Preconditions : & metav1.Preconditions {ResourceVersion : & pod .ResourceVersion }}
751
+ },
752
+ },
753
+ {
754
+ name : "invalid resourceVersion" ,
755
+ initialPod : true ,
756
+ makeDeleteOptions : func (pod * api.Pod ) * metav1.DeleteOptions {
757
+ badRV := pod .ResourceVersion + "1"
758
+ return & metav1.DeleteOptions {Preconditions : & metav1.Preconditions {ResourceVersion : & badRV }}
759
+ },
760
+ expectErr : "The object might have been modified" ,
761
+ },
762
+ }
763
+
764
+ testContext := genericapirequest .WithNamespace (genericapirequest .NewContext (), metav1 .NamespaceDefault )
765
+
766
+ storage , _ , _ , server := newStorage (t )
767
+ defer server .Terminate (t )
768
+ defer storage .Store .DestroyFunc ()
769
+
770
+ client := fake .NewSimpleClientset ()
771
+ evictionRest := newEvictionStorage (storage .Store , client .PolicyV1 ())
772
+
773
+ for _ , tc := range cases {
774
+ for _ , conditionsEnabled := range []bool {true , false } {
775
+ name := fmt .Sprintf ("%s_conditions=%v" , tc .name , conditionsEnabled )
776
+ t .Run (name , func (t * testing.T ) {
777
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .PodDisruptionConditions , conditionsEnabled )()
778
+ var deleteOptions * metav1.DeleteOptions
779
+ if tc .initialPod {
780
+ newPod := validNewPod ()
781
+ createdObj , err := storage .Create (testContext , newPod , rest .ValidateAllObjectFunc , & metav1.CreateOptions {})
782
+ if err != nil {
783
+ t .Fatal (err )
784
+ }
785
+ t .Cleanup (func () {
786
+ zero := int64 (0 )
787
+ storage .Delete (testContext , newPod .Name , rest .ValidateAllObjectFunc , & metav1.DeleteOptions {GracePeriodSeconds : & zero })
788
+ })
789
+ deleteOptions = tc .makeDeleteOptions (createdObj .(* api.Pod ))
790
+ } else {
791
+ deleteOptions = tc .makeDeleteOptions (nil )
792
+ }
793
+ if deleteOptions == nil {
794
+ deleteOptions = & metav1.DeleteOptions {}
795
+ }
796
+
797
+ err := addConditionAndDeletePod (evictionRest , testContext , "foo" , rest .ValidateAllObjectFunc , deleteOptions )
798
+ if err == nil {
799
+ if tc .expectErr != "" {
800
+ t .Fatalf ("expected err containing %q, got none" , tc .expectErr )
801
+ }
802
+ return
803
+ }
804
+ if tc .expectErr == "" {
805
+ t .Fatalf ("unexpected err: %v" , err )
806
+ }
807
+ if ! strings .Contains (err .Error (), tc .expectErr ) {
808
+ t .Fatalf ("expected err containing %q, got %v" , tc .expectErr , err )
809
+ }
810
+ })
811
+ }
812
+ }
813
+ }
814
+
712
815
func resource (resource string ) schema.GroupResource {
713
816
return schema.GroupResource {Group : "" , Resource : resource }
714
817
}
@@ -765,7 +868,7 @@ func (ms *mockStore) Watch(ctx context.Context, options *metainternalversion.Lis
765
868
}
766
869
767
870
func (ms * mockStore ) Update (ctx context.Context , name string , objInfo rest.UpdatedObjectInfo , createValidation rest.ValidateObjectFunc , updateValidation rest.ValidateObjectUpdateFunc , forceAllowCreate bool , options * metav1.UpdateOptions ) (runtime.Object , bool , error ) {
768
- return nil , false , nil
871
+ return ms . pod , false , nil
769
872
}
770
873
771
874
func (ms * mockStore ) Get (ctx context.Context , name string , options * metav1.GetOptions ) (runtime.Object , error ) {
0 commit comments