Skip to content

improve validation of cpu-shares, and migrate TestRunInvalidCPUShares#49180

Merged
thaJeztah merged 3 commits intomoby:masterfrom
thaJeztah:improve_cpushares_validation
Jan 9, 2025
Merged

improve validation of cpu-shares, and migrate TestRunInvalidCPUShares#49180
thaJeztah merged 3 commits intomoby:masterfrom
thaJeztah:improve_cpushares_validation

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

runconfig: TestValidateResources: fix duplicate test-case

Make sure we're testing for the right condition.

runconfig: TestValidateResources: use subtests

  • rewrite to use subtests
  • check for actual error returned

improve validation of cpu-shares, and migrate TestRunInvalidCPUShares

This test was testing errors produced by runc; both the "maximum" and
"minimum" values originate from the OCI runtime;
https://github.com/opencontainers/runc/blob/d48d9cfefcdc13862ade35fd2df428cf9798bf22/libcontainer/cgroups/fs/cpu.go#L66-L83

docker run --cpu-shares=1 alpine
docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error setting cgroup config for procHooks process: the minimum allowed cpu-shares is 2: unknown.

Happy path for this setting is covered by TestRunWithCPUShares, and
various other tests, so we validate that the options take effect;

func (s *DockerCLIRunSuite) TestRunWithCPUShares(c *testing.T) {
testRequires(c, cpuShare)
const file = "/sys/fs/cgroup/cpu/cpu.shares"
out := cli.DockerCmd(c, "run", "--cpu-shares", "1000", "--name", "test", "busybox", "cat", file).Combined()
assert.Equal(c, strings.TrimSpace(out), "1000")
out = inspectField(c, "test", "HostConfig.CPUShares")
assert.Equal(c, out, "1000")
}

This patch:

  • removes the test and migrates it to an integration test
  • removes the checks for errors that might be produced by runc
  • updates our validation for invalid (negative) values to happen
    when creating the contaienr; the existing check that happened when
    creating the OCI spec is preserved, so that configs of existing containers
    are still validated.
  • updates validateResources to return the correct error-type
  • updated unit-test to validate

With this patch:

make TEST_FILTER='TestCreateInvalidHostConfig' TEST_SKIP_INTEGRATION_CLI=1 test-integration
--- PASS: TestCreateInvalidHostConfig (0.00s)
    --- PASS: TestCreateInvalidHostConfig/invalid_IpcMode (0.00s)
    --- PASS: TestCreateInvalidHostConfig/invalid_CPUShares (0.00s)
    --- PASS: TestCreateInvalidHostConfig/invalid_PidMode (0.00s)
    --- PASS: TestCreateInvalidHostConfig/invalid_PidMode_without_container_ID (0.00s)
    --- PASS: TestCreateInvalidHostConfig/invalid_Annotations (0.00s)
    --- PASS: TestCreateInvalidHostConfig/invalid_UTSMode (0.00s)

- What I did

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added status/2-code-review kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. area/daemon Core Engine labels Dec 31, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Dec 31, 2024
@thaJeztah thaJeztah self-assigned this Dec 31, 2024
@thaJeztah thaJeztah force-pushed the improve_cpushares_validation branch from 3f48ddc to fdaee58 Compare December 31, 2024 18:46
Comment thread daemon/daemon_unix.go Outdated
Comment on lines 138 to 141
if config.CPUShares < 0 {
return nil, fmt.Errorf("shares: invalid argument")
return nil, fmt.Errorf("invalid CPU shares (%d): value must be a positive integer", config.CPUShares)
}
if config.CPUShares > 0 {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, let me combine these 2 branches.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️ done 👍

@thaJeztah thaJeztah force-pushed the improve_cpushares_validation branch 2 times, most recently from 3624b61 to 0ea952e Compare January 3, 2025 15:47
}
// We're only producing an error when supported to preserve old behavior.
// The OCI runtime may still reject the config though.
if si.CPUShares {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is written a bit poorly; "when supported" should probably be something like "if CPU-shares are supported"; let me touch it up.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

☝️ done 👍

@thaJeztah thaJeztah force-pushed the improve_cpushares_validation branch from 0ea952e to c6e9db9 Compare January 6, 2025 11:10
Make sure we're testing for the right condition.

Signed-off-by: Sebastiaan van Stijn <[email protected]>
- rewrite to use subtests
- check for actual error returned

Signed-off-by: Sebastiaan van Stijn <[email protected]>
This test was testing errors produced by runc; both the "maximum" and
"minimum" values originate from the OCI runtime;
https://github.com/opencontainers/runc/blob/d48d9cfefcdc13862ade35fd2df428cf9798bf22/libcontainer/cgroups/fs/cpu.go#L66-L83

    docker run --cpu-shares=1 alpine
    docker: Error response from daemon: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: error setting cgroup config for procHooks process: the minimum allowed cpu-shares is 2: unknown.

Happy path for this setting is covered by TestRunWithCPUShares, and
various other tests, so we validate that the options take effect;
https://github.com/moby/moby/blob/f5af46d4d5db591da5ce330c9936baecd1df5d9c/integration-cli/docker_cli_run_unix_test.go#L494-L503

This patch:

- removes the test and migrates it to an integration test
- removes the checks for errors that might be produced by runc
- updates our validation for invalid (negative) values to happen
  when creating the contaienr; the existing check that happened when
  creating the OCI spec is preserved, so that configs of existing containers
  are still validated.
- updates validateResources to return the correct error-type
- updated unit-test to validate

With this patch:

    make TEST_FILTER='TestCreateInvalidHostConfig' TEST_SKIP_INTEGRATION_CLI=1 test-integration
    --- PASS: TestCreateInvalidHostConfig (0.00s)
        --- PASS: TestCreateInvalidHostConfig/invalid_IpcMode (0.00s)
        --- PASS: TestCreateInvalidHostConfig/invalid_CPUShares (0.00s)
        --- PASS: TestCreateInvalidHostConfig/invalid_PidMode (0.00s)
        --- PASS: TestCreateInvalidHostConfig/invalid_PidMode_without_container_ID (0.00s)
        --- PASS: TestCreateInvalidHostConfig/invalid_Annotations (0.00s)
        --- PASS: TestCreateInvalidHostConfig/invalid_UTSMode (0.00s)

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the improve_cpushares_validation branch from c6e9db9 to 6d24a21 Compare January 9, 2025 12:24
// CPU-shares on a system that doesn't support it instead of silently
// ignoring.
if hc.Resources.CPUShares < 0 {
return validationError(fmt.Sprintf("invalid CPU shares (%d): value must be a positive integer", hc.Resources.CPUShares))
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While rebasing, changed this to validationError (I used errdefs.InvalidParameter, but we have this error that we used above, so might as well be consistent).

@thaJeztah thaJeztah merged commit 4938e84 into moby:master Jan 9, 2025
@thaJeztah thaJeztah deleted the improve_cpushares_validation branch January 9, 2025 14:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/daemon Core Engine kind/enhancement Enhancements are not bugs or new features but can improve usability or performance. status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants