Repository: moby/moby
File: integration-cli/docker_cli_run_unix_test.go (Go)

Code before:
}

func (s *DockerCLIRunSuite) TestRunInvalidCpusetMemsFlagValue(c *testing.T) {
	testRequires(c, cgroupCpuset)

	sysInfo := sysinfo.New()
	mems, err := parsers.ParseUintList(sysInfo.Mems)
	assert.NilError(c, err)
	var invalid int
	for i := 0; i <= len(mems)+1; i++ {
		if !mems[i] {
			invalid = i
			break
		}
	}
	out, _, err := dockerCmdWithError("run", "--cpuset-mems", strconv.Itoa(invalid), "busybox", "true")
	assert.ErrorContains(c, err, "")
	expected := fmt.Sprintf("Error response from daemon: Requested memory nodes are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Mems)
	assert.Assert(c, strings.Contains(out, expected))
}

func (s *DockerCLIRunSuite) TestRunInvalidCPUShares(c *testing.T) {
	testRequires(c, cpuShare, DaemonIsLinux)
	out, _, err := dockerCmdWithError("run", "--cpu-shares", "1", "busybox", "echo", "test")
	assert.ErrorContains(c, err, "", out)
	expected := "cpu.weight: numerical result out of range"
	assert.Assert(c, strings.Contains(out, expected))

	out, _, err = dockerCmdWithError("run", "--cpu-shares", "-1", "busybox", "echo", "test")
	assert.ErrorContains(c, err, "", out)
	expected = "shares: invalid argument"
	assert.Assert(c, strings.Contains(out, expected))

	out, _, err = dockerCmdWithError("run", "--cpu-shares", "99999999", "busybox", "echo", "test")
	assert.ErrorContains(c, err, "", out)
	expected = "cpu.weight: numerical result out of range"
	assert.Assert(c, strings.Contains(out, expected))
}

func (s *DockerCLIRunSuite) TestRunWithDefaultShmSize(c *testing.T) {
	testRequires(c, DaemonIsLinux)

	const name = "shm-default"
	out := cli.DockerCmd(c, "run", "--name", name, "busybox", "mount").Combined()
	shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
	if !shmRegex.MatchString(out) {
		c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
	}
	shmSize := inspectField(c, name, "HostConfig.ShmSize")
	assert.Equal(c, shmSize, "67108864")
}

Review comment (suggestion):
Interesting; do we know why this error changed? This looks to be a `ERANGE`;
https://github.com/moby/moby/blob/e325564f38ec79a09093a94e72935f582cc83d13/vendor/golang.org/x/sys/unix/zerrors_linux_amd64.go#L736

Are we somehow skipping local validation in the daemon (and fall through), or otherwise? I know we don't have this PR in the v25 branch;

- https://github.com/moby/moby/pull/49180

But looking at those changes, there should be some validation on our side before that :thinking:


Code after:
}

func (s *DockerCLIRunSuite) TestRunInvalidCpusetMemsFlagValue(c *testing.T) {
	testRequires(c, cgroupCpuset)

	sysInfo := sysinfo.New()
	mems, err := parsers.ParseUintList(sysInfo.Mems)
	assert.NilError(c, err)
	var invalid int
	for i := 0; i <= len(mems)+1; i++ {
		if !mems[i] {
			invalid = i
			break
		}
	}
	out, _, err := dockerCmdWithError("run", "--cpuset-mems", strconv.Itoa(invalid), "busybox", "true")
	assert.ErrorContains(c, err, "")
	expected := fmt.Sprintf("Error response from daemon: Requested memory nodes are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Mems)
	assert.Assert(c, strings.Contains(out, expected))
}

func (s *DockerCLIRunSuite) TestRunInvalidCPUShares(c *testing.T) {
	testRequires(c, cpuShare, DaemonIsLinux)
	skip.If(c, onlyCgroupsv2(), "FIXME: cgroupsV2 not supported yet")
	out, _, err := dockerCmdWithError("run", "--cpu-shares", "1", "busybox", "echo", "test")
	assert.ErrorContains(c, err, "", out)
	expected := "minimum allowed cpu-shares is 2"
	assert.Assert(c, strings.Contains(out, expected))

	out, _, err = dockerCmdWithError("run", "--cpu-shares", "-1", "busybox", "echo", "test")
	assert.ErrorContains(c, err, "", out)
	expected = "shares: invalid argument"
	assert.Assert(c, strings.Contains(out, expected))

	out, _, err = dockerCmdWithError("run", "--cpu-shares", "99999999", "busybox", "echo", "test")
	assert.ErrorContains(c, err, "", out)
	expected = "maximum allowed cpu-shares is"
	assert.Assert(c, strings.Contains(out, expected))
}

func (s *DockerCLIRunSuite) TestRunWithDefaultShmSize(c *testing.T) {
	testRequires(c, DaemonIsLinux)

	const name = "shm-default"
	out := cli.DockerCmd(c, "run", "--name", name, "busybox", "mount").Combined()
	shmRegex := regexp.MustCompile(`shm on /dev/shm type tmpfs(.*)size=65536k`)
	if !shmRegex.MatchString(out) {
		c.Fatalf("Expected shm of 64MB in mount command, got %v", out)
	}
	shmSize := inspectField(c, name, "HostConfig.ShmSize")
	assert.Equal(c, shmSize, "67108864")