daemon_unix_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // +build !windows
  2. package main
  3. import (
  4. "io/ioutil"
  5. "os"
  6. "testing"
  7. cliflags "github.com/docker/docker/cli/flags"
  8. "github.com/docker/docker/daemon"
  9. "github.com/docker/docker/opts"
  10. "github.com/docker/docker/pkg/mflag"
  11. )
  12. func TestLoadDaemonCliConfigWithDaemonFlags(t *testing.T) {
  13. c := &daemon.Config{}
  14. common := &cliflags.CommonFlags{
  15. Debug: true,
  16. LogLevel: "info",
  17. }
  18. f, err := ioutil.TempFile("", "docker-config-")
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. configFile := f.Name()
  23. f.Write([]byte(`{"log-opts": {"max-size": "1k"}}`))
  24. f.Close()
  25. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  26. flags.String([]string{daemonConfigFileFlag}, "", "")
  27. flags.BoolVar(&c.EnableSelinuxSupport, []string{"-selinux-enabled"}, true, "")
  28. flags.StringVar(&c.LogConfig.Type, []string{"-log-driver"}, "json-file", "")
  29. flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
  30. flags.Set(daemonConfigFileFlag, configFile)
  31. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  32. if err != nil {
  33. t.Fatal(err)
  34. }
  35. if loadedConfig == nil {
  36. t.Fatalf("expected configuration %v, got nil", c)
  37. }
  38. if !loadedConfig.Debug {
  39. t.Fatalf("expected debug mode, got false")
  40. }
  41. if loadedConfig.LogLevel != "info" {
  42. t.Fatalf("expected info log level, got %v", loadedConfig.LogLevel)
  43. }
  44. if !loadedConfig.EnableSelinuxSupport {
  45. t.Fatalf("expected enabled selinux support, got disabled")
  46. }
  47. if loadedConfig.LogConfig.Type != "json-file" {
  48. t.Fatalf("expected LogConfig type json-file, got %v", loadedConfig.LogConfig.Type)
  49. }
  50. if maxSize := loadedConfig.LogConfig.Config["max-size"]; maxSize != "1k" {
  51. t.Fatalf("expected log max-size `1k`, got %s", maxSize)
  52. }
  53. }
  54. func TestLoadDaemonConfigWithNetwork(t *testing.T) {
  55. c := &daemon.Config{}
  56. common := &cliflags.CommonFlags{}
  57. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  58. flags.String([]string{"-bip"}, "", "")
  59. flags.String([]string{"-ip"}, "", "")
  60. f, err := ioutil.TempFile("", "docker-config-")
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. configFile := f.Name()
  65. f.Write([]byte(`{"bip": "127.0.0.2", "ip": "127.0.0.1"}`))
  66. f.Close()
  67. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. if loadedConfig == nil {
  72. t.Fatalf("expected configuration %v, got nil", c)
  73. }
  74. if loadedConfig.IP != "127.0.0.2" {
  75. t.Fatalf("expected IP 127.0.0.2, got %v", loadedConfig.IP)
  76. }
  77. if loadedConfig.DefaultIP.String() != "127.0.0.1" {
  78. t.Fatalf("expected DefaultIP 127.0.0.1, got %s", loadedConfig.DefaultIP)
  79. }
  80. }
  81. func TestLoadDaemonConfigWithMapOptions(t *testing.T) {
  82. c := &daemon.Config{}
  83. common := &cliflags.CommonFlags{}
  84. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  85. flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "")
  86. flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
  87. f, err := ioutil.TempFile("", "docker-config-")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. configFile := f.Name()
  92. f.Write([]byte(`{
  93. "cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"},
  94. "log-opts": {"tag": "test"}
  95. }`))
  96. f.Close()
  97. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. if loadedConfig == nil {
  102. t.Fatal("expected configuration, got nil")
  103. }
  104. if loadedConfig.ClusterOpts == nil {
  105. t.Fatal("expected cluster options, got nil")
  106. }
  107. expectedPath := "/var/lib/docker/discovery_certs/ca.pem"
  108. if caPath := loadedConfig.ClusterOpts["kv.cacertfile"]; caPath != expectedPath {
  109. t.Fatalf("expected %s, got %s", expectedPath, caPath)
  110. }
  111. if loadedConfig.LogConfig.Config == nil {
  112. t.Fatal("expected log config options, got nil")
  113. }
  114. if tag := loadedConfig.LogConfig.Config["tag"]; tag != "test" {
  115. t.Fatalf("expected log tag `test`, got %s", tag)
  116. }
  117. }
  118. func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) {
  119. c := &daemon.Config{}
  120. common := &cliflags.CommonFlags{}
  121. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  122. flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")
  123. f, err := ioutil.TempFile("", "docker-config-")
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if err := flags.ParseFlags([]string{}, false); err != nil {
  128. t.Fatal(err)
  129. }
  130. configFile := f.Name()
  131. f.Write([]byte(`{
  132. "userland-proxy": false
  133. }`))
  134. f.Close()
  135. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  136. if err != nil {
  137. t.Fatal(err)
  138. }
  139. if loadedConfig == nil {
  140. t.Fatal("expected configuration, got nil")
  141. }
  142. if loadedConfig.EnableUserlandProxy {
  143. t.Fatal("expected userland proxy to be disabled, got enabled")
  144. }
  145. // make sure reloading doesn't generate configuration
  146. // conflicts after normalizing boolean values.
  147. err = daemon.ReloadConfiguration(configFile, flags, func(reloadedConfig *daemon.Config) {
  148. if reloadedConfig.EnableUserlandProxy {
  149. t.Fatal("expected userland proxy to be disabled, got enabled")
  150. }
  151. })
  152. if err != nil {
  153. t.Fatal(err)
  154. }
  155. }
  156. func TestLoadDaemonConfigWithTrueDefaultValuesLeaveDefaults(t *testing.T) {
  157. c := &daemon.Config{}
  158. common := &cliflags.CommonFlags{}
  159. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  160. flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")
  161. f, err := ioutil.TempFile("", "docker-config-")
  162. if err != nil {
  163. t.Fatal(err)
  164. }
  165. if err := flags.ParseFlags([]string{}, false); err != nil {
  166. t.Fatal(err)
  167. }
  168. configFile := f.Name()
  169. f.Write([]byte(`{}`))
  170. f.Close()
  171. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if loadedConfig == nil {
  176. t.Fatal("expected configuration, got nil")
  177. }
  178. if !loadedConfig.EnableUserlandProxy {
  179. t.Fatal("expected userland proxy to be enabled, got disabled")
  180. }
  181. }
  182. func TestLoadDaemonConfigWithLegacyRegistryOptions(t *testing.T) {
  183. c := &daemon.Config{}
  184. common := &cliflags.CommonFlags{}
  185. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  186. c.ServiceOptions.InstallCliFlags(flags, absentFromHelp)
  187. f, err := ioutil.TempFile("", "docker-config-")
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. configFile := f.Name()
  192. defer os.Remove(configFile)
  193. f.Write([]byte(`{"disable-legacy-registry": true}`))
  194. f.Close()
  195. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. if loadedConfig == nil {
  200. t.Fatal("expected configuration, got nil")
  201. }
  202. if !loadedConfig.V2Only {
  203. t.Fatal("expected disable-legacy-registry to be true, got false")
  204. }
  205. }