Browse code

Test for systemd cgroupdriver memory setting

This is a test case for issue https://github.com/moby/moby/issues/35123,
making sure we can set container's memory limit when using
`native.cgroupdriver=systemd`.

[v2: skip if no systemd present]
[v3: add --iptables=false to avoid flaky tests with t.Parallel()]
[v4: rebase after PR#36507 merge]

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>

Kir Kolyshkin authored on 2018/02/01 06:28:09
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,64 @@
0
+package system
1
+
2
+import (
3
+	"context"
4
+	"os"
5
+	"testing"
6
+
7
+	"github.com/docker/docker/api/types"
8
+	"github.com/docker/docker/api/types/container"
9
+	"github.com/docker/docker/integration-cli/daemon"
10
+
11
+	"github.com/gotestyourself/gotestyourself/assert"
12
+)
13
+
14
+// hasSystemd checks whether the host was booted with systemd as its init
15
+// system. Stolen from
16
+// https://github.com/coreos/go-systemd/blob/176f85496f4e/util/util.go#L68
17
+func hasSystemd() bool {
18
+	fi, err := os.Lstat("/run/systemd/system")
19
+	if err != nil {
20
+		return false
21
+	}
22
+	return fi.IsDir()
23
+}
24
+
25
+// TestCgroupDriverSystemdMemoryLimit checks that container
26
+// memory limit can be set when using systemd cgroupdriver.
27
+//  https://github.com/moby/moby/issues/35123
28
+func TestCgroupDriverSystemdMemoryLimit(t *testing.T) {
29
+	t.Parallel()
30
+
31
+	if !hasSystemd() {
32
+		t.Skip("systemd not available")
33
+	}
34
+
35
+	d := daemon.New(t, "docker", "dockerd", daemon.Config{})
36
+	client, err := d.NewClient()
37
+	assert.NilError(t, err)
38
+	d.StartWithBusybox(t, "--exec-opt", "native.cgroupdriver=systemd", "--iptables=false")
39
+	defer d.Stop(t)
40
+
41
+	const mem = 64 * 1024 * 1024 // 64 MB
42
+	cfg := container.Config{
43
+		Image: "busybox",
44
+		Cmd:   []string{"top"},
45
+	}
46
+	hostcfg := container.HostConfig{
47
+		Resources: container.Resources{
48
+			Memory: mem,
49
+		},
50
+	}
51
+
52
+	ctx := context.Background()
53
+	ctr, err := client.ContainerCreate(ctx, &cfg, &hostcfg, nil, "")
54
+	assert.NilError(t, err)
55
+	defer client.ContainerRemove(ctx, ctr.ID, types.ContainerRemoveOptions{Force: true})
56
+
57
+	err = client.ContainerStart(ctx, ctr.ID, types.ContainerStartOptions{})
58
+	assert.NilError(t, err)
59
+
60
+	s, err := client.ContainerInspect(ctx, ctr.ID)
61
+	assert.NilError(t, err)
62
+	assert.Equal(t, s.HostConfig.Memory, mem)
63
+}