Files
moby/runconfig/hostconfig_test.go
Sebastiaan van Stijn 6d24a21643 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;
d48d9cfefc/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;
f5af46d4d5/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 <github@gone.nl>
2025-01-09 13:24:02 +01:00

88 lines
2.1 KiB
Go

//go:build !windows
package runconfig // import "github.com/docker/docker/runconfig"
import (
"testing"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/pkg/sysinfo"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
)
func TestValidateResources(t *testing.T) {
type resourceTest struct {
doc string
resources container.Resources
sysInfoCPURealtime bool
sysInfoCPUShares bool
expectedError string
}
tests := []resourceTest{
{
doc: "empty configuration",
},
{
doc: "valid configuration",
resources: container.Resources{
CPURealtimePeriod: 1000,
CPURealtimeRuntime: 1000,
},
sysInfoCPURealtime: true,
},
{
doc: "cpu-rt-period not supported",
resources: container.Resources{
CPURealtimePeriod: 5000,
},
expectedError: "kernel does not support CPU real-time scheduler",
},
{
doc: "cpu-rt-runtime not supported",
resources: container.Resources{
CPURealtimeRuntime: 5000,
},
expectedError: "kernel does not support CPU real-time scheduler",
},
{
doc: "cpu-rt-runtime greater than cpu-rt-period",
resources: container.Resources{
CPURealtimePeriod: 5000,
CPURealtimeRuntime: 10000,
},
sysInfoCPURealtime: true,
expectedError: "cpu real-time runtime cannot be higher than cpu real-time period",
},
{
doc: "negative CPU shares",
resources: container.Resources{
CPUShares: -1,
},
sysInfoCPUShares: true,
expectedError: "invalid CPU shares (-1): value must be a positive integer",
},
}
for _, tc := range tests {
t.Run(tc.doc, func(t *testing.T) {
var hc container.HostConfig
hc.Resources = tc.resources
var si sysinfo.SysInfo
si.CPURealtime = tc.sysInfoCPURealtime
si.CPUShares = tc.sysInfoCPUShares
err := validateResources(&hc, &si)
if tc.expectedError != "" {
assert.Check(t, is.ErrorType(err, errdefs.IsInvalidParameter))
assert.Check(t, is.Error(err, tc.expectedError))
} else {
assert.NilError(t, err)
}
})
}
}