daemon_test.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. // +build daemon
  2. package main
  3. import (
  4. "io/ioutil"
  5. "strings"
  6. "testing"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/docker/docker/cli"
  9. "github.com/docker/docker/daemon"
  10. "github.com/docker/docker/opts"
  11. "github.com/docker/docker/pkg/mflag"
  12. "github.com/docker/go-connections/tlsconfig"
  13. )
  14. func TestLoadDaemonCliConfigWithoutOverriding(t *testing.T) {
  15. c := &daemon.Config{}
  16. common := &cli.CommonFlags{
  17. Debug: true,
  18. }
  19. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  20. loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz")
  21. if err != nil {
  22. t.Fatal(err)
  23. }
  24. if loadedConfig == nil {
  25. t.Fatalf("expected configuration %v, got nil", c)
  26. }
  27. if !loadedConfig.Debug {
  28. t.Fatalf("expected debug to be copied from the common flags, got false")
  29. }
  30. }
  31. func TestLoadDaemonCliConfigWithTLS(t *testing.T) {
  32. c := &daemon.Config{}
  33. common := &cli.CommonFlags{
  34. TLS: true,
  35. TLSOptions: &tlsconfig.Options{
  36. CAFile: "/tmp/ca.pem",
  37. },
  38. }
  39. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  40. loadedConfig, err := loadDaemonCliConfig(c, flags, common, "/tmp/fooobarbaz")
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. if loadedConfig == nil {
  45. t.Fatalf("expected configuration %v, got nil", c)
  46. }
  47. if loadedConfig.CommonTLSOptions.CAFile != "/tmp/ca.pem" {
  48. t.Fatalf("expected /tmp/ca.pem, got %s: %q", loadedConfig.CommonTLSOptions.CAFile, loadedConfig)
  49. }
  50. }
  51. func TestLoadDaemonCliConfigWithConflicts(t *testing.T) {
  52. c := &daemon.Config{}
  53. common := &cli.CommonFlags{}
  54. f, err := ioutil.TempFile("", "docker-config-")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. configFile := f.Name()
  59. f.Write([]byte(`{"labels": ["l3=foo"]}`))
  60. f.Close()
  61. var labels []string
  62. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  63. flags.String([]string{daemonConfigFileFlag}, "", "")
  64. flags.Var(opts.NewNamedListOptsRef("labels", &labels, opts.ValidateLabel), []string{"-label"}, "")
  65. flags.Set(daemonConfigFileFlag, configFile)
  66. if err := flags.Set("-label", "l1=bar"); err != nil {
  67. t.Fatal(err)
  68. }
  69. if err := flags.Set("-label", "l2=baz"); err != nil {
  70. t.Fatal(err)
  71. }
  72. _, err = loadDaemonCliConfig(c, flags, common, configFile)
  73. if err == nil {
  74. t.Fatalf("expected configuration error, got nil")
  75. }
  76. if !strings.Contains(err.Error(), "labels") {
  77. t.Fatalf("expected labels conflict, got %v", err)
  78. }
  79. }
  80. func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
  81. c := &daemon.Config{}
  82. common := &cli.CommonFlags{
  83. TLSOptions: &tlsconfig.Options{
  84. CAFile: "/tmp/ca.pem",
  85. },
  86. }
  87. f, err := ioutil.TempFile("", "docker-config-")
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. configFile := f.Name()
  92. f.Write([]byte(`{"tlsverify": true}`))
  93. f.Close()
  94. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  95. flags.Bool([]string{"-tlsverify"}, false, "")
  96. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if loadedConfig == nil {
  101. t.Fatalf("expected configuration %v, got nil", c)
  102. }
  103. if !loadedConfig.TLS {
  104. t.Fatalf("expected TLS enabled, got %q", loadedConfig)
  105. }
  106. }
  107. func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
  108. c := &daemon.Config{}
  109. common := &cli.CommonFlags{
  110. TLSOptions: &tlsconfig.Options{
  111. CAFile: "/tmp/ca.pem",
  112. },
  113. }
  114. f, err := ioutil.TempFile("", "docker-config-")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. configFile := f.Name()
  119. f.Write([]byte(`{"tlsverify": false}`))
  120. f.Close()
  121. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  122. flags.Bool([]string{"-tlsverify"}, false, "")
  123. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. if loadedConfig == nil {
  128. t.Fatalf("expected configuration %v, got nil", c)
  129. }
  130. if !loadedConfig.TLS {
  131. t.Fatalf("expected TLS enabled, got %q", loadedConfig)
  132. }
  133. }
  134. func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
  135. c := &daemon.Config{}
  136. common := &cli.CommonFlags{
  137. TLSOptions: &tlsconfig.Options{
  138. CAFile: "/tmp/ca.pem",
  139. },
  140. }
  141. f, err := ioutil.TempFile("", "docker-config-")
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. configFile := f.Name()
  146. f.Write([]byte(`{}`))
  147. f.Close()
  148. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  149. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. if loadedConfig == nil {
  154. t.Fatalf("expected configuration %v, got nil", c)
  155. }
  156. if loadedConfig.TLS {
  157. t.Fatalf("expected TLS disabled, got %q", loadedConfig)
  158. }
  159. }
  160. func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
  161. c := &daemon.Config{}
  162. common := &cli.CommonFlags{}
  163. f, err := ioutil.TempFile("", "docker-config-")
  164. if err != nil {
  165. t.Fatal(err)
  166. }
  167. configFile := f.Name()
  168. f.Write([]byte(`{"log-level": "warn"}`))
  169. f.Close()
  170. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  171. flags.String([]string{"-log-level"}, "", "")
  172. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. if loadedConfig == nil {
  177. t.Fatalf("expected configuration %v, got nil", c)
  178. }
  179. if loadedConfig.LogLevel != "warn" {
  180. t.Fatalf("expected warn log level, got %v", loadedConfig.LogLevel)
  181. }
  182. if logrus.GetLevel() != logrus.WarnLevel {
  183. t.Fatalf("expected warn log level, got %v", logrus.GetLevel())
  184. }
  185. }
  186. func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
  187. c := &daemon.Config{}
  188. common := &cli.CommonFlags{}
  189. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  190. flags.String([]string{"-tlscacert"}, "", "")
  191. flags.String([]string{"-log-driver"}, "", "")
  192. f, err := ioutil.TempFile("", "docker-config-")
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. configFile := f.Name()
  197. f.Write([]byte(`{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`))
  198. f.Close()
  199. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. if loadedConfig == nil {
  204. t.Fatal("expected configuration, got nil")
  205. }
  206. if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" {
  207. t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile)
  208. }
  209. if loadedConfig.LogConfig.Type != "syslog" {
  210. t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type)
  211. }
  212. }
  213. func TestLoadDaemonConfigWithMapOptions(t *testing.T) {
  214. c := &daemon.Config{}
  215. common := &cli.CommonFlags{}
  216. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  217. flags.Var(opts.NewNamedMapOpts("cluster-store-opts", c.ClusterOpts, nil), []string{"-cluster-store-opt"}, "")
  218. flags.Var(opts.NewNamedMapOpts("log-opts", c.LogConfig.Config, nil), []string{"-log-opt"}, "")
  219. f, err := ioutil.TempFile("", "docker-config-")
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. configFile := f.Name()
  224. f.Write([]byte(`{
  225. "cluster-store-opts": {"kv.cacertfile": "/var/lib/docker/discovery_certs/ca.pem"},
  226. "log-opts": {"tag": "test"}
  227. }`))
  228. f.Close()
  229. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  230. if err != nil {
  231. t.Fatal(err)
  232. }
  233. if loadedConfig == nil {
  234. t.Fatal("expected configuration, got nil")
  235. }
  236. if loadedConfig.ClusterOpts == nil {
  237. t.Fatal("expected cluster options, got nil")
  238. }
  239. expectedPath := "/var/lib/docker/discovery_certs/ca.pem"
  240. if caPath := loadedConfig.ClusterOpts["kv.cacertfile"]; caPath != expectedPath {
  241. t.Fatalf("expected %s, got %s", expectedPath, caPath)
  242. }
  243. if loadedConfig.LogConfig.Config == nil {
  244. t.Fatal("expected log config options, got nil")
  245. }
  246. if tag := loadedConfig.LogConfig.Config["tag"]; tag != "test" {
  247. t.Fatalf("expected log tag `test`, got %s", tag)
  248. }
  249. }
  250. func TestLoadDaemonConfigWithTrueDefaultValues(t *testing.T) {
  251. c := &daemon.Config{}
  252. common := &cli.CommonFlags{}
  253. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  254. flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")
  255. f, err := ioutil.TempFile("", "docker-config-")
  256. if err != nil {
  257. t.Fatal(err)
  258. }
  259. if err := flags.ParseFlags([]string{}, false); err != nil {
  260. t.Fatal(err)
  261. }
  262. configFile := f.Name()
  263. f.Write([]byte(`{
  264. "userland-proxy": false
  265. }`))
  266. f.Close()
  267. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  268. if err != nil {
  269. t.Fatal(err)
  270. }
  271. if loadedConfig == nil {
  272. t.Fatal("expected configuration, got nil")
  273. }
  274. if loadedConfig.EnableUserlandProxy {
  275. t.Fatal("expected userland proxy to be disabled, got enabled")
  276. }
  277. // make sure reloading doesn't generate configuration
  278. // conflicts after normalizing boolean values.
  279. err = daemon.ReloadConfiguration(configFile, flags, func(reloadedConfig *daemon.Config) {
  280. if reloadedConfig.EnableUserlandProxy {
  281. t.Fatal("expected userland proxy to be disabled, got enabled")
  282. }
  283. })
  284. if err != nil {
  285. t.Fatal(err)
  286. }
  287. }
  288. func TestLoadDaemonConfigWithTrueDefaultValuesLeaveDefaults(t *testing.T) {
  289. c := &daemon.Config{}
  290. common := &cli.CommonFlags{}
  291. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  292. flags.BoolVar(&c.EnableUserlandProxy, []string{"-userland-proxy"}, true, "")
  293. f, err := ioutil.TempFile("", "docker-config-")
  294. if err != nil {
  295. t.Fatal(err)
  296. }
  297. if err := flags.ParseFlags([]string{}, false); err != nil {
  298. t.Fatal(err)
  299. }
  300. configFile := f.Name()
  301. f.Write([]byte(`{}`))
  302. f.Close()
  303. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  304. if err != nil {
  305. t.Fatal(err)
  306. }
  307. if loadedConfig == nil {
  308. t.Fatal("expected configuration, got nil")
  309. }
  310. if !loadedConfig.EnableUserlandProxy {
  311. t.Fatal("expected userland proxy to be enabled, got disabled")
  312. }
  313. }