Skip to content
This repository has been archived by the owner on Jun 28, 2024. It is now read-only.

Commit

Permalink
docker: fix cpu tests
Browse files Browse the repository at this point in the history
We should take the initial cpu count when setting cpu constraints.
IOW, if there is already enough cpu in the vm for a container, we
should not hotplug more CPUs.

Depends-on: github.com/kata-containers/runtime#696
Fixes: #705

Signed-off-by: Peng Tao <bergwolf@gmail.com>
  • Loading branch information
bergwolf committed Sep 5, 2018
1 parent 865378c commit 763eaec
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions integration/docker/cpu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func withCPUPeriodAndQuota(quota, period, defaultVCPUs int, fail bool) TableEntr
if fail {
msg = "should fail"
} else {
msg = fmt.Sprintf("should have %d CPUs", ((quota+period-1)/period)+defaultVCPUs)
vCPUs := (quota + period - 1) / period
if vCPUs < defaultVCPUs {
vCPUs = defaultVCPUs
}
msg = fmt.Sprintf("should have %d CPUs", vCPUs)
}

return Entry(msg, quota, period, fail)
Expand All @@ -48,6 +52,10 @@ func withCPUConstraint(cpus float64, defaultVCPUs int, fail bool) TableEntry {
return Entry(msg, c, fail)
}

func maxCPUCount(count, defaultVCPUs int) int {
return int(math.Max(float64(count), float64(defaultVCPUs)))
}

var _ = Describe("Hot plug CPUs", func() {
var (
args []string
Expand Down Expand Up @@ -75,7 +83,7 @@ var _ = Describe("Hot plug CPUs", func() {

DescribeTable("container with CPU period and quota",
func(quota, period int, fail bool) {
vCPUs = ((quota + period - 1) / period) + defaultVCPUs
vCPUs = maxCPUCount((quota+period-1)/period, defaultVCPUs)
args = append(args, "--cpu-quota", fmt.Sprintf("%d", quota),
"--cpu-period", fmt.Sprintf("%d", period), DebianImage, "bash", "-c",
fmt.Sprintf(checkCpusCmdFmt, maxTries, vCPUs-1, waitTime))
Expand All @@ -95,7 +103,7 @@ var _ = Describe("Hot plug CPUs", func() {

DescribeTable("container with CPU constraint",
func(cpus int, fail bool) {
vCPUs = cpus + defaultVCPUs
vCPUs = maxCPUCount(cpus, defaultVCPUs)
args = append(args, "--cpus", fmt.Sprintf("%d", cpus), DebianImage, "bash", "-c",
fmt.Sprintf(checkCpusCmdFmt, maxTries, vCPUs-1, waitTime))
stdout, _, exitCode := dockerRun(args...)
Expand Down Expand Up @@ -263,7 +271,7 @@ var _ = Describe("Update number of CPUs", func() {

DescribeTable("Update CPU period and quota",
func(quota, period int, fail bool) {
vCPUs = ((quota + period - 1) / period) + defaultVCPUs
vCPUs = maxCPUCount((quota+period-1)/period, defaultVCPUs)
updateArgs = append(updateArgs, "--cpu-quota", fmt.Sprintf("%d", quota),
"--cpu-period", fmt.Sprintf("%d", period), id)
stdout, _, exitCode = dockerUpdate(updateArgs...)
Expand All @@ -286,7 +294,7 @@ var _ = Describe("Update number of CPUs", func() {

DescribeTable("Update CPU constraint",
func(cpus int, fail bool) {
vCPUs = cpus + defaultVCPUs
vCPUs = maxCPUCount(cpus, defaultVCPUs)
updateArgs = append(updateArgs, "--cpus", fmt.Sprintf("%d", cpus), id)
stdout, _, exitCode = dockerUpdate(updateArgs...)
if fail {
Expand Down Expand Up @@ -420,7 +428,7 @@ var _ = Describe("CPUs and CPU set", func() {
id = RandID(30)
args = []string{"--rm", "-dt", "--name", id, Image, "sh"}
cpuTests = []cpuTest{
{"1", "0-1", "2"},
{"1", "0", "1"},
{"3", "1,2", "2"},
{"2", "1", "1"},
}
Expand Down

0 comments on commit 763eaec

Please sign in to comment.