config_linux_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package config // import "github.com/docker/docker/daemon/config"
  2. import (
  3. "testing"
  4. "github.com/docker/docker/api/types"
  5. "github.com/docker/docker/opts"
  6. units "github.com/docker/go-units"
  7. "github.com/spf13/pflag"
  8. "gotest.tools/v3/assert"
  9. is "gotest.tools/v3/assert/cmp"
  10. "gotest.tools/v3/fs"
  11. )
  12. func TestGetConflictFreeConfiguration(t *testing.T) {
  13. configFileData := `
  14. {
  15. "debug": true,
  16. "default-ulimits": {
  17. "nofile": {
  18. "Name": "nofile",
  19. "Hard": 2048,
  20. "Soft": 1024
  21. }
  22. },
  23. "log-opts": {
  24. "tag": "test_tag"
  25. }
  26. }`
  27. file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
  28. defer file.Remove()
  29. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  30. var debug bool
  31. flags.BoolVarP(&debug, "debug", "D", false, "")
  32. flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
  33. flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
  34. cc, err := getConflictFreeConfiguration(file.Path(), flags)
  35. assert.NilError(t, err)
  36. assert.Check(t, cc.Debug)
  37. expectedUlimits := map[string]*units.Ulimit{
  38. "nofile": {
  39. Name: "nofile",
  40. Hard: 2048,
  41. Soft: 1024,
  42. },
  43. }
  44. assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
  45. }
  46. func TestDaemonConfigurationMerge(t *testing.T) {
  47. configFileData := `
  48. {
  49. "debug": true,
  50. "default-ulimits": {
  51. "nofile": {
  52. "Name": "nofile",
  53. "Hard": 2048,
  54. "Soft": 1024
  55. }
  56. }
  57. }`
  58. file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
  59. defer file.Remove()
  60. conf, err := New()
  61. assert.NilError(t, err)
  62. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  63. flags.BoolVarP(&conf.Debug, "debug", "D", false, "")
  64. flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "")
  65. flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "")
  66. flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "")
  67. flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "")
  68. assert.Check(t, flags.Set("restart", "true"))
  69. assert.Check(t, flags.Set("log-driver", "syslog"))
  70. assert.Check(t, flags.Set("log-opt", "tag=from_flag"))
  71. cc, err := MergeDaemonConfigurations(conf, flags, file.Path())
  72. assert.NilError(t, err)
  73. assert.Check(t, cc.Debug)
  74. assert.Check(t, cc.AutoRestart)
  75. expectedLogConfig := LogConfig{
  76. Type: "syslog",
  77. Config: map[string]string{"tag": "from_flag"},
  78. }
  79. assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
  80. expectedUlimits := map[string]*units.Ulimit{
  81. "nofile": {
  82. Name: "nofile",
  83. Hard: 2048,
  84. Soft: 1024,
  85. },
  86. }
  87. assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
  88. }
  89. func TestDaemonConfigurationMergeShmSize(t *testing.T) {
  90. data := `{"default-shm-size": "1g"}`
  91. file := fs.NewFile(t, "docker-config", fs.WithContent(data))
  92. defer file.Remove()
  93. c := &Config{}
  94. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  95. shmSize := opts.MemBytes(DefaultShmSize)
  96. flags.Var(&shmSize, "default-shm-size", "")
  97. cc, err := MergeDaemonConfigurations(c, flags, file.Path())
  98. assert.NilError(t, err)
  99. expectedValue := 1 * 1024 * 1024 * 1024
  100. assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
  101. }
  102. func TestUnixValidateConfigurationErrors(t *testing.T) {
  103. testCases := []struct {
  104. doc string
  105. config *Config
  106. expectedErr string
  107. }{
  108. {
  109. doc: `cannot override the stock runtime`,
  110. config: &Config{
  111. Runtimes: map[string]types.Runtime{
  112. StockRuntimeName: {},
  113. },
  114. },
  115. expectedErr: `runtime name 'runc' is reserved`,
  116. },
  117. }
  118. for _, tc := range testCases {
  119. tc := tc
  120. t.Run(tc.doc, func(t *testing.T) {
  121. err := Validate(tc.config)
  122. assert.ErrorContains(t, err, tc.expectedErr)
  123. })
  124. }
  125. }
  126. func TestUnixGetInitPath(t *testing.T) {
  127. testCases := []struct {
  128. config *Config
  129. expectedInitPath string
  130. }{
  131. {
  132. config: &Config{
  133. InitPath: "some-init-path",
  134. },
  135. expectedInitPath: "some-init-path",
  136. },
  137. {
  138. config: &Config{
  139. DefaultInitBinary: "foo-init-bin",
  140. },
  141. expectedInitPath: "foo-init-bin",
  142. },
  143. {
  144. config: &Config{
  145. InitPath: "init-path-A",
  146. DefaultInitBinary: "init-path-B",
  147. },
  148. expectedInitPath: "init-path-A",
  149. },
  150. {
  151. config: &Config{},
  152. expectedInitPath: "docker-init",
  153. },
  154. }
  155. for _, tc := range testCases {
  156. assert.Equal(t, tc.config.GetInitPath(), tc.expectedInitPath)
  157. }
  158. }