docker_cli_daemon_test.go 57 KB

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