2018-01-31 16:28:09 -05:00
|
|
|
package system
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
2018-04-14 12:52:02 -04:00
|
|
|
"github.com/docker/docker/integration/internal/container"
|
2019-08-29 16:52:40 -04:00
|
|
|
"github.com/docker/docker/testutil/daemon"
|
2018-01-31 16:28:09 -05:00
|
|
|
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/skip"
|
2018-01-31 16:28:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// hasSystemd checks whether the host was booted with systemd as its init
|
|
|
|
// system. Stolen from
|
|
|
|
// https://github.com/coreos/go-systemd/blob/176f85496f4e/util/util.go#L68
|
|
|
|
func hasSystemd() bool {
|
|
|
|
fi, err := os.Lstat("/run/systemd/system")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return fi.IsDir()
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestCgroupDriverSystemdMemoryLimit checks that container
|
|
|
|
// memory limit can be set when using systemd cgroupdriver.
|
|
|
|
// https://github.com/moby/moby/issues/35123
|
|
|
|
func TestCgroupDriverSystemdMemoryLimit(t *testing.T) {
|
2018-04-19 05:14:15 -04:00
|
|
|
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
|
2019-01-14 11:11:36 -05:00
|
|
|
skip.If(t, !hasSystemd())
|
2018-01-31 16:28:09 -05:00
|
|
|
t.Parallel()
|
|
|
|
|
2018-04-10 10:29:48 -04:00
|
|
|
d := daemon.New(t)
|
2018-12-22 09:53:02 -05:00
|
|
|
c := d.NewClientT(t)
|
|
|
|
|
2018-01-31 16:28:09 -05:00
|
|
|
d.StartWithBusybox(t, "--exec-opt", "native.cgroupdriver=systemd", "--iptables=false")
|
|
|
|
defer d.Stop(t)
|
|
|
|
|
2020-02-10 17:01:12 -05:00
|
|
|
const mem = int64(64 * 1024 * 1024) // 64 MB
|
2018-01-31 16:28:09 -05:00
|
|
|
|
|
|
|
ctx := context.Background()
|
2019-06-06 07:00:37 -04:00
|
|
|
ctrID := container.Create(ctx, t, c, func(ctr *container.TestContainerConfig) {
|
2018-12-22 09:53:02 -05:00
|
|
|
ctr.HostConfig.Resources.Memory = mem
|
2018-04-14 12:52:02 -04:00
|
|
|
})
|
2018-12-22 09:53:02 -05:00
|
|
|
defer c.ContainerRemove(ctx, ctrID, types.ContainerRemoveOptions{Force: true})
|
2018-01-31 16:28:09 -05:00
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
err := c.ContainerStart(ctx, ctrID, types.ContainerStartOptions{})
|
2018-01-31 16:28:09 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
|
2018-12-22 09:53:02 -05:00
|
|
|
s, err := c.ContainerInspect(ctx, ctrID)
|
2018-01-31 16:28:09 -05:00
|
|
|
assert.NilError(t, err)
|
|
|
|
assert.Equal(t, s.HostConfig.Memory, mem)
|
|
|
|
}
|