@@ -18,14 +18,17 @@ package container
18
18
19
19
import (
20
20
"fmt"
21
+ "os"
21
22
"strings"
22
23
"testing"
23
24
24
25
"github.com/docker/go-connections/nat"
25
26
"gotest.tools/v3/assert"
26
27
28
+ "github.com/containerd/nerdctl/v2/pkg/infoutil"
27
29
"github.com/containerd/nerdctl/v2/pkg/inspecttypes/dockercompat"
28
30
"github.com/containerd/nerdctl/v2/pkg/labels"
31
+ "github.com/containerd/nerdctl/v2/pkg/rootlessutil"
29
32
"github.com/containerd/nerdctl/v2/pkg/testutil"
30
33
)
31
34
@@ -68,13 +71,12 @@ func TestContainerInspectContainsMounts(t *testing.T) {
68
71
testutil .NginxAlpineImage ).AssertOK ()
69
72
70
73
inspect := base .InspectContainer (testContainer )
71
-
72
74
// convert array to map to get by key of Destination
73
75
actual := make (map [string ]dockercompat.MountPoint )
74
76
for i := range inspect .Mounts {
75
77
actual [inspect .Mounts [i ].Destination ] = inspect .Mounts [i ]
76
78
}
77
-
79
+ t . Logf ( "actual in TestContainerInspectContainsMounts: %+v" , actual )
78
80
const localDriver = "local"
79
81
80
82
expected := []struct {
@@ -232,6 +234,9 @@ func TestContainerInspectState(t *testing.T) {
232
234
233
235
func TestContainerInspectHostConfig (t * testing.T ) {
234
236
testContainer := testutil .Identifier (t )
237
+ if rootlessutil .IsRootless () && infoutil .CgroupsVersion () == "1" {
238
+ t .Skip ("test skipped for rootless containers on cgroup v1" )
239
+ }
235
240
236
241
base := testutil .NewBase (t )
237
242
defer base .Cmd ("rm" , "-f" , testContainer ).Run ()
@@ -249,13 +254,11 @@ func TestContainerInspectHostConfig(t *testing.T) {
249
254
"--add-host" , "host2:10.0.0.2" ,
250
255
"--ipc" , "host" ,
251
256
"--memory" , "512m" ,
252
- "--oom-kill-disable" ,
253
257
"--read-only" ,
254
- "--uts" , "host" ,
255
258
"--shm-size" , "256m" ,
256
- "--runtime " , "io.containerd.runtime.v1.linux " ,
259
+ "--uts " , "host " ,
257
260
"--sysctl" , "net.core.somaxconn=1024" ,
258
- "--device " , "/dev/null:/dev/null " ,
261
+ "--runtime " , "io.containerd.runc.v2 " ,
259
262
testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
260
263
261
264
inspect := base .InspectContainer (testContainer )
@@ -265,24 +268,16 @@ func TestContainerInspectHostConfig(t *testing.T) {
265
268
assert .Equal (t , uint16 (500 ), inspect .HostConfig .BlkioWeight )
266
269
assert .Equal (t , uint64 (1024 ), inspect .HostConfig .CPUShares )
267
270
assert .Equal (t , int64 (100000 ), inspect .HostConfig .CPUQuota )
268
- assert .DeepEqual (t , []string {"1000" , "2000" }, inspect .HostConfig .GroupAdd )
271
+ assert .Assert (t , contains (inspect .HostConfig .GroupAdd , "1000" ), "Expected '1000' to be in GroupAdd" )
272
+ assert .Assert (t , contains (inspect .HostConfig .GroupAdd , "2000" ), "Expected '2000' to be in GroupAdd" )
269
273
expectedExtraHosts := []string {"host1:10.0.0.1" , "host2:10.0.0.2" }
270
274
assert .DeepEqual (t , expectedExtraHosts , inspect .HostConfig .ExtraHosts )
271
275
assert .Equal (t , "host" , inspect .HostConfig .IpcMode )
272
- assert .Equal (t , "json-file" , inspect .HostConfig .LogConfig .Driver )
273
276
assert .Equal (t , int64 (536870912 ), inspect .HostConfig .Memory )
274
277
assert .Equal (t , int64 (1073741824 ), inspect .HostConfig .MemorySwap )
275
- assert .Equal (t , bool (true ), inspect .HostConfig .OomKillDisable )
276
278
assert .Equal (t , true , inspect .HostConfig .ReadonlyRootfs )
277
279
assert .Equal (t , "host" , inspect .HostConfig .UTSMode )
278
280
assert .Equal (t , int64 (268435456 ), inspect .HostConfig .ShmSize )
279
- assert .Equal (t , "io.containerd.runtime.v1.linux" , inspect .HostConfig .Runtime )
280
- expectedSysctls := map [string ]string {
281
- "net.core.somaxconn" : "1024" ,
282
- }
283
- assert .DeepEqual (t , expectedSysctls , inspect .HostConfig .Sysctls )
284
- expectedDevices := []string {"/dev/null:/dev/null" }
285
- assert .DeepEqual (t , expectedDevices , inspect .HostConfig .Devices )
286
281
}
287
282
288
283
func TestContainerInspectHostConfigDefaults (t * testing.T ) {
@@ -291,26 +286,41 @@ func TestContainerInspectHostConfigDefaults(t *testing.T) {
291
286
base := testutil .NewBase (t )
292
287
defer base .Cmd ("rm" , "-f" , testContainer ).Run ()
293
288
289
+ var hc hostConfigValues
290
+
291
+ if testutil .GetTarget () == testutil .Docker {
292
+ hc .Driver = ""
293
+ hc .GroupAddSize = 0
294
+ hc .ShmSize = int64 (67108864 )
295
+ hc .Runtime = "runc"
296
+ } else {
297
+ hc .GroupAddSize = 10
298
+ hc .Driver = "json-file"
299
+ hc .ShmSize = int64 (0 )
300
+ hc .Runtime = "io.containerd.runc.v2"
301
+ }
302
+
294
303
// Run a container without specifying HostConfig options
295
304
base .Cmd ("run" , "-d" , "--name" , testContainer , testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
296
305
297
306
inspect := base .InspectContainer (testContainer )
307
+ t .Logf ("HostConfig in TestContainerInspectHostConfigDefaults: %+v" , inspect .HostConfig )
298
308
assert .Equal (t , "" , inspect .HostConfig .CPUSetCPUs )
299
309
assert .Equal (t , "" , inspect .HostConfig .CPUSetMems )
300
310
assert .Equal (t , uint16 (0 ), inspect .HostConfig .BlkioWeight )
301
311
assert .Equal (t , uint64 (0 ), inspect .HostConfig .CPUShares )
302
312
assert .Equal (t , int64 (0 ), inspect .HostConfig .CPUQuota )
303
- assert .Equal (t , 0 , len (inspect .HostConfig .GroupAdd ))
313
+ assert .Equal (t , hc . GroupAddSize , len (inspect .HostConfig .GroupAdd ))
304
314
assert .Equal (t , 0 , len (inspect .HostConfig .ExtraHosts ))
305
- assert .Equal (t , "" , inspect .HostConfig .IpcMode )
306
- assert .Equal (t , "json-file" , inspect .HostConfig .LogConfig .Driver )
315
+ assert .Equal (t , "private " , inspect .HostConfig .IpcMode )
316
+ assert .Equal (t , hc . Driver , inspect .HostConfig .LogConfig .Driver )
307
317
assert .Equal (t , int64 (0 ), inspect .HostConfig .Memory )
308
318
assert .Equal (t , int64 (0 ), inspect .HostConfig .MemorySwap )
309
319
assert .Equal (t , bool (false ), inspect .HostConfig .OomKillDisable )
310
- assert .Equal (t , false , inspect .HostConfig .ReadonlyRootfs )
320
+ assert .Equal (t , bool ( false ) , inspect .HostConfig .ReadonlyRootfs )
311
321
assert .Equal (t , "" , inspect .HostConfig .UTSMode )
312
- assert .Equal (t , int64 ( 67108864 ) , inspect .HostConfig .ShmSize )
313
- assert .Equal (t , "io.containerd.runc.v2" , inspect .HostConfig .Runtime )
322
+ assert .Equal (t , hc . ShmSize , inspect .HostConfig .ShmSize )
323
+ assert .Equal (t , hc . Runtime , inspect .HostConfig .Runtime )
314
324
assert .Equal (t , 0 , len (inspect .HostConfig .Sysctls ))
315
325
assert .Equal (t , 0 , len (inspect .HostConfig .Devices ))
316
326
}
@@ -364,23 +374,32 @@ func TestContainerInspectHostConfigDNSDefaults(t *testing.T) {
364
374
}
365
375
366
376
func TestContainerInspectHostConfigPID (t * testing.T ) {
367
- testContainer1 := testutil .Identifier (t )
368
- testContainer2 := testutil .Identifier (t )
377
+ testContainer1 := testutil .Identifier (t ) + "-container1"
378
+ testContainer2 := testutil .Identifier (t ) + "-container2"
369
379
370
380
base := testutil .NewBase (t )
371
381
defer base .Cmd ("rm" , "-f" , testContainer1 , testContainer2 ).Run ()
372
382
373
383
// Run the first container
374
384
base .Cmd ("run" , "-d" , "--name" , testContainer1 , testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
375
385
376
- // Run a container with PID namespace options
386
+ containerID1 := strings .TrimSpace (base .Cmd ("inspect" , "-f" , "{{.Id}}" , testContainer1 ).Out ())
387
+
388
+ var hc hostConfigValues
389
+
390
+ if testutil .GetTarget () == testutil .Docker {
391
+ hc .PidMode = "container:" + containerID1
392
+ } else {
393
+ hc .PidMode = containerID1
394
+ }
395
+
377
396
base .Cmd ("run" , "-d" , "--name" , testContainer2 ,
378
397
"--pid" , fmt .Sprintf ("container:%s" , testContainer1 ),
379
398
testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
380
399
381
400
inspect := base .InspectContainer (testContainer2 )
382
401
383
- assert .Equal (t , fmt . Sprintf ( "container:%s" , testContainer1 ) , inspect .HostConfig .PidMode )
402
+ assert .Equal (t , hc . PidMode , inspect .HostConfig .PidMode )
384
403
385
404
}
386
405
@@ -390,11 +409,59 @@ func TestContainerInspectHostConfigPIDDefaults(t *testing.T) {
390
409
base := testutil .NewBase (t )
391
410
defer base .Cmd ("rm" , "-f" , testContainer ).Run ()
392
411
393
- // Run a container without specifying PID options
394
412
base .Cmd ("run" , "-d" , "--name" , testContainer , testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
395
413
396
414
inspect := base .InspectContainer (testContainer )
397
415
398
- // Check that PID mode is empty (private) by default
399
416
assert .Equal (t , "" , inspect .HostConfig .PidMode )
400
417
}
418
+
419
+ func TestContainerInspectDevices (t * testing.T ) {
420
+ testContainer := testutil .Identifier (t )
421
+
422
+ base := testutil .NewBase (t )
423
+ defer base .Cmd ("rm" , "-f" , testContainer ).Run ()
424
+
425
+ // Create a temporary directory
426
+ dir , err := os .MkdirTemp (t .TempDir (), "device-dir" )
427
+ if err != nil {
428
+ t .Fatal (err )
429
+ }
430
+
431
+ if testutil .GetTarget () == testutil .Docker {
432
+ dir = "/dev/zero"
433
+ }
434
+
435
+ // Run the container with the directory mapped as a device
436
+ base .Cmd ("run" , "-d" , "--name" , testContainer ,
437
+ "--device" , dir + ":/dev/xvda" ,
438
+ testutil .AlpineImage , "sleep" , "infinity" ).AssertOK ()
439
+
440
+ inspect := base .InspectContainer (testContainer )
441
+
442
+ expectedDevices := []dockercompat.DeviceMapping {
443
+ {
444
+ PathOnHost : dir ,
445
+ PathInContainer : "/dev/xvda" ,
446
+ CgroupPermissions : "rwm" ,
447
+ },
448
+ }
449
+ assert .DeepEqual (t , expectedDevices , inspect .HostConfig .Devices )
450
+ }
451
+
452
+ func contains (slice []string , item string ) bool {
453
+ for _ , s := range slice {
454
+ if s == item {
455
+ return true
456
+ }
457
+ }
458
+ return false
459
+ }
460
+
461
+ type hostConfigValues struct {
462
+ Driver string
463
+ ShmSize int64
464
+ PidMode string
465
+ GroupAddSize int
466
+ Runtime string
467
+ }
0 commit comments