daemon_test.go 8.7 KB

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