daemon_unix_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. //go:build !windows
  2. package daemon // import "github.com/docker/docker/daemon"
  3. import (
  4. "errors"
  5. "os"
  6. "path/filepath"
  7. "testing"
  8. "github.com/docker/docker/api/types/blkiodev"
  9. containertypes "github.com/docker/docker/api/types/container"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/daemon/config"
  12. "github.com/docker/docker/pkg/sysinfo"
  13. "github.com/opencontainers/selinux/go-selinux"
  14. "golang.org/x/sys/unix"
  15. "gotest.tools/v3/assert"
  16. is "gotest.tools/v3/assert/cmp"
  17. )
  18. type fakeContainerGetter struct {
  19. containers map[string]*container.Container
  20. }
  21. func (f *fakeContainerGetter) GetContainer(cid string) (*container.Container, error) {
  22. ctr, ok := f.containers[cid]
  23. if !ok {
  24. return nil, errors.New("container not found")
  25. }
  26. return ctr, nil
  27. }
  28. // Unix test as uses settings which are not available on Windows
  29. func TestAdjustSharedNamespaceContainerName(t *testing.T) {
  30. fakeID := "abcdef1234567890"
  31. hostConfig := &containertypes.HostConfig{
  32. IpcMode: containertypes.IpcMode("container:base"),
  33. PidMode: containertypes.PidMode("container:base"),
  34. NetworkMode: containertypes.NetworkMode("container:base"),
  35. }
  36. containerStore := &fakeContainerGetter{}
  37. containerStore.containers = make(map[string]*container.Container)
  38. containerStore.containers["base"] = &container.Container{
  39. ID: fakeID,
  40. }
  41. adaptSharedNamespaceContainer(containerStore, hostConfig)
  42. if hostConfig.IpcMode != containertypes.IpcMode("container:"+fakeID) {
  43. t.Errorf("Expected IpcMode to be container:%s", fakeID)
  44. }
  45. if hostConfig.PidMode != containertypes.PidMode("container:"+fakeID) {
  46. t.Errorf("Expected PidMode to be container:%s", fakeID)
  47. }
  48. if hostConfig.NetworkMode != containertypes.NetworkMode("container:"+fakeID) {
  49. t.Errorf("Expected NetworkMode to be container:%s", fakeID)
  50. }
  51. }
  52. // Unix test as uses settings which are not available on Windows
  53. func TestAdjustCPUShares(t *testing.T) {
  54. tmp, err := os.MkdirTemp("", "docker-daemon-unix-test-")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. defer os.RemoveAll(tmp)
  59. daemon := &Daemon{
  60. repository: tmp,
  61. root: tmp,
  62. }
  63. cfg := &config.Config{}
  64. muteLogs()
  65. hostConfig := &containertypes.HostConfig{
  66. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  67. }
  68. daemon.adaptContainerSettings(cfg, hostConfig, true)
  69. if hostConfig.CPUShares != linuxMinCPUShares {
  70. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares)
  71. }
  72. hostConfig.CPUShares = linuxMaxCPUShares + 1
  73. daemon.adaptContainerSettings(cfg, hostConfig, true)
  74. if hostConfig.CPUShares != linuxMaxCPUShares {
  75. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares)
  76. }
  77. hostConfig.CPUShares = 0
  78. daemon.adaptContainerSettings(cfg, hostConfig, true)
  79. if hostConfig.CPUShares != 0 {
  80. t.Error("Expected CPUShares to be unchanged")
  81. }
  82. hostConfig.CPUShares = 1024
  83. daemon.adaptContainerSettings(cfg, hostConfig, true)
  84. if hostConfig.CPUShares != 1024 {
  85. t.Error("Expected CPUShares to be unchanged")
  86. }
  87. }
  88. // Unix test as uses settings which are not available on Windows
  89. func TestAdjustCPUSharesNoAdjustment(t *testing.T) {
  90. tmp, err := os.MkdirTemp("", "docker-daemon-unix-test-")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. defer os.RemoveAll(tmp)
  95. daemon := &Daemon{
  96. repository: tmp,
  97. root: tmp,
  98. }
  99. cfg := &config.Config{}
  100. hostConfig := &containertypes.HostConfig{
  101. Resources: containertypes.Resources{CPUShares: linuxMinCPUShares - 1},
  102. }
  103. daemon.adaptContainerSettings(cfg, hostConfig, false)
  104. if hostConfig.CPUShares != linuxMinCPUShares-1 {
  105. t.Errorf("Expected CPUShares to be %d", linuxMinCPUShares-1)
  106. }
  107. hostConfig.CPUShares = linuxMaxCPUShares + 1
  108. daemon.adaptContainerSettings(cfg, hostConfig, false)
  109. if hostConfig.CPUShares != linuxMaxCPUShares+1 {
  110. t.Errorf("Expected CPUShares to be %d", linuxMaxCPUShares+1)
  111. }
  112. hostConfig.CPUShares = 0
  113. daemon.adaptContainerSettings(cfg, hostConfig, false)
  114. if hostConfig.CPUShares != 0 {
  115. t.Error("Expected CPUShares to be unchanged")
  116. }
  117. hostConfig.CPUShares = 1024
  118. daemon.adaptContainerSettings(cfg, hostConfig, false)
  119. if hostConfig.CPUShares != 1024 {
  120. t.Error("Expected CPUShares to be unchanged")
  121. }
  122. }
  123. // Unix test as uses settings which are not available on Windows
  124. func TestParseSecurityOptWithDeprecatedColon(t *testing.T) {
  125. opts := &container.SecurityOptions{}
  126. cfg := &containertypes.HostConfig{}
  127. // test apparmor
  128. cfg.SecurityOpt = []string{"apparmor=test_profile"}
  129. if err := parseSecurityOpt(opts, cfg); err != nil {
  130. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  131. }
  132. if opts.AppArmorProfile != "test_profile" {
  133. t.Fatalf(`Unexpected AppArmorProfile, expected: "test_profile", got %q`, opts.AppArmorProfile)
  134. }
  135. // test seccomp
  136. sp := "/path/to/seccomp_test.json"
  137. cfg.SecurityOpt = []string{"seccomp=" + sp}
  138. if err := parseSecurityOpt(opts, cfg); err != nil {
  139. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  140. }
  141. if opts.SeccompProfile != sp {
  142. t.Fatalf("Unexpected AppArmorProfile, expected: %q, got %q", sp, opts.SeccompProfile)
  143. }
  144. // test valid label
  145. cfg.SecurityOpt = []string{"label=user:USER"}
  146. if err := parseSecurityOpt(opts, cfg); err != nil {
  147. t.Fatalf("Unexpected parseSecurityOpt error: %v", err)
  148. }
  149. // test invalid label
  150. cfg.SecurityOpt = []string{"label"}
  151. if err := parseSecurityOpt(opts, cfg); err == nil {
  152. t.Fatal("Expected parseSecurityOpt error, got nil")
  153. }
  154. // test invalid opt
  155. cfg.SecurityOpt = []string{"test"}
  156. if err := parseSecurityOpt(opts, cfg); err == nil {
  157. t.Fatal("Expected parseSecurityOpt error, got nil")
  158. }
  159. }
  160. func TestParseSecurityOpt(t *testing.T) {
  161. t.Run("apparmor", func(t *testing.T) {
  162. secOpts := &container.SecurityOptions{}
  163. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  164. SecurityOpt: []string{"apparmor=test_profile"},
  165. })
  166. assert.Check(t, err)
  167. assert.Equal(t, secOpts.AppArmorProfile, "test_profile")
  168. })
  169. t.Run("apparmor using legacy separator", func(t *testing.T) {
  170. secOpts := &container.SecurityOptions{}
  171. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  172. SecurityOpt: []string{"apparmor:test_profile"},
  173. })
  174. assert.Check(t, err)
  175. assert.Equal(t, secOpts.AppArmorProfile, "test_profile")
  176. })
  177. t.Run("seccomp", func(t *testing.T) {
  178. secOpts := &container.SecurityOptions{}
  179. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  180. SecurityOpt: []string{"seccomp=/path/to/seccomp_test.json"},
  181. })
  182. assert.Check(t, err)
  183. assert.Equal(t, secOpts.SeccompProfile, "/path/to/seccomp_test.json")
  184. })
  185. t.Run("valid label", func(t *testing.T) {
  186. secOpts := &container.SecurityOptions{}
  187. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  188. SecurityOpt: []string{"label=user:USER"},
  189. })
  190. assert.Check(t, err)
  191. if selinux.GetEnabled() {
  192. // TODO(thaJeztah): set expected labels here (or "partial" if depends on host)
  193. // assert.Check(t, is.Equal(secOpts.MountLabel, ""))
  194. // assert.Check(t, is.Equal(secOpts.ProcessLabel, ""))
  195. } else {
  196. assert.Check(t, is.Equal(secOpts.MountLabel, ""))
  197. assert.Check(t, is.Equal(secOpts.ProcessLabel, ""))
  198. }
  199. })
  200. t.Run("invalid label", func(t *testing.T) {
  201. secOpts := &container.SecurityOptions{}
  202. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  203. SecurityOpt: []string{"label"},
  204. })
  205. assert.Error(t, err, `invalid --security-opt 1: "label"`)
  206. })
  207. t.Run("invalid option (no value)", func(t *testing.T) {
  208. secOpts := &container.SecurityOptions{}
  209. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  210. SecurityOpt: []string{"unknown"},
  211. })
  212. assert.Error(t, err, `invalid --security-opt 1: "unknown"`)
  213. })
  214. t.Run("unknown option", func(t *testing.T) {
  215. secOpts := &container.SecurityOptions{}
  216. err := parseSecurityOpt(secOpts, &containertypes.HostConfig{
  217. SecurityOpt: []string{"unknown=something"},
  218. })
  219. assert.Error(t, err, `invalid --security-opt 2: "unknown=something"`)
  220. })
  221. }
  222. func TestParseNNPSecurityOptions(t *testing.T) {
  223. daemonCfg := &configStore{Config: config.Config{NoNewPrivileges: true}}
  224. daemon := &Daemon{}
  225. daemon.configStore.Store(daemonCfg)
  226. opts := &container.SecurityOptions{}
  227. cfg := &containertypes.HostConfig{}
  228. // test NNP when "daemon:true" and "no-new-privileges=false""
  229. cfg.SecurityOpt = []string{"no-new-privileges=false"}
  230. if err := daemon.parseSecurityOpt(&daemonCfg.Config, opts, cfg); err != nil {
  231. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  232. }
  233. if opts.NoNewPrivileges {
  234. t.Fatalf("container.NoNewPrivileges should be FALSE: %v", opts.NoNewPrivileges)
  235. }
  236. // test NNP when "daemon:false" and "no-new-privileges=true""
  237. daemonCfg.NoNewPrivileges = false
  238. cfg.SecurityOpt = []string{"no-new-privileges=true"}
  239. if err := daemon.parseSecurityOpt(&daemonCfg.Config, opts, cfg); err != nil {
  240. t.Fatalf("Unexpected daemon.parseSecurityOpt error: %v", err)
  241. }
  242. if !opts.NoNewPrivileges {
  243. t.Fatalf("container.NoNewPrivileges should be TRUE: %v", opts.NoNewPrivileges)
  244. }
  245. }
  246. func TestVerifyPlatformContainerResources(t *testing.T) {
  247. t.Parallel()
  248. var (
  249. no = false
  250. yes = true
  251. )
  252. withMemoryLimit := func(si *sysinfo.SysInfo) {
  253. si.MemoryLimit = true
  254. }
  255. withSwapLimit := func(si *sysinfo.SysInfo) {
  256. si.SwapLimit = true
  257. }
  258. withOomKillDisable := func(si *sysinfo.SysInfo) {
  259. si.OomKillDisable = true
  260. }
  261. tests := []struct {
  262. name string
  263. resources containertypes.Resources
  264. sysInfo sysinfo.SysInfo
  265. update bool
  266. expectedWarnings []string
  267. }{
  268. {
  269. name: "no-oom-kill-disable",
  270. resources: containertypes.Resources{},
  271. sysInfo: sysInfo(t, withMemoryLimit),
  272. expectedWarnings: []string{},
  273. },
  274. {
  275. name: "oom-kill-disable-disabled",
  276. resources: containertypes.Resources{
  277. OomKillDisable: &no,
  278. },
  279. sysInfo: sysInfo(t, withMemoryLimit),
  280. expectedWarnings: []string{},
  281. },
  282. {
  283. name: "oom-kill-disable-not-supported",
  284. resources: containertypes.Resources{
  285. OomKillDisable: &yes,
  286. },
  287. sysInfo: sysInfo(t, withMemoryLimit),
  288. expectedWarnings: []string{
  289. "Your kernel does not support OomKillDisable. OomKillDisable discarded.",
  290. },
  291. },
  292. {
  293. name: "oom-kill-disable-without-memory-constraints",
  294. resources: containertypes.Resources{
  295. OomKillDisable: &yes,
  296. Memory: 0,
  297. },
  298. sysInfo: sysInfo(t, withMemoryLimit, withOomKillDisable, withSwapLimit),
  299. expectedWarnings: []string{
  300. "OOM killer is disabled for the container, but no memory limit is set, this can result in the system running out of resources.",
  301. },
  302. },
  303. {
  304. name: "oom-kill-disable-with-memory-constraints-but-no-memory-limit-support",
  305. resources: containertypes.Resources{
  306. OomKillDisable: &yes,
  307. Memory: linuxMinMemory,
  308. },
  309. sysInfo: sysInfo(t, withOomKillDisable),
  310. expectedWarnings: []string{
  311. "Your kernel does not support memory limit capabilities or the cgroup is not mounted. Limitation discarded.",
  312. "OOM killer is disabled for the container, but no memory limit is set, this can result in the system running out of resources.",
  313. },
  314. },
  315. {
  316. name: "oom-kill-disable-with-memory-constraints",
  317. resources: containertypes.Resources{
  318. OomKillDisable: &yes,
  319. Memory: linuxMinMemory,
  320. },
  321. sysInfo: sysInfo(t, withMemoryLimit, withOomKillDisable, withSwapLimit),
  322. expectedWarnings: []string{},
  323. },
  324. }
  325. for _, tc := range tests {
  326. tc := tc
  327. t.Run(tc.name, func(t *testing.T) {
  328. t.Parallel()
  329. warnings, err := verifyPlatformContainerResources(&tc.resources, &tc.sysInfo, tc.update)
  330. assert.NilError(t, err)
  331. for _, w := range tc.expectedWarnings {
  332. assert.Assert(t, is.Contains(warnings, w))
  333. }
  334. })
  335. }
  336. }
  337. func sysInfo(t *testing.T, opts ...func(*sysinfo.SysInfo)) sysinfo.SysInfo {
  338. t.Helper()
  339. si := sysinfo.SysInfo{}
  340. for _, opt := range opts {
  341. opt(&si)
  342. }
  343. if si.OomKillDisable {
  344. t.Log(t.Name(), "OOM disable supported")
  345. }
  346. return si
  347. }
  348. const (
  349. // prepare major 0x1FD(509 in decimal) and minor 0x130(304)
  350. DEVNO = 0x11FD30
  351. MAJOR = 509
  352. MINOR = 304
  353. WEIGHT = 1024
  354. )
  355. func deviceTypeMock(t *testing.T, testAndCheck func(string)) {
  356. if os.Getuid() != 0 {
  357. t.Skip("root required") // for mknod
  358. }
  359. t.Parallel()
  360. tempDir, err := os.MkdirTemp("", "tempDevDir"+t.Name())
  361. assert.NilError(t, err, "create temp file")
  362. tempFile := filepath.Join(tempDir, "dev")
  363. defer os.RemoveAll(tempDir)
  364. if err = unix.Mknod(tempFile, unix.S_IFCHR, DEVNO); err != nil {
  365. t.Fatalf("mknod error %s(%x): %v", tempFile, DEVNO, err)
  366. }
  367. testAndCheck(tempFile)
  368. }
  369. func TestGetBlkioWeightDevices(t *testing.T) {
  370. deviceTypeMock(t, func(tempFile string) {
  371. mockResource := containertypes.Resources{
  372. BlkioWeightDevice: []*blkiodev.WeightDevice{{Path: tempFile, Weight: WEIGHT}},
  373. }
  374. weightDevs, err := getBlkioWeightDevices(mockResource)
  375. assert.NilError(t, err, "getBlkioWeightDevices")
  376. assert.Check(t, is.Len(weightDevs, 1), "getBlkioWeightDevices")
  377. assert.Check(t, weightDevs[0].Major == MAJOR, "get major device type")
  378. assert.Check(t, weightDevs[0].Minor == MINOR, "get minor device type")
  379. assert.Check(t, *weightDevs[0].Weight == WEIGHT, "get device weight")
  380. })
  381. }
  382. func TestGetBlkioThrottleDevices(t *testing.T) {
  383. deviceTypeMock(t, func(tempFile string) {
  384. mockDevs := []*blkiodev.ThrottleDevice{{Path: tempFile, Rate: WEIGHT}}
  385. retDevs, err := getBlkioThrottleDevices(mockDevs)
  386. assert.NilError(t, err, "getBlkioThrottleDevices")
  387. assert.Check(t, is.Len(retDevs, 1), "getBlkioThrottleDevices")
  388. assert.Check(t, retDevs[0].Major == MAJOR, "get major device type")
  389. assert.Check(t, retDevs[0].Minor == MINOR, "get minor device type")
  390. assert.Check(t, retDevs[0].Rate == WEIGHT, "get device rate")
  391. })
  392. }