config_linux_test.go 4.5 KB

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