@@ -176,7 +176,7 @@ func TestGeneralContainerSpec(t *testing.T) {
176176 testPid := uint32 (1234 )
177177 config , sandboxConfig , imageConfig , specCheck := getStartContainerTestData ()
178178 c := newTestCRIContainerdService ()
179- spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig )
179+ spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig , nil )
180180 assert .NoError (t , err )
181181 specCheck (t , testID , testPid , spec )
182182}
@@ -188,7 +188,7 @@ func TestContainerSpecTty(t *testing.T) {
188188 c := newTestCRIContainerdService ()
189189 for _ , tty := range []bool {true , false } {
190190 config .Tty = tty
191- spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig )
191+ spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig , nil )
192192 assert .NoError (t , err )
193193 specCheck (t , testID , testPid , spec )
194194 assert .Equal (t , tty , spec .Process .Terminal )
@@ -202,13 +202,46 @@ func TestContainerSpecReadonlyRootfs(t *testing.T) {
202202 c := newTestCRIContainerdService ()
203203 for _ , readonly := range []bool {true , false } {
204204 config .Linux .SecurityContext .ReadonlyRootfs = readonly
205- spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig )
205+ spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig , nil )
206206 assert .NoError (t , err )
207207 specCheck (t , testID , testPid , spec )
208208 assert .Equal (t , readonly , spec .Root .Readonly )
209209 }
210210}
211211
212+ func TestContainerSpecWithExtraMounts (t * testing.T ) {
213+ testID := "test-id"
214+ testPid := uint32 (1234 )
215+ config , sandboxConfig , imageConfig , specCheck := getStartContainerTestData ()
216+ c := newTestCRIContainerdService ()
217+ mountInConfig := & runtime.Mount {
218+ ContainerPath : "test-container-path" ,
219+ HostPath : "test-host-path" ,
220+ Readonly : false ,
221+ }
222+ config .Mounts = append (config .Mounts , mountInConfig )
223+ extraMount := & runtime.Mount {
224+ ContainerPath : "test-container-path" ,
225+ HostPath : "test-host-path-extra" ,
226+ Readonly : true ,
227+ }
228+ spec , err := c .generateContainerSpec (testID , testPid , config , sandboxConfig , imageConfig , []* runtime.Mount {extraMount })
229+ assert .NoError (t , err )
230+ specCheck (t , testID , testPid , spec )
231+ var mounts []runtimespec.Mount
232+ for _ , m := range spec .Mounts {
233+ if m .Destination == "test-container-path" {
234+ mounts = append (mounts , m )
235+ }
236+ }
237+ t .Logf ("Extra mounts should come first" )
238+ require .Len (t , mounts , 2 )
239+ assert .Equal (t , "test-host-path-extra" , mounts [0 ].Source )
240+ assert .Contains (t , mounts [0 ].Options , "ro" )
241+ assert .Equal (t , "test-host-path" , mounts [1 ].Source )
242+ assert .Contains (t , mounts [1 ].Options , "rw" )
243+ }
244+
212245func TestContainerSpecCommand (t * testing.T ) {
213246 for desc , test := range map [string ]struct {
214247 criEntrypoint []string
@@ -270,6 +303,46 @@ func TestContainerSpecCommand(t *testing.T) {
270303 }
271304}
272305
306+ func TestGenerateContainerMounts (t * testing.T ) {
307+ testSandboxRootDir := "test-sandbox-root"
308+ for desc , test := range map [string ]struct {
309+ securityContext * runtime.LinuxContainerSecurityContext
310+ expectedMounts []* runtime.Mount
311+ }{
312+ "should setup ro /etc/hosts mount when rootfs is read-only" : {
313+ securityContext : & runtime.LinuxContainerSecurityContext {
314+ ReadonlyRootfs : true ,
315+ },
316+ expectedMounts : []* runtime.Mount {{
317+ ContainerPath : "/etc/hosts" ,
318+ HostPath : testSandboxRootDir + "/hosts" ,
319+ Readonly : true ,
320+ }},
321+ },
322+ "should setup rw /etc/hosts mount when rootfs is read-write" : {
323+ securityContext : & runtime.LinuxContainerSecurityContext {},
324+ expectedMounts : []* runtime.Mount {{
325+ ContainerPath : "/etc/hosts" ,
326+ HostPath : testSandboxRootDir + "/hosts" ,
327+ Readonly : false ,
328+ }},
329+ },
330+ } {
331+ config := & runtime.ContainerConfig {
332+ Metadata : & runtime.ContainerMetadata {
333+ Name : "test-name" ,
334+ Attempt : 1 ,
335+ },
336+ Linux : & runtime.LinuxContainerConfig {
337+ SecurityContext : test .securityContext ,
338+ },
339+ }
340+ c := newTestCRIContainerdService ()
341+ mounts := c .generateContainerMounts (testSandboxRootDir , config )
342+ assert .Equal (t , test .expectedMounts , mounts , desc )
343+ }
344+ }
345+
273346func TestStartContainer (t * testing.T ) {
274347 testID := "test-id"
275348 testSandboxID := "test-sandbox-id"
0 commit comments