daemon_unix_test.go 7.8 KB

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