Skip to content

Commit cdb4929

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 cdb4929

File tree

5 files changed

+886
-5
lines changed

5 files changed

+886
-5
lines changed

cmd/nerdctl/container/container_run_network_linux_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,37 @@ func TestSharedNetworkStack(t *testing.T) {
504504
AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)
505505
}
506506

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+
507538
func TestRunContainerInExistingNetNS(t *testing.T) {
508539
if rootlessutil.IsRootless() {
509540
t.Skip("Can't create new netns in rootless mode")
@@ -629,6 +660,8 @@ func TestHostsFileMounts(t *testing.T) {
629660
"sh", "-euxc", "echo >> /etc/hosts").AssertOK()
630661
base.Cmd("run", "--rm", "-v", "/etc/hosts:/etc/hosts", "--network", "host", testutil.CommonImage,
631662
"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()
632665

633666
base.Cmd("run", "--rm", testutil.CommonImage,
634667
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
@@ -641,6 +674,8 @@ func TestHostsFileMounts(t *testing.T) {
641674
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
642675
base.Cmd("run", "--rm", "-v", "/etc/resolv.conf:/etc/resolv.conf", "--network", "host", testutil.CommonImage,
643676
"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()
644679
}
645680

646681
func TestRunContainerWithStaticIP6(t *testing.T) {
@@ -712,3 +747,42 @@ func TestRunContainerWithStaticIP6(t *testing.T) {
712747
})
713748
}
714749
}
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

Comments
 (0)