daemon_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package main
  2. import (
  3. "testing"
  4. "github.com/docker/docker/daemon/config"
  5. "github.com/sirupsen/logrus"
  6. "github.com/spf13/pflag"
  7. "gotest.tools/assert"
  8. is "gotest.tools/assert/cmp"
  9. "gotest.tools/fs"
  10. )
  11. func defaultOptions(t *testing.T, configFile string) *daemonOptions {
  12. opts := newDaemonOptions(&config.Config{})
  13. opts.flags = &pflag.FlagSet{}
  14. opts.InstallFlags(opts.flags)
  15. if err := installConfigFlags(opts.daemonConfig, opts.flags); err != nil {
  16. t.Fatal(err)
  17. }
  18. defaultDaemonConfigFile, err := getDefaultDaemonConfigFile()
  19. assert.NilError(t, err)
  20. opts.flags.StringVar(&opts.configFile, "config-file", defaultDaemonConfigFile, "")
  21. opts.configFile = configFile
  22. return opts
  23. }
  24. func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
  25. opts := defaultOptions(t, "")
  26. opts.Debug = true
  27. loadedConfig, err := loadDaemonCliConfig(opts)
  28. assert.NilError(t, err)
  29. assert.Assert(t, loadedConfig != nil)
  30. if !loadedConfig.Debug {
  31. t.Fatalf("expected debug to be copied from the common flags, got false")
  32. }
  33. }
  34. func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
  35. opts := defaultOptions(t, "")
  36. opts.TLSOptions.CAFile = "/tmp/ca.pem"
  37. opts.TLS = true
  38. loadedConfig, err := loadDaemonCliConfig(opts)
  39. assert.NilError(t, err)
  40. assert.Assert(t, loadedConfig != nil)
  41. assert.Check(t, is.Equal("/tmp/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
  42. }
  43. func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
  44. tempFile := fs.NewFile(t, "config", fs.WithContent(`{"labels": ["l3=foo"]}`))
  45. defer tempFile.Remove()
  46. configFile := tempFile.Path()
  47. opts := defaultOptions(t, configFile)
  48. flags := opts.flags
  49. assert.Check(t, flags.Set("config-file", configFile))
  50. assert.Check(t, flags.Set("label", "l1=bar"))
  51. assert.Check(t, flags.Set("label", "l2=baz"))
  52. _, err := loadDaemonCliConfig(opts)
  53. assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: labels"))
  54. }
  55. func TestLoadDaemonCliWithConflictingNodeGenericResources(t *testing.T) {
  56. tempFile := fs.NewFile(t, "config", fs.WithContent(`{"node-generic-resources": ["foo=bar", "bar=baz"]}`))
  57. defer tempFile.Remove()
  58. configFile := tempFile.Path()
  59. opts := defaultOptions(t, configFile)
  60. flags := opts.flags
  61. assert.Check(t, flags.Set("config-file", configFile))
  62. assert.Check(t, flags.Set("node-generic-resource", "r1=bar"))
  63. assert.Check(t, flags.Set("node-generic-resource", "r2=baz"))
  64. _, err := loadDaemonCliConfig(opts)
  65. assert.Check(t, is.ErrorContains(err, "as a flag and in the configuration file: node-generic-resources"))
  66. }
  67. func TestLoadDaemonCliWithConflictingLabels(t *testing.T) {
  68. opts := defaultOptions(t, "")
  69. flags := opts.flags
  70. assert.Check(t, flags.Set("label", "foo=bar"))
  71. assert.Check(t, flags.Set("label", "foo=baz"))
  72. _, err := loadDaemonCliConfig(opts)
  73. assert.Check(t, is.Error(err, "conflict labels for foo=baz and foo=bar"))
  74. }
  75. func TestLoadDaemonCliWithDuplicateLabels(t *testing.T) {
  76. opts := defaultOptions(t, "")
  77. flags := opts.flags
  78. assert.Check(t, flags.Set("label", "foo=the-same"))
  79. assert.Check(t, flags.Set("label", "foo=the-same"))
  80. _, err := loadDaemonCliConfig(opts)
  81. assert.Check(t, err)
  82. }
  83. func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
  84. tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": true}`))
  85. defer tempFile.Remove()
  86. opts := defaultOptions(t, tempFile.Path())
  87. opts.TLSOptions.CAFile = "/tmp/ca.pem"
  88. loadedConfig, err := loadDaemonCliConfig(opts)
  89. assert.NilError(t, err)
  90. assert.Assert(t, loadedConfig != nil)
  91. assert.Check(t, is.Equal(loadedConfig.TLS, true))
  92. }
  93. func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
  94. tempFile := fs.NewFile(t, "config", fs.WithContent(`{"tlsverify": false}`))
  95. defer tempFile.Remove()
  96. opts := defaultOptions(t, tempFile.Path())
  97. opts.TLSOptions.CAFile = "/tmp/ca.pem"
  98. loadedConfig, err := loadDaemonCliConfig(opts)
  99. assert.NilError(t, err)
  100. assert.Assert(t, loadedConfig != nil)
  101. assert.Check(t, loadedConfig.TLS)
  102. }
  103. func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
  104. tempFile := fs.NewFile(t, "config", fs.WithContent(`{}`))
  105. defer tempFile.Remove()
  106. opts := defaultOptions(t, tempFile.Path())
  107. opts.TLSOptions.CAFile = "/tmp/ca.pem"
  108. loadedConfig, err := loadDaemonCliConfig(opts)
  109. assert.NilError(t, err)
  110. assert.Assert(t, loadedConfig != nil)
  111. assert.Check(t, !loadedConfig.TLS)
  112. }
  113. func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
  114. tempFile := fs.NewFile(t, "config", fs.WithContent(`{"log-level": "warn"}`))
  115. defer tempFile.Remove()
  116. opts := defaultOptions(t, tempFile.Path())
  117. loadedConfig, err := loadDaemonCliConfig(opts)
  118. assert.NilError(t, err)
  119. assert.Assert(t, loadedConfig != nil)
  120. assert.Check(t, is.Equal("warn", loadedConfig.LogLevel))
  121. }
  122. func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
  123. content := `{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`
  124. tempFile := fs.NewFile(t, "config", fs.WithContent(content))
  125. defer tempFile.Remove()
  126. opts := defaultOptions(t, tempFile.Path())
  127. loadedConfig, err := loadDaemonCliConfig(opts)
  128. assert.NilError(t, err)
  129. assert.Assert(t, loadedConfig != nil)
  130. assert.Check(t, is.Equal("/etc/certs/ca.pem", loadedConfig.CommonTLSOptions.CAFile))
  131. assert.Check(t, is.Equal("syslog", loadedConfig.LogConfig.Type))
  132. }
  133. func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
  134. content := `{
  135. "allow-nondistributable-artifacts": ["allow-nondistributable-artifacts.com"],
  136. "registry-mirrors": ["https://mirrors.docker.com"],
  137. "insecure-registries": ["https://insecure.docker.com"]
  138. }`
  139. tempFile := fs.NewFile(t, "config", fs.WithContent(content))
  140. defer tempFile.Remove()
  141. opts := defaultOptions(t, tempFile.Path())
  142. loadedConfig, err := loadDaemonCliConfig(opts)
  143. assert.NilError(t, err)
  144. assert.Assert(t, loadedConfig != nil)
  145. assert.Check(t, is.Len(loadedConfig.AllowNondistributableArtifacts, 1))
  146. assert.Check(t, is.Len(loadedConfig.Mirrors, 1))
  147. assert.Check(t, is.Len(loadedConfig.InsecureRegistries, 1))
  148. }
  149. func TestConfigureDaemonLogs(t *testing.T) {
  150. conf := &config.Config{}
  151. err := configureDaemonLogs(conf)
  152. assert.NilError(t, err)
  153. assert.Check(t, is.Equal(logrus.InfoLevel, logrus.GetLevel()))
  154. conf.LogLevel = "warn"
  155. err = configureDaemonLogs(conf)
  156. assert.NilError(t, err)
  157. assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
  158. conf.LogLevel = "foobar"
  159. err = configureDaemonLogs(conf)
  160. assert.Error(t, err, "unable to parse logging level: foobar")
  161. // log level should not be changed after a failure
  162. assert.Check(t, is.Equal(logrus.WarnLevel, logrus.GetLevel()))
  163. }