@@ -98,6 +98,17 @@ func getRunPodSandboxTestData() (*runtime.PodSandboxConfig, *imagespec.ImageConf
9898func TestLinuxSandboxContainerSpec (t * testing.T ) {
9999 testID := "test-id"
100100 nsPath := "test-cni"
101+ idMap := runtime.IDMapping {
102+ HostId : 1000 ,
103+ ContainerId : 1000 ,
104+ Length : 10 ,
105+ }
106+ expIDMap := runtimespec.LinuxIDMapping {
107+ HostID : 1000 ,
108+ ContainerID : 1000 ,
109+ Size : 10 ,
110+ }
111+
101112 for desc , test := range map [string ]struct {
102113 configChange func (* runtime.PodSandboxConfig )
103114 specCheck func (* testing.T , * runtimespec.Spec )
@@ -122,6 +133,9 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
122133 })
123134 assert .Contains (t , spec .Linux .Sysctl ["net.ipv4.ip_unprivileged_port_start" ], "0" )
124135 assert .Contains (t , spec .Linux .Sysctl ["net.ipv4.ping_group_range" ], "0 2147483647" )
136+ assert .NotContains (t , spec .Linux .Namespaces , runtimespec.LinuxNamespace {
137+ Type : runtimespec .UserNamespace ,
138+ })
125139 },
126140 },
127141 "host namespace" : {
@@ -149,10 +163,113 @@ func TestLinuxSandboxContainerSpec(t *testing.T) {
149163 assert .NotContains (t , spec .Linux .Namespaces , runtimespec.LinuxNamespace {
150164 Type : runtimespec .IPCNamespace ,
151165 })
166+ assert .NotContains (t , spec .Linux .Namespaces , runtimespec.LinuxNamespace {
167+ Type : runtimespec .UserNamespace ,
168+ })
152169 assert .NotContains (t , spec .Linux .Sysctl ["net.ipv4.ip_unprivileged_port_start" ], "0" )
153170 assert .NotContains (t , spec .Linux .Sysctl ["net.ipv4.ping_group_range" ], "0 2147483647" )
154171 },
155172 },
173+ "user namespace" : {
174+ configChange : func (c * runtime.PodSandboxConfig ) {
175+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
176+ NamespaceOptions : & runtime.NamespaceOption {
177+ UsernsOptions : & runtime.UserNamespace {
178+ Mode : runtime .NamespaceMode_POD ,
179+ Uids : []* runtime.IDMapping {& idMap },
180+ Gids : []* runtime.IDMapping {& idMap },
181+ },
182+ },
183+ }
184+ },
185+ specCheck : func (t * testing.T , spec * runtimespec.Spec ) {
186+ require .NotNil (t , spec .Linux )
187+ assert .Contains (t , spec .Linux .Namespaces , runtimespec.LinuxNamespace {
188+ Type : runtimespec .UserNamespace ,
189+ })
190+ require .Equal (t , spec .Linux .UIDMappings , []runtimespec.LinuxIDMapping {expIDMap })
191+ require .Equal (t , spec .Linux .GIDMappings , []runtimespec.LinuxIDMapping {expIDMap })
192+
193+ },
194+ },
195+ "user namespace mode node and mappings" : {
196+ configChange : func (c * runtime.PodSandboxConfig ) {
197+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
198+ NamespaceOptions : & runtime.NamespaceOption {
199+ UsernsOptions : & runtime.UserNamespace {
200+ Mode : runtime .NamespaceMode_NODE ,
201+ Uids : []* runtime.IDMapping {& idMap },
202+ Gids : []* runtime.IDMapping {& idMap },
203+ },
204+ },
205+ }
206+ },
207+ expectErr : true ,
208+ },
209+ "user namespace with several mappings" : {
210+ configChange : func (c * runtime.PodSandboxConfig ) {
211+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
212+ NamespaceOptions : & runtime.NamespaceOption {
213+ UsernsOptions : & runtime.UserNamespace {
214+ Mode : runtime .NamespaceMode_NODE ,
215+ Uids : []* runtime.IDMapping {& idMap , & idMap },
216+ Gids : []* runtime.IDMapping {& idMap , & idMap },
217+ },
218+ },
219+ }
220+ },
221+ expectErr : true ,
222+ },
223+ "user namespace with uneven mappings" : {
224+ configChange : func (c * runtime.PodSandboxConfig ) {
225+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
226+ NamespaceOptions : & runtime.NamespaceOption {
227+ UsernsOptions : & runtime.UserNamespace {
228+ Mode : runtime .NamespaceMode_NODE ,
229+ Uids : []* runtime.IDMapping {& idMap , & idMap },
230+ Gids : []* runtime.IDMapping {& idMap },
231+ },
232+ },
233+ }
234+ },
235+ expectErr : true ,
236+ },
237+ "user namespace mode container" : {
238+ configChange : func (c * runtime.PodSandboxConfig ) {
239+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
240+ NamespaceOptions : & runtime.NamespaceOption {
241+ UsernsOptions : & runtime.UserNamespace {
242+ Mode : runtime .NamespaceMode_CONTAINER ,
243+ },
244+ },
245+ }
246+ },
247+ expectErr : true ,
248+ },
249+ "user namespace mode target" : {
250+ configChange : func (c * runtime.PodSandboxConfig ) {
251+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
252+ NamespaceOptions : & runtime.NamespaceOption {
253+ UsernsOptions : & runtime.UserNamespace {
254+ Mode : runtime .NamespaceMode_TARGET ,
255+ },
256+ },
257+ }
258+ },
259+ expectErr : true ,
260+ },
261+ "user namespace unknown mode" : {
262+ configChange : func (c * runtime.PodSandboxConfig ) {
263+ c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
264+ NamespaceOptions : & runtime.NamespaceOption {
265+ UsernsOptions : & runtime.UserNamespace {
266+ Mode : runtime .NamespaceMode (100 ),
267+ },
268+ },
269+ }
270+ },
271+ expectErr : true ,
272+ },
156273 "should set supplemental groups correctly" : {
157274 configChange : func (c * runtime.PodSandboxConfig ) {
158275 c .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
0 commit comments