config_unix_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // +build !windows
  2. package daemon
  3. import (
  4. "io/ioutil"
  5. "runtime"
  6. "testing"
  7. "github.com/docker/docker/pkg/testutil/assert"
  8. "github.com/spf13/pflag"
  9. )
  10. func TestDaemonConfigurationMerge(t *testing.T) {
  11. f, err := ioutil.TempFile("", "docker-config-")
  12. if err != nil {
  13. t.Fatal(err)
  14. }
  15. configFile := f.Name()
  16. f.Write([]byte(`
  17. {
  18. "debug": true,
  19. "default-ulimits": {
  20. "nofile": {
  21. "Name": "nofile",
  22. "Hard": 2048,
  23. "Soft": 1024
  24. }
  25. },
  26. "log-opts": {
  27. "tag": "test_tag"
  28. }
  29. }`))
  30. f.Close()
  31. c := &Config{
  32. CommonConfig: CommonConfig{
  33. AutoRestart: true,
  34. LogConfig: LogConfig{
  35. Type: "syslog",
  36. Config: map[string]string{"tag": "test"},
  37. },
  38. },
  39. }
  40. cc, err := MergeDaemonConfigurations(c, nil, configFile)
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if !cc.Debug {
  45. t.Fatalf("expected %v, got %v\n", true, cc.Debug)
  46. }
  47. if !cc.AutoRestart {
  48. t.Fatalf("expected %v, got %v\n", true, cc.AutoRestart)
  49. }
  50. if cc.LogConfig.Type != "syslog" {
  51. t.Fatalf("expected syslog config, got %q\n", cc.LogConfig)
  52. }
  53. if configValue, OK := cc.LogConfig.Config["tag"]; !OK {
  54. t.Fatal("expected syslog config attributes, got nil\n")
  55. } else {
  56. if configValue != "test_tag" {
  57. t.Fatalf("expected syslog config attributes 'tag=test_tag', got 'tag=%s'\n", configValue)
  58. }
  59. }
  60. if cc.Ulimits == nil {
  61. t.Fatal("expected default ulimit config, got nil\n")
  62. } else {
  63. if _, OK := cc.Ulimits["nofile"]; OK {
  64. if cc.Ulimits["nofile"].Name != "nofile" ||
  65. cc.Ulimits["nofile"].Hard != 2048 ||
  66. cc.Ulimits["nofile"].Soft != 1024 {
  67. t.Fatalf("expected default ulimit name, hard and soft are nofile, 2048, 1024, got %s, %d, %d\n", cc.Ulimits["nofile"].Name, cc.Ulimits["nofile"].Hard, cc.Ulimits["nofile"].Soft)
  68. }
  69. } else {
  70. t.Fatal("expected default ulimit name nofile, got nil\n")
  71. }
  72. }
  73. }
  74. func TestDaemonParseShmSize(t *testing.T) {
  75. if runtime.GOOS == "solaris" {
  76. t.Skip("ShmSize not supported on Solaris\n")
  77. }
  78. flags := pflag.NewFlagSet("test", pflag.ContinueOnError)
  79. config := &Config{}
  80. config.InstallFlags(flags)
  81. // By default `--default-shm-size=64M`
  82. expectedValue := 64 * 1024 * 1024
  83. if config.ShmSize.Value() != int64(expectedValue) {
  84. t.Fatalf("expected default shm size %d, got %d", expectedValue, config.ShmSize.Value())
  85. }
  86. assert.NilError(t, flags.Set("default-shm-size", "128M"))
  87. expectedValue = 128 * 1024 * 1024
  88. if config.ShmSize.Value() != int64(expectedValue) {
  89. t.Fatalf("expected default shm size %d, got %d", expectedValue, config.ShmSize.Value())
  90. }
  91. }
  92. func TestDaemonConfigurationMergeShmSize(t *testing.T) {
  93. if runtime.GOOS == "solaris" {
  94. t.Skip("ShmSize not supported on Solaris\n")
  95. }
  96. f, err := ioutil.TempFile("", "docker-config-")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. configFile := f.Name()
  101. f.Write([]byte(`
  102. {
  103. "default-shm-size": "1g"
  104. }`))
  105. f.Close()
  106. c := &Config{}
  107. cc, err := MergeDaemonConfigurations(c, nil, configFile)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. expectedValue := 1 * 1024 * 1024 * 1024
  112. if cc.ShmSize.Value() != int64(expectedValue) {
  113. t.Fatalf("expected default shm size %d, got %d", expectedValue, cc.ShmSize.Value())
  114. }
  115. }