daemon_test.go 4.6 KB

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