docker_cli_daemon_test.go 54 KB

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