docker_cli_daemon_test.go 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728
  1. // +build daemon,!windows
  2. package main
  3. import (
  4. "bytes"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "regexp"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/docker/libnetwork/iptables"
  17. "github.com/docker/libtrust"
  18. "github.com/go-check/check"
  19. )
  20. func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
  21. if err := s.d.StartWithBusybox(); err != nil {
  22. c.Fatalf("Could not start daemon with busybox: %v", err)
  23. }
  24. if out, err := s.d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
  25. c.Fatalf("Could not run top1: err=%v\n%s", err, out)
  26. }
  27. // --restart=no by default
  28. if out, err := s.d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
  29. c.Fatalf("Could not run top2: err=%v\n%s", err, out)
  30. }
  31. testRun := func(m map[string]bool, prefix string) {
  32. var format string
  33. for cont, shouldRun := range m {
  34. out, err := s.d.Cmd("ps")
  35. if err != nil {
  36. c.Fatalf("Could not run ps: err=%v\n%q", err, out)
  37. }
  38. if shouldRun {
  39. format = "%scontainer %q is not running"
  40. } else {
  41. format = "%scontainer %q is running"
  42. }
  43. if shouldRun != strings.Contains(out, cont) {
  44. c.Fatalf(format, prefix, cont)
  45. }
  46. }
  47. }
  48. testRun(map[string]bool{"top1": true, "top2": true}, "")
  49. if err := s.d.Restart(); err != nil {
  50. c.Fatalf("Could not restart daemon: %v", err)
  51. }
  52. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  53. }
  54. func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) {
  55. if err := s.d.StartWithBusybox(); err != nil {
  56. c.Fatal(err)
  57. }
  58. if out, err := s.d.Cmd("run", "-d", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  59. c.Fatal(err, out)
  60. }
  61. if err := s.d.Restart(); err != nil {
  62. c.Fatal(err)
  63. }
  64. if _, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
  65. c.Fatal(err)
  66. }
  67. if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  68. c.Fatal(err, out)
  69. }
  70. out, err := s.d.Cmd("inspect", "-f", "{{json .Mounts}}", "volrestarttest1")
  71. c.Assert(err, check.IsNil)
  72. if _, err := inspectMountPointJSON(out, "/foo"); err != nil {
  73. c.Fatalf("Expected volume to exist: /foo, error: %v\n", err)
  74. }
  75. }
  76. // #11008
  77. func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
  78. err := s.d.StartWithBusybox()
  79. c.Assert(err, check.IsNil)
  80. out, err := s.d.Cmd("run", "-d", "--name", "top1", "--restart", "always", "busybox:latest", "top")
  81. c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out))
  82. out, err = s.d.Cmd("run", "-d", "--name", "top2", "--restart", "unless-stopped", "busybox:latest", "top")
  83. c.Assert(err, check.IsNil, check.Commentf("run top2: %v", out))
  84. testRun := func(m map[string]bool, prefix string) {
  85. var format string
  86. for name, shouldRun := range m {
  87. out, err := s.d.Cmd("ps")
  88. c.Assert(err, check.IsNil, check.Commentf("run ps: %v", out))
  89. if shouldRun {
  90. format = "%scontainer %q is not running"
  91. } else {
  92. format = "%scontainer %q is running"
  93. }
  94. c.Assert(strings.Contains(out, name), check.Equals, shouldRun, check.Commentf(format, prefix, name))
  95. }
  96. }
  97. // both running
  98. testRun(map[string]bool{"top1": true, "top2": true}, "")
  99. out, err = s.d.Cmd("stop", "top1")
  100. c.Assert(err, check.IsNil, check.Commentf(out))
  101. out, err = s.d.Cmd("stop", "top2")
  102. c.Assert(err, check.IsNil, check.Commentf(out))
  103. // both stopped
  104. testRun(map[string]bool{"top1": false, "top2": false}, "")
  105. err = s.d.Restart()
  106. c.Assert(err, check.IsNil)
  107. // restart=always running
  108. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  109. out, err = s.d.Cmd("start", "top2")
  110. c.Assert(err, check.IsNil, check.Commentf("start top2: %v", out))
  111. err = s.d.Restart()
  112. c.Assert(err, check.IsNil)
  113. // both running
  114. testRun(map[string]bool{"top1": true, "top2": true}, "After second daemon restart: ")
  115. }
  116. func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) {
  117. if err := s.d.Start("--iptables=false"); err != nil {
  118. c.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  119. }
  120. }
  121. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  122. // no longer has an IP associated, we should gracefully handle that case and associate
  123. // an IP with it rather than fail daemon start
  124. func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) {
  125. // rather than depending on brctl commands to verify docker0 is created and up
  126. // let's start the daemon and stop it, and then make a modification to run the
  127. // actual test
  128. if err := s.d.Start(); err != nil {
  129. c.Fatalf("Could not start daemon: %v", err)
  130. }
  131. if err := s.d.Stop(); err != nil {
  132. c.Fatalf("Could not stop daemon: %v", err)
  133. }
  134. // now we will remove the ip from docker0 and then try starting the daemon
  135. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  136. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  137. if err != nil {
  138. c.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  139. }
  140. if err := s.d.Start(); err != nil {
  141. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  142. c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  143. }
  144. }
  145. func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) {
  146. if err := s.d.StartWithBusybox(); err != nil {
  147. c.Fatalf("Could not start daemon with busybox: %v", err)
  148. }
  149. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  150. c.Fatalf("Could not run top: %s, %v", out, err)
  151. }
  152. // get output from iptables with container running
  153. ipTablesSearchString := "tcp dpt:80"
  154. ipTablesCmd := exec.Command("iptables", "-nvL")
  155. out, _, err := runCommandWithOutput(ipTablesCmd)
  156. if err != nil {
  157. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  158. }
  159. if !strings.Contains(out, ipTablesSearchString) {
  160. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  161. }
  162. if err := s.d.Stop(); err != nil {
  163. c.Fatalf("Could not stop daemon: %v", err)
  164. }
  165. // get output from iptables after restart
  166. ipTablesCmd = exec.Command("iptables", "-nvL")
  167. out, _, err = runCommandWithOutput(ipTablesCmd)
  168. if err != nil {
  169. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  170. }
  171. if strings.Contains(out, ipTablesSearchString) {
  172. c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  173. }
  174. }
  175. func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) {
  176. if err := s.d.StartWithBusybox(); err != nil {
  177. c.Fatalf("Could not start daemon with busybox: %v", err)
  178. }
  179. if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  180. c.Fatalf("Could not run top: %s, %v", out, err)
  181. }
  182. // get output from iptables with container running
  183. ipTablesSearchString := "tcp dpt:80"
  184. ipTablesCmd := exec.Command("iptables", "-nvL")
  185. out, _, err := runCommandWithOutput(ipTablesCmd)
  186. if err != nil {
  187. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  188. }
  189. if !strings.Contains(out, ipTablesSearchString) {
  190. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  191. }
  192. if err := s.d.Restart(); err != nil {
  193. c.Fatalf("Could not restart daemon: %v", err)
  194. }
  195. // make sure the container is not running
  196. runningOut, err := s.d.Cmd("inspect", "--format='{{.State.Running}}'", "top")
  197. if err != nil {
  198. c.Fatalf("Could not inspect on container: %s, %v", out, err)
  199. }
  200. if strings.TrimSpace(runningOut) != "true" {
  201. c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  202. }
  203. // get output from iptables after restart
  204. ipTablesCmd = exec.Command("iptables", "-nvL")
  205. out, _, err = runCommandWithOutput(ipTablesCmd)
  206. if err != nil {
  207. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  208. }
  209. if !strings.Contains(out, ipTablesSearchString) {
  210. c.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  211. }
  212. }
  213. // TestDaemonIPv6Enabled checks that when the daemon is started with --ipv6=true that the docker0 bridge
  214. // has the fe80::1 address and that a container is assigned a link-local address
  215. func (s *DockerSuite) TestDaemonIPv6Enabled(c *check.C) {
  216. testRequires(c, IPv6)
  217. if err := setupV6(); err != nil {
  218. c.Fatal("Could not set up host for IPv6 tests")
  219. }
  220. d := NewDaemon(c)
  221. if err := d.StartWithBusybox("--ipv6"); err != nil {
  222. c.Fatal(err)
  223. }
  224. defer d.Stop()
  225. iface, err := net.InterfaceByName("docker0")
  226. if err != nil {
  227. c.Fatalf("Error getting docker0 interface: %v", err)
  228. }
  229. addrs, err := iface.Addrs()
  230. if err != nil {
  231. c.Fatalf("Error getting addresses for docker0 interface: %v", err)
  232. }
  233. var found bool
  234. expected := "fe80::1/64"
  235. for i := range addrs {
  236. if addrs[i].String() == expected {
  237. found = true
  238. }
  239. }
  240. if !found {
  241. c.Fatalf("Bridge does not have an IPv6 Address")
  242. }
  243. if out, err := d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest"); err != nil {
  244. c.Fatalf("Could not run container: %s, %v", out, err)
  245. }
  246. out, err := d.Cmd("inspect", "--format", "'{{.NetworkSettings.LinkLocalIPv6Address}}'", "ipv6test")
  247. out = strings.Trim(out, " \r\n'")
  248. if err != nil {
  249. c.Fatalf("Error inspecting container: %s, %v", out, err)
  250. }
  251. if ip := net.ParseIP(out); ip == nil {
  252. c.Fatalf("Container should have a link-local IPv6 address")
  253. }
  254. out, err = d.Cmd("inspect", "--format", "'{{.NetworkSettings.GlobalIPv6Address}}'", "ipv6test")
  255. out = strings.Trim(out, " \r\n'")
  256. if err != nil {
  257. c.Fatalf("Error inspecting container: %s, %v", out, err)
  258. }
  259. if ip := net.ParseIP(out); ip != nil {
  260. c.Fatalf("Container should not have a global IPv6 address: %v", out)
  261. }
  262. if err := teardownV6(); err != nil {
  263. c.Fatal("Could not perform teardown for IPv6 tests")
  264. }
  265. }
  266. // TestDaemonIPv6FixedCIDR checks that when the daemon is started with --ipv6=true and a fixed CIDR
  267. // that running containers are given a link-local and global IPv6 address
  268. func (s *DockerSuite) TestDaemonIPv6FixedCIDR(c *check.C) {
  269. testRequires(c, IPv6)
  270. if err := setupV6(); err != nil {
  271. c.Fatal("Could not set up host for IPv6 tests")
  272. }
  273. d := NewDaemon(c)
  274. if err := d.StartWithBusybox("--ipv6", "--fixed-cidr-v6='2001:db8:1::/64'"); err != nil {
  275. c.Fatalf("Could not start daemon with busybox: %v", err)
  276. }
  277. defer d.Stop()
  278. if out, err := d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest"); err != nil {
  279. c.Fatalf("Could not run container: %s, %v", out, err)
  280. }
  281. out, err := d.Cmd("inspect", "--format", "'{{.NetworkSettings.LinkLocalIPv6Address}}'", "ipv6test")
  282. out = strings.Trim(out, " \r\n'")
  283. if err != nil {
  284. c.Fatalf("Error inspecting container: %s, %v", out, err)
  285. }
  286. if ip := net.ParseIP(out); ip == nil {
  287. c.Fatalf("Container should have a link-local IPv6 address")
  288. }
  289. out, err = d.Cmd("inspect", "--format", "'{{.NetworkSettings.GlobalIPv6Address}}'", "ipv6test")
  290. out = strings.Trim(out, " \r\n'")
  291. if err != nil {
  292. c.Fatalf("Error inspecting container: %s, %v", out, err)
  293. }
  294. if ip := net.ParseIP(out); ip == nil {
  295. c.Fatalf("Container should have a global IPv6 address")
  296. }
  297. if err := teardownV6(); err != nil {
  298. c.Fatal("Could not perform teardown for IPv6 tests")
  299. }
  300. }
  301. func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
  302. c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
  303. }
  304. func (s *DockerSuite) TestDaemonStartWithBackwardCompatibility(c *check.C) {
  305. var validCommandArgs = [][]string{
  306. {"--selinux-enabled", "-l", "info"},
  307. {"--insecure-registry", "daemon"},
  308. }
  309. var invalidCommandArgs = [][]string{
  310. {"--selinux-enabled", "--storage-opt"},
  311. {"-D", "-b"},
  312. {"--config", "/tmp"},
  313. }
  314. for _, args := range validCommandArgs {
  315. d := NewDaemon(c)
  316. d.Command = "--daemon"
  317. if err := d.Start(args...); err != nil {
  318. c.Fatalf("Daemon should have started successfully with --daemon %v: %v", args, err)
  319. }
  320. d.Stop()
  321. }
  322. for _, args := range invalidCommandArgs {
  323. d := NewDaemon(c)
  324. if err := d.Start(args...); err == nil {
  325. d.Stop()
  326. c.Fatalf("Daemon should have failed to start with %v", args)
  327. }
  328. }
  329. }
  330. func (s *DockerSuite) TestDaemonStartWithDaemonCommand(c *check.C) {
  331. type kind int
  332. const (
  333. common kind = iota
  334. daemon
  335. )
  336. var flags = []map[kind][]string{
  337. {common: {"-l", "info"}, daemon: {"--selinux-enabled"}},
  338. {common: {"-D"}, daemon: {"--selinux-enabled", "-r"}},
  339. {common: {"-D"}, daemon: {"--restart"}},
  340. {common: {"--debug"}, daemon: {"--log-driver=json-file", "--log-opt=max-size=1k"}},
  341. }
  342. var invalidGlobalFlags = [][]string{
  343. //Invalid because you cannot pass daemon flags as global flags.
  344. {"--selinux-enabled", "-l", "info"},
  345. {"-D", "-r"},
  346. {"--config", "/tmp"},
  347. }
  348. // `docker daemon -l info --selinux-enabled`
  349. // should NOT error out
  350. for _, f := range flags {
  351. d := NewDaemon(c)
  352. args := append(f[common], f[daemon]...)
  353. if err := d.Start(args...); err != nil {
  354. c.Fatalf("Daemon should have started successfully with %v: %v", args, err)
  355. }
  356. d.Stop()
  357. }
  358. // `docker -l info daemon --selinux-enabled`
  359. // should error out
  360. for _, f := range flags {
  361. d := NewDaemon(c)
  362. d.GlobalFlags = f[common]
  363. if err := d.Start(f[daemon]...); err == nil {
  364. d.Stop()
  365. c.Fatalf("Daemon should have failed to start with docker %v daemon %v", d.GlobalFlags, f[daemon])
  366. }
  367. }
  368. for _, f := range invalidGlobalFlags {
  369. cmd := exec.Command(dockerBinary, append(f, "daemon")...)
  370. errch := make(chan error)
  371. var err error
  372. go func() {
  373. errch <- cmd.Run()
  374. }()
  375. select {
  376. case <-time.After(time.Second):
  377. cmd.Process.Kill()
  378. case err = <-errch:
  379. }
  380. if err == nil {
  381. c.Fatalf("Daemon should have failed to start with docker %v daemon", f)
  382. }
  383. }
  384. }
  385. func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
  386. if err := s.d.Start("--log-level=debug"); err != nil {
  387. c.Fatal(err)
  388. }
  389. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  390. if !strings.Contains(string(content), `level=debug`) {
  391. c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
  392. }
  393. }
  394. func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) {
  395. // we creating new daemons to create new logFile
  396. if err := s.d.Start("--log-level=fatal"); err != nil {
  397. c.Fatal(err)
  398. }
  399. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  400. if strings.Contains(string(content), `level=debug`) {
  401. c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
  402. }
  403. }
  404. func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) {
  405. if err := s.d.Start("-D"); err != nil {
  406. c.Fatal(err)
  407. }
  408. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  409. if !strings.Contains(string(content), `level=debug`) {
  410. c.Fatalf(`Should have level="debug" in log file using -D:\n%s`, string(content))
  411. }
  412. }
  413. func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) {
  414. if err := s.d.Start("--debug"); err != nil {
  415. c.Fatal(err)
  416. }
  417. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  418. if !strings.Contains(string(content), `level=debug`) {
  419. c.Fatalf(`Should have level="debug" in log file using --debug:\n%s`, string(content))
  420. }
  421. }
  422. func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) {
  423. if err := s.d.Start("--debug", "--log-level=fatal"); err != nil {
  424. c.Fatal(err)
  425. }
  426. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  427. if !strings.Contains(string(content), `level=debug`) {
  428. c.Fatalf(`Should have level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
  429. }
  430. }
  431. func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) {
  432. listeningPorts := [][]string{
  433. {"0.0.0.0", "0.0.0.0", "5678"},
  434. {"127.0.0.1", "127.0.0.1", "1234"},
  435. {"localhost", "127.0.0.1", "1235"},
  436. }
  437. cmdArgs := make([]string, 0, len(listeningPorts)*2)
  438. for _, hostDirective := range listeningPorts {
  439. cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
  440. }
  441. if err := s.d.StartWithBusybox(cmdArgs...); err != nil {
  442. c.Fatalf("Could not start daemon with busybox: %v", err)
  443. }
  444. for _, hostDirective := range listeningPorts {
  445. output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
  446. if err == nil {
  447. c.Fatalf("Container should not start, expected port already allocated error: %q", output)
  448. } else if !strings.Contains(output, "port is already allocated") {
  449. c.Fatalf("Expected port is already allocated error: %q", output)
  450. }
  451. }
  452. }
  453. func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) {
  454. // TODO: skip or update for Windows daemon
  455. os.Remove("/etc/docker/key.json")
  456. if err := s.d.Start(); err != nil {
  457. c.Fatalf("Could not start daemon: %v", err)
  458. }
  459. s.d.Stop()
  460. k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  461. if err != nil {
  462. c.Fatalf("Error opening key file")
  463. }
  464. kid := k.KeyID()
  465. // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
  466. if len(kid) != 59 {
  467. c.Fatalf("Bad key ID: %s", kid)
  468. }
  469. }
  470. func (s *DockerDaemonSuite) TestDaemonKeyMigration(c *check.C) {
  471. // TODO: skip or update for Windows daemon
  472. os.Remove("/etc/docker/key.json")
  473. k1, err := libtrust.GenerateECP256PrivateKey()
  474. if err != nil {
  475. c.Fatalf("Error generating private key: %s", err)
  476. }
  477. if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil {
  478. c.Fatalf("Error creating .docker directory: %s", err)
  479. }
  480. if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil {
  481. c.Fatalf("Error saving private key: %s", err)
  482. }
  483. if err := s.d.Start(); err != nil {
  484. c.Fatalf("Could not start daemon: %v", err)
  485. }
  486. s.d.Stop()
  487. k2, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  488. if err != nil {
  489. c.Fatalf("Error opening key file")
  490. }
  491. if k1.KeyID() != k2.KeyID() {
  492. c.Fatalf("Key not migrated")
  493. }
  494. }
  495. // GH#11320 - verify that the daemon exits on failure properly
  496. // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
  497. // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
  498. func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
  499. //attempt to start daemon with incorrect flags (we know -b and --bip conflict)
  500. if err := s.d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
  501. //verify we got the right error
  502. if !strings.Contains(err.Error(), "Daemon exited and never started") {
  503. c.Fatalf("Expected daemon not to start, got %v", err)
  504. }
  505. // look in the log and make sure we got the message that daemon is shutting down
  506. runCmd := exec.Command("grep", "Error starting daemon", s.d.LogfileName())
  507. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  508. c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
  509. }
  510. } else {
  511. //if we didn't get an error and the daemon is running, this is a failure
  512. c.Fatal("Conflicting options should cause the daemon to error out with a failure")
  513. }
  514. }
  515. func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
  516. d := s.d
  517. err := d.Start("--bridge", "nosuchbridge")
  518. c.Assert(err, check.NotNil, check.Commentf("--bridge option with an invalid bridge should cause the daemon to fail"))
  519. defer d.Restart()
  520. bridgeName := "external-bridge"
  521. bridgeIP := "192.169.1.1/24"
  522. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  523. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  524. c.Assert(err, check.IsNil, check.Commentf(out))
  525. defer deleteInterface(c, bridgeName)
  526. err = d.StartWithBusybox("--bridge", bridgeName)
  527. c.Assert(err, check.IsNil)
  528. ipTablesSearchString := bridgeIPNet.String()
  529. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  530. out, _, err = runCommandWithOutput(ipTablesCmd)
  531. c.Assert(err, check.IsNil)
  532. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  533. check.Commentf("iptables output should have contained %q, but was %q",
  534. ipTablesSearchString, out))
  535. _, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
  536. c.Assert(err, check.IsNil)
  537. containerIP := d.findContainerIP("ExtContainer")
  538. ip := net.ParseIP(containerIP)
  539. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  540. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  541. containerIP))
  542. }
  543. func createInterface(c *check.C, ifType string, ifName string, ipNet string) (string, error) {
  544. args := []string{"link", "add", "name", ifName, "type", ifType}
  545. ipLinkCmd := exec.Command("ip", args...)
  546. out, _, err := runCommandWithOutput(ipLinkCmd)
  547. if err != nil {
  548. return out, err
  549. }
  550. ifCfgCmd := exec.Command("ifconfig", ifName, ipNet, "up")
  551. out, _, err = runCommandWithOutput(ifCfgCmd)
  552. return out, err
  553. }
  554. func deleteInterface(c *check.C, ifName string) {
  555. ifCmd := exec.Command("ip", "link", "delete", ifName)
  556. out, _, err := runCommandWithOutput(ifCmd)
  557. c.Assert(err, check.IsNil, check.Commentf(out))
  558. flushCmd := exec.Command("iptables", "-t", "nat", "--flush")
  559. out, _, err = runCommandWithOutput(flushCmd)
  560. c.Assert(err, check.IsNil, check.Commentf(out))
  561. flushCmd = exec.Command("iptables", "--flush")
  562. out, _, err = runCommandWithOutput(flushCmd)
  563. c.Assert(err, check.IsNil, check.Commentf(out))
  564. }
  565. func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
  566. // TestDaemonBridgeIP Steps
  567. // 1. Delete the existing docker0 Bridge
  568. // 2. Set --bip daemon configuration and start the new Docker Daemon
  569. // 3. Check if the bip config has taken effect using ifconfig and iptables commands
  570. // 4. Launch a Container and make sure the IP-Address is in the expected subnet
  571. // 5. Delete the docker0 Bridge
  572. // 6. Restart the Docker Daemon (via deferred action)
  573. // This Restart takes care of bringing docker0 interface back to auto-assigned IP
  574. defaultNetworkBridge := "docker0"
  575. deleteInterface(c, defaultNetworkBridge)
  576. d := s.d
  577. bridgeIP := "192.169.1.1/24"
  578. ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  579. err := d.StartWithBusybox("--bip", bridgeIP)
  580. c.Assert(err, check.IsNil)
  581. defer d.Restart()
  582. ifconfigSearchString := ip.String()
  583. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  584. out, _, _, err := runCommandWithStdoutStderr(ifconfigCmd)
  585. c.Assert(err, check.IsNil)
  586. c.Assert(strings.Contains(out, ifconfigSearchString), check.Equals, true,
  587. check.Commentf("ifconfig output should have contained %q, but was %q",
  588. ifconfigSearchString, out))
  589. ipTablesSearchString := bridgeIPNet.String()
  590. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  591. out, _, err = runCommandWithOutput(ipTablesCmd)
  592. c.Assert(err, check.IsNil)
  593. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  594. check.Commentf("iptables output should have contained %q, but was %q",
  595. ipTablesSearchString, out))
  596. out, err = d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  597. c.Assert(err, check.IsNil)
  598. containerIP := d.findContainerIP("test")
  599. ip = net.ParseIP(containerIP)
  600. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  601. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  602. containerIP))
  603. deleteInterface(c, defaultNetworkBridge)
  604. }
  605. func (s *DockerDaemonSuite) TestDaemonRestartWithBridgeIPChange(c *check.C) {
  606. if err := s.d.Start(); err != nil {
  607. c.Fatalf("Could not start daemon: %v", err)
  608. }
  609. defer s.d.Restart()
  610. if err := s.d.Stop(); err != nil {
  611. c.Fatalf("Could not stop daemon: %v", err)
  612. }
  613. // now we will change the docker0's IP and then try starting the daemon
  614. bridgeIP := "192.169.100.1/24"
  615. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  616. ipCmd := exec.Command("ifconfig", "docker0", bridgeIP)
  617. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  618. if err != nil {
  619. c.Fatalf("failed to change docker0's IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  620. }
  621. if err := s.d.Start("--bip", bridgeIP); err != nil {
  622. c.Fatalf("Could not start daemon: %v", err)
  623. }
  624. //check if the iptables contains new bridgeIP MASQUERADE rule
  625. ipTablesSearchString := bridgeIPNet.String()
  626. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  627. out, _, err := runCommandWithOutput(ipTablesCmd)
  628. if err != nil {
  629. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  630. }
  631. if !strings.Contains(out, ipTablesSearchString) {
  632. c.Fatalf("iptables output should have contained new MASQUERADE rule with IP %q, but was %q", ipTablesSearchString, out)
  633. }
  634. }
  635. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
  636. d := s.d
  637. bridgeName := "external-bridge"
  638. bridgeIP := "192.169.1.1/24"
  639. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  640. c.Assert(err, check.IsNil, check.Commentf(out))
  641. defer deleteInterface(c, bridgeName)
  642. args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"}
  643. err = d.StartWithBusybox(args...)
  644. c.Assert(err, check.IsNil)
  645. defer d.Restart()
  646. for i := 0; i < 4; i++ {
  647. cName := "Container" + strconv.Itoa(i)
  648. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  649. if err != nil {
  650. c.Assert(strings.Contains(out, "no available IPv4 addresses"), check.Equals, true,
  651. check.Commentf("Could not run a Container : %s %s", err.Error(), out))
  652. }
  653. }
  654. }
  655. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) {
  656. defaultNetworkBridge := "docker0"
  657. deleteInterface(c, defaultNetworkBridge)
  658. d := s.d
  659. bridgeIP := "192.169.1.1"
  660. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  661. err := d.StartWithBusybox("--bip", bridgeIPNet)
  662. c.Assert(err, check.IsNil)
  663. defer d.Restart()
  664. expectedMessage := fmt.Sprintf("default via %s dev", bridgeIP)
  665. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  666. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  667. check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'",
  668. bridgeIP, strings.TrimSpace(out)))
  669. deleteInterface(c, defaultNetworkBridge)
  670. }
  671. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) {
  672. defaultNetworkBridge := "docker0"
  673. deleteInterface(c, defaultNetworkBridge)
  674. d := s.d
  675. bridgeIP := "192.169.1.1"
  676. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  677. gatewayIP := "192.169.1.254"
  678. err := d.StartWithBusybox("--bip", bridgeIPNet, "--default-gateway", gatewayIP)
  679. c.Assert(err, check.IsNil)
  680. defer d.Restart()
  681. expectedMessage := fmt.Sprintf("default via %s dev", gatewayIP)
  682. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  683. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  684. check.Commentf("Explicit default gateway should be %s, but default route was '%s'",
  685. gatewayIP, strings.TrimSpace(out)))
  686. deleteInterface(c, defaultNetworkBridge)
  687. }
  688. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4ExplicitOutsideContainerSubnet(c *check.C) {
  689. defaultNetworkBridge := "docker0"
  690. deleteInterface(c, defaultNetworkBridge)
  691. // Program a custom default gateway outside of the container subnet, daemon should accept it and start
  692. err := s.d.StartWithBusybox("--bip", "172.16.0.10/16", "--fixed-cidr", "172.16.1.0/24", "--default-gateway", "172.16.0.254")
  693. c.Assert(err, check.IsNil)
  694. deleteInterface(c, defaultNetworkBridge)
  695. s.d.Restart()
  696. }
  697. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  698. d := s.d
  699. ipStr := "192.170.1.1/24"
  700. ip, _, _ := net.ParseCIDR(ipStr)
  701. args := []string{"--ip", ip.String()}
  702. err := d.StartWithBusybox(args...)
  703. c.Assert(err, check.IsNil)
  704. defer d.Restart()
  705. out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  706. c.Assert(err, check.NotNil,
  707. check.Commentf("Running a container must fail with an invalid --ip option"))
  708. c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
  709. ifName := "dummy"
  710. out, err = createInterface(c, "dummy", ifName, ipStr)
  711. c.Assert(err, check.IsNil, check.Commentf(out))
  712. defer deleteInterface(c, ifName)
  713. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  714. c.Assert(err, check.IsNil)
  715. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  716. out, _, err = runCommandWithOutput(ipTablesCmd)
  717. c.Assert(err, check.IsNil)
  718. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  719. matched, _ := regexp.MatchString(regex, out)
  720. c.Assert(matched, check.Equals, true,
  721. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  722. }
  723. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  724. d := s.d
  725. bridgeName := "external-bridge"
  726. bridgeIP := "192.169.1.1/24"
  727. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  728. c.Assert(err, check.IsNil, check.Commentf(out))
  729. defer deleteInterface(c, bridgeName)
  730. args := []string{"--bridge", bridgeName, "--icc=false"}
  731. err = d.StartWithBusybox(args...)
  732. c.Assert(err, check.IsNil)
  733. defer d.Restart()
  734. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  735. out, _, err = runCommandWithOutput(ipTablesCmd)
  736. c.Assert(err, check.IsNil)
  737. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  738. matched, _ := regexp.MatchString(regex, out)
  739. c.Assert(matched, check.Equals, true,
  740. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  741. // Pinging another container must fail with --icc=false
  742. pingContainers(c, d, true)
  743. ipStr := "192.171.1.1/24"
  744. ip, _, _ := net.ParseCIDR(ipStr)
  745. ifName := "icc-dummy"
  746. createInterface(c, "dummy", ifName, ipStr)
  747. // But, Pinging external or a Host interface must succeed
  748. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  749. runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd}
  750. _, err = d.Cmd("run", runArgs...)
  751. c.Assert(err, check.IsNil)
  752. }
  753. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  754. d := s.d
  755. bridgeName := "external-bridge"
  756. bridgeIP := "192.169.1.1/24"
  757. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  758. c.Assert(err, check.IsNil, check.Commentf(out))
  759. defer deleteInterface(c, bridgeName)
  760. args := []string{"--bridge", bridgeName, "--icc=false"}
  761. err = d.StartWithBusybox(args...)
  762. c.Assert(err, check.IsNil)
  763. defer d.Restart()
  764. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  765. out, _, err = runCommandWithOutput(ipTablesCmd)
  766. c.Assert(err, check.IsNil)
  767. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  768. matched, _ := regexp.MatchString(regex, out)
  769. c.Assert(matched, check.Equals, true,
  770. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  771. out, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  772. c.Assert(err, check.IsNil, check.Commentf(out))
  773. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  774. c.Assert(err, check.IsNil, check.Commentf(out))
  775. }
  776. func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
  777. bridgeName := "external-bridge"
  778. bridgeIP := "192.169.1.1/24"
  779. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  780. c.Assert(err, check.IsNil, check.Commentf(out))
  781. defer deleteInterface(c, bridgeName)
  782. err = s.d.StartWithBusybox("--bridge", bridgeName, "--icc=false")
  783. c.Assert(err, check.IsNil)
  784. defer s.d.Restart()
  785. _, err = s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
  786. c.Assert(err, check.IsNil)
  787. _, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
  788. c.Assert(err, check.IsNil)
  789. childIP := s.d.findContainerIP("child")
  790. parentIP := s.d.findContainerIP("parent")
  791. sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  792. destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  793. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  794. c.Fatal("Iptables rules not found")
  795. }
  796. s.d.Cmd("rm", "--link", "parent/http")
  797. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  798. c.Fatal("Iptables rules should be removed when unlink")
  799. }
  800. s.d.Cmd("kill", "child")
  801. s.d.Cmd("kill", "parent")
  802. }
  803. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  804. testRequires(c, NativeExecDriver)
  805. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  806. c.Fatal(err)
  807. }
  808. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  809. if err != nil {
  810. c.Fatal(out, err)
  811. }
  812. outArr := strings.Split(out, "\n")
  813. if len(outArr) < 2 {
  814. c.Fatalf("got unexpected output: %s", out)
  815. }
  816. nofile := strings.TrimSpace(outArr[0])
  817. nproc := strings.TrimSpace(outArr[1])
  818. if nofile != "42" {
  819. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  820. }
  821. if nproc != "2048" {
  822. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  823. }
  824. // Now restart daemon with a new default
  825. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  826. c.Fatal(err)
  827. }
  828. out, err = s.d.Cmd("start", "-a", "test")
  829. if err != nil {
  830. c.Fatal(err)
  831. }
  832. outArr = strings.Split(out, "\n")
  833. if len(outArr) < 2 {
  834. c.Fatalf("got unexpected output: %s", out)
  835. }
  836. nofile = strings.TrimSpace(outArr[0])
  837. nproc = strings.TrimSpace(outArr[1])
  838. if nofile != "43" {
  839. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  840. }
  841. if nproc != "2048" {
  842. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  843. }
  844. }
  845. // #11315
  846. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  847. if err := s.d.StartWithBusybox(); err != nil {
  848. c.Fatal(err)
  849. }
  850. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  851. c.Fatal(err, out)
  852. }
  853. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  854. c.Fatal(err, out)
  855. }
  856. if err := s.d.Restart(); err != nil {
  857. c.Fatal(err)
  858. }
  859. if out, err := s.d.Cmd("start", "test2"); err != nil {
  860. c.Fatal(err, out)
  861. }
  862. }
  863. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  864. if err := s.d.StartWithBusybox(); err != nil {
  865. c.Fatal(err)
  866. }
  867. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  868. if err != nil {
  869. c.Fatal(out, err)
  870. }
  871. id := strings.TrimSpace(out)
  872. if out, err := s.d.Cmd("wait", id); err != nil {
  873. c.Fatal(out, err)
  874. }
  875. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  876. if _, err := os.Stat(logPath); err != nil {
  877. c.Fatal(err)
  878. }
  879. f, err := os.Open(logPath)
  880. if err != nil {
  881. c.Fatal(err)
  882. }
  883. var res struct {
  884. Log string `json:"log"`
  885. Stream string `json:"stream"`
  886. Time time.Time `json:"time"`
  887. }
  888. if err := json.NewDecoder(f).Decode(&res); err != nil {
  889. c.Fatal(err)
  890. }
  891. if res.Log != "testline\n" {
  892. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  893. }
  894. if res.Stream != "stdout" {
  895. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  896. }
  897. if !time.Now().After(res.Time) {
  898. c.Fatalf("Log time %v in future", res.Time)
  899. }
  900. }
  901. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  902. if err := s.d.StartWithBusybox(); err != nil {
  903. c.Fatal(err)
  904. }
  905. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  906. if err != nil {
  907. c.Fatal(out, err)
  908. }
  909. id := strings.TrimSpace(out)
  910. if out, err := s.d.Cmd("wait", id); err != nil {
  911. c.Fatal(out, err)
  912. }
  913. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  914. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  915. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  916. }
  917. }
  918. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  919. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  920. c.Fatal(err)
  921. }
  922. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  923. if err != nil {
  924. c.Fatal(out, err)
  925. }
  926. id := strings.TrimSpace(out)
  927. if out, err := s.d.Cmd("wait", id); err != nil {
  928. c.Fatal(out, err)
  929. }
  930. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  931. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  932. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  933. }
  934. }
  935. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  936. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  937. c.Fatal(err)
  938. }
  939. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  940. if err != nil {
  941. c.Fatal(out, err)
  942. }
  943. id := strings.TrimSpace(out)
  944. if out, err := s.d.Cmd("wait", id); err != nil {
  945. c.Fatal(out, err)
  946. }
  947. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  948. if _, err := os.Stat(logPath); err != nil {
  949. c.Fatal(err)
  950. }
  951. f, err := os.Open(logPath)
  952. if err != nil {
  953. c.Fatal(err)
  954. }
  955. var res struct {
  956. Log string `json:"log"`
  957. Stream string `json:"stream"`
  958. Time time.Time `json:"time"`
  959. }
  960. if err := json.NewDecoder(f).Decode(&res); err != nil {
  961. c.Fatal(err)
  962. }
  963. if res.Log != "testline\n" {
  964. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  965. }
  966. if res.Stream != "stdout" {
  967. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  968. }
  969. if !time.Now().After(res.Time) {
  970. c.Fatalf("Log time %v in future", res.Time)
  971. }
  972. }
  973. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  974. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  975. c.Fatal(err)
  976. }
  977. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  978. if err != nil {
  979. c.Fatal(out, err)
  980. }
  981. id := strings.TrimSpace(out)
  982. out, err = s.d.Cmd("logs", id)
  983. if err != nil {
  984. c.Fatalf("Logs request should be sent and then fail with \"none\" driver")
  985. }
  986. if !strings.Contains(out, `Error running logs job: Failed to get logging factory: logger: no log driver named 'none' is registered`) {
  987. c.Fatalf("There should be an error about none not being a recognized log driver, got: %s", out)
  988. }
  989. }
  990. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  991. if err := s.d.StartWithBusybox(); err != nil {
  992. c.Fatal(err)
  993. }
  994. // Now create 4 containers
  995. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  996. c.Fatalf("Error creating container: %q", err)
  997. }
  998. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  999. c.Fatalf("Error creating container: %q", err)
  1000. }
  1001. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1002. c.Fatalf("Error creating container: %q", err)
  1003. }
  1004. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1005. c.Fatalf("Error creating container: %q", err)
  1006. }
  1007. s.d.Stop()
  1008. s.d.Start("--log-level=debug")
  1009. s.d.Stop()
  1010. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1011. if strings.Contains(string(content), "....") {
  1012. c.Fatalf("Debug level should not have ....\n%s", string(content))
  1013. }
  1014. s.d.Start("--log-level=error")
  1015. s.d.Stop()
  1016. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1017. if strings.Contains(string(content), "....") {
  1018. c.Fatalf("Error level should not have ....\n%s", string(content))
  1019. }
  1020. s.d.Start("--log-level=info")
  1021. s.d.Stop()
  1022. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1023. if !strings.Contains(string(content), "....") {
  1024. c.Fatalf("Info level should have ....\n%s", string(content))
  1025. }
  1026. }
  1027. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  1028. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  1029. if err != nil {
  1030. c.Fatal(err)
  1031. }
  1032. defer os.RemoveAll(dir)
  1033. sockPath := filepath.Join(dir, "docker.sock")
  1034. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  1035. c.Fatal(err)
  1036. }
  1037. if _, err := os.Stat(sockPath); err != nil {
  1038. c.Fatal("socket does not exist")
  1039. }
  1040. if err := s.d.Stop(); err != nil {
  1041. c.Fatal(err)
  1042. }
  1043. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  1044. c.Fatal("unix socket is not cleaned up")
  1045. }
  1046. }
  1047. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  1048. type Config struct {
  1049. Crv string `json:"crv"`
  1050. D string `json:"d"`
  1051. Kid string `json:"kid"`
  1052. Kty string `json:"kty"`
  1053. X string `json:"x"`
  1054. Y string `json:"y"`
  1055. }
  1056. os.Remove("/etc/docker/key.json")
  1057. if err := s.d.Start(); err != nil {
  1058. c.Fatalf("Failed to start daemon: %v", err)
  1059. }
  1060. if err := s.d.Stop(); err != nil {
  1061. c.Fatalf("Could not stop daemon: %v", err)
  1062. }
  1063. config := &Config{}
  1064. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  1065. if err != nil {
  1066. c.Fatalf("Error reading key.json file: %s", err)
  1067. }
  1068. // byte[] to Data-Struct
  1069. if err := json.Unmarshal(bytes, &config); err != nil {
  1070. c.Fatalf("Error Unmarshal: %s", err)
  1071. }
  1072. //replace config.Kid with the fake value
  1073. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  1074. // NEW Data-Struct to byte[]
  1075. newBytes, err := json.Marshal(&config)
  1076. if err != nil {
  1077. c.Fatalf("Error Marshal: %s", err)
  1078. }
  1079. // write back
  1080. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  1081. c.Fatalf("Error ioutil.WriteFile: %s", err)
  1082. }
  1083. defer os.Remove("/etc/docker/key.json")
  1084. if err := s.d.Start(); err == nil {
  1085. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  1086. }
  1087. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1088. if !strings.Contains(string(content), "Public Key ID does not match") {
  1089. c.Fatal("Missing KeyID message from daemon logs")
  1090. }
  1091. }
  1092. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  1093. if err := s.d.StartWithBusybox(); err != nil {
  1094. c.Fatalf("Could not start daemon with busybox: %v", err)
  1095. }
  1096. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  1097. if err != nil {
  1098. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  1099. }
  1100. containerID := strings.TrimSpace(out)
  1101. if out, err := s.d.Cmd("kill", containerID); err != nil {
  1102. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  1103. }
  1104. if err := s.d.Restart(); err != nil {
  1105. c.Fatalf("Could not restart daemon: %v", err)
  1106. }
  1107. errchan := make(chan error)
  1108. go func() {
  1109. if out, err := s.d.Cmd("wait", containerID); err != nil {
  1110. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1111. }
  1112. close(errchan)
  1113. }()
  1114. select {
  1115. case <-time.After(5 * time.Second):
  1116. c.Fatal("Waiting on a stopped (killed) container timed out")
  1117. case err := <-errchan:
  1118. if err != nil {
  1119. c.Fatal(err)
  1120. }
  1121. }
  1122. }
  1123. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  1124. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  1125. const (
  1126. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1127. )
  1128. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1129. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1130. c.Fatalf("Could not start daemon with busybox: %v", err)
  1131. }
  1132. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  1133. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1134. if err != nil {
  1135. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1136. }
  1137. }
  1138. // TestTlsVerify verifies that --tlsverify=false turns on tls
  1139. func (s *DockerDaemonSuite) TestTlsVerify(c *check.C) {
  1140. out, err := exec.Command(dockerBinary, "daemon", "--tlsverify=false").CombinedOutput()
  1141. if err == nil || !strings.Contains(string(out), "Could not load X509 key pair") {
  1142. c.Fatalf("Daemon should not have started due to missing certs: %v\n%s", err, string(out))
  1143. }
  1144. }
  1145. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  1146. // by using a rogue client certificate and checks that it fails with the expected error.
  1147. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  1148. const (
  1149. errBadCertificate = "remote error: bad certificate"
  1150. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1151. )
  1152. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1153. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1154. c.Fatalf("Could not start daemon with busybox: %v", err)
  1155. }
  1156. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  1157. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1158. if err == nil || !strings.Contains(out, errBadCertificate) {
  1159. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  1160. }
  1161. }
  1162. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  1163. // which provides a rogue server certificate and checks that it fails with the expected error
  1164. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  1165. const (
  1166. errCaUnknown = "x509: certificate signed by unknown authority"
  1167. testDaemonRogueHTTPSAddr = "tcp://localhost:4272"
  1168. )
  1169. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  1170. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHTTPSAddr); err != nil {
  1171. c.Fatalf("Could not start daemon with busybox: %v", err)
  1172. }
  1173. daemonArgs := []string{"--host", testDaemonRogueHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  1174. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1175. if err == nil || !strings.Contains(out, errCaUnknown) {
  1176. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  1177. }
  1178. }
  1179. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  1180. var dargs []string
  1181. if d != nil {
  1182. dargs = []string{"--host", d.sock()}
  1183. }
  1184. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  1185. dockerCmd(c, args...)
  1186. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  1187. pingCmd := "ping -c 1 %s -W 1"
  1188. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  1189. _, _, err := dockerCmdWithError(args...)
  1190. if expectFailure {
  1191. c.Assert(err, check.NotNil)
  1192. } else {
  1193. c.Assert(err, check.IsNil)
  1194. }
  1195. args = append(dargs, "rm", "-f", "container1")
  1196. dockerCmd(c, args...)
  1197. }
  1198. func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) {
  1199. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1200. socket := filepath.Join(s.d.folder, "docker.sock")
  1201. out, err := s.d.Cmd("run", "-d", "--restart=always", "-v", socket+":/sock", "busybox")
  1202. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1203. c.Assert(s.d.Restart(), check.IsNil)
  1204. }
  1205. func (s *DockerDaemonSuite) TestCleanupMountsAfterCrash(c *check.C) {
  1206. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1207. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  1208. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1209. id := strings.TrimSpace(out)
  1210. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  1211. c.Assert(s.d.Start(), check.IsNil)
  1212. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1213. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1214. comment := check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1215. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1216. }
  1217. func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
  1218. testRequires(c, NativeExecDriver, NotUserNamespace)
  1219. c.Assert(s.d.StartWithBusybox("-b", "none"), check.IsNil)
  1220. out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
  1221. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1222. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1223. check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out))
  1224. out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l")
  1225. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1226. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1227. check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out))
  1228. // the extra grep and awk clean up the output of `ip` to only list the number and name of
  1229. // interfaces, allowing for different versions of ip (e.g. inside and outside the container) to
  1230. // be used while still verifying that the interface list is the exact same
  1231. cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1232. stdout := bytes.NewBuffer(nil)
  1233. cmd.Stdout = stdout
  1234. if err := cmd.Run(); err != nil {
  1235. c.Fatal("Failed to get host network interface")
  1236. }
  1237. out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1238. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1239. c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout),
  1240. check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out))
  1241. }
  1242. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
  1243. if err := s.d.StartWithBusybox(); err != nil {
  1244. t.Fatal(err)
  1245. }
  1246. if out, err := s.d.Cmd("run", "-ti", "-d", "--name", "test", "busybox"); err != nil {
  1247. t.Fatal(out, err)
  1248. }
  1249. if err := s.d.Restart(); err != nil {
  1250. t.Fatal(err)
  1251. }
  1252. // Container 'test' should be removed without error
  1253. if out, err := s.d.Cmd("rm", "test"); err != nil {
  1254. t.Fatal(out, err)
  1255. }
  1256. }
  1257. func (s *DockerDaemonSuite) TestDaemonRestartCleanupNetns(c *check.C) {
  1258. if err := s.d.StartWithBusybox(); err != nil {
  1259. c.Fatal(err)
  1260. }
  1261. out, err := s.d.Cmd("run", "--name", "netns", "-d", "busybox", "top")
  1262. if err != nil {
  1263. c.Fatal(out, err)
  1264. }
  1265. // Get sandbox key via inspect
  1266. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.SandboxKey}}'", "netns")
  1267. if err != nil {
  1268. c.Fatalf("Error inspecting container: %s, %v", out, err)
  1269. }
  1270. fileName := strings.Trim(out, " \r\n'")
  1271. if out, err := s.d.Cmd("stop", "netns"); err != nil {
  1272. c.Fatal(out, err)
  1273. }
  1274. // Test if the file still exists
  1275. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1276. out = strings.TrimSpace(out)
  1277. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1278. c.Assert(out, check.Equals, fileName, check.Commentf("Output: %s", out))
  1279. // Remove the container and restart the daemon
  1280. if out, err := s.d.Cmd("rm", "netns"); err != nil {
  1281. c.Fatal(out, err)
  1282. }
  1283. if err := s.d.Restart(); err != nil {
  1284. c.Fatal(err)
  1285. }
  1286. // Test again and see now the netns file does not exist
  1287. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1288. out = strings.TrimSpace(out)
  1289. c.Assert(err, check.Not(check.IsNil), check.Commentf("Output: %s", out))
  1290. }
  1291. // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored
  1292. func (s *DockerDaemonSuite) TestDaemonNoTlsCliTlsVerifyWithEnv(c *check.C) {
  1293. host := "tcp://localhost:4271"
  1294. c.Assert(s.d.Start("-H", host), check.IsNil)
  1295. cmd := exec.Command(dockerBinary, "-H", host, "info")
  1296. cmd.Env = []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"}
  1297. out, _, err := runCommandWithOutput(cmd)
  1298. c.Assert(err, check.Not(check.IsNil), check.Commentf("%s", out))
  1299. c.Assert(strings.Contains(out, "error occurred trying to connect"), check.Equals, true)
  1300. }
  1301. func setupV6() error {
  1302. // Hack to get the right IPv6 address on docker0, which has already been created
  1303. err := exec.Command("ip", "addr", "add", "fe80::1/64", "dev", "docker0").Run()
  1304. if err != nil {
  1305. return err
  1306. }
  1307. return nil
  1308. }
  1309. func teardownV6() error {
  1310. err := exec.Command("ip", "addr", "del", "fe80::1/64", "dev", "docker0").Run()
  1311. if err != nil {
  1312. return err
  1313. }
  1314. return nil
  1315. }
  1316. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) {
  1317. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1318. out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  1319. c.Assert(err, check.IsNil)
  1320. id := strings.TrimSpace(out)
  1321. _, err = s.d.Cmd("stop", id)
  1322. c.Assert(err, check.IsNil)
  1323. _, err = s.d.Cmd("wait", id)
  1324. c.Assert(err, check.IsNil)
  1325. out, err = s.d.Cmd("ps", "-q")
  1326. c.Assert(err, check.IsNil)
  1327. c.Assert(out, check.Equals, "")
  1328. c.Assert(s.d.Restart(), check.IsNil)
  1329. out, err = s.d.Cmd("ps", "-q")
  1330. c.Assert(err, check.IsNil)
  1331. c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
  1332. }
  1333. func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) {
  1334. if err := s.d.StartWithBusybox("--log-driver=json-file", "--log-opt=max-size=1k"); err != nil {
  1335. c.Fatal(err)
  1336. }
  1337. out, err := s.d.Cmd("run", "-d", "--name=logtest", "busybox", "top")
  1338. c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err))
  1339. out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", "logtest")
  1340. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1341. cfg := strings.TrimSpace(out)
  1342. if cfg != "map[max-size:1k]" {
  1343. c.Fatalf("Unexpected log-opt: %s, expected map[max-size:1k]", cfg)
  1344. }
  1345. }
  1346. func (s *DockerDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
  1347. if err := s.d.StartWithBusybox(); err != nil {
  1348. c.Fatal(err)
  1349. }
  1350. if out, err := s.d.Cmd("run", "-i", "-d", "--name", "test", "busybox", "top"); err != nil {
  1351. c.Fatal(err, out)
  1352. }
  1353. if out, err := s.d.Cmd("pause", "test"); err != nil {
  1354. c.Fatal(err, out)
  1355. }
  1356. if err := s.d.Restart(); err != nil {
  1357. c.Fatal(err)
  1358. }
  1359. errchan := make(chan error)
  1360. go func() {
  1361. out, err := s.d.Cmd("start", "test")
  1362. if err != nil {
  1363. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1364. }
  1365. name := strings.TrimSpace(out)
  1366. if name != "test" {
  1367. errchan <- fmt.Errorf("Paused container start error on docker daemon restart, expected 'test' but got '%s'", name)
  1368. }
  1369. close(errchan)
  1370. }()
  1371. select {
  1372. case <-time.After(5 * time.Second):
  1373. c.Fatal("Waiting on start a container timed out")
  1374. case err := <-errchan:
  1375. if err != nil {
  1376. c.Fatal(err)
  1377. }
  1378. }
  1379. }
  1380. func (s *DockerDaemonSuite) TestDaemonRestartRmVolumeInUse(c *check.C) {
  1381. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1382. out, err := s.d.Cmd("create", "-v", "test:/foo", "busybox")
  1383. c.Assert(err, check.IsNil, check.Commentf(out))
  1384. c.Assert(s.d.Restart(), check.IsNil)
  1385. out, err = s.d.Cmd("volume", "rm", "test")
  1386. c.Assert(err, check.Not(check.IsNil), check.Commentf("should not be able to remove in use volume after daemon restart"))
  1387. c.Assert(strings.Contains(out, "in use"), check.Equals, true)
  1388. }
  1389. func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
  1390. c.Assert(s.d.Start(), check.IsNil)
  1391. _, err := s.d.Cmd("volume", "create", "--name", "test")
  1392. c.Assert(err, check.IsNil)
  1393. c.Assert(s.d.Restart(), check.IsNil)
  1394. _, err = s.d.Cmd("volume", "inspect", "test")
  1395. c.Assert(err, check.IsNil)
  1396. }
  1397. func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
  1398. for _, driver := range []string{
  1399. "syslog",
  1400. "gelf",
  1401. } {
  1402. args := []string{"--log-driver=" + driver, "--log-opt", driver + "-address=corrupted:42"}
  1403. c.Assert(s.d.Start(args...), check.NotNil, check.Commentf(fmt.Sprintf("Expected daemon not to start with invalid %s-address provided", driver)))
  1404. expected := fmt.Sprintf("Failed to set log opts: %s-address should be in form proto://address", driver)
  1405. runCmd := exec.Command("grep", expected, s.d.LogfileName())
  1406. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1407. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1408. }
  1409. }
  1410. }
  1411. func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
  1412. c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
  1413. expected := "Failed to set log opts: invalid fluentd-address corrupted:c: "
  1414. runCmd := exec.Command("grep", expected, s.d.LogfileName())
  1415. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1416. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1417. }
  1418. }
  1419. func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) {
  1420. s.d.useDefaultHost = true
  1421. defer func() {
  1422. s.d.useDefaultHost = false
  1423. }()
  1424. c.Assert(s.d.Start(), check.IsNil)
  1425. }