Skip to content

Commit

Permalink
enable host name and default resolv.conf and hosts file in none network
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Oct 30, 2024
1 parent 49a19ed commit c64c4f7
Show file tree
Hide file tree
Showing 5 changed files with 889 additions and 5 deletions.
75 changes: 75 additions & 0 deletions cmd/nerdctl/container/container_run_network_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"time"

"github.com/containernetworking/plugins/pkg/ns"
"github.com/stretchr/testify/require"
"github.com/vishvananda/netlink"
"gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
Expand Down Expand Up @@ -504,6 +505,37 @@ func TestSharedNetworkStack(t *testing.T) {
AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)
}

func TestSharedNetworkWithNone(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("--network=container:<container name|id> only supports linux now")
}
base := testutil.NewBase(t)

containerName := testutil.Identifier(t)
defer base.Cmd("rm", "-f", containerName).AssertOK()
base.Cmd("run", "-d", "--name", containerName, "--network", "none",
testutil.NginxAlpineImage).AssertOK()
base.EnsureContainerStarted(containerName)

containerNameJoin := testutil.Identifier(t) + "-network"
defer base.Cmd("rm", "-f", containerNameJoin).AssertOK()
base.Cmd("run",
"-d",
"--name", containerNameJoin,
"--network=container:"+containerName,
testutil.CommonImage,
"sleep", "infinity").AssertOK()

base.Cmd("exec", containerNameJoin, "wget", "-qO-", "http://127.0.0.1:80").
AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)

base.Cmd("restart", containerName).AssertOK()
base.Cmd("stop", "--time=1", containerNameJoin).AssertOK()
base.Cmd("start", containerNameJoin).AssertOK()
base.Cmd("exec", containerNameJoin, "wget", "-qO-", "http://127.0.0.1:80").
AssertOutContains(testutil.NginxAlpineIndexHTMLSnippet)
}

func TestRunContainerInExistingNetNS(t *testing.T) {
if rootlessutil.IsRootless() {
t.Skip("Can't create new netns in rootless mode")
Expand Down Expand Up @@ -629,6 +661,8 @@ func TestHostsFileMounts(t *testing.T) {
"sh", "-euxc", "echo >> /etc/hosts").AssertOK()
base.Cmd("run", "--rm", "-v", "/etc/hosts:/etc/hosts", "--network", "host", testutil.CommonImage,
"sh", "-euxc", "head -n -1 /etc/hosts > temp && cat temp > /etc/hosts").AssertOK()
base.Cmd("run", "--rm", "--network", "none", testutil.CommonImage,
"sh", "-euxc", "echo >> /etc/hosts").AssertOK()

base.Cmd("run", "--rm", testutil.CommonImage,
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
Expand All @@ -641,6 +675,8 @@ func TestHostsFileMounts(t *testing.T) {
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
base.Cmd("run", "--rm", "-v", "/etc/resolv.conf:/etc/resolv.conf", "--network", "host", testutil.CommonImage,
"sh", "-euxc", "head -n -1 /etc/resolv.conf > temp && cat temp > /etc/resolv.conf").AssertOK()
base.Cmd("run", "--rm", "--network", "host", testutil.CommonImage,
"sh", "-euxc", "echo >> /etc/resolv.conf").AssertOK()
}

func TestRunContainerWithStaticIP6(t *testing.T) {
Expand Down Expand Up @@ -712,3 +748,42 @@ func TestRunContainerWithStaticIP6(t *testing.T) {
})
}
}

func TestNoneNetworkStaticConfigs(t *testing.T) {
testutil.DockerIncompatible(t)
base := testutil.NewBase(t)

cmd := base.Cmd("run", "--rm", "--net", "none", testutil.CommonImage, "cat", "/etc/hosts")
cmd.AssertOutContains("127.0.0.1 localhost")
cmd.AssertOutContains("::1 localhost")

cmd = base.Cmd("run", "--rm", "--net", "none", testutil.CommonImage, "cat", "/etc/resolv.conf")
cmd.AssertOutContains("nameserver 127.0.0.1")

// If running on Linux, verify /etc/hostname is correctly set
if runtime.GOOS == "linux" {
containerHostName := "testcontainer"
cmd := base.Cmd("run", "--rm", "--net", "none", "--hostname", containerHostName, testutil.CommonImage, "cat", "/etc/hostname")
output := cmd.Run().Combined()
hostname := strings.TrimSpace(output)

if len(containerHostName) > 12 {
require.Equal(t, containerHostName[:12], hostname[:12])
} else {
require.Equal(t, containerHostName, hostname[:12])
}

containerName := "testNoneNetworkHostname"
defer base.Cmd("rm", "-f", containerName).AssertOK()
cmd = base.Cmd("run", "-d", "--net", "none", "--name", containerName, testutil.CommonImage, "sleep", "infinity")
output = cmd.Run().Combined()
containerIDShort := strings.TrimSpace(output)[:12]

cmd = base.Cmd("exec", containerName, "cat", "/etc/hostname")
output = cmd.Run().Combined()
containerHostName = strings.TrimSpace(output)

require.Equal(t, containerHostName, containerIDShort)

}
}
Loading

0 comments on commit c64c4f7

Please sign in to comment.