2015-08-06 00:15:14 +00:00
|
|
|
// +build !windows
|
2015-06-03 19:01:53 +00:00
|
|
|
|
2015-08-06 00:15:14 +00:00
|
|
|
package daemon
|
2015-06-03 19:01:53 +00:00
|
|
|
|
|
|
|
import (
|
2015-08-06 00:15:14 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-06-03 19:01:53 +00:00
|
|
|
"testing"
|
|
|
|
|
2015-12-18 18:36:17 +00:00
|
|
|
"github.com/docker/docker/api/types/container"
|
2015-06-03 19:01:53 +00:00
|
|
|
)
|
|
|
|
|
2015-08-06 00:15:14 +00:00
|
|
|
func TestAdjustCPUShares(t *testing.T) {
|
|
|
|
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
daemon := &Daemon{
|
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
|
|
|
}
|
|
|
|
|
2015-12-18 18:36:17 +00:00
|
|
|
hostConfig := &container.HostConfig{
|
|
|
|
Resources: container.Resources{CPUShares: linuxMinCPUShares - 1},
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-21 05:15:44 +00:00
|
|
|
if hostConfig.CPUShares != linuxMinCPUShares {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
hostConfig.CPUShares = linuxMaxCPUShares + 1
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-21 05:15:44 +00:00
|
|
|
if hostConfig.CPUShares != linuxMaxCPUShares {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
hostConfig.CPUShares = 0
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-25 09:11:45 +00:00
|
|
|
if hostConfig.CPUShares != 0 {
|
2015-07-21 05:15:44 +00:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
hostConfig.CPUShares = 1024
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-25 09:11:45 +00:00
|
|
|
if hostConfig.CPUShares != 1024 {
|
2015-07-21 05:15:44 +00:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
|
2015-08-06 00:15:14 +00:00
|
|
|
tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(tmp)
|
|
|
|
daemon := &Daemon{
|
|
|
|
repository: tmp,
|
|
|
|
root: tmp,
|
|
|
|
}
|
|
|
|
|
2015-12-18 18:36:17 +00:00
|
|
|
hostConfig := &container.HostConfig{
|
|
|
|
Resources: container.Resources{CPUShares: linuxMinCPUShares - 1},
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-21 05:15:44 +00:00
|
|
|
if hostConfig.CPUShares != linuxMinCPUShares-1 {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 05:15:44 +00:00
|
|
|
hostConfig.CPUShares = linuxMaxCPUShares + 1
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-21 05:15:44 +00:00
|
|
|
if hostConfig.CPUShares != linuxMaxCPUShares+1 {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
hostConfig.CPUShares = 0
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-25 09:11:45 +00:00
|
|
|
if hostConfig.CPUShares != 0 {
|
2015-07-21 05:15:44 +00:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
|
2015-07-25 09:11:45 +00:00
|
|
|
hostConfig.CPUShares = 1024
|
2015-08-06 00:15:14 +00:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-25 09:11:45 +00:00
|
|
|
if hostConfig.CPUShares != 1024 {
|
2015-07-21 05:15:44 +00:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 19:01:53 +00:00
|
|
|
}
|
|
|
|
}
|