daemon_unix_test.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 TestParseNNPSecurityOptions(t *testing.T) {
  158. daemon := &Daemon{
  159. configStore: &config.Config{NoNewPrivileges: true},
  160. }
  161. container := &container.Container{}
  162. config := &containertypes.HostConfig{}
  163. // test NNP when "daemon:true" and "no-new-privileges=false""
  164. config.SecurityOpt = []string{"no-new-privileges=false"}
  165. if err := daemon.parseSecurityOpt(container, config); err != nil {
  166. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  167. }
  168. if container.NoNewPrivileges {
  169. t.Fatalf("container.NoNewPrivileges should be FALSE: %v", container.NoNewPrivileges)
  170. }
  171. // test NNP when "daemon:false" and "no-new-privileges=true""
  172. daemon.configStore.NoNewPrivileges = false
  173. config.SecurityOpt = []string{"no-new-privileges=true"}
  174. if err := daemon.parseSecurityOpt(container, config); err != nil {
  175. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  176. }
  177. if !container.NoNewPrivileges {
  178. t.Fatalf("container.NoNewPrivileges should be TRUE: %v", container.NoNewPrivileges)
  179. }
  180. }
  181. func TestNetworkOptions(t *testing.T) {
  182. daemon := &Daemon{}
  183. dconfigCorrect := &config.Config{
  184. CommonConfig: config.CommonConfig{
  185. ClusterStore: "consul://localhost:8500",
  186. ClusterAdvertise: "192.168.0.1:8000",
  187. },
  188. }
  189. if _, err := daemon.networkOptions(dconfigCorrect, nil, nil); err != nil {
  190. t.Fatalf("Expect networkOptions success, got error: %v", err)
  191. }
  192. dconfigWrong := &config.Config{
  193. CommonConfig: config.CommonConfig{
  194. ClusterStore: "consul://localhost:8500://test://bbb",
  195. },
  196. }
  197. if _, err := daemon.networkOptions(dconfigWrong, nil, nil); err == nil {
  198. t.Fatalf("Expected networkOptions error, got nil")
  199. }
  200. }
  201. func TestMigratePre17Volumes(t *testing.T) {
  202. rootDir, err := ioutil.TempDir("", "test-daemon-volumes")
  203. if err != nil {
  204. t.Fatal(err)
  205. }
  206. defer os.RemoveAll(rootDir)
  207. volumeRoot := filepath.Join(rootDir, "volumes")
  208. err = os.MkdirAll(volumeRoot, 0755)
  209. if err != nil {
  210. t.Fatal(err)
  211. }
  212. containerRoot := filepath.Join(rootDir, "containers")
  213. cid := "1234"
  214. err = os.MkdirAll(filepath.Join(containerRoot, cid), 0755)
  215. vid := "5678"
  216. vfsPath := filepath.Join(rootDir, "vfs", "dir", vid)
  217. err = os.MkdirAll(vfsPath, 0755)
  218. if err != nil {
  219. t.Fatal(err)
  220. }
  221. config := []byte(`
  222. {
  223. "ID": "` + cid + `",
  224. "Volumes": {
  225. "/foo": "` + vfsPath + `",
  226. "/bar": "/foo",
  227. "/quux": "/quux"
  228. },
  229. "VolumesRW": {
  230. "/foo": true,
  231. "/bar": true,
  232. "/quux": false
  233. }
  234. }
  235. `)
  236. volStore, err := store.New(volumeRoot)
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. drv, err := local.New(volumeRoot, 0, 0)
  241. if err != nil {
  242. t.Fatal(err)
  243. }
  244. volumedrivers.Register(drv, volume.DefaultDriverName)
  245. daemon := &Daemon{root: rootDir, repository: containerRoot, volumes: volStore}
  246. err = ioutil.WriteFile(filepath.Join(containerRoot, cid, "config.v2.json"), config, 600)
  247. if err != nil {
  248. t.Fatal(err)
  249. }
  250. c, err := daemon.load(cid)
  251. if err != nil {
  252. t.Fatal(err)
  253. }
  254. if err := daemon.verifyVolumesInfo(c); err != nil {
  255. t.Fatal(err)
  256. }
  257. expected := map[string]volume.MountPoint{
  258. "/foo": {Destination: "/foo", RW: true, Name: vid},
  259. "/bar": {Source: "/foo", Destination: "/bar", RW: true},
  260. "/quux": {Source: "/quux", Destination: "/quux", RW: false},
  261. }
  262. for id, mp := range c.MountPoints {
  263. x, exists := expected[id]
  264. if !exists {
  265. t.Fatal("volume not migrated")
  266. }
  267. if mp.Source != x.Source || mp.Destination != x.Destination || mp.RW != x.RW || mp.Name != x.Name {
  268. t.Fatalf("got unexpected mountpoint, expected: %+v, got: %+v", x, mp)
  269. }
  270. }
  271. }