daemon_unix_test.go 5.7 KB

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