config_unix_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //go:build !windows
  2. // +build !windows
  3. package config // import "github.com/docker/docker/daemon/config"
  4. import (
  5. "testing"
  6. "github.com/docker/docker/opts"
  7. units "github.com/docker/go-units"
  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. "log-opts": {
  59. "tag": "test_tag"
  60. }
  61. }`
  62. file := fs.NewFile(t, "docker-config", fs.WithContent(configFileData))
  63. defer file.Remove()
  64. c := &Config{
  65. CommonConfig: CommonConfig{
  66. AutoRestart: true,
  67. LogConfig: LogConfig{
  68. Type: "syslog",
  69. Config: map[string]string{"tag": "test"},
  70. },
  71. },
  72. }
  73. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  74. var debug bool
  75. flags.BoolVarP(&debug, "debug", "D", false, "")
  76. flags.Var(opts.NewNamedUlimitOpt("default-ulimits", nil), "default-ulimit", "")
  77. flags.Var(opts.NewNamedMapOpts("log-opts", nil, nil), "log-opt", "")
  78. cc, err := MergeDaemonConfigurations(c, flags, file.Path())
  79. assert.NilError(t, err)
  80. assert.Check(t, cc.Debug)
  81. assert.Check(t, cc.AutoRestart)
  82. expectedLogConfig := LogConfig{
  83. Type: "syslog",
  84. Config: map[string]string{"tag": "test_tag"},
  85. }
  86. assert.Check(t, is.DeepEqual(expectedLogConfig, cc.LogConfig))
  87. expectedUlimits := map[string]*units.Ulimit{
  88. "nofile": {
  89. Name: "nofile",
  90. Hard: 2048,
  91. Soft: 1024,
  92. },
  93. }
  94. assert.Check(t, is.DeepEqual(expectedUlimits, cc.Ulimits))
  95. }
  96. func TestDaemonConfigurationMergeShmSize(t *testing.T) {
  97. data := `{"default-shm-size": "1g"}`
  98. file := fs.NewFile(t, "docker-config", fs.WithContent(data))
  99. defer file.Remove()
  100. c := &Config{}
  101. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  102. shmSize := opts.MemBytes(DefaultShmSize)
  103. flags.Var(&shmSize, "default-shm-size", "")
  104. cc, err := MergeDaemonConfigurations(c, flags, file.Path())
  105. assert.NilError(t, err)
  106. expectedValue := 1 * 1024 * 1024 * 1024
  107. assert.Check(t, is.Equal(int64(expectedValue), cc.ShmSize.Value()))
  108. }