config_unix_test.go 3.0 KB

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