@@ -29,6 +29,7 @@ import (
29
29
"time"
30
30
31
31
"github.com/containernetworking/plugins/pkg/ns"
32
+ "github.com/stretchr/testify/require"
32
33
"github.com/vishvananda/netlink"
33
34
"gotest.tools/v3/assert"
34
35
"gotest.tools/v3/icmd"
@@ -504,6 +505,37 @@ func TestSharedNetworkStack(t *testing.T) {
504
505
AssertOutContains (testutil .NginxAlpineIndexHTMLSnippet )
505
506
}
506
507
508
+ func TestSharedNetworkWithNone (t * testing.T ) {
509
+ if runtime .GOOS != "linux" {
510
+ t .Skip ("--network=container:<container name|id> only supports linux now" )
511
+ }
512
+ base := testutil .NewBase (t )
513
+
514
+ containerName := testutil .Identifier (t )
515
+ defer base .Cmd ("rm" , "-f" , containerName ).AssertOK ()
516
+ base .Cmd ("run" , "-d" , "--name" , containerName , "--network" , "none" ,
517
+ testutil .NginxAlpineImage ).AssertOK ()
518
+ base .EnsureContainerStarted (containerName )
519
+
520
+ containerNameJoin := testutil .Identifier (t ) + "-network"
521
+ defer base .Cmd ("rm" , "-f" , containerNameJoin ).AssertOK ()
522
+ base .Cmd ("run" ,
523
+ "-d" ,
524
+ "--name" , containerNameJoin ,
525
+ "--network=container:" + containerName ,
526
+ testutil .CommonImage ,
527
+ "sleep" , "infinity" ).AssertOK ()
528
+
529
+ base .Cmd ("exec" , containerNameJoin , "wget" , "-qO-" , "http://127.0.0.1:80" ).
530
+ AssertOutContains (testutil .NginxAlpineIndexHTMLSnippet )
531
+
532
+ base .Cmd ("restart" , containerName ).AssertOK ()
533
+ base .Cmd ("stop" , "--time=1" , containerNameJoin ).AssertOK ()
534
+ base .Cmd ("start" , containerNameJoin ).AssertOK ()
535
+ base .Cmd ("exec" , containerNameJoin , "wget" , "-qO-" , "http://127.0.0.1:80" ).
536
+ AssertOutContains (testutil .NginxAlpineIndexHTMLSnippet )
537
+ }
538
+
507
539
func TestRunContainerInExistingNetNS (t * testing.T ) {
508
540
if rootlessutil .IsRootless () {
509
541
t .Skip ("Can't create new netns in rootless mode" )
@@ -629,6 +661,8 @@ func TestHostsFileMounts(t *testing.T) {
629
661
"sh" , "-euxc" , "echo >> /etc/hosts" ).AssertOK ()
630
662
base .Cmd ("run" , "--rm" , "-v" , "/etc/hosts:/etc/hosts" , "--network" , "host" , testutil .CommonImage ,
631
663
"sh" , "-euxc" , "head -n -1 /etc/hosts > temp && cat temp > /etc/hosts" ).AssertOK ()
664
+ base .Cmd ("run" , "--rm" , "--network" , "none" , testutil .CommonImage ,
665
+ "sh" , "-euxc" , "echo >> /etc/hosts" ).AssertOK ()
632
666
633
667
base .Cmd ("run" , "--rm" , testutil .CommonImage ,
634
668
"sh" , "-euxc" , "echo >> /etc/resolv.conf" ).AssertOK ()
@@ -641,6 +675,8 @@ func TestHostsFileMounts(t *testing.T) {
641
675
"sh" , "-euxc" , "echo >> /etc/resolv.conf" ).AssertOK ()
642
676
base .Cmd ("run" , "--rm" , "-v" , "/etc/resolv.conf:/etc/resolv.conf" , "--network" , "host" , testutil .CommonImage ,
643
677
"sh" , "-euxc" , "head -n -1 /etc/resolv.conf > temp && cat temp > /etc/resolv.conf" ).AssertOK ()
678
+ base .Cmd ("run" , "--rm" , "--network" , "host" , testutil .CommonImage ,
679
+ "sh" , "-euxc" , "echo >> /etc/resolv.conf" ).AssertOK ()
644
680
}
645
681
646
682
func TestRunContainerWithStaticIP6 (t * testing.T ) {
@@ -712,3 +748,41 @@ func TestRunContainerWithStaticIP6(t *testing.T) {
712
748
})
713
749
}
714
750
}
751
+
752
+ func TestNoneNetworkStaticConfigs (t * testing.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
+ require .Equal (t , containerHostName [:12 ], hostname )
771
+ } else {
772
+ require .Equal (t , containerHostName , hostname )
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
+ require .Equal (t , containerHostName , containerIdShort )
786
+
787
+ }
788
+ }
0 commit comments