docker_cli_daemon_test.go 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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.Networks.bridge.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.Networks.bridge.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.Networks.bridge.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.Networks.bridge.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) TestDaemonBridgeFixedCidrFixedCIDREqualBridgeNetwork(c *check.C) {
  657. d := s.d
  658. bridgeName := "external-bridge"
  659. bridgeIP := "172.27.42.1/16"
  660. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  661. c.Assert(err, check.IsNil, check.Commentf(out))
  662. defer deleteInterface(c, bridgeName)
  663. err = d.StartWithBusybox("--bridge", bridgeName, "--fixed-cidr", bridgeIP)
  664. c.Assert(err, check.IsNil)
  665. defer s.d.Restart()
  666. out, err = d.Cmd("run", "-d", "busybox", "top")
  667. c.Assert(err, check.IsNil, check.Commentf(out))
  668. cid1 := strings.TrimSpace(out)
  669. defer d.Cmd("stop", cid1)
  670. }
  671. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) {
  672. defaultNetworkBridge := "docker0"
  673. deleteInterface(c, defaultNetworkBridge)
  674. d := s.d
  675. bridgeIP := "192.169.1.1"
  676. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  677. err := d.StartWithBusybox("--bip", bridgeIPNet)
  678. c.Assert(err, check.IsNil)
  679. defer d.Restart()
  680. expectedMessage := fmt.Sprintf("default via %s dev", bridgeIP)
  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("Implicit default gateway should be bridge IP %s, but default route was '%s'",
  684. bridgeIP, strings.TrimSpace(out)))
  685. deleteInterface(c, defaultNetworkBridge)
  686. }
  687. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) {
  688. defaultNetworkBridge := "docker0"
  689. deleteInterface(c, defaultNetworkBridge)
  690. d := s.d
  691. bridgeIP := "192.169.1.1"
  692. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  693. gatewayIP := "192.169.1.254"
  694. err := d.StartWithBusybox("--bip", bridgeIPNet, "--default-gateway", gatewayIP)
  695. c.Assert(err, check.IsNil)
  696. defer d.Restart()
  697. expectedMessage := fmt.Sprintf("default via %s dev", gatewayIP)
  698. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  699. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  700. check.Commentf("Explicit default gateway should be %s, but default route was '%s'",
  701. gatewayIP, strings.TrimSpace(out)))
  702. deleteInterface(c, defaultNetworkBridge)
  703. }
  704. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4ExplicitOutsideContainerSubnet(c *check.C) {
  705. defaultNetworkBridge := "docker0"
  706. deleteInterface(c, defaultNetworkBridge)
  707. // Program a custom default gateway outside of the container subnet, daemon should accept it and start
  708. err := s.d.StartWithBusybox("--bip", "172.16.0.10/16", "--fixed-cidr", "172.16.1.0/24", "--default-gateway", "172.16.0.254")
  709. c.Assert(err, check.IsNil)
  710. deleteInterface(c, defaultNetworkBridge)
  711. s.d.Restart()
  712. }
  713. func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *check.C) {
  714. testRequires(c, DaemonIsLinux, SameHostDaemon)
  715. // Start daemon without docker0 bridge
  716. defaultNetworkBridge := "docker0"
  717. deleteInterface(c, defaultNetworkBridge)
  718. d := NewDaemon(c)
  719. discoveryBackend := "consul://consuladdr:consulport/some/path"
  720. err := d.Start(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
  721. c.Assert(err, checker.IsNil)
  722. // Start daemon with docker0 bridge
  723. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  724. _, err = runCommand(ifconfigCmd)
  725. c.Assert(err, check.IsNil)
  726. err = d.Restart(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
  727. c.Assert(err, checker.IsNil)
  728. d.Stop()
  729. }
  730. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  731. d := s.d
  732. ipStr := "192.170.1.1/24"
  733. ip, _, _ := net.ParseCIDR(ipStr)
  734. args := []string{"--ip", ip.String()}
  735. err := d.StartWithBusybox(args...)
  736. c.Assert(err, check.IsNil)
  737. defer d.Restart()
  738. out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  739. c.Assert(err, check.NotNil,
  740. check.Commentf("Running a container must fail with an invalid --ip option"))
  741. c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
  742. ifName := "dummy"
  743. out, err = createInterface(c, "dummy", ifName, ipStr)
  744. c.Assert(err, check.IsNil, check.Commentf(out))
  745. defer deleteInterface(c, ifName)
  746. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  747. c.Assert(err, check.IsNil)
  748. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  749. out, _, err = runCommandWithOutput(ipTablesCmd)
  750. c.Assert(err, check.IsNil)
  751. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  752. matched, _ := regexp.MatchString(regex, out)
  753. c.Assert(matched, check.Equals, true,
  754. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  755. }
  756. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  757. d := s.d
  758. bridgeName := "external-bridge"
  759. bridgeIP := "192.169.1.1/24"
  760. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  761. c.Assert(err, check.IsNil, check.Commentf(out))
  762. defer deleteInterface(c, bridgeName)
  763. args := []string{"--bridge", bridgeName, "--icc=false"}
  764. err = d.StartWithBusybox(args...)
  765. c.Assert(err, check.IsNil)
  766. defer d.Restart()
  767. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  768. out, _, err = runCommandWithOutput(ipTablesCmd)
  769. c.Assert(err, check.IsNil)
  770. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  771. matched, _ := regexp.MatchString(regex, out)
  772. c.Assert(matched, check.Equals, true,
  773. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  774. // Pinging another container must fail with --icc=false
  775. pingContainers(c, d, true)
  776. ipStr := "192.171.1.1/24"
  777. ip, _, _ := net.ParseCIDR(ipStr)
  778. ifName := "icc-dummy"
  779. createInterface(c, "dummy", ifName, ipStr)
  780. // But, Pinging external or a Host interface must succeed
  781. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  782. runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd}
  783. _, err = d.Cmd("run", runArgs...)
  784. c.Assert(err, check.IsNil)
  785. }
  786. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  787. d := s.d
  788. bridgeName := "external-bridge"
  789. bridgeIP := "192.169.1.1/24"
  790. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  791. c.Assert(err, check.IsNil, check.Commentf(out))
  792. defer deleteInterface(c, bridgeName)
  793. args := []string{"--bridge", bridgeName, "--icc=false"}
  794. err = d.StartWithBusybox(args...)
  795. c.Assert(err, check.IsNil)
  796. defer d.Restart()
  797. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  798. out, _, err = runCommandWithOutput(ipTablesCmd)
  799. c.Assert(err, check.IsNil)
  800. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  801. matched, _ := regexp.MatchString(regex, out)
  802. c.Assert(matched, check.Equals, true,
  803. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  804. out, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  805. c.Assert(err, check.IsNil, check.Commentf(out))
  806. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  807. c.Assert(err, check.IsNil, check.Commentf(out))
  808. }
  809. func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
  810. bridgeName := "external-bridge"
  811. bridgeIP := "192.169.1.1/24"
  812. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  813. c.Assert(err, check.IsNil, check.Commentf(out))
  814. defer deleteInterface(c, bridgeName)
  815. err = s.d.StartWithBusybox("--bridge", bridgeName, "--icc=false")
  816. c.Assert(err, check.IsNil)
  817. defer s.d.Restart()
  818. _, err = s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
  819. c.Assert(err, check.IsNil)
  820. _, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
  821. c.Assert(err, check.IsNil)
  822. childIP := s.d.findContainerIP("child")
  823. parentIP := s.d.findContainerIP("parent")
  824. sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  825. destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  826. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  827. c.Fatal("Iptables rules not found")
  828. }
  829. s.d.Cmd("rm", "--link", "parent/http")
  830. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  831. c.Fatal("Iptables rules should be removed when unlink")
  832. }
  833. s.d.Cmd("kill", "child")
  834. s.d.Cmd("kill", "parent")
  835. }
  836. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  837. testRequires(c, DaemonIsLinux)
  838. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  839. c.Fatal(err)
  840. }
  841. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  842. if err != nil {
  843. c.Fatal(out, err)
  844. }
  845. outArr := strings.Split(out, "\n")
  846. if len(outArr) < 2 {
  847. c.Fatalf("got unexpected output: %s", out)
  848. }
  849. nofile := strings.TrimSpace(outArr[0])
  850. nproc := strings.TrimSpace(outArr[1])
  851. if nofile != "42" {
  852. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  853. }
  854. if nproc != "2048" {
  855. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  856. }
  857. // Now restart daemon with a new default
  858. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  859. c.Fatal(err)
  860. }
  861. out, err = s.d.Cmd("start", "-a", "test")
  862. if err != nil {
  863. c.Fatal(err)
  864. }
  865. outArr = strings.Split(out, "\n")
  866. if len(outArr) < 2 {
  867. c.Fatalf("got unexpected output: %s", out)
  868. }
  869. nofile = strings.TrimSpace(outArr[0])
  870. nproc = strings.TrimSpace(outArr[1])
  871. if nofile != "43" {
  872. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  873. }
  874. if nproc != "2048" {
  875. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  876. }
  877. }
  878. // #11315
  879. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  880. if err := s.d.StartWithBusybox(); err != nil {
  881. c.Fatal(err)
  882. }
  883. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  884. c.Fatal(err, out)
  885. }
  886. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  887. c.Fatal(err, out)
  888. }
  889. if err := s.d.Restart(); err != nil {
  890. c.Fatal(err)
  891. }
  892. if out, err := s.d.Cmd("start", "test2"); err != nil {
  893. c.Fatal(err, out)
  894. }
  895. }
  896. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  897. if err := s.d.StartWithBusybox(); err != nil {
  898. c.Fatal(err)
  899. }
  900. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  901. if err != nil {
  902. c.Fatal(out, err)
  903. }
  904. id := strings.TrimSpace(out)
  905. if out, err := s.d.Cmd("wait", id); err != nil {
  906. c.Fatal(out, err)
  907. }
  908. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  909. if _, err := os.Stat(logPath); err != nil {
  910. c.Fatal(err)
  911. }
  912. f, err := os.Open(logPath)
  913. if err != nil {
  914. c.Fatal(err)
  915. }
  916. var res struct {
  917. Log string `json:"log"`
  918. Stream string `json:"stream"`
  919. Time time.Time `json:"time"`
  920. }
  921. if err := json.NewDecoder(f).Decode(&res); err != nil {
  922. c.Fatal(err)
  923. }
  924. if res.Log != "testline\n" {
  925. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  926. }
  927. if res.Stream != "stdout" {
  928. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  929. }
  930. if !time.Now().After(res.Time) {
  931. c.Fatalf("Log time %v in future", res.Time)
  932. }
  933. }
  934. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  935. if err := s.d.StartWithBusybox(); err != nil {
  936. c.Fatal(err)
  937. }
  938. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "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.root, "containers", id, id+"-json.log")
  947. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  948. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  949. }
  950. }
  951. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  952. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  953. c.Fatal(err)
  954. }
  955. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  956. if err != nil {
  957. c.Fatal(out, err)
  958. }
  959. id := strings.TrimSpace(out)
  960. if out, err := s.d.Cmd("wait", id); err != nil {
  961. c.Fatal(out, err)
  962. }
  963. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  964. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  965. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  966. }
  967. }
  968. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  969. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  970. c.Fatal(err)
  971. }
  972. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  973. if err != nil {
  974. c.Fatal(out, err)
  975. }
  976. id := strings.TrimSpace(out)
  977. if out, err := s.d.Cmd("wait", id); err != nil {
  978. c.Fatal(out, err)
  979. }
  980. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  981. if _, err := os.Stat(logPath); err != nil {
  982. c.Fatal(err)
  983. }
  984. f, err := os.Open(logPath)
  985. if err != nil {
  986. c.Fatal(err)
  987. }
  988. var res struct {
  989. Log string `json:"log"`
  990. Stream string `json:"stream"`
  991. Time time.Time `json:"time"`
  992. }
  993. if err := json.NewDecoder(f).Decode(&res); err != nil {
  994. c.Fatal(err)
  995. }
  996. if res.Log != "testline\n" {
  997. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  998. }
  999. if res.Stream != "stdout" {
  1000. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  1001. }
  1002. if !time.Now().After(res.Time) {
  1003. c.Fatalf("Log time %v in future", res.Time)
  1004. }
  1005. }
  1006. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  1007. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  1008. c.Fatal(err)
  1009. }
  1010. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  1011. if err != nil {
  1012. c.Fatal(out, err)
  1013. }
  1014. id := strings.TrimSpace(out)
  1015. out, err = s.d.Cmd("logs", id)
  1016. if err != nil {
  1017. c.Fatalf("Logs request should be sent and then fail with \"none\" driver")
  1018. }
  1019. if !strings.Contains(out, `Error running logs job: Failed to get logging factory: logger: no log driver named 'none' is registered`) {
  1020. c.Fatalf("There should be an error about none not being a recognized log driver, got: %s", out)
  1021. }
  1022. }
  1023. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  1024. if err := s.d.StartWithBusybox(); err != nil {
  1025. c.Fatal(err)
  1026. }
  1027. // Now create 4 containers
  1028. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1029. c.Fatalf("Error creating container: %q", err)
  1030. }
  1031. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1032. c.Fatalf("Error creating container: %q", err)
  1033. }
  1034. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1035. c.Fatalf("Error creating container: %q", err)
  1036. }
  1037. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1038. c.Fatalf("Error creating container: %q", err)
  1039. }
  1040. s.d.Stop()
  1041. s.d.Start("--log-level=debug")
  1042. s.d.Stop()
  1043. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1044. if strings.Contains(string(content), "....") {
  1045. c.Fatalf("Debug level should not have ....\n%s", string(content))
  1046. }
  1047. s.d.Start("--log-level=error")
  1048. s.d.Stop()
  1049. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1050. if strings.Contains(string(content), "....") {
  1051. c.Fatalf("Error level should not have ....\n%s", string(content))
  1052. }
  1053. s.d.Start("--log-level=info")
  1054. s.d.Stop()
  1055. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1056. if !strings.Contains(string(content), "....") {
  1057. c.Fatalf("Info level should have ....\n%s", string(content))
  1058. }
  1059. }
  1060. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  1061. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  1062. if err != nil {
  1063. c.Fatal(err)
  1064. }
  1065. defer os.RemoveAll(dir)
  1066. sockPath := filepath.Join(dir, "docker.sock")
  1067. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  1068. c.Fatal(err)
  1069. }
  1070. if _, err := os.Stat(sockPath); err != nil {
  1071. c.Fatal("socket does not exist")
  1072. }
  1073. if err := s.d.Stop(); err != nil {
  1074. c.Fatal(err)
  1075. }
  1076. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  1077. c.Fatal("unix socket is not cleaned up")
  1078. }
  1079. }
  1080. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  1081. type Config struct {
  1082. Crv string `json:"crv"`
  1083. D string `json:"d"`
  1084. Kid string `json:"kid"`
  1085. Kty string `json:"kty"`
  1086. X string `json:"x"`
  1087. Y string `json:"y"`
  1088. }
  1089. os.Remove("/etc/docker/key.json")
  1090. if err := s.d.Start(); err != nil {
  1091. c.Fatalf("Failed to start daemon: %v", err)
  1092. }
  1093. if err := s.d.Stop(); err != nil {
  1094. c.Fatalf("Could not stop daemon: %v", err)
  1095. }
  1096. config := &Config{}
  1097. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  1098. if err != nil {
  1099. c.Fatalf("Error reading key.json file: %s", err)
  1100. }
  1101. // byte[] to Data-Struct
  1102. if err := json.Unmarshal(bytes, &config); err != nil {
  1103. c.Fatalf("Error Unmarshal: %s", err)
  1104. }
  1105. //replace config.Kid with the fake value
  1106. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  1107. // NEW Data-Struct to byte[]
  1108. newBytes, err := json.Marshal(&config)
  1109. if err != nil {
  1110. c.Fatalf("Error Marshal: %s", err)
  1111. }
  1112. // write back
  1113. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  1114. c.Fatalf("Error ioutil.WriteFile: %s", err)
  1115. }
  1116. defer os.Remove("/etc/docker/key.json")
  1117. if err := s.d.Start(); err == nil {
  1118. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  1119. }
  1120. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1121. if !strings.Contains(string(content), "Public Key ID does not match") {
  1122. c.Fatal("Missing KeyID message from daemon logs")
  1123. }
  1124. }
  1125. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  1126. if err := s.d.StartWithBusybox(); err != nil {
  1127. c.Fatalf("Could not start daemon with busybox: %v", err)
  1128. }
  1129. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  1130. if err != nil {
  1131. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  1132. }
  1133. containerID := strings.TrimSpace(out)
  1134. if out, err := s.d.Cmd("kill", containerID); err != nil {
  1135. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  1136. }
  1137. if err := s.d.Restart(); err != nil {
  1138. c.Fatalf("Could not restart daemon: %v", err)
  1139. }
  1140. errchan := make(chan error)
  1141. go func() {
  1142. if out, err := s.d.Cmd("wait", containerID); err != nil {
  1143. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1144. }
  1145. close(errchan)
  1146. }()
  1147. select {
  1148. case <-time.After(5 * time.Second):
  1149. c.Fatal("Waiting on a stopped (killed) container timed out")
  1150. case err := <-errchan:
  1151. if err != nil {
  1152. c.Fatal(err)
  1153. }
  1154. }
  1155. }
  1156. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  1157. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  1158. const (
  1159. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1160. )
  1161. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1162. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1163. c.Fatalf("Could not start daemon with busybox: %v", err)
  1164. }
  1165. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  1166. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1167. if err != nil {
  1168. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1169. }
  1170. }
  1171. // TestTlsVerify verifies that --tlsverify=false turns on tls
  1172. func (s *DockerDaemonSuite) TestTlsVerify(c *check.C) {
  1173. out, err := exec.Command(dockerBinary, "daemon", "--tlsverify=false").CombinedOutput()
  1174. if err == nil || !strings.Contains(string(out), "Could not load X509 key pair") {
  1175. c.Fatalf("Daemon should not have started due to missing certs: %v\n%s", err, string(out))
  1176. }
  1177. }
  1178. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  1179. // by using a rogue client certificate and checks that it fails with the expected error.
  1180. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  1181. const (
  1182. errBadCertificate = "remote error: bad certificate"
  1183. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1184. )
  1185. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1186. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1187. c.Fatalf("Could not start daemon with busybox: %v", err)
  1188. }
  1189. daemonArgs := []string{"--host", testDaemonHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  1190. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1191. if err == nil || !strings.Contains(out, errBadCertificate) {
  1192. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  1193. }
  1194. }
  1195. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  1196. // which provides a rogue server certificate and checks that it fails with the expected error
  1197. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  1198. const (
  1199. errCaUnknown = "x509: certificate signed by unknown authority"
  1200. testDaemonRogueHTTPSAddr = "tcp://localhost:4272"
  1201. )
  1202. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  1203. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHTTPSAddr); err != nil {
  1204. c.Fatalf("Could not start daemon with busybox: %v", err)
  1205. }
  1206. daemonArgs := []string{"--host", testDaemonRogueHTTPSAddr, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  1207. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  1208. if err == nil || !strings.Contains(out, errCaUnknown) {
  1209. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  1210. }
  1211. }
  1212. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  1213. var dargs []string
  1214. if d != nil {
  1215. dargs = []string{"--host", d.sock()}
  1216. }
  1217. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  1218. dockerCmd(c, args...)
  1219. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  1220. pingCmd := "ping -c 1 %s -W 1"
  1221. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  1222. _, _, err := dockerCmdWithError(args...)
  1223. if expectFailure {
  1224. c.Assert(err, check.NotNil)
  1225. } else {
  1226. c.Assert(err, check.IsNil)
  1227. }
  1228. args = append(dargs, "rm", "-f", "container1")
  1229. dockerCmd(c, args...)
  1230. }
  1231. func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) {
  1232. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1233. socket := filepath.Join(s.d.folder, "docker.sock")
  1234. out, err := s.d.Cmd("run", "-d", "--restart=always", "-v", socket+":/sock", "busybox")
  1235. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1236. c.Assert(s.d.Restart(), check.IsNil)
  1237. }
  1238. func (s *DockerDaemonSuite) TestCleanupMountsAfterCrash(c *check.C) {
  1239. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1240. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  1241. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1242. id := strings.TrimSpace(out)
  1243. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  1244. c.Assert(s.d.Start(), check.IsNil)
  1245. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1246. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1247. comment := check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1248. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1249. }
  1250. func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
  1251. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1252. c.Assert(s.d.StartWithBusybox("-b", "none"), check.IsNil)
  1253. out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
  1254. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1255. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1256. check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out))
  1257. out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l")
  1258. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1259. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1260. check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out))
  1261. // the extra grep and awk clean up the output of `ip` to only list the number and name of
  1262. // interfaces, allowing for different versions of ip (e.g. inside and outside the container) to
  1263. // be used while still verifying that the interface list is the exact same
  1264. cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1265. stdout := bytes.NewBuffer(nil)
  1266. cmd.Stdout = stdout
  1267. if err := cmd.Run(); err != nil {
  1268. c.Fatal("Failed to get host network interface")
  1269. }
  1270. out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1271. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1272. c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout),
  1273. check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out))
  1274. }
  1275. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
  1276. if err := s.d.StartWithBusybox(); err != nil {
  1277. t.Fatal(err)
  1278. }
  1279. if out, err := s.d.Cmd("run", "-ti", "-d", "--name", "test", "busybox"); err != nil {
  1280. t.Fatal(out, err)
  1281. }
  1282. if err := s.d.Restart(); err != nil {
  1283. t.Fatal(err)
  1284. }
  1285. // Container 'test' should be removed without error
  1286. if out, err := s.d.Cmd("rm", "test"); err != nil {
  1287. t.Fatal(out, err)
  1288. }
  1289. }
  1290. func (s *DockerDaemonSuite) TestDaemonRestartCleanupNetns(c *check.C) {
  1291. if err := s.d.StartWithBusybox(); err != nil {
  1292. c.Fatal(err)
  1293. }
  1294. out, err := s.d.Cmd("run", "--name", "netns", "-d", "busybox", "top")
  1295. if err != nil {
  1296. c.Fatal(out, err)
  1297. }
  1298. // Get sandbox key via inspect
  1299. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.SandboxKey}}'", "netns")
  1300. if err != nil {
  1301. c.Fatalf("Error inspecting container: %s, %v", out, err)
  1302. }
  1303. fileName := strings.Trim(out, " \r\n'")
  1304. if out, err := s.d.Cmd("stop", "netns"); err != nil {
  1305. c.Fatal(out, err)
  1306. }
  1307. // Test if the file still exists
  1308. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1309. out = strings.TrimSpace(out)
  1310. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1311. c.Assert(out, check.Equals, fileName, check.Commentf("Output: %s", out))
  1312. // Remove the container and restart the daemon
  1313. if out, err := s.d.Cmd("rm", "netns"); err != nil {
  1314. c.Fatal(out, err)
  1315. }
  1316. if err := s.d.Restart(); err != nil {
  1317. c.Fatal(err)
  1318. }
  1319. // Test again and see now the netns file does not exist
  1320. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1321. out = strings.TrimSpace(out)
  1322. c.Assert(err, check.Not(check.IsNil), check.Commentf("Output: %s", out))
  1323. }
  1324. // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored
  1325. func (s *DockerDaemonSuite) TestDaemonNoTlsCliTlsVerifyWithEnv(c *check.C) {
  1326. host := "tcp://localhost:4271"
  1327. c.Assert(s.d.Start("-H", host), check.IsNil)
  1328. cmd := exec.Command(dockerBinary, "-H", host, "info")
  1329. cmd.Env = []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"}
  1330. out, _, err := runCommandWithOutput(cmd)
  1331. c.Assert(err, check.Not(check.IsNil), check.Commentf("%s", out))
  1332. c.Assert(strings.Contains(out, "error occurred trying to connect"), check.Equals, true)
  1333. }
  1334. func setupV6() error {
  1335. // Hack to get the right IPv6 address on docker0, which has already been created
  1336. err := exec.Command("ip", "addr", "add", "fe80::1/64", "dev", "docker0").Run()
  1337. if err != nil {
  1338. return err
  1339. }
  1340. return nil
  1341. }
  1342. func teardownV6() error {
  1343. err := exec.Command("ip", "addr", "del", "fe80::1/64", "dev", "docker0").Run()
  1344. if err != nil {
  1345. return err
  1346. }
  1347. return nil
  1348. }
  1349. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) {
  1350. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1351. out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  1352. c.Assert(err, check.IsNil)
  1353. id := strings.TrimSpace(out)
  1354. _, err = s.d.Cmd("stop", id)
  1355. c.Assert(err, check.IsNil)
  1356. _, err = s.d.Cmd("wait", id)
  1357. c.Assert(err, check.IsNil)
  1358. out, err = s.d.Cmd("ps", "-q")
  1359. c.Assert(err, check.IsNil)
  1360. c.Assert(out, check.Equals, "")
  1361. c.Assert(s.d.Restart(), check.IsNil)
  1362. out, err = s.d.Cmd("ps", "-q")
  1363. c.Assert(err, check.IsNil)
  1364. c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
  1365. }
  1366. func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) {
  1367. if err := s.d.StartWithBusybox("--log-driver=json-file", "--log-opt=max-size=1k"); err != nil {
  1368. c.Fatal(err)
  1369. }
  1370. out, err := s.d.Cmd("run", "-d", "--name=logtest", "busybox", "top")
  1371. c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err))
  1372. out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", "logtest")
  1373. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1374. cfg := strings.TrimSpace(out)
  1375. if cfg != "map[max-size:1k]" {
  1376. c.Fatalf("Unexpected log-opt: %s, expected map[max-size:1k]", cfg)
  1377. }
  1378. }
  1379. func (s *DockerDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
  1380. if err := s.d.StartWithBusybox(); err != nil {
  1381. c.Fatal(err)
  1382. }
  1383. if out, err := s.d.Cmd("run", "-i", "-d", "--name", "test", "busybox", "top"); err != nil {
  1384. c.Fatal(err, out)
  1385. }
  1386. if out, err := s.d.Cmd("pause", "test"); err != nil {
  1387. c.Fatal(err, out)
  1388. }
  1389. if err := s.d.Restart(); err != nil {
  1390. c.Fatal(err)
  1391. }
  1392. errchan := make(chan error)
  1393. go func() {
  1394. out, err := s.d.Cmd("start", "test")
  1395. if err != nil {
  1396. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1397. }
  1398. name := strings.TrimSpace(out)
  1399. if name != "test" {
  1400. errchan <- fmt.Errorf("Paused container start error on docker daemon restart, expected 'test' but got '%s'", name)
  1401. }
  1402. close(errchan)
  1403. }()
  1404. select {
  1405. case <-time.After(5 * time.Second):
  1406. c.Fatal("Waiting on start a container timed out")
  1407. case err := <-errchan:
  1408. if err != nil {
  1409. c.Fatal(err)
  1410. }
  1411. }
  1412. }
  1413. func (s *DockerDaemonSuite) TestDaemonRestartRmVolumeInUse(c *check.C) {
  1414. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1415. out, err := s.d.Cmd("create", "-v", "test:/foo", "busybox")
  1416. c.Assert(err, check.IsNil, check.Commentf(out))
  1417. c.Assert(s.d.Restart(), check.IsNil)
  1418. out, err = s.d.Cmd("volume", "rm", "test")
  1419. c.Assert(err, check.Not(check.IsNil), check.Commentf("should not be able to remove in use volume after daemon restart"))
  1420. c.Assert(strings.Contains(out, "in use"), check.Equals, true)
  1421. }
  1422. func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
  1423. c.Assert(s.d.Start(), check.IsNil)
  1424. _, err := s.d.Cmd("volume", "create", "--name", "test")
  1425. c.Assert(err, check.IsNil)
  1426. c.Assert(s.d.Restart(), check.IsNil)
  1427. _, err = s.d.Cmd("volume", "inspect", "test")
  1428. c.Assert(err, check.IsNil)
  1429. }
  1430. func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
  1431. for _, driver := range []string{
  1432. "syslog",
  1433. "gelf",
  1434. } {
  1435. args := []string{"--log-driver=" + driver, "--log-opt", driver + "-address=corrupted:42"}
  1436. c.Assert(s.d.Start(args...), check.NotNil, check.Commentf(fmt.Sprintf("Expected daemon not to start with invalid %s-address provided", driver)))
  1437. expected := fmt.Sprintf("Failed to set log opts: %s-address should be in form proto://address", driver)
  1438. runCmd := exec.Command("grep", expected, s.d.LogfileName())
  1439. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1440. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1441. }
  1442. }
  1443. }
  1444. func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
  1445. c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
  1446. expected := "Failed to set log opts: invalid fluentd-address corrupted:c: "
  1447. runCmd := exec.Command("grep", expected, s.d.LogfileName())
  1448. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1449. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1450. }
  1451. }
  1452. func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) {
  1453. s.d.useDefaultHost = true
  1454. defer func() {
  1455. s.d.useDefaultHost = false
  1456. }()
  1457. c.Assert(s.d.Start(), check.IsNil)
  1458. }
  1459. func (s *DockerDaemonSuite) TestDaemonStartWithDefalutTlsHost(c *check.C) {
  1460. s.d.useDefaultTLSHost = true
  1461. defer func() {
  1462. s.d.useDefaultTLSHost = false
  1463. }()
  1464. if err := s.d.Start(
  1465. "--tlsverify",
  1466. "--tlscacert", "fixtures/https/ca.pem",
  1467. "--tlscert", "fixtures/https/server-cert.pem",
  1468. "--tlskey", "fixtures/https/server-key.pem"); err != nil {
  1469. c.Fatalf("Could not start daemon: %v", err)
  1470. }
  1471. // The client with --tlsverify should also use default host localhost:2376
  1472. tmpHost := os.Getenv("DOCKER_HOST")
  1473. defer func() {
  1474. os.Setenv("DOCKER_HOST", tmpHost)
  1475. }()
  1476. os.Setenv("DOCKER_HOST", "")
  1477. out, _ := dockerCmd(
  1478. c,
  1479. "--tlsverify",
  1480. "--tlscacert", "fixtures/https/ca.pem",
  1481. "--tlscert", "fixtures/https/client-cert.pem",
  1482. "--tlskey", "fixtures/https/client-key.pem",
  1483. "version",
  1484. )
  1485. if !strings.Contains(out, "Server") {
  1486. c.Fatalf("docker version should return information of server side")
  1487. }
  1488. }