@@ -814,6 +814,11 @@ func TestUserNamespace(t *testing.T) {
814814 ContainerId : 1000 ,
815815 Length : 10 ,
816816 }
817+ otherIDMap := runtime.IDMapping {
818+ HostId : 2000 ,
819+ ContainerId : 2000 ,
820+ Length : 10 ,
821+ }
817822 expIDMap := runtimespec.LinuxIDMapping {
818823 HostID : 1000 ,
819824 ContainerID : 1000 ,
@@ -824,6 +829,7 @@ func TestUserNamespace(t *testing.T) {
824829 c := newTestCRIService ()
825830 for desc , test := range map [string ]struct {
826831 userNS * runtime.UserNamespace
832+ sandboxUserNS * runtime.UserNamespace
827833 expNS * runtimespec.LinuxNamespace
828834 expNotNS * runtimespec.LinuxNamespace // Does NOT contain this namespace
829835 expUIDMapping []runtimespec.LinuxIDMapping
@@ -871,6 +877,58 @@ func TestUserNamespace(t *testing.T) {
871877 expUIDMapping : []runtimespec.LinuxIDMapping {expIDMap },
872878 expGIDMapping : []runtimespec.LinuxIDMapping {expIDMap },
873879 },
880+ "pod namespace mode with inconsistent sandbox config (different GIDs)" : {
881+ userNS : & runtime.UserNamespace {
882+ Mode : runtime .NamespaceMode_POD ,
883+ Uids : []* runtime.IDMapping {& idMap },
884+ Gids : []* runtime.IDMapping {& idMap },
885+ },
886+ sandboxUserNS : & runtime.UserNamespace {
887+ Mode : runtime .NamespaceMode_POD ,
888+ Uids : []* runtime.IDMapping {& idMap },
889+ Gids : []* runtime.IDMapping {& otherIDMap },
890+ },
891+ err : true ,
892+ },
893+ "pod namespace mode with inconsistent sandbox config (different UIDs)" : {
894+ userNS : & runtime.UserNamespace {
895+ Mode : runtime .NamespaceMode_POD ,
896+ Uids : []* runtime.IDMapping {& idMap },
897+ Gids : []* runtime.IDMapping {& idMap },
898+ },
899+ sandboxUserNS : & runtime.UserNamespace {
900+ Mode : runtime .NamespaceMode_POD ,
901+ Uids : []* runtime.IDMapping {& otherIDMap },
902+ Gids : []* runtime.IDMapping {& idMap },
903+ },
904+ err : true ,
905+ },
906+ "pod namespace mode with inconsistent sandbox config (different len)" : {
907+ userNS : & runtime.UserNamespace {
908+ Mode : runtime .NamespaceMode_POD ,
909+ Uids : []* runtime.IDMapping {& idMap },
910+ Gids : []* runtime.IDMapping {& idMap },
911+ },
912+ sandboxUserNS : & runtime.UserNamespace {
913+ Mode : runtime .NamespaceMode_POD ,
914+ Uids : []* runtime.IDMapping {& idMap , & idMap },
915+ Gids : []* runtime.IDMapping {& idMap , & idMap },
916+ },
917+ err : true ,
918+ },
919+ "pod namespace mode with inconsistent sandbox config (different mode)" : {
920+ userNS : & runtime.UserNamespace {
921+ Mode : runtime .NamespaceMode_POD ,
922+ Uids : []* runtime.IDMapping {& idMap },
923+ Gids : []* runtime.IDMapping {& idMap },
924+ },
925+ sandboxUserNS : & runtime.UserNamespace {
926+ Mode : runtime .NamespaceMode_NODE ,
927+ Uids : []* runtime.IDMapping {& idMap },
928+ Gids : []* runtime.IDMapping {& idMap },
929+ },
930+ err : true ,
931+ },
874932 "pod namespace mode with several mappings" : {
875933 userNS : & runtime.UserNamespace {
876934 Mode : runtime .NamespaceMode_POD ,
@@ -892,14 +950,23 @@ func TestUserNamespace(t *testing.T) {
892950 test := test
893951 t .Run (desc , func (t * testing.T ) {
894952 containerConfig .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {UsernsOptions : test .userNS }
953+ // By default, set sandbox and container config to the same (this is
954+ // required by containerSpec). However, if the test wants to test for what
955+ // happens when they don't match, the test.sandboxUserNS should be set and
956+ // we just use that.
957+ sandboxUserns := test .userNS
958+ if test .sandboxUserNS != nil {
959+ sandboxUserns = test .sandboxUserNS
960+ }
961+ sandboxConfig .Linux .SecurityContext .NamespaceOptions = & runtime.NamespaceOption {UsernsOptions : sandboxUserns }
895962 spec , err := c .containerSpec (testID , testSandboxID , testPid , "" , testContainerName , testImageName , containerConfig , sandboxConfig , imageConfig , nil , ociRuntime )
896963
897964 if test .err {
898- assert .Error (t , err )
965+ require .Error (t , err )
899966 assert .Nil (t , spec )
900967 return
901968 }
902- assert .NoError (t , err )
969+ require .NoError (t , err )
903970 assert .Equal (t , spec .Linux .UIDMappings , test .expUIDMapping )
904971 assert .Equal (t , spec .Linux .GIDMappings , test .expGIDMapping )
905972
0 commit comments