@@ -405,50 +405,98 @@ func dockerInGvisorCapabilities() []string {
405
405
}
406
406
407
407
func TestDockerOverlayWithHostNetwork (t * testing.T ) {
408
- testDocker (t , true , true , false )
408
+ if testutil .IsRunningWithHostNet () {
409
+ t .Skip ("docker doesn't work with hostinet" )
410
+ }
411
+ ctx := context .Background ()
412
+ d := startDockerdInGvisor (ctx , t , true )
413
+ defer d .CleanUp (ctx )
414
+ testDockerBuild (ctx , t , d , true )
415
+ testDockerRun (ctx , t , d , true , false )
409
416
}
410
417
411
418
func TestPrivilegedDockerOverlayWithHostNetwork (t * testing.T ) {
412
- testDocker (t , true , true , true )
419
+ if testutil .IsRunningWithHostNet () {
420
+ t .Skip ("docker doesn't work with hostinet" )
421
+ }
422
+ ctx := context .Background ()
423
+ d := startDockerdInGvisor (ctx , t , true )
424
+ defer d .CleanUp (ctx )
425
+ testDockerRun (ctx , t , d , true , true )
413
426
}
414
427
415
428
func TestDockerOverlay (t * testing.T ) {
416
- testDocker (t , true , false , false )
429
+ if testutil .IsRunningWithHostNet () {
430
+ t .Skip ("docker doesn't work with hostinet" )
431
+ }
432
+ ctx := context .Background ()
433
+ d := startDockerdInGvisor (ctx , t , true )
434
+ defer d .CleanUp (ctx )
435
+ testDockerBuild (ctx , t , d , false )
436
+ testDockerRun (ctx , t , d , false , false )
417
437
}
418
438
419
439
func TestPrivilegedDockerOverlay (t * testing.T ) {
420
- testDocker (t , true , false , true )
440
+ if testutil .IsRunningWithHostNet () {
441
+ t .Skip ("docker doesn't work with hostinet" )
442
+ }
443
+ ctx := context .Background ()
444
+ d := startDockerdInGvisor (ctx , t , true )
445
+ defer d .CleanUp (ctx )
446
+ testDockerRun (ctx , t , d , false , true )
421
447
}
422
448
423
449
func TestDockerWithHostNetwork (t * testing.T ) {
424
- testDocker (t , false , true , false )
450
+ if testutil .IsRunningWithHostNet () {
451
+ t .Skip ("docker doesn't work with hostinet" )
452
+ }
453
+ ctx := context .Background ()
454
+ d := startDockerdInGvisor (ctx , t , false )
455
+ defer d .CleanUp (ctx )
456
+ testDockerBuild (ctx , t , d , true )
457
+ testDockerRun (ctx , t , d , true , false )
425
458
}
426
459
427
460
func TestPrivilegedDockerWithHostNetwork (t * testing.T ) {
428
- testDocker (t , false , true , true )
461
+ if testutil .IsRunningWithHostNet () {
462
+ t .Skip ("docker doesn't work with hostinet" )
463
+ }
464
+ ctx := context .Background ()
465
+ d := startDockerdInGvisor (ctx , t , false )
466
+ defer d .CleanUp (ctx )
467
+ testDockerRun (ctx , t , d , true , true )
429
468
}
430
469
431
470
func TestDocker (t * testing.T ) {
471
+ if testutil .IsRunningWithHostNet () {
472
+ t .Skip ("docker doesn't work with hostinet" )
473
+ }
474
+ ctx := context .Background ()
475
+ d := startDockerdInGvisor (ctx , t , false )
476
+ defer d .CleanUp (ctx )
477
+ testDockerBuild (ctx , t , d , false )
432
478
// Overlayfs can't be built on top of another overlayfs, so docket has
433
479
// to fall back to the vfs driver.
434
- testDocker ( t , false , false , false )
480
+ testDockerRun ( ctx , t , d , false , false )
435
481
}
436
482
437
483
func TestPrivilegedDocker (t * testing.T ) {
438
- // Overlayfs can't be built on top of another overlayfs, so docket has
439
- // to fall back to the vfs driver.
440
- testDocker (t , false , false , true )
441
- }
442
-
443
- func testDocker (t * testing.T , overlay , hostNetwork , startPrivilegedContainer bool ) {
444
484
if testutil .IsRunningWithHostNet () {
445
485
t .Skip ("docker doesn't work with hostinet" )
446
486
}
447
487
ctx := context .Background ()
448
- d := dockerutil . MakeContainerWithRuntime (ctx , t , "-docker" )
488
+ d := startDockerdInGvisor (ctx , t , true )
449
489
defer d .CleanUp (ctx )
490
+ // Overlayfs can't be built on top of another overlayfs, so docket has
491
+ // to fall back to the vfs driver.
492
+ testDockerRun (ctx , t , d , false , true )
493
+ }
450
494
451
- // Start the container.
495
+ // The container returned by this function has to be cleaned up by the caller.
496
+ func startDockerdInGvisor (ctx context.Context , t * testing.T , overlay bool ) * dockerutil.Container {
497
+ d := dockerutil .MakeContainerWithRuntime (ctx , t , "-docker" )
498
+
499
+ // Start the container which starts dockerd.
452
500
opts := dockerutil.RunOpts {
453
501
Image : "basic/docker" ,
454
502
CapAdd : dockerInGvisorCapabilities (),
@@ -477,27 +525,63 @@ func testDocker(t *testing.T, overlay, hostNetwork, startPrivilegedContainer boo
477
525
}
478
526
// Wait for the docker daemon.
479
527
for i := 0 ; i < 10 ; i ++ {
480
- output , err := d .Exec (ctx , dockerutil.ExecOpts {}, "docker" , "info" )
481
- t .Logf ("== docker info ==\n %s" , output )
528
+ _ , err := d .Exec (ctx , dockerutil.ExecOpts {}, "docker" , "info" )
482
529
if err != nil {
483
530
t .Logf ("docker exec failed: %v" , err )
484
531
time .Sleep (5 * time .Second )
485
532
continue
486
533
}
487
534
break
488
535
}
536
+ return d
537
+ }
538
+
539
+ func testDockerRun (ctx context.Context , t * testing.T , d * dockerutil.Container , hostNetwork , startPrivilegedContainer bool ) {
489
540
cmd := []string {"docker" , "run" , "--rm" }
490
541
if hostNetwork {
491
542
cmd = append (cmd , "--network" , "host" )
492
543
}
493
544
if startPrivilegedContainer {
494
545
cmd = append (cmd , "--privileged" )
495
546
}
496
- cmd = append (cmd , "alpine" , "sh" , "-c" , "apk add curl && curl -h" )
547
+ cmd = append (cmd , "alpine" , "sh" , "-c" , "apk add curl && apk info -d curl" )
548
+ execProc , err := d .ExecProcess (ctx , dockerutil.ExecOpts {}, cmd ... )
549
+ if err != nil {
550
+ t .Fatalf ("docker exec failed: %v" , err )
551
+ }
552
+ output , err := execProc .Logs ()
553
+ if err != nil {
554
+ t .Fatalf ("docker logs failed: %v" , err )
555
+ }
556
+ expectedOutput := "URL retrival utility and library"
557
+ if ! strings .Contains (output , expectedOutput ) {
558
+ t .Fatalf ("docker didn't get output expected: %q, got: %q" , expectedOutput , output )
559
+ }
560
+ }
561
+
562
+ func testDockerBuild (ctx context.Context , t * testing.T , d * dockerutil.Container , hostNetwork bool ) {
563
+ cmd := []string {"echo" , "-e" , "FROM alpine:3.19\n RUN apk add git" , "|" , "docker" , "build" }
564
+ if hostNetwork {
565
+ cmd = append (cmd , "--network" , "host" )
566
+ }
567
+ imageName := "test_docker_build_in_gvisor"
568
+ cmd = append (cmd , "-t" , imageName , "-f" , "-" , "." )
497
569
_ , err := d .ExecProcess (ctx , dockerutil.ExecOpts {}, cmd ... )
498
570
if err != nil {
499
571
t .Fatalf ("docker exec failed: %v" , err )
500
572
}
573
+ inspectImage , err := d .ExecProcess (ctx , dockerutil.ExecOpts {}, []string {"docker" , "image" , "inspect" , imageName }... )
574
+ if err != nil {
575
+ t .Fatalf ("docker exec failed: %v" , err )
576
+ }
577
+ got , err := inspectImage .Logs ()
578
+ if err != nil {
579
+ t .Fatalf ("docker logs failed: %v" , err )
580
+ }
581
+ output := imageName + ":latest"
582
+ if ! strings .Contains (got , output ) {
583
+ t .Fatalf ("docker didn't get output expected: %q, got: %q" , output , got )
584
+ }
501
585
}
502
586
503
587
func TestMain (m * testing.M ) {
0 commit comments