@@ -504,6 +504,37 @@ func TestSharedNetworkStack(t *testing.T) {
504
504
AssertOutContains (testutil .NginxAlpineIndexHTMLSnippet )
505
505
}
506
506
507
+ func TestSharedNetworkWithNone (t * testing.T ) {
508
+ if runtime .GOOS != "linux" {
509
+ t .Skip ("--network=container:<container name|id> only supports linux now" )
510
+ }
511
+ base := testutil .NewBase (t )
512
+
513
+ containerName := testutil .Identifier (t )
514
+ defer base .Cmd ("rm" , "-f" , containerName ).AssertOK ()
515
+ base .Cmd ("run" , "-d" , "--name" , containerName , "--network" , "none" ,
516
+ testutil .NginxAlpineImage ).AssertOK ()
517
+ base .EnsureContainerStarted (containerName )
518
+
519
+ containerNameJoin := testutil .Identifier (t ) + "-network"
520
+ defer base .Cmd ("rm" , "-f" , containerNameJoin ).AssertOK ()
521
+ base .Cmd ("run" ,
522
+ "-d" ,
523
+ "--name" , containerNameJoin ,
524
+ "--network=container:" + containerName ,
525
+ testutil .CommonImage ,
526
+ "sleep" , "infinity" ).AssertOK ()
527
+
528
+ base .Cmd ("exec" , containerNameJoin , "wget" , "-qO-" , "http://127.0.0.1:80" ).
529
+ AssertOutContains (testutil .NginxAlpineIndexHTMLSnippet )
530
+
531
+ base .Cmd ("restart" , containerName ).AssertOK ()
532
+ base .Cmd ("stop" , "--time=1" , containerNameJoin ).AssertOK ()
533
+ base .Cmd ("start" , containerNameJoin ).AssertOK ()
534
+ base .Cmd ("exec" , containerNameJoin , "wget" , "-qO-" , "http://127.0.0.1:80" ).
535
+ AssertOutContains (testutil .NginxAlpineIndexHTMLSnippet )
536
+ }
537
+
507
538
func TestRunContainerInExistingNetNS (t * testing.T ) {
508
539
if rootlessutil .IsRootless () {
509
540
t .Skip ("Can't create new netns in rootless mode" )
@@ -629,6 +660,8 @@ func TestHostsFileMounts(t *testing.T) {
629
660
"sh" , "-euxc" , "echo >> /etc/hosts" ).AssertOK ()
630
661
base .Cmd ("run" , "--rm" , "-v" , "/etc/hosts:/etc/hosts" , "--network" , "host" , testutil .CommonImage ,
631
662
"sh" , "-euxc" , "head -n -1 /etc/hosts > temp && cat temp > /etc/hosts" ).AssertOK ()
663
+ base .Cmd ("run" , "--rm" , "--network" , "none" , testutil .CommonImage ,
664
+ "sh" , "-euxc" , "echo >> /etc/hosts" ).AssertOK ()
632
665
633
666
base .Cmd ("run" , "--rm" , testutil .CommonImage ,
634
667
"sh" , "-euxc" , "echo >> /etc/resolv.conf" ).AssertOK ()
@@ -641,6 +674,8 @@ func TestHostsFileMounts(t *testing.T) {
641
674
"sh" , "-euxc" , "echo >> /etc/resolv.conf" ).AssertOK ()
642
675
base .Cmd ("run" , "--rm" , "-v" , "/etc/resolv.conf:/etc/resolv.conf" , "--network" , "host" , testutil .CommonImage ,
643
676
"sh" , "-euxc" , "head -n -1 /etc/resolv.conf > temp && cat temp > /etc/resolv.conf" ).AssertOK ()
677
+ base .Cmd ("run" , "--rm" , "--network" , "host" , testutil .CommonImage ,
678
+ "sh" , "-euxc" , "echo >> /etc/resolv.conf" ).AssertOK ()
644
679
}
645
680
646
681
func TestRunContainerWithStaticIP6 (t * testing.T ) {
@@ -712,3 +747,42 @@ func TestRunContainerWithStaticIP6(t *testing.T) {
712
747
})
713
748
}
714
749
}
750
+
751
+ func TestNoneNetworkStaticConfigs (t * testing.T ) {
752
+ testutil .DockerIncompatible (t )
753
+ base := testutil .NewBase (t )
754
+
755
+ cmd := base .Cmd ("run" , "--rm" , "--net" , "none" , testutil .CommonImage , "cat" , "/etc/hosts" )
756
+ cmd .AssertOutContains ("127.0.0.1 localhost" )
757
+ cmd .AssertOutContains ("::1 localhost" )
758
+
759
+ cmd = base .Cmd ("run" , "--rm" , "--net" , "none" , testutil .CommonImage , "cat" , "/etc/resolv.conf" )
760
+ cmd .AssertOutContains ("nameserver 127.0.0.1" )
761
+
762
+ // If running on Linux, verify /etc/hostname is correctly set
763
+ if runtime .GOOS == "linux" {
764
+ containerHostName := "testcontainer"
765
+ cmd := base .Cmd ("run" , "--rm" , "--net" , "none" , "--hostname" , containerHostName , testutil .CommonImage , "cat" , "/etc/hostname" )
766
+ output := cmd .Run ().Combined ()
767
+ hostname := strings .TrimSpace (output )
768
+
769
+ if len (containerHostName ) > 12 {
770
+ assert .Equal (t , containerHostName [:12 ], hostname [:12 ])
771
+ } else {
772
+ assert .Equal (t , containerHostName , hostname [:12 ])
773
+ }
774
+
775
+ containerName := "testNoneNetworkHostname"
776
+ defer base .Cmd ("rm" , "-f" , containerName ).AssertOK ()
777
+ cmd = base .Cmd ("run" , "-d" , "--net" , "none" , "--name" , containerName , testutil .CommonImage , "sleep" , "infinity" )
778
+ output = cmd .Run ().Combined ()
779
+ containerIDShort := strings .TrimSpace (output )[:12 ]
780
+
781
+ cmd = base .Cmd ("exec" , containerName , "cat" , "/etc/hostname" )
782
+ output = cmd .Run ().Combined ()
783
+ containerHostName = strings .TrimSpace (output )
784
+
785
+ assert .Equal (t , containerHostName , containerIDShort )
786
+
787
+ }
788
+ }
0 commit comments