github.com/psychoss/docker@v1.9.0/daemon/daemon_unix_test.go (about) 1 // +build !windows 2 3 package daemon 4 5 import ( 6 "io/ioutil" 7 "os" 8 "testing" 9 10 "github.com/docker/docker/runconfig" 11 ) 12 13 func TestAdjustCPUShares(t *testing.T) { 14 tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-") 15 if err != nil { 16 t.Fatal(err) 17 } 18 defer os.RemoveAll(tmp) 19 daemon := &Daemon{ 20 repository: tmp, 21 root: tmp, 22 } 23 24 hostConfig := &runconfig.HostConfig{ 25 CPUShares: linuxMinCPUShares - 1, 26 } 27 daemon.adaptContainerSettings(hostConfig, true) 28 if hostConfig.CPUShares != linuxMinCPUShares { 29 t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares) 30 } 31 32 hostConfig.CPUShares = linuxMaxCPUShares + 1 33 daemon.adaptContainerSettings(hostConfig, true) 34 if hostConfig.CPUShares != linuxMaxCPUShares { 35 t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares) 36 } 37 38 hostConfig.CPUShares = 0 39 daemon.adaptContainerSettings(hostConfig, true) 40 if hostConfig.CPUShares != 0 { 41 t.Error("Expected CPUShares to be unchanged") 42 } 43 44 hostConfig.CPUShares = 1024 45 daemon.adaptContainerSettings(hostConfig, true) 46 if hostConfig.CPUShares != 1024 { 47 t.Error("Expected CPUShares to be unchanged") 48 } 49 } 50 51 func TestAdjustCPUSharesNoAdjustment(t *testing.T) { 52 tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-") 53 if err != nil { 54 t.Fatal(err) 55 } 56 defer os.RemoveAll(tmp) 57 daemon := &Daemon{ 58 repository: tmp, 59 root: tmp, 60 } 61 62 hostConfig := &runconfig.HostConfig{ 63 CPUShares: linuxMinCPUShares - 1, 64 } 65 daemon.adaptContainerSettings(hostConfig, false) 66 if hostConfig.CPUShares != linuxMinCPUShares-1 { 67 t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1) 68 } 69 70 hostConfig.CPUShares = linuxMaxCPUShares + 1 71 daemon.adaptContainerSettings(hostConfig, false) 72 if hostConfig.CPUShares != linuxMaxCPUShares+1 { 73 t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1) 74 } 75 76 hostConfig.CPUShares = 0 77 daemon.adaptContainerSettings(hostConfig, false) 78 if hostConfig.CPUShares != 0 { 79 t.Error("Expected CPUShares to be unchanged") 80 } 81 82 hostConfig.CPUShares = 1024 83 daemon.adaptContainerSettings(hostConfig, false) 84 if hostConfig.CPUShares != 1024 { 85 t.Error("Expected CPUShares to be unchanged") 86 } 87 }