daemon_unix_test.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // +build !windows
  2. package daemon
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/docker/docker/container"
  9. "github.com/docker/docker/volume"
  10. "github.com/docker/docker/volume/drivers"
  11. "github.com/docker/docker/volume/local"
  12. "github.com/docker/docker/volume/store"
  13. containertypes "github.com/docker/engine-api/types/container"
  14. )
  15. // Unix test as uses settings which are not available on Windows
  16. func TestAdjustCPUShares(t *testing.T) {
  17. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  18. if err != nil {
  19. t.Fatal(err)
  20. }
  21. defer os.RemoveAll(tmp)
  22. daemon := &Daemon{
  23. repository: tmp,
  24. root: tmp,
  25. }
  26. hostConfig := &containertypes.HostConfig{
  27. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  28. }
  29. daemon.adaptContainerSettings(hostConfig, true)
  30. if hostConfig.CPUShares != linuxMinCPUShares {
  31. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
  32. }
  33. hostConfig.CPUShares = linuxMaxCPUShares + 1
  34. daemon.adaptContainerSettings(hostConfig, true)
  35. if hostConfig.CPUShares != linuxMaxCPUShares {
  36. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
  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. hostConfig.CPUShares = 1024
  44. daemon.adaptContainerSettings(hostConfig, true)
  45. if hostConfig.CPUShares != 1024 {
  46. t.Error("Expected CPUShares to be unchanged")
  47. }
  48. }
  49. // Unix test as uses settings which are not available on Windows
  50. func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
  51. tmp, err := ioutil.TempDir("", "docker-daemon-unix-test-")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. defer os.RemoveAll(tmp)
  56. daemon := &Daemon{
  57. repository: tmp,
  58. root: tmp,
  59. }
  60. hostConfig := &containertypes.HostConfig{
  61. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  62. }
  63. daemon.adaptContainerSettings(hostConfig, false)
  64. if hostConfig.CPUShares != linuxMinCPUShares-1 {
  65. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
  66. }
  67. hostConfig.CPUShares = linuxMaxCPUShares + 1
  68. daemon.adaptContainerSettings(hostConfig, false)
  69. if hostConfig.CPUShares != linuxMaxCPUShares+1 {
  70. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
  71. }
  72. hostConfig.CPUShares = 0
  73. daemon.adaptContainerSettings(hostConfig, false)
  74. if hostConfig.CPUShares != 0 {
  75. t.Error("Expected CPUShares to be unchanged")
  76. }
  77. hostConfig.CPUShares = 1024
  78. daemon.adaptContainerSettings(hostConfig, false)
  79. if hostConfig.CPUShares != 1024 {
  80. t.Error("Expected CPUShares to be unchanged")
  81. }
  82. }
  83. // Unix test as uses settings which are not available on Windows
  84. func TestParseSecurityOptWithDeprecatedColon(t *testing.T) {
  85. container := &container.Container{}
  86. config := &containertypes.HostConfig{}
  87. // test apparmor
  88. config.SecurityOpt = []string{"apparmor=test_profile"}
  89. if err := parseSecurityOpt(container, config); err != nil {
  90. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  91. }
  92. if container.AppArmorProfile != "test_profile" {
  93. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  94. }
  95. // test seccomp
  96. sp := "/path/to/seccomp_test.json"
  97. config.SecurityOpt = []string{"seccomp=" + sp}
  98. if err := parseSecurityOpt(container, config); err != nil {
  99. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  100. }
  101. if container.SeccompProfile != sp {
  102. t.Fatalf("Unexpected AppArmorProfile, expected: %q, got %q", sp, container.SeccompProfile)
  103. }
  104. // test valid label
  105. config.SecurityOpt = []string{"label=user:USER"}
  106. if err := parseSecurityOpt(container, config); err != nil {
  107. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  108. }
  109. // test invalid label
  110. config.SecurityOpt = []string{"label"}
  111. if err := parseSecurityOpt(container, config); err == nil {
  112. t.Fatal("Expected parseSecurityOpt error, got nil")
  113. }
  114. // test invalid opt
  115. config.SecurityOpt = []string{"test"}
  116. if err := parseSecurityOpt(container, config); err == nil {
  117. t.Fatal("Expected parseSecurityOpt error, got nil")
  118. }
  119. }
  120. func TestParseSecurityOpt(t *testing.T) {
  121. container := &container.Container{}
  122. config := &containertypes.HostConfig{}
  123. // test apparmor
  124. config.SecurityOpt = []string{"apparmor=test_profile"}
  125. if err := parseSecurityOpt(container, config); err != nil {
  126. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  127. }
  128. if container.AppArmorProfile != "test_profile" {
  129. t.Fatalf("Unexpected AppArmorProfile, expected: \"test_profile\", got %q", container.AppArmorProfile)
  130. }
  131. // test seccomp
  132. sp := "/path/to/seccomp_test.json"
  133. config.SecurityOpt = []string{"seccomp=" + sp}
  134. if err := parseSecurityOpt(container, config); err != nil {
  135. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  136. }
  137. if container.SeccompProfile != sp {
  138. t.Fatalf("Unexpected SeccompProfile, expected: %q, got %q", sp, container.SeccompProfile)
  139. }
  140. // test valid label
  141. config.SecurityOpt = []string{"label=user:USER"}
  142. if err := parseSecurityOpt(container, config); err != nil {
  143. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  144. }
  145. // test invalid label
  146. config.SecurityOpt = []string{"label"}
  147. if err := parseSecurityOpt(container, config); err == nil {
  148. t.Fatal("Expected parseSecurityOpt error, got nil")
  149. }
  150. // test invalid opt
  151. config.SecurityOpt = []string{"test"}
  152. if err := parseSecurityOpt(container, config); err == nil {
  153. t.Fatal("Expected parseSecurityOpt error, got nil")
  154. }
  155. }
  156. func TestNetworkOptions(t *testing.T) {
  157. daemon := &Daemon{}
  158. dconfigCorrect := &Config{
  159. CommonConfig: CommonConfig{
  160. ClusterStore: "consul://localhost:8500",
  161. ClusterAdvertise: "192.168.0.1:8000",
  162. },
  163. }
  164. if _, err := daemon.networkOptions(dconfigCorrect, nil); err != nil {
  165. t.Fatalf("Expect networkOptions success, got error: %v", err)
  166. }
  167. dconfigWrong := &Config{
  168. CommonConfig: CommonConfig{
  169. ClusterStore: "consul://localhost:8500://test://bbb",
  170. },
  171. }
  172. if _, err := daemon.networkOptions(dconfigWrong, nil); err == nil {
  173. t.Fatalf("Expected networkOptions error, got nil")
  174. }
  175. }
  176. func TestMigratePre17Volumes(t *testing.T) {
  177. rootDir, err := ioutil.TempDir("", "test-daemon-volumes")
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. defer os.RemoveAll(rootDir)
  182. volumeRoot := filepath.Join(rootDir, "volumes")
  183. err = os.MkdirAll(volumeRoot, 0755)
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. containerRoot := filepath.Join(rootDir, "containers")
  188. cid := "1234"
  189. err = os.MkdirAll(filepath.Join(containerRoot, cid), 0755)
  190. vid := "5678"
  191. vfsPath := filepath.Join(rootDir, "vfs", "dir", vid)
  192. err = os.MkdirAll(vfsPath, 0755)
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. config := []byte(`
  197. {
  198. "ID": "` + cid + `",
  199. "Volumes": {
  200. "/foo": "` + vfsPath + `",
  201. "/bar": "/foo",
  202. "/quux": "/quux"
  203. },
  204. "VolumesRW": {
  205. "/foo": true,
  206. "/bar": true,
  207. "/quux": false
  208. }
  209. }
  210. `)
  211. volStore, err := store.New(volumeRoot)
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. drv, err := local.New(volumeRoot, 0, 0)
  216. if err != nil {
  217. t.Fatal(err)
  218. }
  219. volumedrivers.Register(drv, volume.DefaultDriverName)
  220. daemon := &Daemon{root: rootDir, repository: containerRoot, volumes: volStore}
  221. err = ioutil.WriteFile(filepath.Join(containerRoot, cid, "config.v2.json"), config, 600)
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. c, err := daemon.load(cid)
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. if err := daemon.verifyVolumesInfo(c); err != nil {
  230. t.Fatal(err)
  231. }
  232. expected := map[string]volume.MountPoint{
  233. "/foo": {Destination: "/foo", RW: true, Name: vid},
  234. "/bar": {Source: "/foo", Destination: "/bar", RW: true},
  235. "/quux": {Source: "/quux", Destination: "/quux", RW: false},
  236. }
  237. for id, mp := range c.MountPoints {
  238. x, exists := expected[id]
  239. if !exists {
  240. t.Fatal("volume not migrated")
  241. }
  242. if mp.Source != x.Source || mp.Destination != x.Destination || mp.RW != x.RW || mp.Name != x.Name {
  243. t.Fatalf("got unexpected mountpoint, expected: %+v, got: %+v", x, mp)
  244. }
  245. }
  246. }