daemon_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package main
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "strings"
  6. "testing"
  7. "github.com/Sirupsen/logrus"
  8. cliflags "github.com/docker/docker/cli/flags"
  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 := &cliflags.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 := &cliflags.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 := &cliflags.CommonFlags{}
  54. f, err := ioutil.TempFile("", "docker-config-")
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. configFile := f.Name()
  59. defer os.Remove(configFile)
  60. f.Write([]byte(`{"labels": ["l3=foo"]}`))
  61. f.Close()
  62. var labels []string
  63. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  64. flags.String([]string{daemonConfigFileFlag}, "", "")
  65. flags.Var(opts.NewNamedListOptsRef("labels", &labels, opts.ValidateLabel), []string{"-label"}, "")
  66. flags.Set(daemonConfigFileFlag, configFile)
  67. if err := flags.Set("-label", "l1=bar"); err != nil {
  68. t.Fatal(err)
  69. }
  70. if err := flags.Set("-label", "l2=baz"); err != nil {
  71. t.Fatal(err)
  72. }
  73. _, err = loadDaemonCliConfig(c, flags, common, configFile)
  74. if err == nil {
  75. t.Fatalf("expected configuration error, got nil")
  76. }
  77. if !strings.Contains(err.Error(), "labels") {
  78. t.Fatalf("expected labels conflict, got %v", err)
  79. }
  80. }
  81. func TestLoadDaemonCliConfigWithTLSVerify(t *testing.T) {
  82. c := &daemon.Config{}
  83. common := &cliflags.CommonFlags{
  84. TLSOptions: &tlsconfig.Options{
  85. CAFile: "/tmp/ca.pem",
  86. },
  87. }
  88. f, err := ioutil.TempFile("", "docker-config-")
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. configFile := f.Name()
  93. defer os.Remove(configFile)
  94. f.Write([]byte(`{"tlsverify": true}`))
  95. f.Close()
  96. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  97. flags.Bool([]string{"-tlsverify"}, false, "")
  98. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. if loadedConfig == nil {
  103. t.Fatalf("expected configuration %v, got nil", c)
  104. }
  105. if !loadedConfig.TLS {
  106. t.Fatalf("expected TLS enabled, got %q", loadedConfig)
  107. }
  108. }
  109. func TestLoadDaemonCliConfigWithExplicitTLSVerifyFalse(t *testing.T) {
  110. c := &daemon.Config{}
  111. common := &cliflags.CommonFlags{
  112. TLSOptions: &tlsconfig.Options{
  113. CAFile: "/tmp/ca.pem",
  114. },
  115. }
  116. f, err := ioutil.TempFile("", "docker-config-")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. configFile := f.Name()
  121. defer os.Remove(configFile)
  122. f.Write([]byte(`{"tlsverify": false}`))
  123. f.Close()
  124. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  125. flags.Bool([]string{"-tlsverify"}, false, "")
  126. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. if loadedConfig == nil {
  131. t.Fatalf("expected configuration %v, got nil", c)
  132. }
  133. if !loadedConfig.TLS {
  134. t.Fatalf("expected TLS enabled, got %q", loadedConfig)
  135. }
  136. }
  137. func TestLoadDaemonCliConfigWithoutTLSVerify(t *testing.T) {
  138. c := &daemon.Config{}
  139. common := &cliflags.CommonFlags{
  140. TLSOptions: &tlsconfig.Options{
  141. CAFile: "/tmp/ca.pem",
  142. },
  143. }
  144. f, err := ioutil.TempFile("", "docker-config-")
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. configFile := f.Name()
  149. defer os.Remove(configFile)
  150. f.Write([]byte(`{}`))
  151. f.Close()
  152. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  153. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. if loadedConfig == nil {
  158. t.Fatalf("expected configuration %v, got nil", c)
  159. }
  160. if loadedConfig.TLS {
  161. t.Fatalf("expected TLS disabled, got %q", loadedConfig)
  162. }
  163. }
  164. func TestLoadDaemonCliConfigWithLogLevel(t *testing.T) {
  165. c := &daemon.Config{}
  166. common := &cliflags.CommonFlags{}
  167. f, err := ioutil.TempFile("", "docker-config-")
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. configFile := f.Name()
  172. defer os.Remove(configFile)
  173. f.Write([]byte(`{"log-level": "warn"}`))
  174. f.Close()
  175. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  176. flags.String([]string{"-log-level"}, "", "")
  177. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  178. if err != nil {
  179. t.Fatal(err)
  180. }
  181. if loadedConfig == nil {
  182. t.Fatalf("expected configuration %v, got nil", c)
  183. }
  184. if loadedConfig.LogLevel != "warn" {
  185. t.Fatalf("expected warn log level, got %v", loadedConfig.LogLevel)
  186. }
  187. if logrus.GetLevel() != logrus.WarnLevel {
  188. t.Fatalf("expected warn log level, got %v", logrus.GetLevel())
  189. }
  190. }
  191. func TestLoadDaemonConfigWithEmbeddedOptions(t *testing.T) {
  192. c := &daemon.Config{}
  193. common := &cliflags.CommonFlags{}
  194. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  195. flags.String([]string{"-tlscacert"}, "", "")
  196. flags.String([]string{"-log-driver"}, "", "")
  197. f, err := ioutil.TempFile("", "docker-config-")
  198. if err != nil {
  199. t.Fatal(err)
  200. }
  201. configFile := f.Name()
  202. defer os.Remove(configFile)
  203. f.Write([]byte(`{"tlscacert": "/etc/certs/ca.pem", "log-driver": "syslog"}`))
  204. f.Close()
  205. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  206. if err != nil {
  207. t.Fatal(err)
  208. }
  209. if loadedConfig == nil {
  210. t.Fatal("expected configuration, got nil")
  211. }
  212. if loadedConfig.CommonTLSOptions.CAFile != "/etc/certs/ca.pem" {
  213. t.Fatalf("expected CA file path /etc/certs/ca.pem, got %v", loadedConfig.CommonTLSOptions.CAFile)
  214. }
  215. if loadedConfig.LogConfig.Type != "syslog" {
  216. t.Fatalf("expected LogConfig type syslog, got %v", loadedConfig.LogConfig.Type)
  217. }
  218. }
  219. func TestLoadDaemonConfigWithRegistryOptions(t *testing.T) {
  220. c := &daemon.Config{}
  221. common := &cliflags.CommonFlags{}
  222. flags := mflag.NewFlagSet("test", mflag.ContinueOnError)
  223. c.ServiceOptions.InstallCliFlags(flags, absentFromHelp)
  224. f, err := ioutil.TempFile("", "docker-config-")
  225. if err != nil {
  226. t.Fatal(err)
  227. }
  228. configFile := f.Name()
  229. defer os.Remove(configFile)
  230. f.Write([]byte(`{"registry-mirrors": ["https://mirrors.docker.com"], "insecure-registries": ["https://insecure.docker.com"]}`))
  231. f.Close()
  232. loadedConfig, err := loadDaemonCliConfig(c, flags, common, configFile)
  233. if err != nil {
  234. t.Fatal(err)
  235. }
  236. if loadedConfig == nil {
  237. t.Fatal("expected configuration, got nil")
  238. }
  239. m := loadedConfig.Mirrors
  240. if len(m) != 1 {
  241. t.Fatalf("expected 1 mirror, got %d", len(m))
  242. }
  243. r := loadedConfig.InsecureRegistries
  244. if len(r) != 1 {
  245. t.Fatalf("expected 1 insecure registries, got %d", len(r))
  246. }
  247. }