daemon_unix_test.go 8.9 KB

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