docker_cli_daemon_test.go 68 KB

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