daemon_unix_test.go 13 KB

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