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

Code before:
}

func (s *DockerCLIRunSuite) TestRunInvalidCpusetCpusFlagValue(c *testing.T) {
	testRequires(c, cgroupCpuset, testEnv.IsLocalDaemon)

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

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("create", "--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, is.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 := "minimum allowed cpu-shares is 2"
	assert.Assert(c, is.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, is.Contains(out, expected))

Review comment (suggestion):
I think I had a branch to deprecate this one, now that it's no longer used, but I need to find it back 😂 

Code after:

func (s *DockerCLIRunSuite) TestRunInvalidCpusetCpusFlagValue(c *testing.T) {
	testRequires(c, cgroupCpuset, testEnv.IsLocalDaemon)

	cpus := sysinfo.New().CPUSets
	var invalid int
	for i := 0; i <= len(cpus)+1; i++ {
		if _, ok := cpus[i]; !ok {
			invalid = i
			break
		}
	}
	out, _, err := dockerCmdWithError("create", "--cpuset-cpus", strconv.Itoa(invalid), "busybox", "true")
	assert.ErrorContains(c, err, "")
	expected := fmt.Sprintf("Error response from daemon: Requested CPUs are not available - requested %s, available: %s", strconv.Itoa(invalid), sysInfo.Cpus)
	assert.Assert(c, is.Contains(out, expected))
}

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

	mems := sysinfo.New().MemSets
	var invalid int
	for i := 0; i <= len(mems)+1; i++ {
		if _, ok := mems[i]; !ok {
			invalid = i
			break
		}
	}
	out, _, err := dockerCmdWithError("create", "--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, is.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 := "minimum allowed cpu-shares is 2"
	assert.Assert(c, is.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, is.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, is.Contains(out, expected))