daemon_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package main
  2. import (
  3. "testing"
  4. "github.com/Sirupsen/logrus"
  5. cliflags "github.com/docker/docker/cli/flags"
  6. "github.com/docker/docker/daemon"
  7. "github.com/docker/docker/pkg/testutil/assert"
  8. "github.com/docker/docker/pkg/testutil/tempfile"
  9. "github.com/spf13/pflag"
  10. )
  11. func defaultOptions(configFile string) daemonOptions {
  12. opts := daemonOptions{
  13. daemonConfig: &daemon.Config{},
  14. flags: &pflag.FlagSet{},
  15. common: cliflags.NewCommonOptions(),
  16. }
  17. opts.common.InstallFlags(opts.flags)
  18. opts.daemonConfig.InstallFlags(opts.flags)
  19. opts.flags.StringVar(&opts.configFile, flagDaemonConfigFile, defaultDaemonConfigFile, "")
  20. opts.configFile = configFile
  21. return opts
  22. }
  23. func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
  24. opts := defaultOptions("")
  25. opts.common.Debug = true
  26. loadedConfig, err := loadDaemonCliConfig(opts)
  27. assert.NilError(t, err)
  28. assert.NotNil(t, loadedConfig)
  29. if !loadedConfig.Debug {
  30. t.Fatalf("expected debug to be copied from the common flags, got false")
  31. }
  32. }
  33. func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
  34. opts := defaultOptions("")
  35. opts.common.TLSOptions.CAFile = "/tmp/ca.pem"
  36. opts.common.TLS = true
  37. loadedConfig, err := loadDaemonCliConfig(opts)
  38. assert.NilError(t, err)
  39. assert.NotNil(t, loadedConfig)
  40. assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/tmp/ca.pem")
  41. }
  42. func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
  43. tempFile := tempfile.NewTempFile(t, "config", `{"labels": ["l3=foo"]}`)
  44. defer tempFile.Remove()
  45. configFile := tempFile.Name()
  46. opts := defaultOptions(configFile)
  47. flags := opts.flags
  48. assert.NilError(t, flags.Set(flagDaemonConfigFile, configFile))
  49. assert.NilError(t, flags.Set("label", "l1=bar"))
  50. assert.NilError(t, flags.Set("label", "l2=baz"))
  51. _, err := loadDaemonCliConfig(opts)
  52. assert.Error(t, err, "as a flag and in the configuration file: labels")
  53. }
  54. func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
  55. tempFile := tempfile.NewTempFile(t, "config", `{"tlsverify": true}`)
  56. defer tempFile.Remove()
  57. opts := defaultOptions(tempFile.Name())
  58. opts.common.TLSOptions.CAFile = "/tmp/ca.pem"
  59. loadedConfig, err := loadDaemonCliConfig(opts)
  60. assert.NilError(t, err)
  61. assert.NotNil(t, loadedConfig)
  62. assert.Equal(t, loadedConfig.TLS, true)
  63. }
  64. func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
  65. tempFile := tempfile.NewTempFile(t, "config", `{"tlsverify": false}`)
  66. defer tempFile.Remove()
  67. opts := defaultOptions(tempFile.Name())
  68. opts.common.TLSOptions.CAFile = "/tmp/ca.pem"
  69. loadedConfig, err := loadDaemonCliConfig(opts)
  70. assert.NilError(t, err)
  71. assert.NotNil(t, loadedConfig)
  72. assert.Equal(t, loadedConfig.TLS, true)
  73. }
  74. func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
  75. tempFile := tempfile.NewTempFile(t, "config", `{}`)
  76. defer tempFile.Remove()
  77. opts := defaultOptions(tempFile.Name())
  78. opts.common.TLSOptions.CAFile = "/tmp/ca.pem"
  79. loadedConfig, err := loadDaemonCliConfig(opts)
  80. assert.NilError(t, err)
  81. assert.NotNil(t, loadedConfig)
  82. assert.Equal(t, loadedConfig.TLS, false)
  83. }
  84. func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
  85. tempFile := tempfile.NewTempFile(t, "config", `{"log-level": "warn"}`)
  86. defer tempFile.Remove()
  87. opts := defaultOptions(tempFile.Name())
  88. loadedConfig, err := loadDaemonCliConfig(opts)
  89. assert.NilError(t, err)
  90. assert.NotNil(t, loadedConfig)
  91. assert.Equal(t, loadedConfig.LogLevel, "warn")
  92. assert.Equal(t, logrus.GetLevel(), logrus.WarnLevel)
  93. }
  94. func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
  95. content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
  96. tempFile := tempfile.NewTempFile(t, "config", content)
  97. defer tempFile.Remove()
  98. opts := defaultOptions(tempFile.Name())
  99. loadedConfig, err := loadDaemonCliConfig(opts)
  100. assert.NilError(t, err)
  101. assert.NotNil(t, loadedConfig)
  102. assert.Equal(t, loadedConfig.CommonTLSOptions.CAFile, "/etc/certs/ca.pem")
  103. assert.Equal(t, loadedConfig.LogConfig.Type, "syslog")
  104. }
  105. func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
  106. content := `{
  107. "registry-mirrors": ["https://mirrors.docker.com"],
  108. "insecure-registries": ["https://insecure.docker.com"]
  109. }`
  110. tempFile := tempfile.NewTempFile(t, "config", content)
  111. defer tempFile.Remove()
  112. opts := defaultOptions(tempFile.Name())
  113. loadedConfig, err := loadDaemonCliConfig(opts)
  114. assert.NilError(t, err)
  115. assert.NotNil(t, loadedConfig)
  116. assert.Equal(t, len(loadedConfig.Mirrors), 1)
  117. assert.Equal(t, len(loadedConfig.InsecureRegistries), 1)
  118. }