daemon_unix_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 TestParseSecurityOptWithDeprecatedColon(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 TestParseSecurityOpt(t *testing.T) {
  116. container := &container.Container{}
  117. config := &containertypes.HostConfig{}
  118. // test apparmor
  119. config.SecurityOpt = []string{"apparmor=test_profile"}
  120. if err := parseSecurityOpt(container, config); err != nil {
  121. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  122. }
  123. if container.AppArmorProfile != "test_profile" {
  124. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  125. }
  126. // test seccomp
  127. sp := "/path/to/seccomp_test.json"
  128. config.SecurityOpt = []string{"seccomp=" + sp}
  129. if err := parseSecurityOpt(container, config); err != nil {
  130. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  131. }
  132. if container.SeccompProfile != sp {
  133. t.Fatalf("Unexpected SeccompProfile, expected: %q, got %q", sp, container.SeccompProfile)
  134. }
  135. // test valid label
  136. config.SecurityOpt = []string{"label=user:USER"}
  137. if err := parseSecurityOpt(container, config); err != nil {
  138. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  139. }
  140. // test invalid label
  141. config.SecurityOpt = []string{"label"}
  142. if err := parseSecurityOpt(container, config); err == nil {
  143. t.Fatal("Expected parseSecurityOpt error, got nil")
  144. }
  145. // test invalid opt
  146. config.SecurityOpt = []string{"test"}
  147. if err := parseSecurityOpt(container, config); err == nil {
  148. t.Fatal("Expected parseSecurityOpt error, got nil")
  149. }
  150. }
  151. func TestNetworkOptions(t *testing.T) {
  152. daemon := &Daemon{}
  153. dconfigCorrect := &Config{
  154. CommonConfig: CommonConfig{
  155. ClusterStore: "consul://localhost:8500",
  156. ClusterAdvertise: "192.168.0.1:8000",
  157. },
  158. }
  159. if _, err := daemon.networkOptions(dconfigCorrect, nil); err != nil {
  160. t.Fatalf("Expect networkOptions success, got error: %v", err)
  161. }
  162. dconfigWrong := &Config{
  163. CommonConfig: CommonConfig{
  164. ClusterStore: "consul://localhost:8500://test://bbb",
  165. },
  166. }
  167. if _, err := daemon.networkOptions(dconfigWrong, nil); err == nil {
  168. t.Fatalf("Expected networkOptions error, got nil")
  169. }
  170. }