daemon_test.go 11 KB

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