2015-08-05 20:15:14 -04:00
|
|
|
// +build !windows
|
2015-06-03 15:01:53 -04:00
|
|
|
|
2015-08-05 20:15:14 -04:00
|
|
|
package daemon
|
2015-06-03 15:01:53 -04:00
|
|
|
|
|
|
|
import (
|
2015-08-05 20:15:14 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-06-03 15:01:53 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/docker/docker/runconfig"
|
|
|
|
)
|
|
|
|
|
2015-08-05 20:15:14 -04: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-06-03 15:01:53 -04:00
|
|
|
hostConfig := &runconfig.HostConfig{
|
2015-07-21 01:15:44 -04:00
|
|
|
CPUShares: linuxMinCPUShares - 1,
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-21 01:15:44 -04:00
|
|
|
if hostConfig.CPUShares != linuxMinCPUShares {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
hostConfig.CPUShares = linuxMaxCPUShares + 1
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-21 01:15:44 -04:00
|
|
|
if hostConfig.CPUShares != linuxMaxCPUShares {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
hostConfig.CPUShares = 0
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-25 05:11:45 -04:00
|
|
|
if hostConfig.CPUShares != 0 {
|
2015-07-21 01:15:44 -04:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
hostConfig.CPUShares = 1024
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, true)
|
2015-07-25 05:11:45 -04:00
|
|
|
if hostConfig.CPUShares != 1024 {
|
2015-07-21 01:15:44 -04:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
|
2015-08-05 20:15:14 -04: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-06-03 15:01:53 -04:00
|
|
|
hostConfig := &runconfig.HostConfig{
|
2015-07-21 01:15:44 -04:00
|
|
|
CPUShares: linuxMinCPUShares - 1,
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-21 01:15:44 -04:00
|
|
|
if hostConfig.CPUShares != linuxMinCPUShares-1 {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-21 01:15:44 -04:00
|
|
|
hostConfig.CPUShares = linuxMaxCPUShares + 1
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-21 01:15:44 -04:00
|
|
|
if hostConfig.CPUShares != linuxMaxCPUShares+1 {
|
|
|
|
t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
hostConfig.CPUShares = 0
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-25 05:11:45 -04:00
|
|
|
if hostConfig.CPUShares != 0 {
|
2015-07-21 01:15:44 -04:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:11:45 -04:00
|
|
|
hostConfig.CPUShares = 1024
|
2015-08-05 20:15:14 -04:00
|
|
|
daemon.adaptContainerSettings(hostConfig, false)
|
2015-07-25 05:11:45 -04:00
|
|
|
if hostConfig.CPUShares != 1024 {
|
2015-07-21 01:15:44 -04:00
|
|
|
t.Error("Expected CPUShares to be unchanged")
|
2015-06-03 15:01:53 -04:00
|
|
|
}
|
|
|
|
}
|