Skip to content

Commit c64c4f7

Browse files
enable host name and default resolv.conf and hosts file in none network
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
1 parent 49a19ed commit c64c4f7

File tree

5 files changed

+889
-5
lines changed

5 files changed

+889
-5
lines changed

cmd/nerdctl/container/container_run_network_linux_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/containernetworking/plugins/pkg/ns"
32+
"github.com/stretchr/testify/require"
3233
"github.com/vishvananda/netlink"
3334
"gotest.tools/v3/assert"
3435
"gotest.tools/v3/icmd"
@@ -504,6 +505,37 @@ func TestSharedNetworkStack(t *testing.T) {
504505
AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)
505506
}
506507

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+
507539
func TestRunContainerInExistingNetNS(t *testing.T) {
508540
if rootlessutil.IsRootless() {
509541
t.Skip("Can't create new netns in rootless mode")
@@ -629,6 +661,8 @@ func TestHostsFileMounts(t *testing.T) {
629661
"sh", "-euxc", "echo >> /etc/hosts").AssertOK()
630662
base.Cmd("run", "--rm", "-v", "/etc/hosts:/etc/hosts", "--network", "host", testutil.CommonImage,
631663
"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()
632666

633667
base.Cmd("run", "--rm", testutil.CommonImage,
634668
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
@@ -641,6 +675,8 @@ func TestHostsFileMounts(t *testing.T) {
641675
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
642676
base.Cmd("run", "--rm", "-v", "/etc/resolv.conf:/etc/resolv.conf", "--network", "host", testutil.CommonImage,
643677
"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()
644680
}
645681

646682
func TestRunContainerWithStaticIP6(t *testing.T) {
@@ -712,3 +748,42 @@ func TestRunContainerWithStaticIP6(t *testing.T) {
712748
})
713749
}
714750
}
751+
752+
func TestNoneNetworkStaticConfigs(t *testing.T) {
753+
testutil.DockerIncompatible(t)
754+
base := testutil.NewBase(t)
755+
756+
cmd := base.Cmd("run", "--rm", "--net", "none", testutil.CommonImage, "cat", "/etc/hosts")
757+
cmd.AssertOutContains("127.0.0.1 localhost")
758+
cmd.AssertOutContains("::1 localhost")
759+
760+
cmd = base.Cmd("run", "--rm", "--net", "none", testutil.CommonImage, "cat", "/etc/resolv.conf")
761+
cmd.AssertOutContains("nameserver 127.0.0.1")
762+
763+
// If running on Linux, verify /etc/hostname is correctly set
764+
if runtime.GOOS == "linux" {
765+
containerHostName := "testcontainer"
766+
cmd := base.Cmd("run", "--rm", "--net", "none", "--hostname", containerHostName, testutil.CommonImage, "cat", "/etc/hostname")
767+
output := cmd.Run().Combined()
768+
hostname := strings.TrimSpace(output)
769+
770+
if len(containerHostName) > 12 {
771+
require.Equal(t, containerHostName[:12], hostname[:12])
772+
} else {
773+
require.Equal(t, containerHostName, hostname[:12])
774+
}
775+
776+
containerName := "testNoneNetworkHostname"
777+
defer base.Cmd("rm", "-f", containerName).AssertOK()
778+
cmd = base.Cmd("run", "-d", "--net", "none", "--name", containerName, testutil.CommonImage, "sleep", "infinity")
779+
output = cmd.Run().Combined()
780+
containerIDShort := strings.TrimSpace(output)[:12]
781+
782+
cmd = base.Cmd("exec", containerName, "cat", "/etc/hostname")
783+
output = cmd.Run().Combined()
784+
containerHostName = strings.TrimSpace(output)
785+
786+
require.Equal(t, containerHostName, containerIDShort)
787+
788+
}
789+
}

0 commit comments

Comments
 (0)