@@ -911,3 +911,140 @@ func TestDropEmptyDirSizeLimit(t *testing.T) {
911
911
}
912
912
}
913
913
}
914
+
915
+ func TestDropRunAsGroup (t * testing.T ) {
916
+ group := func () * int64 {
917
+ testGroup := int64 (1000 )
918
+ return & testGroup
919
+ }
920
+ defaultProcMount := api .DefaultProcMount
921
+ defaultSecurityContext := func () * api.SecurityContext {
922
+ return & api.SecurityContext {ProcMount : & defaultProcMount }
923
+ }
924
+ securityContextWithRunAsGroup := func () * api.SecurityContext {
925
+ return & api.SecurityContext {ProcMount : & defaultProcMount , RunAsGroup : group ()}
926
+ }
927
+ podWithoutRunAsGroup := func () * api.Pod {
928
+ return & api.Pod {
929
+ Spec : api.PodSpec {
930
+ RestartPolicy : api .RestartPolicyNever ,
931
+ SecurityContext : & api.PodSecurityContext {},
932
+ Containers : []api.Container {{Name : "container1" , Image : "testimage" , SecurityContext : defaultSecurityContext ()}},
933
+ InitContainers : []api.Container {{Name : "initContainer1" , Image : "testimage" , SecurityContext : defaultSecurityContext ()}},
934
+ },
935
+ }
936
+ }
937
+ podWithRunAsGroupInPod := func () * api.Pod {
938
+ return & api.Pod {
939
+ Spec : api.PodSpec {
940
+ RestartPolicy : api .RestartPolicyNever ,
941
+ SecurityContext : & api.PodSecurityContext {RunAsGroup : group ()},
942
+ Containers : []api.Container {{Name : "container1" , Image : "testimage" , SecurityContext : defaultSecurityContext ()}},
943
+ InitContainers : []api.Container {{Name : "initContainer1" , Image : "testimage" , SecurityContext : defaultSecurityContext ()}},
944
+ },
945
+ }
946
+ }
947
+ podWithRunAsGroupInContainers := func () * api.Pod {
948
+ return & api.Pod {
949
+ Spec : api.PodSpec {
950
+ RestartPolicy : api .RestartPolicyNever ,
951
+ SecurityContext : & api.PodSecurityContext {},
952
+ Containers : []api.Container {{Name : "container1" , Image : "testimage" , SecurityContext : securityContextWithRunAsGroup ()}},
953
+ InitContainers : []api.Container {{Name : "initContainer1" , Image : "testimage" , SecurityContext : defaultSecurityContext ()}},
954
+ },
955
+ }
956
+ }
957
+ podWithRunAsGroupInInitContainers := func () * api.Pod {
958
+ return & api.Pod {
959
+ Spec : api.PodSpec {
960
+ RestartPolicy : api .RestartPolicyNever ,
961
+ SecurityContext : & api.PodSecurityContext {},
962
+ Containers : []api.Container {{Name : "container1" , Image : "testimage" , SecurityContext : defaultSecurityContext ()}},
963
+ InitContainers : []api.Container {{Name : "initContainer1" , Image : "testimage" , SecurityContext : securityContextWithRunAsGroup ()}},
964
+ },
965
+ }
966
+ }
967
+
968
+ podInfo := []struct {
969
+ description string
970
+ hasRunAsGroup bool
971
+ pod func () * api.Pod
972
+ }{
973
+ {
974
+ description : "have RunAsGroup in Pod" ,
975
+ hasRunAsGroup : true ,
976
+ pod : podWithRunAsGroupInPod ,
977
+ },
978
+ {
979
+ description : "have RunAsGroup in Container" ,
980
+ hasRunAsGroup : true ,
981
+ pod : podWithRunAsGroupInContainers ,
982
+ },
983
+ {
984
+ description : "have RunAsGroup in InitContainer" ,
985
+ hasRunAsGroup : true ,
986
+ pod : podWithRunAsGroupInInitContainers ,
987
+ },
988
+ {
989
+ description : "does not have RunAsGroup" ,
990
+ hasRunAsGroup : false ,
991
+ pod : podWithoutRunAsGroup ,
992
+ },
993
+ {
994
+ description : "is nil" ,
995
+ hasRunAsGroup : false ,
996
+ pod : func () * api.Pod { return nil },
997
+ },
998
+ }
999
+
1000
+ for _ , enabled := range []bool {true , false } {
1001
+ for _ , oldPodInfo := range podInfo {
1002
+ for _ , newPodInfo := range podInfo {
1003
+ oldPodHasRunAsGroup , oldPod := oldPodInfo .hasRunAsGroup , oldPodInfo .pod ()
1004
+ newPodHasRunAsGroup , newPod := newPodInfo .hasRunAsGroup , newPodInfo .pod ()
1005
+ if newPod == nil {
1006
+ continue
1007
+ }
1008
+
1009
+ t .Run (fmt .Sprintf ("feature enabled=%v, old pod %v, new pod %v" , enabled , oldPodInfo .description , newPodInfo .description ), func (t * testing.T ) {
1010
+ defer utilfeaturetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .RunAsGroup , enabled )()
1011
+
1012
+ var oldPodSpec * api.PodSpec
1013
+ if oldPod != nil {
1014
+ oldPodSpec = & oldPod .Spec
1015
+ }
1016
+ DropDisabledFields (& newPod .Spec , oldPodSpec )
1017
+
1018
+ // old pod should never be changed
1019
+ if ! reflect .DeepEqual (oldPod , oldPodInfo .pod ()) {
1020
+ t .Errorf ("old pod changed: %v" , diff .ObjectReflectDiff (oldPod , oldPodInfo .pod ()))
1021
+ }
1022
+
1023
+ switch {
1024
+ case enabled || oldPodHasRunAsGroup :
1025
+ // new pod should not be changed if the feature is enabled, or if the old pod had RunAsGroup
1026
+ if ! reflect .DeepEqual (newPod , newPodInfo .pod ()) {
1027
+ t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .pod ()))
1028
+ }
1029
+ case newPodHasRunAsGroup :
1030
+ // new pod should be changed
1031
+ if reflect .DeepEqual (newPod , newPodInfo .pod ()) {
1032
+ t .Errorf ("%v" , oldPod )
1033
+ t .Errorf ("%v" , newPod )
1034
+ t .Errorf ("new pod was not changed" )
1035
+ }
1036
+ // new pod should not have RunAsGroup
1037
+ if ! reflect .DeepEqual (newPod , podWithoutRunAsGroup ()) {
1038
+ t .Errorf ("new pod had RunAsGroup: %v" , diff .ObjectReflectDiff (newPod , podWithoutRunAsGroup ()))
1039
+ }
1040
+ default :
1041
+ // new pod should not need to be changed
1042
+ if ! reflect .DeepEqual (newPod , newPodInfo .pod ()) {
1043
+ t .Errorf ("new pod changed: %v" , diff .ObjectReflectDiff (newPod , newPodInfo .pod ()))
1044
+ }
1045
+ }
1046
+ })
1047
+ }
1048
+ }
1049
+ }
1050
+ }
0 commit comments