config_linux_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 := New()
  61. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  62. flags.BoolVarP(&conf.Debug, "debug", "D", false, "")
  63. flags.BoolVarP(&conf.AutoRestart, "restart", "r", true, "")
  64. flags.Var(opts.NewNamedUlimitOpt("default-ulimits", &conf.Ulimits), "default-ulimit", "")
  65. flags.StringVar(&conf.LogConfig.Type, "log-driver", "json-file", "")
  66. flags.Var(opts.NewNamedMapOpts("log-opts", conf.LogConfig.Config, nil), "log-opt", "")
  67. assert.Check(t, flags.Set("restart", "true"))
  68. assert.Check(t, flags.Set("log-driver", "syslog"))
  69. assert.Check(t, flags.Set("log-opt", "tag=from_flag"))
  70. cc, err := MergeDaemonConfigurations(conf, flags, file.Path())
  71. assert.NilError(t, err)
  72. assert.Check(t, cc.Debug)
  73. assert.Check(t, cc.AutoRestart)
  74. expectedLogConfig := LogConfig{
  75. Type: "syslog",
  76. Config: map[string]string{"tag": "from_flag"},
  77. }
  78. assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
  79. expectedUlimits := map[string]*units.Ulimit{
  80. "nofile": {
  81. Name: "nofile",
  82. Hard: 2048,
  83. Soft: 1024,
  84. },
  85. }
  86. assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
  87. }
  88. func TestDaemonConfigurationMergeShmSize(t *testing.T) {
  89. data := `{"default-shm-size": "1g"}`
  90. file := fs.NewFile(t, "docker-config", fs.WithContent(data))
  91. defer file.Remove()
  92. c := &Config{}
  93. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  94. shmSize := opts.MemBytes(DefaultShmSize)
  95. flags.Var(&shmSize, "default-shm-size", "")
  96. cc, err := MergeDaemonConfigurations(c, flags, file.Path())
  97. assert.NilError(t, err)
  98. expectedValue := 1 * 1024 * 1024 * 1024
  99. assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
  100. }
  101. func TestUnixValidateConfigurationErrors(t *testing.T) {
  102. testCases := []struct {
  103. doc string
  104. config *Config
  105. expectedErr string
  106. }{
  107. {
  108. doc: `cannot override the stock runtime`,
  109. config: &Config{
  110. Runtimes: map[string]types.Runtime{
  111. StockRuntimeName: {},
  112. },
  113. },
  114. expectedErr: `runtime name 'runc' is reserved`,
  115. },
  116. }
  117. for _, tc := range testCases {
  118. tc := tc
  119. t.Run(tc.doc, func(t *testing.T) {
  120. err := Validate(tc.config)
  121. assert.ErrorContains(t, err, tc.expectedErr)
  122. })
  123. }
  124. }
  125. func TestUnixGetInitPath(t *testing.T) {
  126. testCases := []struct {
  127. config *Config
  128. expectedInitPath string
  129. }{
  130. {
  131. config: &Config{
  132. InitPath: "some-init-path",
  133. },
  134. expectedInitPath: "some-init-path",
  135. },
  136. {
  137. config: &Config{
  138. DefaultInitBinary: "foo-init-bin",
  139. },
  140. expectedInitPath: "foo-init-bin",
  141. },
  142. {
  143. config: &Config{
  144. InitPath: "init-path-A",
  145. DefaultInitBinary: "init-path-B",
  146. },
  147. expectedInitPath: "init-path-A",
  148. },
  149. {
  150. config: &Config{},
  151. expectedInitPath: "docker-init",
  152. },
  153. }
  154. for _, tc := range testCases {
  155. assert.Equal(t, tc.config.GetInitPath(), tc.expectedInitPath)
  156. }
  157. }