github.com/zhouyu0/docker-note@v0.0.0-20190722021225-b8d3825084db/integration/system/cgroupdriver_systemd_test.go (about)

     1  package system
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/integration/internal/container"
    10  	"github.com/docker/docker/internal/test/daemon"
    11  
    12  	"gotest.tools/assert"
    13  	"gotest.tools/skip"
    14  )
    15  
    16  // hasSystemd checks whether the host was booted with systemd as its init
    17  // system. Stolen from
    18  // https://github.com/coreos/go-systemd/blob/176f85496f4e/util/util.go#L68
    19  func hasSystemd() bool {
    20  	fi, err := os.Lstat("/run/systemd/system")
    21  	if err != nil {
    22  		return false
    23  	}
    24  	return fi.IsDir()
    25  }
    26  
    27  // TestCgroupDriverSystemdMemoryLimit checks that container
    28  // memory limit can be set when using systemd cgroupdriver.
    29  //  https://github.com/moby/moby/issues/35123
    30  func TestCgroupDriverSystemdMemoryLimit(t *testing.T) {
    31  	skip.If(t, testEnv.DaemonInfo.OSType == "windows")
    32  	t.Parallel()
    33  
    34  	if !hasSystemd() {
    35  		t.Skip("systemd not available")
    36  	}
    37  
    38  	d := daemon.New(t)
    39  	c := d.NewClientT(t)
    40  
    41  	d.StartWithBusybox(t, "--exec-opt", "native.cgroupdriver=systemd", "--iptables=false")
    42  	defer d.Stop(t)
    43  
    44  	const mem = 64 * 1024 * 1024 // 64 MB
    45  
    46  	ctx := context.Background()
    47  	ctrID := container.Create(t, ctx, c, func(ctr *container.TestContainerConfig) {
    48  		ctr.HostConfig.Resources.Memory = mem
    49  	})
    50  	defer c.ContainerRemove(ctx, ctrID, types.ContainerRemoveOptions{Force: true})
    51  
    52  	err := c.ContainerStart(ctx, ctrID, types.ContainerStartOptions{})
    53  	assert.NilError(t, err)
    54  
    55  	s, err := c.ContainerInspect(ctx, ctrID)
    56  	assert.NilError(t, err)
    57  	assert.Equal(t, s.HostConfig.Memory, mem)
    58  }