daemon_unix_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // +build !windows
  2. package daemon
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. "github.com/docker/docker/container"
  8. containertypes "github.com/docker/engine-api/types/container"
  9. )
  10. // Unix test as uses settings which are not available on Windows
  11. func TestAdjustCPUShares(t *testing.T) {
  12. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  13. if err != nil {
  14. t.Fatal(err)
  15. }
  16. defer os.RemoveAll(tmp)
  17. daemon := &Daemon{
  18. repository: tmp,
  19. root: tmp,
  20. }
  21. hostConfig := &containertypes.HostConfig{
  22. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  23. }
  24. daemon.adaptContainerSettings(hostConfig, true)
  25. if hostConfig.CPUShares != linuxMinCPUShares {
  26. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
  27. }
  28. hostConfig.CPUShares = linuxMaxCPUShares + 1
  29. daemon.adaptContainerSettings(hostConfig, true)
  30. if hostConfig.CPUShares != linuxMaxCPUShares {
  31. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
  32. }
  33. hostConfig.CPUShares = 0
  34. daemon.adaptContainerSettings(hostConfig, true)
  35. if hostConfig.CPUShares != 0 {
  36. t.Error("Expected CPUShares to be unchanged")
  37. }
  38. hostConfig.CPUShares = 1024
  39. daemon.adaptContainerSettings(hostConfig, true)
  40. if hostConfig.CPUShares != 1024 {
  41. t.Error("Expected CPUShares to be unchanged")
  42. }
  43. }
  44. // Unix test as uses settings which are not available on Windows
  45. func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
  46. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. defer os.RemoveAll(tmp)
  51. daemon := &Daemon{
  52. repository: tmp,
  53. root: tmp,
  54. }
  55. hostConfig := &containertypes.HostConfig{
  56. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  57. }
  58. daemon.adaptContainerSettings(hostConfig, false)
  59. if hostConfig.CPUShares != linuxMinCPUShares-1 {
  60. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
  61. }
  62. hostConfig.CPUShares = linuxMaxCPUShares + 1
  63. daemon.adaptContainerSettings(hostConfig, false)
  64. if hostConfig.CPUShares != linuxMaxCPUShares+1 {
  65. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
  66. }
  67. hostConfig.CPUShares = 0
  68. daemon.adaptContainerSettings(hostConfig, false)
  69. if hostConfig.CPUShares != 0 {
  70. t.Error("Expected CPUShares to be unchanged")
  71. }
  72. hostConfig.CPUShares = 1024
  73. daemon.adaptContainerSettings(hostConfig, false)
  74. if hostConfig.CPUShares != 1024 {
  75. t.Error("Expected CPUShares to be unchanged")
  76. }
  77. }
  78. // Unix test as uses settings which are not available on Windows
  79. func TestParseSecurityOpt(t *testing.T) {
  80. container := &container.Container{}
  81. config := &containertypes.HostConfig{}
  82. // test apparmor
  83. config.SecurityOpt = []string{"apparmor:test_profile"}
  84. if err := parseSecurityOpt(container, config); err != nil {
  85. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  86. }
  87. if container.AppArmorProfile != "test_profile" {
  88. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  89. }
  90. // test seccomp
  91. sp := "/path/to/seccomp_test.json"
  92. config.SecurityOpt = []string{"seccomp:" + sp}
  93. if err := parseSecurityOpt(container, config); err != nil {
  94. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  95. }
  96. if container.SeccompProfile != sp {
  97. t.Fatalf("Unexpected AppArmorProfile, expected: %q, got %q", sp, container.SeccompProfile)
  98. }
  99. // test valid label
  100. config.SecurityOpt = []string{"label:user:USER"}
  101. if err := parseSecurityOpt(container, config); err != nil {
  102. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  103. }
  104. // test invalid label
  105. config.SecurityOpt = []string{"label"}
  106. if err := parseSecurityOpt(container, config); err == nil {
  107. t.Fatal("Expected parseSecurityOpt error, got nil")
  108. }
  109. // test invalid opt
  110. config.SecurityOpt = []string{"test"}
  111. if err := parseSecurityOpt(container, config); err == nil {
  112. t.Fatal("Expected parseSecurityOpt error, got nil")
  113. }
  114. }
  115. func TestNetworkOptions(t *testing.T) {
  116. daemon := &Daemon{}
  117. dconfigCorrect := &Config{
  118. CommonConfig: CommonConfig{
  119. ClusterStore: "consul://localhost:8500",
  120. ClusterAdvertise: "192.168.0.1:8000",
  121. },
  122. }
  123. if _, err := daemon.networkOptions(dconfigCorrect); err != nil {
  124. t.Fatalf("Expect networkOptions success, got error: %v", err)
  125. }
  126. dconfigWrong := &Config{
  127. CommonConfig: CommonConfig{
  128. ClusterStore: "consul://localhost:8500://test://bbb",
  129. },
  130. }
  131. if _, err := daemon.networkOptions(dconfigWrong); err == nil {
  132. t.Fatalf("Expected networkOptions error, got nil")
  133. }
  134. }