github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/integration/container/run_linux_test.go (about)

     1  package container // import "github.com/demonoid81/moby/integration/container"
     2  
     3  import (
     4  	"context"
     5  	"strconv"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	containertypes "github.com/demonoid81/moby/api/types/container"
    11  	"github.com/demonoid81/moby/api/types/versions"
    12  	"github.com/demonoid81/moby/integration/internal/container"
    13  	net "github.com/demonoid81/moby/integration/internal/network"
    14  	"gotest.tools/v3/assert"
    15  	is "gotest.tools/v3/assert/cmp"
    16  	"gotest.tools/v3/poll"
    17  	"gotest.tools/v3/skip"
    18  )
    19  
    20  func TestKernelTCPMemory(t *testing.T) {
    21  	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
    22  	skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "skip test from new feature")
    23  	skip.If(t, testEnv.DaemonInfo.CgroupDriver == "none")
    24  	skip.If(t, !testEnv.DaemonInfo.KernelMemoryTCP)
    25  
    26  	defer setupTest(t)()
    27  	client := testEnv.APIClient()
    28  	ctx := context.Background()
    29  
    30  	const (
    31  		kernelMemoryTCP int64 = 200 * 1024 * 1024
    32  	)
    33  
    34  	cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
    35  		c.HostConfig.Resources = containertypes.Resources{
    36  			KernelMemoryTCP: kernelMemoryTCP,
    37  		}
    38  	})
    39  
    40  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    41  
    42  	inspect, err := client.ContainerInspect(ctx, cID)
    43  	assert.NilError(t, err)
    44  	assert.Check(t, is.Equal(kernelMemoryTCP, inspect.HostConfig.KernelMemoryTCP))
    45  
    46  	res, err := container.Exec(ctx, client, cID,
    47  		[]string{"cat", "/sys/fs/cgroup/memory/memory.kmem.tcp.limit_in_bytes"})
    48  	assert.NilError(t, err)
    49  	assert.Assert(t, is.Len(res.Stderr(), 0))
    50  	assert.Equal(t, 0, res.ExitCode)
    51  	assert.Check(t, is.Equal(strconv.FormatInt(kernelMemoryTCP, 10), strings.TrimSpace(res.Stdout())))
    52  }
    53  
    54  func TestNISDomainname(t *testing.T) {
    55  	// Older versions of the daemon would concatenate hostname and domainname,
    56  	// so hostname "foobar" and domainname "baz.cyphar.com" would produce
    57  	// `foobar.baz.cyphar.com` as hostname.
    58  	skip.If(t, versions.LessThan(testEnv.DaemonAPIVersion(), "1.40"), "skip test from new feature")
    59  	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
    60  
    61  	// Rootless supports custom Hostname but doesn't support custom Domainname
    62  	//  OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \
    63  	//  "write sysctl key kernel.domainname: open /proc/sys/kernel/domainname: permission denied\"": unknown.
    64  	skip.If(t, testEnv.IsRootless, "rootless mode doesn't support setting Domainname (TODO: https://github.com/moby/moby/issues/40632)")
    65  
    66  	defer setupTest(t)()
    67  	client := testEnv.APIClient()
    68  	ctx := context.Background()
    69  
    70  	const (
    71  		hostname   = "foobar"
    72  		domainname = "baz.cyphar.com"
    73  	)
    74  
    75  	cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
    76  		c.Config.Hostname = hostname
    77  		c.Config.Domainname = domainname
    78  	})
    79  
    80  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
    81  
    82  	inspect, err := client.ContainerInspect(ctx, cID)
    83  	assert.NilError(t, err)
    84  	assert.Check(t, is.Equal(hostname, inspect.Config.Hostname))
    85  	assert.Check(t, is.Equal(domainname, inspect.Config.Domainname))
    86  
    87  	// Check hostname.
    88  	res, err := container.Exec(ctx, client, cID,
    89  		[]string{"cat", "/proc/sys/kernel/hostname"})
    90  	assert.NilError(t, err)
    91  	assert.Assert(t, is.Len(res.Stderr(), 0))
    92  	assert.Equal(t, 0, res.ExitCode)
    93  	assert.Check(t, is.Equal(hostname, strings.TrimSpace(res.Stdout())))
    94  
    95  	// Check domainname.
    96  	res, err = container.Exec(ctx, client, cID,
    97  		[]string{"cat", "/proc/sys/kernel/domainname"})
    98  	assert.NilError(t, err)
    99  	assert.Assert(t, is.Len(res.Stderr(), 0))
   100  	assert.Equal(t, 0, res.ExitCode)
   101  	assert.Check(t, is.Equal(domainname, strings.TrimSpace(res.Stdout())))
   102  }
   103  
   104  func TestHostnameDnsResolution(t *testing.T) {
   105  	skip.If(t, testEnv.DaemonInfo.OSType != "linux")
   106  
   107  	defer setupTest(t)()
   108  	client := testEnv.APIClient()
   109  	ctx := context.Background()
   110  
   111  	const (
   112  		hostname = "foobar"
   113  	)
   114  
   115  	// using user defined network as we want to use internal DNS
   116  	netName := "foobar-net"
   117  	net.CreateNoError(context.Background(), t, client, netName, net.WithDriver("bridge"))
   118  
   119  	cID := container.Run(ctx, t, client, func(c *container.TestContainerConfig) {
   120  		c.Config.Hostname = hostname
   121  		c.HostConfig.NetworkMode = containertypes.NetworkMode(netName)
   122  	})
   123  
   124  	poll.WaitOn(t, container.IsInState(ctx, client, cID, "running"), poll.WithDelay(100*time.Millisecond))
   125  
   126  	inspect, err := client.ContainerInspect(ctx, cID)
   127  	assert.NilError(t, err)
   128  	assert.Check(t, is.Equal(hostname, inspect.Config.Hostname))
   129  
   130  	// Clear hosts file so ping will use DNS for hostname resolution
   131  	res, err := container.Exec(ctx, client, cID,
   132  		[]string{"sh", "-c", "echo 127.0.0.1 localhost | tee /etc/hosts && ping -c 1 foobar"})
   133  	assert.NilError(t, err)
   134  	assert.Check(t, is.Equal("", res.Stderr()))
   135  	assert.Equal(t, 0, res.ExitCode)
   136  }