daemon_unix_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. // +build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "errors"
  5. "io/ioutil"
  6. "os"
  7. "testing"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/container"
  10. "github.com/docker/docker/daemon/config"
  11. )
  12. type fakeContainerGetter struct {
  13. containers map[string]*container.Container
  14. }
  15. func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
  16. container, ok := f.containers[cid]
  17. if !ok {
  18. return nil, errors.New("container not found")
  19. }
  20. return container, nil
  21. }
  22. // Unix test as uses settings which are not available on Windows
  23. func TestAdjustSharedNamespaceContainerName(t *testing.T) {
  24. fakeID := "abcdef1234567890"
  25. hostConfig := &containertypes.HostConfig{
  26. IpcMode: containertypes.IpcMode("container:base"),
  27. PidMode: containertypes.PidMode("container:base"),
  28. NetworkMode: containertypes.NetworkMode("container:base"),
  29. }
  30. containerStore := &fakeContainerGetter{}
  31. containerStore.containers = make(map[string]*container.Container)
  32. containerStore.containers["base"] = &container.Container{
  33. ID: fakeID,
  34. }
  35. adaptSharedNamespaceContainer(containerStore, hostConfig)
  36. if hostConfig.IpcMode != containertypes.IpcMode("container:"+fakeID) {
  37. t.Errorf("Expected IpcMode to be container:%s", fakeID)
  38. }
  39. if hostConfig.PidMode != containertypes.PidMode("container:"+fakeID) {
  40. t.Errorf("Expected PidMode to be container:%s", fakeID)
  41. }
  42. if hostConfig.NetworkMode != containertypes.NetworkMode("container:"+fakeID) {
  43. t.Errorf("Expected NetworkMode to be container:%s", fakeID)
  44. }
  45. }
  46. // Unix test as uses settings which are not available on Windows
  47. func TestAdjustCPUShares(t *testing.T) {
  48. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. defer os.RemoveAll(tmp)
  53. daemon := &Daemon{
  54. repository: tmp,
  55. root: tmp,
  56. }
  57. hostConfig := &containertypes.HostConfig{
  58. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  59. }
  60. daemon.adaptContainerSettings(hostConfig, true)
  61. if hostConfig.CPUShares != linuxMinCPUShares {
  62. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
  63. }
  64. hostConfig.CPUShares = linuxMaxCPUShares + 1
  65. daemon.adaptContainerSettings(hostConfig, true)
  66. if hostConfig.CPUShares != linuxMaxCPUShares {
  67. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
  68. }
  69. hostConfig.CPUShares = 0
  70. daemon.adaptContainerSettings(hostConfig, true)
  71. if hostConfig.CPUShares != 0 {
  72. t.Error("Expected CPUShares to be unchanged")
  73. }
  74. hostConfig.CPUShares = 1024
  75. daemon.adaptContainerSettings(hostConfig, true)
  76. if hostConfig.CPUShares != 1024 {
  77. t.Error("Expected CPUShares to be unchanged")
  78. }
  79. }
  80. // Unix test as uses settings which are not available on Windows
  81. func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
  82. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. defer os.RemoveAll(tmp)
  87. daemon := &Daemon{
  88. repository: tmp,
  89. root: tmp,
  90. }
  91. hostConfig := &containertypes.HostConfig{
  92. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  93. }
  94. daemon.adaptContainerSettings(hostConfig, false)
  95. if hostConfig.CPUShares != linuxMinCPUShares-1 {
  96. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
  97. }
  98. hostConfig.CPUShares = linuxMaxCPUShares + 1
  99. daemon.adaptContainerSettings(hostConfig, false)
  100. if hostConfig.CPUShares != linuxMaxCPUShares+1 {
  101. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
  102. }
  103. hostConfig.CPUShares = 0
  104. daemon.adaptContainerSettings(hostConfig, false)
  105. if hostConfig.CPUShares != 0 {
  106. t.Error("Expected CPUShares to be unchanged")
  107. }
  108. hostConfig.CPUShares = 1024
  109. daemon.adaptContainerSettings(hostConfig, false)
  110. if hostConfig.CPUShares != 1024 {
  111. t.Error("Expected CPUShares to be unchanged")
  112. }
  113. }
  114. // Unix test as uses settings which are not available on Windows
  115. func TestParseSecurityOptWithDeprecatedColon(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 AppArmorProfile, 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 TestParseSecurityOpt(t *testing.T) {
  152. container := &container.Container{}
  153. config := &containertypes.HostConfig{}
  154. // test apparmor
  155. config.SecurityOpt = []string{"apparmor=test_profile"}
  156. if err := parseSecurityOpt(container, config); err != nil {
  157. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  158. }
  159. if container.AppArmorProfile != "test_profile" {
  160. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  161. }
  162. // test seccomp
  163. sp := "/path/to/seccomp_test.json"
  164. config.SecurityOpt = []string{"seccomp=" + sp}
  165. if err := parseSecurityOpt(container, config); err != nil {
  166. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  167. }
  168. if container.SeccompProfile != sp {
  169. t.Fatalf("Unexpected SeccompProfile, expected: %q, got %q", sp, container.SeccompProfile)
  170. }
  171. // test valid label
  172. config.SecurityOpt = []string{"label=user:USER"}
  173. if err := parseSecurityOpt(container, config); err != nil {
  174. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  175. }
  176. // test invalid label
  177. config.SecurityOpt = []string{"label"}
  178. if err := parseSecurityOpt(container, config); err == nil {
  179. t.Fatal("Expected parseSecurityOpt error, got nil")
  180. }
  181. // test invalid opt
  182. config.SecurityOpt = []string{"test"}
  183. if err := parseSecurityOpt(container, config); err == nil {
  184. t.Fatal("Expected parseSecurityOpt error, got nil")
  185. }
  186. }
  187. func TestParseNNPSecurityOptions(t *testing.T) {
  188. daemon := &Daemon{
  189. configStore: &config.Config{NoNewPrivileges: true},
  190. }
  191. container := &container.Container{}
  192. config := &containertypes.HostConfig{}
  193. // test NNP when "daemon:true" and "no-new-privileges=false""
  194. config.SecurityOpt = []string{"no-new-privileges=false"}
  195. if err := daemon.parseSecurityOpt(container, config); err != nil {
  196. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  197. }
  198. if container.NoNewPrivileges {
  199. t.Fatalf("container.NoNewPrivileges should be FALSE: %v", container.NoNewPrivileges)
  200. }
  201. // test NNP when "daemon:false" and "no-new-privileges=true""
  202. daemon.configStore.NoNewPrivileges = false
  203. config.SecurityOpt = []string{"no-new-privileges=true"}
  204. if err := daemon.parseSecurityOpt(container, config); err != nil {
  205. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  206. }
  207. if !container.NoNewPrivileges {
  208. t.Fatalf("container.NoNewPrivileges should be TRUE: %v", container.NoNewPrivileges)
  209. }
  210. }
  211. func TestNetworkOptions(t *testing.T) {
  212. daemon := &Daemon{}
  213. dconfigCorrect := &config.Config{
  214. CommonConfig: config.CommonConfig{
  215. ClusterStore: "consul://localhost:8500",
  216. ClusterAdvertise: "192.168.0.1:8000",
  217. },
  218. }
  219. if _, err := daemon.networkOptions(dconfigCorrect, nil, nil); err != nil {
  220. t.Fatalf("Expect networkOptions success, got error: %v", err)
  221. }
  222. dconfigWrong := &config.Config{
  223. CommonConfig: config.CommonConfig{
  224. ClusterStore: "consul://localhost:8500://test://bbb",
  225. },
  226. }
  227. if _, err := daemon.networkOptions(dconfigWrong, nil, nil); err == nil {
  228. t.Fatal("Expected networkOptions error, got nil")
  229. }
  230. }