docker_cli_daemon_test.go 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757
  1. // +build linux
  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. "syscall"
  19. "time"
  20. "github.com/docker/docker/pkg/integration/checker"
  21. icmd "github.com/docker/docker/pkg/integration/cmd"
  22. "github.com/docker/docker/pkg/mount"
  23. "github.com/docker/docker/pkg/testutil/tempfile"
  24. "github.com/docker/go-units"
  25. "github.com/docker/libnetwork/iptables"
  26. "github.com/docker/libtrust"
  27. "github.com/go-check/check"
  28. "github.com/kr/pty"
  29. )
  30. // TestLegacyDaemonCommand test starting docker daemon using "deprecated" docker daemon
  31. // command. Remove this test when we remove this.
  32. func (s *DockerDaemonSuite) TestLegacyDaemonCommand(c *check.C) {
  33. cmd := exec.Command(dockerBinary, "daemon", "--storage-driver=vfs", "--debug")
  34. err := cmd.Start()
  35. c.Assert(err, checker.IsNil, check.Commentf("could not start daemon using 'docker daemon'"))
  36. c.Assert(cmd.Process.Kill(), checker.IsNil)
  37. }
  38. func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
  39. if err := s.d.StartWithBusybox(); err != nil {
  40. c.Fatalf("Could not start daemon with busybox: %v", err)
  41. }
  42. if out, err := s.d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
  43. c.Fatalf("Could not run top1: err=%v\n%s", err, out)
  44. }
  45. // --restart=no by default
  46. if out, err := s.d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
  47. c.Fatalf("Could not run top2: err=%v\n%s", err, out)
  48. }
  49. testRun := func(m map[string]bool, prefix string) {
  50. var format string
  51. for cont, shouldRun := range m {
  52. out, err := s.d.Cmd("ps")
  53. if err != nil {
  54. c.Fatalf("Could not run ps: err=%v\n%q", err, out)
  55. }
  56. if shouldRun {
  57. format = "%scontainer %q is not running"
  58. } else {
  59. format = "%scontainer %q is running"
  60. }
  61. if shouldRun != strings.Contains(out, cont) {
  62. c.Fatalf(format, prefix, cont)
  63. }
  64. }
  65. }
  66. testRun(map[string]bool{"top1": true, "top2": true}, "")
  67. if err := s.d.Restart(); err != nil {
  68. c.Fatalf("Could not restart daemon: %v", err)
  69. }
  70. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  71. }
  72. func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) {
  73. if err := s.d.StartWithBusybox(); err != nil {
  74. c.Fatal(err)
  75. }
  76. if out, err := s.d.Cmd("run", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  77. c.Fatal(err, out)
  78. }
  79. if err := s.d.Restart(); err != nil {
  80. c.Fatal(err)
  81. }
  82. if _, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
  83. c.Fatal(err)
  84. }
  85. if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  86. c.Fatal(err, out)
  87. }
  88. out, err := s.d.Cmd("inspect", "-f", "{{json .Mounts}}", "volrestarttest1")
  89. c.Assert(err, check.IsNil)
  90. if _, err := inspectMountPointJSON(out, "/foo"); err != nil {
  91. c.Fatalf("Expected volume to exist: /foo, error: %v\n", err)
  92. }
  93. }
  94. // #11008
  95. func (s *DockerDaemonSuite) TestDaemonRestartUnlessStopped(c *check.C) {
  96. err := s.d.StartWithBusybox()
  97. c.Assert(err, check.IsNil)
  98. out, err := s.d.Cmd("run", "-d", "--name", "top1", "--restart", "always", "busybox:latest", "top")
  99. c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out))
  100. out, err = s.d.Cmd("run", "-d", "--name", "top2", "--restart", "unless-stopped", "busybox:latest", "top")
  101. c.Assert(err, check.IsNil, check.Commentf("run top2: %v", out))
  102. testRun := func(m map[string]bool, prefix string) {
  103. var format string
  104. for name, shouldRun := range m {
  105. out, err := s.d.Cmd("ps")
  106. c.Assert(err, check.IsNil, check.Commentf("run ps: %v", out))
  107. if shouldRun {
  108. format = "%scontainer %q is not running"
  109. } else {
  110. format = "%scontainer %q is running"
  111. }
  112. c.Assert(strings.Contains(out, name), check.Equals, shouldRun, check.Commentf(format, prefix, name))
  113. }
  114. }
  115. // both running
  116. testRun(map[string]bool{"top1": true, "top2": true}, "")
  117. out, err = s.d.Cmd("stop", "top1")
  118. c.Assert(err, check.IsNil, check.Commentf(out))
  119. out, err = s.d.Cmd("stop", "top2")
  120. c.Assert(err, check.IsNil, check.Commentf(out))
  121. // both stopped
  122. testRun(map[string]bool{"top1": false, "top2": false}, "")
  123. err = s.d.Restart()
  124. c.Assert(err, check.IsNil)
  125. // restart=always running
  126. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  127. out, err = s.d.Cmd("start", "top2")
  128. c.Assert(err, check.IsNil, check.Commentf("start top2: %v", out))
  129. err = s.d.Restart()
  130. c.Assert(err, check.IsNil)
  131. // both running
  132. testRun(map[string]bool{"top1": true, "top2": true}, "After second daemon restart: ")
  133. }
  134. func (s *DockerDaemonSuite) TestDaemonRestartOnFailure(c *check.C) {
  135. err := s.d.StartWithBusybox()
  136. c.Assert(err, check.IsNil)
  137. out, err := s.d.Cmd("run", "-d", "--name", "test1", "--restart", "on-failure:3", "busybox:latest", "false")
  138. c.Assert(err, check.IsNil, check.Commentf("run top1: %v", out))
  139. // wait test1 to stop
  140. hostArgs := []string{"--host", s.d.sock()}
  141. err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 10*time.Second, hostArgs...)
  142. c.Assert(err, checker.IsNil, check.Commentf("test1 should exit but not"))
  143. // record last start time
  144. out, err = s.d.Cmd("inspect", "-f={{.State.StartedAt}}", "test1")
  145. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  146. lastStartTime := out
  147. err = s.d.Restart()
  148. c.Assert(err, check.IsNil)
  149. // test1 shouldn't restart at all
  150. err = waitInspectWithArgs("test1", "{{.State.Running}} {{.State.Restarting}}", "false false", 0, hostArgs...)
  151. c.Assert(err, checker.IsNil, check.Commentf("test1 should exit but not"))
  152. // make sure test1 isn't restarted when daemon restart
  153. // if "StartAt" time updates, means test1 was once restarted.
  154. out, err = s.d.Cmd("inspect", "-f={{.State.StartedAt}}", "test1")
  155. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  156. c.Assert(out, checker.Equals, lastStartTime, check.Commentf("test1 shouldn't start after daemon restarts"))
  157. }
  158. func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) {
  159. if err := s.d.Start("--iptables=false"); err != nil {
  160. c.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  161. }
  162. }
  163. // Make sure we cannot shrink base device at daemon restart.
  164. func (s *DockerDaemonSuite) TestDaemonRestartWithInvalidBasesize(c *check.C) {
  165. testRequires(c, Devicemapper)
  166. c.Assert(s.d.Start(), check.IsNil)
  167. oldBasesizeBytes := s.d.getBaseDeviceSize(c)
  168. var newBasesizeBytes int64 = 1073741824 //1GB in bytes
  169. if newBasesizeBytes < oldBasesizeBytes {
  170. err := s.d.Restart("--storage-opt", fmt.Sprintf("dm.basesize=%d", newBasesizeBytes))
  171. c.Assert(err, check.IsNil, check.Commentf("daemon should not have started as new base device size is less than existing base device size: %v", err))
  172. }
  173. c.Assert(s.d.Stop(), check.IsNil)
  174. }
  175. // Make sure we can grow base device at daemon restart.
  176. func (s *DockerDaemonSuite) TestDaemonRestartWithIncreasedBasesize(c *check.C) {
  177. testRequires(c, Devicemapper)
  178. c.Assert(s.d.Start(), check.IsNil)
  179. oldBasesizeBytes := s.d.getBaseDeviceSize(c)
  180. var newBasesizeBytes int64 = 53687091200 //50GB in bytes
  181. if newBasesizeBytes < oldBasesizeBytes {
  182. c.Skip(fmt.Sprintf("New base device size (%v) must be greater than (%s)", units.HumanSize(float64(newBasesizeBytes)), units.HumanSize(float64(oldBasesizeBytes))))
  183. }
  184. err := s.d.Restart("--storage-opt", fmt.Sprintf("dm.basesize=%d", newBasesizeBytes))
  185. c.Assert(err, check.IsNil, check.Commentf("we should have been able to start the daemon with increased base device size: %v", err))
  186. basesizeAfterRestart := s.d.getBaseDeviceSize(c)
  187. newBasesize, err := convertBasesize(newBasesizeBytes)
  188. c.Assert(err, check.IsNil, check.Commentf("Error in converting base device size: %v", err))
  189. c.Assert(newBasesize, check.Equals, basesizeAfterRestart, check.Commentf("Basesize passed is not equal to Basesize set"))
  190. c.Assert(s.d.Stop(), check.IsNil)
  191. }
  192. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  193. // no longer has an IP associated, we should gracefully handle that case and associate
  194. // an IP with it rather than fail daemon start
  195. func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) {
  196. // rather than depending on brctl commands to verify docker0 is created and up
  197. // let's start the daemon and stop it, and then make a modification to run the
  198. // actual test
  199. if err := s.d.Start(); err != nil {
  200. c.Fatalf("Could not start daemon: %v", err)
  201. }
  202. if err := s.d.Stop(); err != nil {
  203. c.Fatalf("Could not stop daemon: %v", err)
  204. }
  205. // now we will remove the ip from docker0 and then try starting the daemon
  206. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  207. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  208. if err != nil {
  209. c.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  210. }
  211. if err := s.d.Start(); err != nil {
  212. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  213. c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  214. }
  215. }
  216. func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) {
  217. if err := s.d.StartWithBusybox(); err != nil {
  218. c.Fatalf("Could not start daemon with busybox: %v", err)
  219. }
  220. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  221. c.Fatalf("Could not run top: %s, %v", out, err)
  222. }
  223. // get output from iptables with container running
  224. ipTablesSearchString := "tcp dpt:80"
  225. ipTablesCmd := exec.Command("iptables", "-nvL")
  226. out, _, err := runCommandWithOutput(ipTablesCmd)
  227. if err != nil {
  228. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  229. }
  230. if !strings.Contains(out, ipTablesSearchString) {
  231. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  232. }
  233. if err := s.d.Stop(); err != nil {
  234. c.Fatalf("Could not stop daemon: %v", err)
  235. }
  236. // get output from iptables after restart
  237. ipTablesCmd = exec.Command("iptables", "-nvL")
  238. out, _, err = runCommandWithOutput(ipTablesCmd)
  239. if err != nil {
  240. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  241. }
  242. if strings.Contains(out, ipTablesSearchString) {
  243. c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  244. }
  245. }
  246. func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) {
  247. if err := s.d.StartWithBusybox(); err != nil {
  248. c.Fatalf("Could not start daemon with busybox: %v", err)
  249. }
  250. if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  251. c.Fatalf("Could not run top: %s, %v", out, err)
  252. }
  253. // get output from iptables with container running
  254. ipTablesSearchString := "tcp dpt:80"
  255. ipTablesCmd := exec.Command("iptables", "-nvL")
  256. out, _, err := runCommandWithOutput(ipTablesCmd)
  257. if err != nil {
  258. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  259. }
  260. if !strings.Contains(out, ipTablesSearchString) {
  261. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  262. }
  263. if err := s.d.Restart(); err != nil {
  264. c.Fatalf("Could not restart daemon: %v", err)
  265. }
  266. // make sure the container is not running
  267. runningOut, err := s.d.Cmd("inspect", "--format={{.State.Running}}", "top")
  268. if err != nil {
  269. c.Fatalf("Could not inspect on container: %s, %v", out, err)
  270. }
  271. if strings.TrimSpace(runningOut) != "true" {
  272. c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  273. }
  274. // get output from iptables after restart
  275. ipTablesCmd = exec.Command("iptables", "-nvL")
  276. out, _, err = runCommandWithOutput(ipTablesCmd)
  277. if err != nil {
  278. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  279. }
  280. if !strings.Contains(out, ipTablesSearchString) {
  281. c.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  282. }
  283. }
  284. // TestDaemonIPv6Enabled checks that when the daemon is started with --ipv6=true that the docker0 bridge
  285. // has the fe80::1 address and that a container is assigned a link-local address
  286. func (s *DockerSuite) TestDaemonIPv6Enabled(c *check.C) {
  287. testRequires(c, IPv6)
  288. setupV6(c)
  289. defer teardownV6(c)
  290. d := NewDaemon(c)
  291. if err := d.StartWithBusybox("--ipv6"); err != nil {
  292. c.Fatal(err)
  293. }
  294. defer d.Stop()
  295. iface, err := net.InterfaceByName("docker0")
  296. if err != nil {
  297. c.Fatalf("Error getting docker0 interface: %v", err)
  298. }
  299. addrs, err := iface.Addrs()
  300. if err != nil {
  301. c.Fatalf("Error getting addresses for docker0 interface: %v", err)
  302. }
  303. var found bool
  304. expected := "fe80::1/64"
  305. for i := range addrs {
  306. if addrs[i].String() == expected {
  307. found = true
  308. break
  309. }
  310. }
  311. if !found {
  312. c.Fatalf("Bridge does not have an IPv6 Address")
  313. }
  314. if out, err := d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest"); err != nil {
  315. c.Fatalf("Could not run container: %s, %v", out, err)
  316. }
  317. out, err := d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.LinkLocalIPv6Address}}'", "ipv6test")
  318. out = strings.Trim(out, " \r\n'")
  319. if err != nil {
  320. c.Fatalf("Error inspecting container: %s, %v", out, err)
  321. }
  322. if ip := net.ParseIP(out); ip == nil {
  323. c.Fatalf("Container should have a link-local IPv6 address")
  324. }
  325. out, err = d.Cmd("inspect", "--format", "'{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}'", "ipv6test")
  326. out = strings.Trim(out, " \r\n'")
  327. if err != nil {
  328. c.Fatalf("Error inspecting container: %s, %v", out, err)
  329. }
  330. if ip := net.ParseIP(out); ip != nil {
  331. c.Fatalf("Container should not have a global IPv6 address: %v", out)
  332. }
  333. }
  334. // TestDaemonIPv6FixedCIDR checks that when the daemon is started with --ipv6=true and a fixed CIDR
  335. // that running containers are given a link-local and global IPv6 address
  336. func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDR(c *check.C) {
  337. // IPv6 setup is messing with local bridge address.
  338. testRequires(c, SameHostDaemon)
  339. setupV6(c)
  340. defer teardownV6(c)
  341. err := s.d.StartWithBusybox("--ipv6", "--fixed-cidr-v6=2001:db8:2::/64", "--default-gateway-v6=2001:db8:2::100")
  342. c.Assert(err, checker.IsNil, check.Commentf("Could not start daemon with busybox: %v", err))
  343. out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "busybox:latest")
  344. c.Assert(err, checker.IsNil, check.Commentf("Could not run container: %s, %v", out, err))
  345. out, err = s.d.Cmd("inspect", "--format", "{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}", "ipv6test")
  346. out = strings.Trim(out, " \r\n'")
  347. c.Assert(err, checker.IsNil, check.Commentf(out))
  348. ip := net.ParseIP(out)
  349. c.Assert(ip, checker.NotNil, check.Commentf("Container should have a global IPv6 address"))
  350. out, err = s.d.Cmd("inspect", "--format", "{{.NetworkSettings.Networks.bridge.IPv6Gateway}}", "ipv6test")
  351. c.Assert(err, checker.IsNil, check.Commentf(out))
  352. c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:2::100", check.Commentf("Container should have a global IPv6 gateway"))
  353. }
  354. // TestDaemonIPv6FixedCIDRAndMac checks that when the daemon is started with ipv6 fixed CIDR
  355. // the running containers are given an IPv6 address derived from the MAC address and the ipv6 fixed CIDR
  356. func (s *DockerDaemonSuite) TestDaemonIPv6FixedCIDRAndMac(c *check.C) {
  357. // IPv6 setup is messing with local bridge address.
  358. testRequires(c, SameHostDaemon)
  359. setupV6(c)
  360. defer teardownV6(c)
  361. err := s.d.StartWithBusybox("--ipv6", "--fixed-cidr-v6=2001:db8:1::/64")
  362. c.Assert(err, checker.IsNil)
  363. out, err := s.d.Cmd("run", "-itd", "--name=ipv6test", "--mac-address", "AA:BB:CC:DD:EE:FF", "busybox")
  364. c.Assert(err, checker.IsNil)
  365. out, err = s.d.Cmd("inspect", "--format", "{{.NetworkSettings.Networks.bridge.GlobalIPv6Address}}", "ipv6test")
  366. c.Assert(err, checker.IsNil)
  367. c.Assert(strings.Trim(out, " \r\n'"), checker.Equals, "2001:db8:1::aabb:ccdd:eeff")
  368. }
  369. func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
  370. c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
  371. }
  372. func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
  373. if err := s.d.Start("--log-level=debug"); err != nil {
  374. c.Fatal(err)
  375. }
  376. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  377. if !strings.Contains(string(content), `level=debug`) {
  378. c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
  379. }
  380. }
  381. func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) {
  382. // we creating new daemons to create new logFile
  383. if err := s.d.Start("--log-level=fatal"); err != nil {
  384. c.Fatal(err)
  385. }
  386. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  387. if strings.Contains(string(content), `level=debug`) {
  388. c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
  389. }
  390. }
  391. func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) {
  392. if err := s.d.Start("-D"); err != nil {
  393. c.Fatal(err)
  394. }
  395. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  396. if !strings.Contains(string(content), `level=debug`) {
  397. c.Fatalf(`Should have level="debug" in log file using -D:\n%s`, string(content))
  398. }
  399. }
  400. func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) {
  401. if err := s.d.Start("--debug"); err != nil {
  402. c.Fatal(err)
  403. }
  404. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  405. if !strings.Contains(string(content), `level=debug`) {
  406. c.Fatalf(`Should have level="debug" in log file using --debug:\n%s`, string(content))
  407. }
  408. }
  409. func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) {
  410. if err := s.d.Start("--debug", "--log-level=fatal"); err != nil {
  411. c.Fatal(err)
  412. }
  413. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  414. if !strings.Contains(string(content), `level=debug`) {
  415. c.Fatalf(`Should have level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
  416. }
  417. }
  418. func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) {
  419. listeningPorts := [][]string{
  420. {"0.0.0.0", "0.0.0.0", "5678"},
  421. {"127.0.0.1", "127.0.0.1", "1234"},
  422. {"localhost", "127.0.0.1", "1235"},
  423. }
  424. cmdArgs := make([]string, 0, len(listeningPorts)*2)
  425. for _, hostDirective := range listeningPorts {
  426. cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
  427. }
  428. if err := s.d.StartWithBusybox(cmdArgs...); err != nil {
  429. c.Fatalf("Could not start daemon with busybox: %v", err)
  430. }
  431. for _, hostDirective := range listeningPorts {
  432. output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
  433. if err == nil {
  434. c.Fatalf("Container should not start, expected port already allocated error: %q", output)
  435. } else if !strings.Contains(output, "port is already allocated") {
  436. c.Fatalf("Expected port is already allocated error: %q", output)
  437. }
  438. }
  439. }
  440. func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) {
  441. // TODO: skip or update for Windows daemon
  442. os.Remove("/etc/docker/key.json")
  443. if err := s.d.Start(); err != nil {
  444. c.Fatalf("Could not start daemon: %v", err)
  445. }
  446. s.d.Stop()
  447. k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  448. if err != nil {
  449. c.Fatalf("Error opening key file")
  450. }
  451. kid := k.KeyID()
  452. // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
  453. if len(kid) != 59 {
  454. c.Fatalf("Bad key ID: %s", kid)
  455. }
  456. }
  457. func (s *DockerDaemonSuite) TestDaemonKeyMigration(c *check.C) {
  458. // TODO: skip or update for Windows daemon
  459. os.Remove("/etc/docker/key.json")
  460. k1, err := libtrust.GenerateECP256PrivateKey()
  461. if err != nil {
  462. c.Fatalf("Error generating private key: %s", err)
  463. }
  464. if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil {
  465. c.Fatalf("Error creating .docker directory: %s", err)
  466. }
  467. if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil {
  468. c.Fatalf("Error saving private key: %s", err)
  469. }
  470. if err := s.d.Start(); err != nil {
  471. c.Fatalf("Could not start daemon: %v", err)
  472. }
  473. s.d.Stop()
  474. k2, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  475. if err != nil {
  476. c.Fatalf("Error opening key file")
  477. }
  478. if k1.KeyID() != k2.KeyID() {
  479. c.Fatalf("Key not migrated")
  480. }
  481. }
  482. // GH#11320 - verify that the daemon exits on failure properly
  483. // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
  484. // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
  485. func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
  486. //attempt to start daemon with incorrect flags (we know -b and --bip conflict)
  487. if err := s.d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
  488. //verify we got the right error
  489. if !strings.Contains(err.Error(), "Daemon exited") {
  490. c.Fatalf("Expected daemon not to start, got %v", err)
  491. }
  492. // look in the log and make sure we got the message that daemon is shutting down
  493. runCmd := exec.Command("grep", "Error starting daemon", s.d.LogFileName())
  494. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  495. c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
  496. }
  497. } else {
  498. //if we didn't get an error and the daemon is running, this is a failure
  499. c.Fatal("Conflicting options should cause the daemon to error out with a failure")
  500. }
  501. }
  502. func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
  503. d := s.d
  504. err := d.Start("--bridge", "nosuchbridge")
  505. c.Assert(err, check.NotNil, check.Commentf("--bridge option with an invalid bridge should cause the daemon to fail"))
  506. defer d.Restart()
  507. bridgeName := "external-bridge"
  508. bridgeIP := "192.169.1.1/24"
  509. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  510. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  511. c.Assert(err, check.IsNil, check.Commentf(out))
  512. defer deleteInterface(c, bridgeName)
  513. err = d.StartWithBusybox("--bridge", bridgeName)
  514. c.Assert(err, check.IsNil)
  515. ipTablesSearchString := bridgeIPNet.String()
  516. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  517. out, _, err = runCommandWithOutput(ipTablesCmd)
  518. c.Assert(err, check.IsNil)
  519. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  520. check.Commentf("iptables output should have contained %q, but was %q",
  521. ipTablesSearchString, out))
  522. _, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
  523. c.Assert(err, check.IsNil)
  524. containerIP := d.findContainerIP("ExtContainer")
  525. ip := net.ParseIP(containerIP)
  526. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  527. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  528. containerIP))
  529. }
  530. func createInterface(c *check.C, ifType string, ifName string, ipNet string) (string, error) {
  531. args := []string{"link", "add", "name", ifName, "type", ifType}
  532. ipLinkCmd := exec.Command("ip", args...)
  533. out, _, err := runCommandWithOutput(ipLinkCmd)
  534. if err != nil {
  535. return out, err
  536. }
  537. ifCfgCmd := exec.Command("ifconfig", ifName, ipNet, "up")
  538. out, _, err = runCommandWithOutput(ifCfgCmd)
  539. return out, err
  540. }
  541. func deleteInterface(c *check.C, ifName string) {
  542. ifCmd := exec.Command("ip", "link", "delete", ifName)
  543. out, _, err := runCommandWithOutput(ifCmd)
  544. c.Assert(err, check.IsNil, check.Commentf(out))
  545. flushCmd := exec.Command("iptables", "-t", "nat", "--flush")
  546. out, _, err = runCommandWithOutput(flushCmd)
  547. c.Assert(err, check.IsNil, check.Commentf(out))
  548. flushCmd = exec.Command("iptables", "--flush")
  549. out, _, err = runCommandWithOutput(flushCmd)
  550. c.Assert(err, check.IsNil, check.Commentf(out))
  551. }
  552. func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
  553. // TestDaemonBridgeIP Steps
  554. // 1. Delete the existing docker0 Bridge
  555. // 2. Set --bip daemon configuration and start the new Docker Daemon
  556. // 3. Check if the bip config has taken effect using ifconfig and iptables commands
  557. // 4. Launch a Container and make sure the IP-Address is in the expected subnet
  558. // 5. Delete the docker0 Bridge
  559. // 6. Restart the Docker Daemon (via deferred action)
  560. // This Restart takes care of bringing docker0 interface back to auto-assigned IP
  561. defaultNetworkBridge := "docker0"
  562. deleteInterface(c, defaultNetworkBridge)
  563. d := s.d
  564. bridgeIP := "192.169.1.1/24"
  565. ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  566. err := d.StartWithBusybox("--bip", bridgeIP)
  567. c.Assert(err, check.IsNil)
  568. defer d.Restart()
  569. ifconfigSearchString := ip.String()
  570. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  571. out, _, _, err := runCommandWithStdoutStderr(ifconfigCmd)
  572. c.Assert(err, check.IsNil)
  573. c.Assert(strings.Contains(out, ifconfigSearchString), check.Equals, true,
  574. check.Commentf("ifconfig output should have contained %q, but was %q",
  575. ifconfigSearchString, out))
  576. ipTablesSearchString := bridgeIPNet.String()
  577. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  578. out, _, err = runCommandWithOutput(ipTablesCmd)
  579. c.Assert(err, check.IsNil)
  580. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  581. check.Commentf("iptables output should have contained %q, but was %q",
  582. ipTablesSearchString, out))
  583. out, err = d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  584. c.Assert(err, check.IsNil)
  585. containerIP := d.findContainerIP("test")
  586. ip = net.ParseIP(containerIP)
  587. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  588. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  589. containerIP))
  590. deleteInterface(c, defaultNetworkBridge)
  591. }
  592. func (s *DockerDaemonSuite) TestDaemonRestartWithBridgeIPChange(c *check.C) {
  593. if err := s.d.Start(); err != nil {
  594. c.Fatalf("Could not start daemon: %v", err)
  595. }
  596. defer s.d.Restart()
  597. if err := s.d.Stop(); err != nil {
  598. c.Fatalf("Could not stop daemon: %v", err)
  599. }
  600. // now we will change the docker0's IP and then try starting the daemon
  601. bridgeIP := "192.169.100.1/24"
  602. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
  603. ipCmd := exec.Command("ifconfig", "docker0", bridgeIP)
  604. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  605. if err != nil {
  606. c.Fatalf("failed to change docker0's IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  607. }
  608. if err := s.d.Start("--bip", bridgeIP); err != nil {
  609. c.Fatalf("Could not start daemon: %v", err)
  610. }
  611. //check if the iptables contains new bridgeIP MASQUERADE rule
  612. ipTablesSearchString := bridgeIPNet.String()
  613. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  614. out, _, err := runCommandWithOutput(ipTablesCmd)
  615. if err != nil {
  616. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  617. }
  618. if !strings.Contains(out, ipTablesSearchString) {
  619. c.Fatalf("iptables output should have contained new MASQUERADE rule with IP %q, but was %q", ipTablesSearchString, out)
  620. }
  621. }
  622. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
  623. d := s.d
  624. bridgeName := "external-bridge"
  625. bridgeIP := "192.169.1.1/24"
  626. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  627. c.Assert(err, check.IsNil, check.Commentf(out))
  628. defer deleteInterface(c, bridgeName)
  629. args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"}
  630. err = d.StartWithBusybox(args...)
  631. c.Assert(err, check.IsNil)
  632. defer d.Restart()
  633. for i := 0; i < 4; i++ {
  634. cName := "Container" + strconv.Itoa(i)
  635. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  636. if err != nil {
  637. c.Assert(strings.Contains(out, "no available IPv4 addresses"), check.Equals, true,
  638. check.Commentf("Could not run a Container : %s %s", err.Error(), out))
  639. }
  640. }
  641. }
  642. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr2(c *check.C) {
  643. d := s.d
  644. bridgeName := "external-bridge"
  645. bridgeIP := "10.2.2.1/16"
  646. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  647. c.Assert(err, check.IsNil, check.Commentf(out))
  648. defer deleteInterface(c, bridgeName)
  649. err = d.StartWithBusybox("--bip", bridgeIP, "--fixed-cidr", "10.2.2.0/24")
  650. c.Assert(err, check.IsNil)
  651. defer s.d.Restart()
  652. out, err = d.Cmd("run", "-d", "--name", "bb", "busybox", "top")
  653. c.Assert(err, checker.IsNil, check.Commentf(out))
  654. defer d.Cmd("stop", "bb")
  655. out, err = d.Cmd("exec", "bb", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
  656. c.Assert(out, checker.Equals, "10.2.2.0\n")
  657. out, err = d.Cmd("run", "--rm", "busybox", "/bin/sh", "-c", "ifconfig eth0 | awk '/inet addr/{print substr($2,6)}'")
  658. c.Assert(err, checker.IsNil, check.Commentf(out))
  659. c.Assert(out, checker.Equals, "10.2.2.2\n")
  660. }
  661. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCIDREqualBridgeNetwork(c *check.C) {
  662. d := s.d
  663. bridgeName := "external-bridge"
  664. bridgeIP := "172.27.42.1/16"
  665. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  666. c.Assert(err, check.IsNil, check.Commentf(out))
  667. defer deleteInterface(c, bridgeName)
  668. err = d.StartWithBusybox("--bridge", bridgeName, "--fixed-cidr", bridgeIP)
  669. c.Assert(err, check.IsNil)
  670. defer s.d.Restart()
  671. out, err = d.Cmd("run", "-d", "busybox", "top")
  672. c.Assert(err, check.IsNil, check.Commentf(out))
  673. cid1 := strings.TrimSpace(out)
  674. defer d.Cmd("stop", cid1)
  675. }
  676. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) {
  677. defaultNetworkBridge := "docker0"
  678. deleteInterface(c, defaultNetworkBridge)
  679. d := s.d
  680. bridgeIP := "192.169.1.1"
  681. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  682. err := d.StartWithBusybox("--bip", bridgeIPNet)
  683. c.Assert(err, check.IsNil)
  684. defer d.Restart()
  685. expectedMessage := fmt.Sprintf("default via %s dev", bridgeIP)
  686. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  687. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  688. check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'",
  689. bridgeIP, strings.TrimSpace(out)))
  690. deleteInterface(c, defaultNetworkBridge)
  691. }
  692. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) {
  693. defaultNetworkBridge := "docker0"
  694. deleteInterface(c, defaultNetworkBridge)
  695. d := s.d
  696. bridgeIP := "192.169.1.1"
  697. bridgeIPNet := fmt.Sprintf("%s/24", bridgeIP)
  698. gatewayIP := "192.169.1.254"
  699. err := d.StartWithBusybox("--bip", bridgeIPNet, "--default-gateway", gatewayIP)
  700. c.Assert(err, check.IsNil)
  701. defer d.Restart()
  702. expectedMessage := fmt.Sprintf("default via %s dev", gatewayIP)
  703. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  704. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  705. check.Commentf("Explicit default gateway should be %s, but default route was '%s'",
  706. gatewayIP, strings.TrimSpace(out)))
  707. deleteInterface(c, defaultNetworkBridge)
  708. }
  709. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4ExplicitOutsideContainerSubnet(c *check.C) {
  710. defaultNetworkBridge := "docker0"
  711. deleteInterface(c, defaultNetworkBridge)
  712. // Program a custom default gateway outside of the container subnet, daemon should accept it and start
  713. err := s.d.StartWithBusybox("--bip", "172.16.0.10/16", "--fixed-cidr", "172.16.1.0/24", "--default-gateway", "172.16.0.254")
  714. c.Assert(err, check.IsNil)
  715. deleteInterface(c, defaultNetworkBridge)
  716. s.d.Restart()
  717. }
  718. func (s *DockerDaemonSuite) TestDaemonDefaultNetworkInvalidClusterConfig(c *check.C) {
  719. testRequires(c, DaemonIsLinux, SameHostDaemon)
  720. // Start daemon without docker0 bridge
  721. defaultNetworkBridge := "docker0"
  722. deleteInterface(c, defaultNetworkBridge)
  723. d := NewDaemon(c)
  724. discoveryBackend := "consul://consuladdr:consulport/some/path"
  725. err := d.Start(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
  726. c.Assert(err, checker.IsNil)
  727. // Start daemon with docker0 bridge
  728. result := icmd.RunCommand("ifconfig", defaultNetworkBridge)
  729. c.Assert(result, icmd.Matches, icmd.Success)
  730. err = d.Restart(fmt.Sprintf("--cluster-store=%s", discoveryBackend))
  731. c.Assert(err, checker.IsNil)
  732. d.Stop()
  733. }
  734. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  735. d := s.d
  736. ipStr := "192.170.1.1/24"
  737. ip, _, _ := net.ParseCIDR(ipStr)
  738. args := []string{"--ip", ip.String()}
  739. err := d.StartWithBusybox(args...)
  740. c.Assert(err, check.IsNil)
  741. defer d.Restart()
  742. out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  743. c.Assert(err, check.NotNil,
  744. check.Commentf("Running a container must fail with an invalid --ip option"))
  745. c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
  746. ifName := "dummy"
  747. out, err = createInterface(c, "dummy", ifName, ipStr)
  748. c.Assert(err, check.IsNil, check.Commentf(out))
  749. defer deleteInterface(c, ifName)
  750. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  751. c.Assert(err, check.IsNil)
  752. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  753. out, _, err = runCommandWithOutput(ipTablesCmd)
  754. c.Assert(err, check.IsNil)
  755. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  756. matched, _ := regexp.MatchString(regex, out)
  757. c.Assert(matched, check.Equals, true,
  758. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  759. }
  760. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  761. testRequires(c, bridgeNfIptables)
  762. d := s.d
  763. bridgeName := "external-bridge"
  764. bridgeIP := "192.169.1.1/24"
  765. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  766. c.Assert(err, check.IsNil, check.Commentf(out))
  767. defer deleteInterface(c, bridgeName)
  768. args := []string{"--bridge", bridgeName, "--icc=false"}
  769. err = d.StartWithBusybox(args...)
  770. c.Assert(err, check.IsNil)
  771. defer d.Restart()
  772. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  773. out, _, err = runCommandWithOutput(ipTablesCmd)
  774. c.Assert(err, check.IsNil)
  775. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  776. matched, _ := regexp.MatchString(regex, out)
  777. c.Assert(matched, check.Equals, true,
  778. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  779. // Pinging another container must fail with --icc=false
  780. pingContainers(c, d, true)
  781. ipStr := "192.171.1.1/24"
  782. ip, _, _ := net.ParseCIDR(ipStr)
  783. ifName := "icc-dummy"
  784. createInterface(c, "dummy", ifName, ipStr)
  785. // But, Pinging external or a Host interface must succeed
  786. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  787. runArgs := []string{"run", "--rm", "busybox", "sh", "-c", pingCmd}
  788. _, err = d.Cmd(runArgs...)
  789. c.Assert(err, check.IsNil)
  790. }
  791. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  792. d := s.d
  793. bridgeName := "external-bridge"
  794. bridgeIP := "192.169.1.1/24"
  795. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  796. c.Assert(err, check.IsNil, check.Commentf(out))
  797. defer deleteInterface(c, bridgeName)
  798. args := []string{"--bridge", bridgeName, "--icc=false"}
  799. err = d.StartWithBusybox(args...)
  800. c.Assert(err, check.IsNil)
  801. defer d.Restart()
  802. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  803. out, _, err = runCommandWithOutput(ipTablesCmd)
  804. c.Assert(err, check.IsNil)
  805. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  806. matched, _ := regexp.MatchString(regex, out)
  807. c.Assert(matched, check.Equals, true,
  808. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  809. out, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  810. c.Assert(err, check.IsNil, check.Commentf(out))
  811. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  812. c.Assert(err, check.IsNil, check.Commentf(out))
  813. }
  814. func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
  815. bridgeName := "external-bridge"
  816. bridgeIP := "192.169.1.1/24"
  817. out, err := createInterface(c, "bridge", bridgeName, bridgeIP)
  818. c.Assert(err, check.IsNil, check.Commentf(out))
  819. defer deleteInterface(c, bridgeName)
  820. err = s.d.StartWithBusybox("--bridge", bridgeName, "--icc=false")
  821. c.Assert(err, check.IsNil)
  822. defer s.d.Restart()
  823. _, err = s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
  824. c.Assert(err, check.IsNil)
  825. _, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
  826. c.Assert(err, check.IsNil)
  827. childIP := s.d.findContainerIP("child")
  828. parentIP := s.d.findContainerIP("parent")
  829. sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  830. destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  831. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  832. c.Fatal("Iptables rules not found")
  833. }
  834. s.d.Cmd("rm", "--link", "parent/http")
  835. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  836. c.Fatal("Iptables rules should be removed when unlink")
  837. }
  838. s.d.Cmd("kill", "child")
  839. s.d.Cmd("kill", "parent")
  840. }
  841. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  842. testRequires(c, DaemonIsLinux)
  843. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  844. c.Fatal(err)
  845. }
  846. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  847. if err != nil {
  848. c.Fatal(out, err)
  849. }
  850. outArr := strings.Split(out, "\n")
  851. if len(outArr) < 2 {
  852. c.Fatalf("got unexpected output: %s", out)
  853. }
  854. nofile := strings.TrimSpace(outArr[0])
  855. nproc := strings.TrimSpace(outArr[1])
  856. if nofile != "42" {
  857. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  858. }
  859. if nproc != "2048" {
  860. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  861. }
  862. // Now restart daemon with a new default
  863. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  864. c.Fatal(err)
  865. }
  866. out, err = s.d.Cmd("start", "-a", "test")
  867. if err != nil {
  868. c.Fatal(err)
  869. }
  870. outArr = strings.Split(out, "\n")
  871. if len(outArr) < 2 {
  872. c.Fatalf("got unexpected output: %s", out)
  873. }
  874. nofile = strings.TrimSpace(outArr[0])
  875. nproc = strings.TrimSpace(outArr[1])
  876. if nofile != "43" {
  877. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  878. }
  879. if nproc != "2048" {
  880. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  881. }
  882. }
  883. // #11315
  884. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  885. if err := s.d.StartWithBusybox(); err != nil {
  886. c.Fatal(err)
  887. }
  888. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  889. c.Fatal(err, out)
  890. }
  891. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  892. c.Fatal(err, out)
  893. }
  894. if err := s.d.Restart(); err != nil {
  895. c.Fatal(err)
  896. }
  897. if out, err := s.d.Cmd("start", "test2"); err != nil {
  898. c.Fatal(err, out)
  899. }
  900. }
  901. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  902. if err := s.d.StartWithBusybox(); err != nil {
  903. c.Fatal(err)
  904. }
  905. out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline")
  906. c.Assert(err, check.IsNil, check.Commentf(out))
  907. id, err := s.d.getIDByName("test")
  908. c.Assert(err, check.IsNil)
  909. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  910. if _, err := os.Stat(logPath); err != nil {
  911. c.Fatal(err)
  912. }
  913. f, err := os.Open(logPath)
  914. if err != nil {
  915. c.Fatal(err)
  916. }
  917. defer f.Close()
  918. var res struct {
  919. Log string `json:"log"`
  920. Stream string `json:"stream"`
  921. Time time.Time `json:"time"`
  922. }
  923. if err := json.NewDecoder(f).Decode(&res); err != nil {
  924. c.Fatal(err)
  925. }
  926. if res.Log != "testline\n" {
  927. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  928. }
  929. if res.Stream != "stdout" {
  930. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  931. }
  932. if !time.Now().After(res.Time) {
  933. c.Fatalf("Log time %v in future", res.Time)
  934. }
  935. }
  936. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  937. if err := s.d.StartWithBusybox(); err != nil {
  938. c.Fatal(err)
  939. }
  940. out, err := s.d.Cmd("run", "--name=test", "--log-driver=none", "busybox", "echo", "testline")
  941. if err != nil {
  942. c.Fatal(out, err)
  943. }
  944. id, err := s.d.getIDByName("test")
  945. c.Assert(err, check.IsNil)
  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", "--name=test", "busybox", "echo", "testline")
  956. if err != nil {
  957. c.Fatal(out, err)
  958. }
  959. id, err := s.d.getIDByName("test")
  960. c.Assert(err, check.IsNil)
  961. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  962. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  963. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  964. }
  965. }
  966. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  967. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  968. c.Fatal(err)
  969. }
  970. out, err := s.d.Cmd("run", "--name=test", "--log-driver=json-file", "busybox", "echo", "testline")
  971. if err != nil {
  972. c.Fatal(out, err)
  973. }
  974. id, err := s.d.getIDByName("test")
  975. c.Assert(err, check.IsNil)
  976. logPath := filepath.Join(s.d.root, "containers", id, id+"-json.log")
  977. if _, err := os.Stat(logPath); err != nil {
  978. c.Fatal(err)
  979. }
  980. f, err := os.Open(logPath)
  981. if err != nil {
  982. c.Fatal(err)
  983. }
  984. defer f.Close()
  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. c.Assert(s.d.StartWithBusybox("--log-driver=none"), checker.IsNil)
  1005. out, err := s.d.Cmd("run", "--name=test", "busybox", "echo", "testline")
  1006. c.Assert(err, checker.IsNil, check.Commentf(out))
  1007. out, err = s.d.Cmd("logs", "test")
  1008. c.Assert(err, check.NotNil, check.Commentf("Logs should fail with 'none' driver"))
  1009. expected := `"logs" command is supported only for "json-file" and "journald" logging drivers (got: none)`
  1010. c.Assert(out, checker.Contains, expected)
  1011. }
  1012. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  1013. if err := s.d.StartWithBusybox(); err != nil {
  1014. c.Fatal(err)
  1015. }
  1016. // Now create 4 containers
  1017. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1018. c.Fatalf("Error creating container: %q", err)
  1019. }
  1020. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1021. c.Fatalf("Error creating container: %q", err)
  1022. }
  1023. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1024. c.Fatalf("Error creating container: %q", err)
  1025. }
  1026. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  1027. c.Fatalf("Error creating container: %q", err)
  1028. }
  1029. s.d.Stop()
  1030. s.d.Start("--log-level=debug")
  1031. s.d.Stop()
  1032. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1033. if strings.Contains(string(content), "....") {
  1034. c.Fatalf("Debug level should not have ....\n%s", string(content))
  1035. }
  1036. s.d.Start("--log-level=error")
  1037. s.d.Stop()
  1038. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1039. if strings.Contains(string(content), "....") {
  1040. c.Fatalf("Error level should not have ....\n%s", string(content))
  1041. }
  1042. s.d.Start("--log-level=info")
  1043. s.d.Stop()
  1044. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1045. if !strings.Contains(string(content), "....") {
  1046. c.Fatalf("Info level should have ....\n%s", string(content))
  1047. }
  1048. }
  1049. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  1050. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  1051. if err != nil {
  1052. c.Fatal(err)
  1053. }
  1054. defer os.RemoveAll(dir)
  1055. sockPath := filepath.Join(dir, "docker.sock")
  1056. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  1057. c.Fatal(err)
  1058. }
  1059. if _, err := os.Stat(sockPath); err != nil {
  1060. c.Fatal("socket does not exist")
  1061. }
  1062. if err := s.d.Stop(); err != nil {
  1063. c.Fatal(err)
  1064. }
  1065. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  1066. c.Fatal("unix socket is not cleaned up")
  1067. }
  1068. }
  1069. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  1070. type Config struct {
  1071. Crv string `json:"crv"`
  1072. D string `json:"d"`
  1073. Kid string `json:"kid"`
  1074. Kty string `json:"kty"`
  1075. X string `json:"x"`
  1076. Y string `json:"y"`
  1077. }
  1078. os.Remove("/etc/docker/key.json")
  1079. if err := s.d.Start(); err != nil {
  1080. c.Fatalf("Failed to start daemon: %v", err)
  1081. }
  1082. if err := s.d.Stop(); err != nil {
  1083. c.Fatalf("Could not stop daemon: %v", err)
  1084. }
  1085. config := &Config{}
  1086. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  1087. if err != nil {
  1088. c.Fatalf("Error reading key.json file: %s", err)
  1089. }
  1090. // byte[] to Data-Struct
  1091. if err := json.Unmarshal(bytes, &config); err != nil {
  1092. c.Fatalf("Error Unmarshal: %s", err)
  1093. }
  1094. //replace config.Kid with the fake value
  1095. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  1096. // NEW Data-Struct to byte[]
  1097. newBytes, err := json.Marshal(&config)
  1098. if err != nil {
  1099. c.Fatalf("Error Marshal: %s", err)
  1100. }
  1101. // write back
  1102. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  1103. c.Fatalf("Error ioutil.WriteFile: %s", err)
  1104. }
  1105. defer os.Remove("/etc/docker/key.json")
  1106. if err := s.d.Start(); err == nil {
  1107. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  1108. }
  1109. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1110. if !strings.Contains(string(content), "Public Key ID does not match") {
  1111. c.Fatal("Missing KeyID message from daemon logs")
  1112. }
  1113. }
  1114. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  1115. if err := s.d.StartWithBusybox(); err != nil {
  1116. c.Fatalf("Could not start daemon with busybox: %v", err)
  1117. }
  1118. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  1119. if err != nil {
  1120. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  1121. }
  1122. containerID := strings.TrimSpace(out)
  1123. if out, err := s.d.Cmd("kill", containerID); err != nil {
  1124. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  1125. }
  1126. if err := s.d.Restart(); err != nil {
  1127. c.Fatalf("Could not restart daemon: %v", err)
  1128. }
  1129. errchan := make(chan error)
  1130. go func() {
  1131. if out, err := s.d.Cmd("wait", containerID); err != nil {
  1132. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1133. }
  1134. close(errchan)
  1135. }()
  1136. select {
  1137. case <-time.After(5 * time.Second):
  1138. c.Fatal("Waiting on a stopped (killed) container timed out")
  1139. case err := <-errchan:
  1140. if err != nil {
  1141. c.Fatal(err)
  1142. }
  1143. }
  1144. }
  1145. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  1146. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  1147. const (
  1148. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1149. )
  1150. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1151. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1152. c.Fatalf("Could not start daemon with busybox: %v", err)
  1153. }
  1154. args := []string{
  1155. "--host", testDaemonHTTPSAddr,
  1156. "--tlsverify", "--tlscacert", "fixtures/https/ca.pem",
  1157. "--tlscert", "fixtures/https/client-cert.pem",
  1158. "--tlskey", "fixtures/https/client-key.pem",
  1159. "info",
  1160. }
  1161. out, err := s.d.Cmd(args...)
  1162. if err != nil {
  1163. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1164. }
  1165. }
  1166. // TestHttpsRun connects via two-way authenticated HTTPS to the create, attach, start, and wait endpoints.
  1167. // https://github.com/docker/docker/issues/19280
  1168. func (s *DockerDaemonSuite) TestHttpsRun(c *check.C) {
  1169. const (
  1170. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1171. )
  1172. if err := s.d.StartWithBusybox("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1173. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1174. c.Fatalf("Could not start daemon with busybox: %v", err)
  1175. }
  1176. args := []string{
  1177. "--host", testDaemonHTTPSAddr,
  1178. "--tlsverify", "--tlscacert", "fixtures/https/ca.pem",
  1179. "--tlscert", "fixtures/https/client-cert.pem",
  1180. "--tlskey", "fixtures/https/client-key.pem",
  1181. "run", "busybox", "echo", "TLS response",
  1182. }
  1183. out, err := s.d.Cmd(args...)
  1184. if err != nil {
  1185. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  1186. }
  1187. if !strings.Contains(out, "TLS response") {
  1188. c.Fatalf("expected output to include `TLS response`, got %v", out)
  1189. }
  1190. }
  1191. // TestTlsVerify verifies that --tlsverify=false turns on tls
  1192. func (s *DockerDaemonSuite) TestTlsVerify(c *check.C) {
  1193. out, err := exec.Command(dockerdBinary, "--tlsverify=false").CombinedOutput()
  1194. if err == nil || !strings.Contains(string(out), "Could not load X509 key pair") {
  1195. c.Fatalf("Daemon should not have started due to missing certs: %v\n%s", err, string(out))
  1196. }
  1197. }
  1198. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  1199. // by using a rogue client certificate and checks that it fails with the expected error.
  1200. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  1201. const (
  1202. errBadCertificate = "bad certificate"
  1203. testDaemonHTTPSAddr = "tcp://localhost:4271"
  1204. )
  1205. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  1206. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHTTPSAddr); err != nil {
  1207. c.Fatalf("Could not start daemon with busybox: %v", err)
  1208. }
  1209. args := []string{
  1210. "--host", testDaemonHTTPSAddr,
  1211. "--tlsverify", "--tlscacert", "fixtures/https/ca.pem",
  1212. "--tlscert", "fixtures/https/client-rogue-cert.pem",
  1213. "--tlskey", "fixtures/https/client-rogue-key.pem",
  1214. "info",
  1215. }
  1216. out, err := s.d.Cmd(args...)
  1217. if err == nil || !strings.Contains(out, errBadCertificate) {
  1218. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  1219. }
  1220. }
  1221. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  1222. // which provides a rogue server certificate and checks that it fails with the expected error
  1223. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  1224. const (
  1225. errCaUnknown = "x509: certificate signed by unknown authority"
  1226. testDaemonRogueHTTPSAddr = "tcp://localhost:4272"
  1227. )
  1228. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  1229. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHTTPSAddr); err != nil {
  1230. c.Fatalf("Could not start daemon with busybox: %v", err)
  1231. }
  1232. args := []string{
  1233. "--host", testDaemonRogueHTTPSAddr,
  1234. "--tlsverify", "--tlscacert", "fixtures/https/ca.pem",
  1235. "--tlscert", "fixtures/https/client-rogue-cert.pem",
  1236. "--tlskey", "fixtures/https/client-rogue-key.pem",
  1237. "info",
  1238. }
  1239. out, err := s.d.Cmd(args...)
  1240. if err == nil || !strings.Contains(out, errCaUnknown) {
  1241. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  1242. }
  1243. }
  1244. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  1245. var dargs []string
  1246. if d != nil {
  1247. dargs = []string{"--host", d.sock()}
  1248. }
  1249. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  1250. dockerCmd(c, args...)
  1251. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  1252. pingCmd := "ping -c 1 %s -W 1"
  1253. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  1254. _, _, err := dockerCmdWithError(args...)
  1255. if expectFailure {
  1256. c.Assert(err, check.NotNil)
  1257. } else {
  1258. c.Assert(err, check.IsNil)
  1259. }
  1260. args = append(dargs, "rm", "-f", "container1")
  1261. dockerCmd(c, args...)
  1262. }
  1263. func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) {
  1264. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1265. socket := filepath.Join(s.d.folder, "docker.sock")
  1266. out, err := s.d.Cmd("run", "--restart=always", "-v", socket+":/sock", "busybox")
  1267. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1268. c.Assert(s.d.Restart(), check.IsNil)
  1269. }
  1270. // os.Kill should kill daemon ungracefully, leaving behind container mounts.
  1271. // A subsequent daemon restart shoud clean up said mounts.
  1272. func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonAndContainerKill(c *check.C) {
  1273. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1274. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  1275. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1276. id := strings.TrimSpace(out)
  1277. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  1278. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1279. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1280. // container mounts should exist even after daemon has crashed.
  1281. comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1282. c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment)
  1283. // kill the container
  1284. runCmd := exec.Command(ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", id)
  1285. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  1286. c.Fatalf("Failed to run ctr, ExitCode: %d, err: %v output: %s id: %s\n", ec, err, out, id)
  1287. }
  1288. // restart daemon.
  1289. if err := s.d.Restart(); err != nil {
  1290. c.Fatal(err)
  1291. }
  1292. // Now, container mounts should be gone.
  1293. mountOut, err = ioutil.ReadFile("/proc/self/mountinfo")
  1294. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1295. comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1296. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1297. }
  1298. // os.Interrupt should perform a graceful daemon shutdown and hence cleanup mounts.
  1299. func (s *DockerDaemonSuite) TestCleanupMountsAfterGracefulShutdown(c *check.C) {
  1300. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1301. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  1302. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1303. id := strings.TrimSpace(out)
  1304. // Send SIGINT and daemon should clean up
  1305. c.Assert(s.d.cmd.Process.Signal(os.Interrupt), check.IsNil)
  1306. // Wait for the daemon to stop.
  1307. c.Assert(<-s.d.wait, checker.IsNil)
  1308. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1309. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1310. comment := check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1311. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1312. }
  1313. func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
  1314. testRequires(c, DaemonIsLinux, NotUserNamespace)
  1315. c.Assert(s.d.StartWithBusybox("-b", "none"), check.IsNil)
  1316. out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
  1317. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1318. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1319. check.Commentf("There shouldn't be eth0 in container in default(bridge) mode when bridge network is disabled: %s", out))
  1320. out, err = s.d.Cmd("run", "--rm", "--net=bridge", "busybox", "ip", "l")
  1321. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1322. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1323. check.Commentf("There shouldn't be eth0 in container in bridge mode when bridge network is disabled: %s", out))
  1324. // the extra grep and awk clean up the output of `ip` to only list the number and name of
  1325. // interfaces, allowing for different versions of ip (e.g. inside and outside the container) to
  1326. // be used while still verifying that the interface list is the exact same
  1327. cmd := exec.Command("sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1328. stdout := bytes.NewBuffer(nil)
  1329. cmd.Stdout = stdout
  1330. if err := cmd.Run(); err != nil {
  1331. c.Fatal("Failed to get host network interface")
  1332. }
  1333. out, err = s.d.Cmd("run", "--rm", "--net=host", "busybox", "sh", "-c", "ip l | grep -E '^[0-9]+:' | awk -F: ' { print $1\":\"$2 } '")
  1334. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1335. c.Assert(out, check.Equals, fmt.Sprintf("%s", stdout),
  1336. check.Commentf("The network interfaces in container should be the same with host when --net=host when bridge network is disabled: %s", out))
  1337. }
  1338. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
  1339. if err := s.d.StartWithBusybox(); err != nil {
  1340. t.Fatal(err)
  1341. }
  1342. if out, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top"); err != nil {
  1343. t.Fatal(out, err)
  1344. }
  1345. if err := s.d.Restart(); err != nil {
  1346. t.Fatal(err)
  1347. }
  1348. // Container 'test' should be removed without error
  1349. if out, err := s.d.Cmd("rm", "test"); err != nil {
  1350. t.Fatal(out, err)
  1351. }
  1352. }
  1353. func (s *DockerDaemonSuite) TestDaemonRestartCleanupNetns(c *check.C) {
  1354. if err := s.d.StartWithBusybox(); err != nil {
  1355. c.Fatal(err)
  1356. }
  1357. out, err := s.d.Cmd("run", "--name", "netns", "-d", "busybox", "top")
  1358. if err != nil {
  1359. c.Fatal(out, err)
  1360. }
  1361. // Get sandbox key via inspect
  1362. out, err = s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.SandboxKey}}'", "netns")
  1363. if err != nil {
  1364. c.Fatalf("Error inspecting container: %s, %v", out, err)
  1365. }
  1366. fileName := strings.Trim(out, " \r\n'")
  1367. if out, err := s.d.Cmd("stop", "netns"); err != nil {
  1368. c.Fatal(out, err)
  1369. }
  1370. // Test if the file still exists
  1371. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1372. out = strings.TrimSpace(out)
  1373. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1374. c.Assert(out, check.Equals, fileName, check.Commentf("Output: %s", out))
  1375. // Remove the container and restart the daemon
  1376. if out, err := s.d.Cmd("rm", "netns"); err != nil {
  1377. c.Fatal(out, err)
  1378. }
  1379. if err := s.d.Restart(); err != nil {
  1380. c.Fatal(err)
  1381. }
  1382. // Test again and see now the netns file does not exist
  1383. out, _, err = runCommandWithOutput(exec.Command("stat", "-c", "%n", fileName))
  1384. out = strings.TrimSpace(out)
  1385. c.Assert(err, check.Not(check.IsNil), check.Commentf("Output: %s", out))
  1386. }
  1387. // tests regression detailed in #13964 where DOCKER_TLS_VERIFY env is ignored
  1388. func (s *DockerDaemonSuite) TestDaemonNoTlsCliTlsVerifyWithEnv(c *check.C) {
  1389. host := "tcp://localhost:4271"
  1390. c.Assert(s.d.Start("-H", host), check.IsNil)
  1391. cmd := exec.Command(dockerBinary, "-H", host, "info")
  1392. cmd.Env = []string{"DOCKER_TLS_VERIFY=1", "DOCKER_CERT_PATH=fixtures/https"}
  1393. out, _, err := runCommandWithOutput(cmd)
  1394. c.Assert(err, check.Not(check.IsNil), check.Commentf("%s", out))
  1395. c.Assert(strings.Contains(out, "error occurred trying to connect"), check.Equals, true)
  1396. }
  1397. func setupV6(c *check.C) {
  1398. // Hack to get the right IPv6 address on docker0, which has already been created
  1399. result := icmd.RunCommand("ip", "addr", "add", "fe80::1/64", "dev", "docker0")
  1400. result.Assert(c, icmd.Expected{})
  1401. }
  1402. func teardownV6(c *check.C) {
  1403. result := icmd.RunCommand("ip", "addr", "del", "fe80::1/64", "dev", "docker0")
  1404. result.Assert(c, icmd.Expected{})
  1405. }
  1406. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerWithRestartPolicyAlways(c *check.C) {
  1407. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1408. out, err := s.d.Cmd("run", "-d", "--restart", "always", "busybox", "top")
  1409. c.Assert(err, check.IsNil)
  1410. id := strings.TrimSpace(out)
  1411. _, err = s.d.Cmd("stop", id)
  1412. c.Assert(err, check.IsNil)
  1413. _, err = s.d.Cmd("wait", id)
  1414. c.Assert(err, check.IsNil)
  1415. out, err = s.d.Cmd("ps", "-q")
  1416. c.Assert(err, check.IsNil)
  1417. c.Assert(out, check.Equals, "")
  1418. c.Assert(s.d.Restart(), check.IsNil)
  1419. out, err = s.d.Cmd("ps", "-q")
  1420. c.Assert(err, check.IsNil)
  1421. c.Assert(strings.TrimSpace(out), check.Equals, id[:12])
  1422. }
  1423. func (s *DockerDaemonSuite) TestDaemonWideLogConfig(c *check.C) {
  1424. if err := s.d.StartWithBusybox("--log-opt=max-size=1k"); err != nil {
  1425. c.Fatal(err)
  1426. }
  1427. name := "logtest"
  1428. out, err := s.d.Cmd("run", "-d", "--log-opt=max-file=5", "--name", name, "busybox", "top")
  1429. c.Assert(err, check.IsNil, check.Commentf("Output: %s, err: %v", out, err))
  1430. out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Config }}", name)
  1431. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1432. c.Assert(out, checker.Contains, "max-size:1k")
  1433. c.Assert(out, checker.Contains, "max-file:5")
  1434. out, err = s.d.Cmd("inspect", "-f", "{{ .HostConfig.LogConfig.Type }}", name)
  1435. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1436. c.Assert(strings.TrimSpace(out), checker.Equals, "json-file")
  1437. }
  1438. func (s *DockerDaemonSuite) TestDaemonRestartWithPausedContainer(c *check.C) {
  1439. if err := s.d.StartWithBusybox(); err != nil {
  1440. c.Fatal(err)
  1441. }
  1442. if out, err := s.d.Cmd("run", "-i", "-d", "--name", "test", "busybox", "top"); err != nil {
  1443. c.Fatal(err, out)
  1444. }
  1445. if out, err := s.d.Cmd("pause", "test"); err != nil {
  1446. c.Fatal(err, out)
  1447. }
  1448. if err := s.d.Restart(); err != nil {
  1449. c.Fatal(err)
  1450. }
  1451. errchan := make(chan error)
  1452. go func() {
  1453. out, err := s.d.Cmd("start", "test")
  1454. if err != nil {
  1455. errchan <- fmt.Errorf("%v:\n%s", err, out)
  1456. }
  1457. name := strings.TrimSpace(out)
  1458. if name != "test" {
  1459. errchan <- fmt.Errorf("Paused container start error on docker daemon restart, expected 'test' but got '%s'", name)
  1460. }
  1461. close(errchan)
  1462. }()
  1463. select {
  1464. case <-time.After(5 * time.Second):
  1465. c.Fatal("Waiting on start a container timed out")
  1466. case err := <-errchan:
  1467. if err != nil {
  1468. c.Fatal(err)
  1469. }
  1470. }
  1471. }
  1472. func (s *DockerDaemonSuite) TestDaemonRestartRmVolumeInUse(c *check.C) {
  1473. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  1474. out, err := s.d.Cmd("create", "-v", "test:/foo", "busybox")
  1475. c.Assert(err, check.IsNil, check.Commentf(out))
  1476. c.Assert(s.d.Restart(), check.IsNil)
  1477. out, err = s.d.Cmd("volume", "rm", "test")
  1478. c.Assert(err, check.NotNil, check.Commentf("should not be able to remove in use volume after daemon restart"))
  1479. c.Assert(out, checker.Contains, "in use")
  1480. }
  1481. func (s *DockerDaemonSuite) TestDaemonRestartLocalVolumes(c *check.C) {
  1482. c.Assert(s.d.Start(), check.IsNil)
  1483. _, err := s.d.Cmd("volume", "create", "--name", "test")
  1484. c.Assert(err, check.IsNil)
  1485. c.Assert(s.d.Restart(), check.IsNil)
  1486. _, err = s.d.Cmd("volume", "inspect", "test")
  1487. c.Assert(err, check.IsNil)
  1488. }
  1489. func (s *DockerDaemonSuite) TestDaemonCorruptedLogDriverAddress(c *check.C) {
  1490. c.Assert(s.d.Start("--log-driver=syslog", "--log-opt", "syslog-address=corrupted:42"), check.NotNil)
  1491. expected := "Failed to set log opts: syslog-address should be in form proto://address"
  1492. runCmd := exec.Command("grep", expected, s.d.LogFileName())
  1493. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1494. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1495. }
  1496. }
  1497. func (s *DockerDaemonSuite) TestDaemonCorruptedFluentdAddress(c *check.C) {
  1498. c.Assert(s.d.Start("--log-driver=fluentd", "--log-opt", "fluentd-address=corrupted:c"), check.NotNil)
  1499. expected := "Failed to set log opts: invalid fluentd-address corrupted:c: "
  1500. runCmd := exec.Command("grep", expected, s.d.LogFileName())
  1501. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  1502. c.Fatalf("Expected %q message; but doesn't exist in log: %q, err: %v", expected, out, err)
  1503. }
  1504. }
  1505. func (s *DockerDaemonSuite) TestDaemonStartWithoutHost(c *check.C) {
  1506. s.d.useDefaultHost = true
  1507. defer func() {
  1508. s.d.useDefaultHost = false
  1509. }()
  1510. c.Assert(s.d.Start(), check.IsNil)
  1511. }
  1512. func (s *DockerDaemonSuite) TestDaemonStartWithDefalutTlsHost(c *check.C) {
  1513. s.d.useDefaultTLSHost = true
  1514. defer func() {
  1515. s.d.useDefaultTLSHost = false
  1516. }()
  1517. if err := s.d.Start(
  1518. "--tlsverify",
  1519. "--tlscacert", "fixtures/https/ca.pem",
  1520. "--tlscert", "fixtures/https/server-cert.pem",
  1521. "--tlskey", "fixtures/https/server-key.pem"); err != nil {
  1522. c.Fatalf("Could not start daemon: %v", err)
  1523. }
  1524. // The client with --tlsverify should also use default host localhost:2376
  1525. tmpHost := os.Getenv("DOCKER_HOST")
  1526. defer func() {
  1527. os.Setenv("DOCKER_HOST", tmpHost)
  1528. }()
  1529. os.Setenv("DOCKER_HOST", "")
  1530. out, _ := dockerCmd(
  1531. c,
  1532. "--tlsverify",
  1533. "--tlscacert", "fixtures/https/ca.pem",
  1534. "--tlscert", "fixtures/https/client-cert.pem",
  1535. "--tlskey", "fixtures/https/client-key.pem",
  1536. "version",
  1537. )
  1538. if !strings.Contains(out, "Server") {
  1539. c.Fatalf("docker version should return information of server side")
  1540. }
  1541. }
  1542. func (s *DockerDaemonSuite) TestBridgeIPIsExcludedFromAllocatorPool(c *check.C) {
  1543. defaultNetworkBridge := "docker0"
  1544. deleteInterface(c, defaultNetworkBridge)
  1545. bridgeIP := "192.169.1.1"
  1546. bridgeRange := bridgeIP + "/30"
  1547. err := s.d.StartWithBusybox("--bip", bridgeRange)
  1548. c.Assert(err, check.IsNil)
  1549. defer s.d.Restart()
  1550. var cont int
  1551. for {
  1552. contName := fmt.Sprintf("container%d", cont)
  1553. _, err = s.d.Cmd("run", "--name", contName, "-d", "busybox", "/bin/sleep", "2")
  1554. if err != nil {
  1555. // pool exhausted
  1556. break
  1557. }
  1558. ip, err := s.d.Cmd("inspect", "--format", "'{{.NetworkSettings.IPAddress}}'", contName)
  1559. c.Assert(err, check.IsNil)
  1560. c.Assert(ip, check.Not(check.Equals), bridgeIP)
  1561. cont++
  1562. }
  1563. }
  1564. // Test daemon for no space left on device error
  1565. func (s *DockerDaemonSuite) TestDaemonNoSpaceLeftOnDeviceError(c *check.C) {
  1566. testRequires(c, SameHostDaemon, DaemonIsLinux, Network)
  1567. testDir, err := ioutil.TempDir("", "no-space-left-on-device-test")
  1568. c.Assert(err, checker.IsNil)
  1569. defer os.RemoveAll(testDir)
  1570. c.Assert(mount.MakeRShared(testDir), checker.IsNil)
  1571. defer mount.Unmount(testDir)
  1572. // create a 2MiB image and mount it as graph root
  1573. // Why in a container? Because `mount` sometimes behaves weirdly and often fails outright on this test in debian:jessie (which is what the test suite runs under if run from the Makefile)
  1574. dockerCmd(c, "run", "--rm", "-v", testDir+":/test", "busybox", "sh", "-c", "dd of=/test/testfs.img bs=1M seek=2 count=0")
  1575. out, _, err := runCommandWithOutput(exec.Command("mkfs.ext4", "-F", filepath.Join(testDir, "testfs.img"))) // `mkfs.ext4` is not in busybox
  1576. c.Assert(err, checker.IsNil, check.Commentf(out))
  1577. cmd := exec.Command("losetup", "-f", "--show", filepath.Join(testDir, "testfs.img"))
  1578. loout, err := cmd.CombinedOutput()
  1579. c.Assert(err, checker.IsNil)
  1580. loopname := strings.TrimSpace(string(loout))
  1581. defer exec.Command("losetup", "-d", loopname).Run()
  1582. dockerCmd(c, "run", "--privileged", "--rm", "-v", testDir+":/test:shared", "busybox", "sh", "-c", fmt.Sprintf("mkdir -p /test/test-mount && mount -t ext4 -no loop,rw %v /test/test-mount", loopname))
  1583. defer mount.Unmount(filepath.Join(testDir, "test-mount"))
  1584. err = s.d.Start("--graph", filepath.Join(testDir, "test-mount"))
  1585. defer s.d.Stop()
  1586. c.Assert(err, check.IsNil)
  1587. // pull a repository large enough to fill the mount point
  1588. pullOut, err := s.d.Cmd("pull", "registry:2")
  1589. c.Assert(err, checker.NotNil, check.Commentf(pullOut))
  1590. c.Assert(pullOut, checker.Contains, "no space left on device")
  1591. }
  1592. // Test daemon restart with container links + auto restart
  1593. func (s *DockerDaemonSuite) TestDaemonRestartContainerLinksRestart(c *check.C) {
  1594. d := NewDaemon(c)
  1595. defer d.Stop()
  1596. err := d.StartWithBusybox()
  1597. c.Assert(err, checker.IsNil)
  1598. parent1Args := []string{}
  1599. parent2Args := []string{}
  1600. wg := sync.WaitGroup{}
  1601. maxChildren := 10
  1602. chErr := make(chan error, maxChildren)
  1603. for i := 0; i < maxChildren; i++ {
  1604. wg.Add(1)
  1605. name := fmt.Sprintf("test%d", i)
  1606. if i < maxChildren/2 {
  1607. parent1Args = append(parent1Args, []string{"--link", name}...)
  1608. } else {
  1609. parent2Args = append(parent2Args, []string{"--link", name}...)
  1610. }
  1611. go func() {
  1612. _, err = d.Cmd("run", "-d", "--name", name, "--restart=always", "busybox", "top")
  1613. chErr <- err
  1614. wg.Done()
  1615. }()
  1616. }
  1617. wg.Wait()
  1618. close(chErr)
  1619. for err := range chErr {
  1620. c.Assert(err, check.IsNil)
  1621. }
  1622. parent1Args = append([]string{"run", "-d"}, parent1Args...)
  1623. parent1Args = append(parent1Args, []string{"--name=parent1", "--restart=always", "busybox", "top"}...)
  1624. parent2Args = append([]string{"run", "-d"}, parent2Args...)
  1625. parent2Args = append(parent2Args, []string{"--name=parent2", "--restart=always", "busybox", "top"}...)
  1626. _, err = d.Cmd(parent1Args...)
  1627. c.Assert(err, check.IsNil)
  1628. _, err = d.Cmd(parent2Args...)
  1629. c.Assert(err, check.IsNil)
  1630. err = d.Stop()
  1631. c.Assert(err, check.IsNil)
  1632. // clear the log file -- we don't need any of it but may for the next part
  1633. // can ignore the error here, this is just a cleanup
  1634. os.Truncate(d.LogFileName(), 0)
  1635. err = d.Start()
  1636. c.Assert(err, check.IsNil)
  1637. for _, num := range []string{"1", "2"} {
  1638. out, err := d.Cmd("inspect", "-f", "{{ .State.Running }}", "parent"+num)
  1639. c.Assert(err, check.IsNil)
  1640. if strings.TrimSpace(out) != "true" {
  1641. log, _ := ioutil.ReadFile(d.LogFileName())
  1642. c.Fatalf("parent container is not running\n%s", string(log))
  1643. }
  1644. }
  1645. }
  1646. func (s *DockerDaemonSuite) TestDaemonCgroupParent(c *check.C) {
  1647. testRequires(c, DaemonIsLinux)
  1648. cgroupParent := "test"
  1649. name := "cgroup-test"
  1650. err := s.d.StartWithBusybox("--cgroup-parent", cgroupParent)
  1651. c.Assert(err, check.IsNil)
  1652. defer s.d.Restart()
  1653. out, err := s.d.Cmd("run", "--name", name, "busybox", "cat", "/proc/self/cgroup")
  1654. c.Assert(err, checker.IsNil)
  1655. cgroupPaths := parseCgroupPaths(string(out))
  1656. c.Assert(len(cgroupPaths), checker.Not(checker.Equals), 0, check.Commentf("unexpected output - %q", string(out)))
  1657. out, err = s.d.Cmd("inspect", "-f", "{{.Id}}", name)
  1658. c.Assert(err, checker.IsNil)
  1659. id := strings.TrimSpace(string(out))
  1660. expectedCgroup := path.Join(cgroupParent, id)
  1661. found := false
  1662. for _, path := range cgroupPaths {
  1663. if strings.HasSuffix(path, expectedCgroup) {
  1664. found = true
  1665. break
  1666. }
  1667. }
  1668. c.Assert(found, checker.True, check.Commentf("Cgroup path for container (%s) doesn't found in cgroups file: %s", expectedCgroup, cgroupPaths))
  1669. }
  1670. func (s *DockerDaemonSuite) TestDaemonRestartWithLinks(c *check.C) {
  1671. testRequires(c, DaemonIsLinux) // Windows does not support links
  1672. err := s.d.StartWithBusybox()
  1673. c.Assert(err, check.IsNil)
  1674. out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1675. c.Assert(err, check.IsNil, check.Commentf(out))
  1676. out, err = s.d.Cmd("run", "--name=test2", "--link", "test:abc", "busybox", "sh", "-c", "ping -c 1 -w 1 abc")
  1677. c.Assert(err, check.IsNil, check.Commentf(out))
  1678. c.Assert(s.d.Restart(), check.IsNil)
  1679. // should fail since test is not running yet
  1680. out, err = s.d.Cmd("start", "test2")
  1681. c.Assert(err, check.NotNil, check.Commentf(out))
  1682. out, err = s.d.Cmd("start", "test")
  1683. c.Assert(err, check.IsNil, check.Commentf(out))
  1684. out, err = s.d.Cmd("start", "-a", "test2")
  1685. c.Assert(err, check.IsNil, check.Commentf(out))
  1686. c.Assert(strings.Contains(out, "1 packets transmitted, 1 packets received"), check.Equals, true, check.Commentf(out))
  1687. }
  1688. func (s *DockerDaemonSuite) TestDaemonRestartWithNames(c *check.C) {
  1689. testRequires(c, DaemonIsLinux) // Windows does not support links
  1690. err := s.d.StartWithBusybox()
  1691. c.Assert(err, check.IsNil)
  1692. out, err := s.d.Cmd("create", "--name=test", "busybox")
  1693. c.Assert(err, check.IsNil, check.Commentf(out))
  1694. out, err = s.d.Cmd("run", "-d", "--name=test2", "busybox", "top")
  1695. c.Assert(err, check.IsNil, check.Commentf(out))
  1696. test2ID := strings.TrimSpace(out)
  1697. out, err = s.d.Cmd("run", "-d", "--name=test3", "--link", "test2:abc", "busybox", "top")
  1698. test3ID := strings.TrimSpace(out)
  1699. c.Assert(s.d.Restart(), check.IsNil)
  1700. out, err = s.d.Cmd("create", "--name=test", "busybox")
  1701. c.Assert(err, check.NotNil, check.Commentf("expected error trying to create container with duplicate name"))
  1702. // this one is no longer needed, removing simplifies the remainder of the test
  1703. out, err = s.d.Cmd("rm", "-f", "test")
  1704. c.Assert(err, check.IsNil, check.Commentf(out))
  1705. out, err = s.d.Cmd("ps", "-a", "--no-trunc")
  1706. c.Assert(err, check.IsNil, check.Commentf(out))
  1707. lines := strings.Split(strings.TrimSpace(out), "\n")[1:]
  1708. test2validated := false
  1709. test3validated := false
  1710. for _, line := range lines {
  1711. fields := strings.Fields(line)
  1712. names := fields[len(fields)-1]
  1713. switch fields[0] {
  1714. case test2ID:
  1715. c.Assert(names, check.Equals, "test2,test3/abc")
  1716. test2validated = true
  1717. case test3ID:
  1718. c.Assert(names, check.Equals, "test3")
  1719. test3validated = true
  1720. }
  1721. }
  1722. c.Assert(test2validated, check.Equals, true)
  1723. c.Assert(test3validated, check.Equals, true)
  1724. }
  1725. // TestDaemonRestartWithKilledRunningContainer requires live restore of running containers
  1726. func (s *DockerDaemonSuite) TestDaemonRestartWithKilledRunningContainer(t *check.C) {
  1727. // TODO(mlaventure): Not sure what would the exit code be on windows
  1728. testRequires(t, DaemonIsLinux)
  1729. if err := s.d.StartWithBusybox(); err != nil {
  1730. t.Fatal(err)
  1731. }
  1732. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  1733. defer s.d.Stop()
  1734. if err != nil {
  1735. t.Fatal(cid, err)
  1736. }
  1737. cid = strings.TrimSpace(cid)
  1738. pid, err := s.d.Cmd("inspect", "-f", "{{.State.Pid}}", cid)
  1739. t.Assert(err, check.IsNil)
  1740. pid = strings.TrimSpace(pid)
  1741. // Kill the daemon
  1742. if err := s.d.Kill(); err != nil {
  1743. t.Fatal(err)
  1744. }
  1745. // kill the container
  1746. runCmd := exec.Command(ctrBinary, "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock", "containers", "kill", cid)
  1747. if out, ec, err := runCommandWithOutput(runCmd); err != nil {
  1748. t.Fatalf("Failed to run ctr, ExitCode: %d, err: '%v' output: '%s' cid: '%s'\n", ec, err, out, cid)
  1749. }
  1750. // Give time to containerd to process the command if we don't
  1751. // the exit event might be received after we do the inspect
  1752. pidCmd := exec.Command("kill", "-0", pid)
  1753. _, ec, _ := runCommandWithOutput(pidCmd)
  1754. for ec == 0 {
  1755. time.Sleep(1 * time.Second)
  1756. _, ec, _ = runCommandWithOutput(pidCmd)
  1757. }
  1758. // restart the daemon
  1759. if err := s.d.Start(); err != nil {
  1760. t.Fatal(err)
  1761. }
  1762. // Check that we've got the correct exit code
  1763. out, err := s.d.Cmd("inspect", "-f", "{{.State.ExitCode}}", cid)
  1764. t.Assert(err, check.IsNil)
  1765. out = strings.TrimSpace(out)
  1766. if out != "143" {
  1767. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "143", out, cid)
  1768. }
  1769. }
  1770. // os.Kill should kill daemon ungracefully, leaving behind live containers.
  1771. // The live containers should be known to the restarted daemon. Stopping
  1772. // them now, should remove the mounts.
  1773. func (s *DockerDaemonSuite) TestCleanupMountsAfterDaemonCrash(c *check.C) {
  1774. testRequires(c, DaemonIsLinux)
  1775. c.Assert(s.d.StartWithBusybox("--live-restore"), check.IsNil)
  1776. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  1777. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1778. id := strings.TrimSpace(out)
  1779. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  1780. mountOut, err := ioutil.ReadFile("/proc/self/mountinfo")
  1781. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1782. // container mounts should exist even after daemon has crashed.
  1783. comment := check.Commentf("%s should stay mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1784. c.Assert(strings.Contains(string(mountOut), id), check.Equals, true, comment)
  1785. // restart daemon.
  1786. if err := s.d.Restart("--live-restore"); err != nil {
  1787. c.Fatal(err)
  1788. }
  1789. // container should be running.
  1790. out, err = s.d.Cmd("inspect", "--format={{.State.Running}}", id)
  1791. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1792. out = strings.TrimSpace(out)
  1793. if out != "true" {
  1794. c.Fatalf("Container %s expected to stay alive after daemon restart", id)
  1795. }
  1796. // 'docker stop' should work.
  1797. out, err = s.d.Cmd("stop", id)
  1798. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1799. // Now, container mounts should be gone.
  1800. mountOut, err = ioutil.ReadFile("/proc/self/mountinfo")
  1801. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1802. comment = check.Commentf("%s is still mounted from older daemon start:\nDaemon root repository %s\n%s", id, s.d.folder, mountOut)
  1803. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, comment)
  1804. }
  1805. // TestDaemonRestartWithUnpausedRunningContainer requires live restore of running containers.
  1806. func (s *DockerDaemonSuite) TestDaemonRestartWithUnpausedRunningContainer(t *check.C) {
  1807. // TODO(mlaventure): Not sure what would the exit code be on windows
  1808. testRequires(t, DaemonIsLinux)
  1809. if err := s.d.StartWithBusybox("--live-restore"); err != nil {
  1810. t.Fatal(err)
  1811. }
  1812. cid, err := s.d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  1813. defer s.d.Stop()
  1814. if err != nil {
  1815. t.Fatal(cid, err)
  1816. }
  1817. cid = strings.TrimSpace(cid)
  1818. pid, err := s.d.Cmd("inspect", "-f", "{{.State.Pid}}", cid)
  1819. t.Assert(err, check.IsNil)
  1820. // pause the container
  1821. if _, err := s.d.Cmd("pause", cid); err != nil {
  1822. t.Fatal(cid, err)
  1823. }
  1824. // Kill the daemon
  1825. if err := s.d.Kill(); err != nil {
  1826. t.Fatal(err)
  1827. }
  1828. // resume the container
  1829. result := icmd.RunCommand(
  1830. ctrBinary,
  1831. "--address", "unix:///var/run/docker/libcontainerd/docker-containerd.sock",
  1832. "containers", "resume", cid)
  1833. t.Assert(result, icmd.Matches, icmd.Success)
  1834. // Give time to containerd to process the command if we don't
  1835. // the resume event might be received after we do the inspect
  1836. waitAndAssert(t, defaultReconciliationTimeout, func(*check.C) (interface{}, check.CommentInterface) {
  1837. result := icmd.RunCommand("kill", "-0", strings.TrimSpace(pid))
  1838. return result.ExitCode, nil
  1839. }, checker.Equals, 0)
  1840. // restart the daemon
  1841. if err := s.d.Start("--live-restore"); err != nil {
  1842. t.Fatal(err)
  1843. }
  1844. // Check that we've got the correct status
  1845. out, err := s.d.Cmd("inspect", "-f", "{{.State.Status}}", cid)
  1846. t.Assert(err, check.IsNil)
  1847. out = strings.TrimSpace(out)
  1848. if out != "running" {
  1849. t.Fatalf("Expected exit code '%s' got '%s' for container '%s'\n", "running", out, cid)
  1850. }
  1851. if _, err := s.d.Cmd("kill", cid); err != nil {
  1852. t.Fatal(err)
  1853. }
  1854. }
  1855. // TestRunLinksChanged checks that creating a new container with the same name does not update links
  1856. // this ensures that the old, pre gh#16032 functionality continues on
  1857. func (s *DockerDaemonSuite) TestRunLinksChanged(c *check.C) {
  1858. testRequires(c, DaemonIsLinux) // Windows does not support links
  1859. err := s.d.StartWithBusybox()
  1860. c.Assert(err, check.IsNil)
  1861. out, err := s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1862. c.Assert(err, check.IsNil, check.Commentf(out))
  1863. out, err = s.d.Cmd("run", "--name=test2", "--link=test:abc", "busybox", "sh", "-c", "ping -c 1 abc")
  1864. c.Assert(err, check.IsNil, check.Commentf(out))
  1865. c.Assert(out, checker.Contains, "1 packets transmitted, 1 packets received")
  1866. out, err = s.d.Cmd("rm", "-f", "test")
  1867. c.Assert(err, check.IsNil, check.Commentf(out))
  1868. out, err = s.d.Cmd("run", "-d", "--name=test", "busybox", "top")
  1869. c.Assert(err, check.IsNil, check.Commentf(out))
  1870. out, err = s.d.Cmd("start", "-a", "test2")
  1871. c.Assert(err, check.NotNil, check.Commentf(out))
  1872. c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
  1873. err = s.d.Restart()
  1874. c.Assert(err, check.IsNil)
  1875. out, err = s.d.Cmd("start", "-a", "test2")
  1876. c.Assert(err, check.NotNil, check.Commentf(out))
  1877. c.Assert(out, check.Not(checker.Contains), "1 packets transmitted, 1 packets received")
  1878. }
  1879. func (s *DockerDaemonSuite) TestDaemonStartWithoutColors(c *check.C) {
  1880. testRequires(c, DaemonIsLinux, NotPpc64le)
  1881. newD := NewDaemon(c)
  1882. infoLog := "\x1b[34mINFO\x1b"
  1883. p, tty, err := pty.Open()
  1884. c.Assert(err, checker.IsNil)
  1885. defer func() {
  1886. tty.Close()
  1887. p.Close()
  1888. }()
  1889. b := bytes.NewBuffer(nil)
  1890. go io.Copy(b, p)
  1891. // Enable coloring explicitly
  1892. newD.StartWithLogFile(tty, "--raw-logs=false")
  1893. newD.Stop()
  1894. c.Assert(b.String(), checker.Contains, infoLog)
  1895. b.Reset()
  1896. // Disable coloring explicitly
  1897. newD.StartWithLogFile(tty, "--raw-logs=true")
  1898. newD.Stop()
  1899. c.Assert(b.String(), check.Not(checker.Contains), infoLog)
  1900. }
  1901. func (s *DockerDaemonSuite) TestDaemonDebugLog(c *check.C) {
  1902. testRequires(c, DaemonIsLinux, NotPpc64le)
  1903. newD := NewDaemon(c)
  1904. debugLog := "\x1b[37mDEBU\x1b"
  1905. p, tty, err := pty.Open()
  1906. c.Assert(err, checker.IsNil)
  1907. defer func() {
  1908. tty.Close()
  1909. p.Close()
  1910. }()
  1911. b := bytes.NewBuffer(nil)
  1912. go io.Copy(b, p)
  1913. newD.StartWithLogFile(tty, "--debug")
  1914. newD.Stop()
  1915. c.Assert(b.String(), checker.Contains, debugLog)
  1916. }
  1917. func (s *DockerSuite) TestDaemonDiscoveryBackendConfigReload(c *check.C) {
  1918. testRequires(c, SameHostDaemon, DaemonIsLinux)
  1919. // daemon config file
  1920. tmpfile := tempfile.NewTempFile(c, "config-test", `{ "debug" : false }`)
  1921. defer tmpfile.Remove()
  1922. d := NewDaemon(c)
  1923. // --log-level needs to be set so that d.Start() doesn't add --debug causing
  1924. // a conflict with the config
  1925. err := d.Start("--config-file", tmpfile.Name(), "--log-level=info")
  1926. c.Assert(err, checker.IsNil)
  1927. defer d.Stop()
  1928. // daemon config file
  1929. daemonConfig := `{
  1930. "cluster-store": "consul://consuladdr:consulport/some/path",
  1931. "cluster-advertise": "192.168.56.100:0",
  1932. "debug" : false
  1933. }`
  1934. os.Remove(tmpfile.Name())
  1935. configFile, err := os.Create(tmpfile.Name())
  1936. c.Assert(err, checker.IsNil)
  1937. fmt.Fprintf(configFile, "%s", daemonConfig)
  1938. configFile.Close()
  1939. syscall.Kill(d.cmd.Process.Pid, syscall.SIGHUP)
  1940. time.Sleep(3 * time.Second)
  1941. out, err := d.Cmd("info")
  1942. c.Assert(err, checker.IsNil)
  1943. c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Store: consul://consuladdr:consulport/some/path"))
  1944. c.Assert(out, checker.Contains, fmt.Sprintf("Cluster Advertise: 192.168.56.100:0"))
  1945. }
  1946. // Test for #21956
  1947. func (s *DockerDaemonSuite) TestDaemonLogOptions(c *check.C) {
  1948. err := s.d.StartWithBusybox("--log-driver=syslog", "--log-opt=syslog-address=udp://127.0.0.1:514")
  1949. c.Assert(err, check.IsNil)
  1950. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "top")
  1951. c.Assert(err, check.IsNil, check.Commentf(out))
  1952. id := strings.TrimSpace(out)
  1953. out, err = s.d.Cmd("inspect", "--format='{{.HostConfig.LogConfig}}'", id)
  1954. c.Assert(err, check.IsNil, check.Commentf(out))
  1955. c.Assert(out, checker.Contains, "{json-file map[]}")
  1956. }
  1957. // Test case for #20936, #22443
  1958. func (s *DockerDaemonSuite) TestDaemonMaxConcurrency(c *check.C) {
  1959. c.Assert(s.d.Start("--max-concurrent-uploads=6", "--max-concurrent-downloads=8"), check.IsNil)
  1960. expectedMaxConcurrentUploads := `level=debug msg="Max Concurrent Uploads: 6"`
  1961. expectedMaxConcurrentDownloads := `level=debug msg="Max Concurrent Downloads: 8"`
  1962. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1963. c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  1964. c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  1965. }
  1966. // Test case for #20936, #22443
  1967. func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFile(c *check.C) {
  1968. testRequires(c, SameHostDaemon, DaemonIsLinux)
  1969. // daemon config file
  1970. configFilePath := "test.json"
  1971. configFile, err := os.Create(configFilePath)
  1972. c.Assert(err, checker.IsNil)
  1973. defer os.Remove(configFilePath)
  1974. daemonConfig := `{ "max-concurrent-downloads" : 8 }`
  1975. fmt.Fprintf(configFile, "%s", daemonConfig)
  1976. configFile.Close()
  1977. c.Assert(s.d.Start(fmt.Sprintf("--config-file=%s", configFilePath)), check.IsNil)
  1978. expectedMaxConcurrentUploads := `level=debug msg="Max Concurrent Uploads: 5"`
  1979. expectedMaxConcurrentDownloads := `level=debug msg="Max Concurrent Downloads: 8"`
  1980. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  1981. c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  1982. c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  1983. configFile, err = os.Create(configFilePath)
  1984. c.Assert(err, checker.IsNil)
  1985. daemonConfig = `{ "max-concurrent-uploads" : 7, "max-concurrent-downloads" : 9 }`
  1986. fmt.Fprintf(configFile, "%s", daemonConfig)
  1987. configFile.Close()
  1988. syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  1989. time.Sleep(3 * time.Second)
  1990. expectedMaxConcurrentUploads = `level=debug msg="Reset Max Concurrent Uploads: 7"`
  1991. expectedMaxConcurrentDownloads = `level=debug msg="Reset Max Concurrent Downloads: 9"`
  1992. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  1993. c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  1994. c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  1995. }
  1996. // Test case for #20936, #22443
  1997. func (s *DockerDaemonSuite) TestDaemonMaxConcurrencyWithConfigFileReload(c *check.C) {
  1998. testRequires(c, SameHostDaemon, DaemonIsLinux)
  1999. // daemon config file
  2000. configFilePath := "test.json"
  2001. configFile, err := os.Create(configFilePath)
  2002. c.Assert(err, checker.IsNil)
  2003. defer os.Remove(configFilePath)
  2004. daemonConfig := `{ "max-concurrent-uploads" : null }`
  2005. fmt.Fprintf(configFile, "%s", daemonConfig)
  2006. configFile.Close()
  2007. c.Assert(s.d.Start(fmt.Sprintf("--config-file=%s", configFilePath)), check.IsNil)
  2008. expectedMaxConcurrentUploads := `level=debug msg="Max Concurrent Uploads: 5"`
  2009. expectedMaxConcurrentDownloads := `level=debug msg="Max Concurrent Downloads: 3"`
  2010. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  2011. c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2012. c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2013. configFile, err = os.Create(configFilePath)
  2014. c.Assert(err, checker.IsNil)
  2015. daemonConfig = `{ "max-concurrent-uploads" : 1, "max-concurrent-downloads" : null }`
  2016. fmt.Fprintf(configFile, "%s", daemonConfig)
  2017. configFile.Close()
  2018. syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2019. time.Sleep(3 * time.Second)
  2020. expectedMaxConcurrentUploads = `level=debug msg="Reset Max Concurrent Uploads: 1"`
  2021. expectedMaxConcurrentDownloads = `level=debug msg="Reset Max Concurrent Downloads: 3"`
  2022. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  2023. c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2024. c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2025. configFile, err = os.Create(configFilePath)
  2026. c.Assert(err, checker.IsNil)
  2027. daemonConfig = `{ "labels":["foo=bar"] }`
  2028. fmt.Fprintf(configFile, "%s", daemonConfig)
  2029. configFile.Close()
  2030. syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2031. time.Sleep(3 * time.Second)
  2032. expectedMaxConcurrentUploads = `level=debug msg="Reset Max Concurrent Uploads: 5"`
  2033. expectedMaxConcurrentDownloads = `level=debug msg="Reset Max Concurrent Downloads: 3"`
  2034. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  2035. c.Assert(string(content), checker.Contains, expectedMaxConcurrentUploads)
  2036. c.Assert(string(content), checker.Contains, expectedMaxConcurrentDownloads)
  2037. }
  2038. func (s *DockerDaemonSuite) TestBuildOnDisabledBridgeNetworkDaemon(c *check.C) {
  2039. err := s.d.StartWithBusybox("-b=none", "--iptables=false")
  2040. c.Assert(err, check.IsNil)
  2041. s.d.c.Logf("dockerBinary %s", dockerBinary)
  2042. out, code, err := s.d.buildImageWithOut("busyboxs",
  2043. `FROM busybox
  2044. RUN cat /etc/hosts`, false)
  2045. comment := check.Commentf("Failed to build image. output %s, exitCode %d, err %v", out, code, err)
  2046. c.Assert(err, check.IsNil, comment)
  2047. c.Assert(code, check.Equals, 0, comment)
  2048. }
  2049. // Test case for #21976
  2050. func (s *DockerDaemonSuite) TestDaemonDNSInHostMode(c *check.C) {
  2051. testRequires(c, SameHostDaemon, DaemonIsLinux)
  2052. err := s.d.StartWithBusybox("--dns", "1.2.3.4")
  2053. c.Assert(err, checker.IsNil)
  2054. expectedOutput := "nameserver 1.2.3.4"
  2055. out, _ := s.d.Cmd("run", "--net=host", "busybox", "cat", "/etc/resolv.conf")
  2056. c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  2057. }
  2058. // Test case for #21976
  2059. func (s *DockerDaemonSuite) TestDaemonDNSSearchInHostMode(c *check.C) {
  2060. testRequires(c, SameHostDaemon, DaemonIsLinux)
  2061. err := s.d.StartWithBusybox("--dns-search", "example.com")
  2062. c.Assert(err, checker.IsNil)
  2063. expectedOutput := "search example.com"
  2064. out, _ := s.d.Cmd("run", "--net=host", "busybox", "cat", "/etc/resolv.conf")
  2065. c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  2066. }
  2067. // Test case for #21976
  2068. func (s *DockerDaemonSuite) TestDaemonDNSOptionsInHostMode(c *check.C) {
  2069. testRequires(c, SameHostDaemon, DaemonIsLinux)
  2070. err := s.d.StartWithBusybox("--dns-opt", "timeout:3")
  2071. c.Assert(err, checker.IsNil)
  2072. expectedOutput := "options timeout:3"
  2073. out, _ := s.d.Cmd("run", "--net=host", "busybox", "cat", "/etc/resolv.conf")
  2074. c.Assert(out, checker.Contains, expectedOutput, check.Commentf("Expected '%s', but got %q", expectedOutput, out))
  2075. }
  2076. func (s *DockerDaemonSuite) TestRunWithRuntimeFromConfigFile(c *check.C) {
  2077. conf, err := ioutil.TempFile("", "config-file-")
  2078. c.Assert(err, check.IsNil)
  2079. configName := conf.Name()
  2080. conf.Close()
  2081. defer os.Remove(configName)
  2082. config := `
  2083. {
  2084. "runtimes": {
  2085. "oci": {
  2086. "path": "docker-runc"
  2087. },
  2088. "vm": {
  2089. "path": "/usr/local/bin/vm-manager",
  2090. "runtimeArgs": [
  2091. "--debug"
  2092. ]
  2093. }
  2094. }
  2095. }
  2096. `
  2097. ioutil.WriteFile(configName, []byte(config), 0644)
  2098. err = s.d.StartWithBusybox("--config-file", configName)
  2099. c.Assert(err, check.IsNil)
  2100. // Run with default runtime
  2101. out, err := s.d.Cmd("run", "--rm", "busybox", "ls")
  2102. c.Assert(err, check.IsNil, check.Commentf(out))
  2103. // Run with default runtime explicitly
  2104. out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2105. c.Assert(err, check.IsNil, check.Commentf(out))
  2106. // Run with oci (same path as default) but keep it around
  2107. out, err = s.d.Cmd("run", "--name", "oci-runtime-ls", "--runtime=oci", "busybox", "ls")
  2108. c.Assert(err, check.IsNil, check.Commentf(out))
  2109. // Run with "vm"
  2110. out, err = s.d.Cmd("run", "--rm", "--runtime=vm", "busybox", "ls")
  2111. c.Assert(err, check.NotNil, check.Commentf(out))
  2112. c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2113. // Reset config to only have the default
  2114. config = `
  2115. {
  2116. "runtimes": {
  2117. }
  2118. }
  2119. `
  2120. ioutil.WriteFile(configName, []byte(config), 0644)
  2121. syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2122. // Give daemon time to reload config
  2123. <-time.After(1 * time.Second)
  2124. // Run with default runtime
  2125. out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2126. c.Assert(err, check.IsNil, check.Commentf(out))
  2127. // Run with "oci"
  2128. out, err = s.d.Cmd("run", "--rm", "--runtime=oci", "busybox", "ls")
  2129. c.Assert(err, check.NotNil, check.Commentf(out))
  2130. c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2131. // Start previously created container with oci
  2132. out, err = s.d.Cmd("start", "oci-runtime-ls")
  2133. c.Assert(err, check.NotNil, check.Commentf(out))
  2134. c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2135. // Check that we can't override the default runtime
  2136. config = `
  2137. {
  2138. "runtimes": {
  2139. "runc": {
  2140. "path": "my-runc"
  2141. }
  2142. }
  2143. }
  2144. `
  2145. ioutil.WriteFile(configName, []byte(config), 0644)
  2146. syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2147. // Give daemon time to reload config
  2148. <-time.After(1 * time.Second)
  2149. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  2150. c.Assert(string(content), checker.Contains, `file configuration validation failed (runtime name 'runc' is reserved)`)
  2151. // Check that we can select a default runtime
  2152. config = `
  2153. {
  2154. "default-runtime": "vm",
  2155. "runtimes": {
  2156. "oci": {
  2157. "path": "docker-runc"
  2158. },
  2159. "vm": {
  2160. "path": "/usr/local/bin/vm-manager",
  2161. "runtimeArgs": [
  2162. "--debug"
  2163. ]
  2164. }
  2165. }
  2166. }
  2167. `
  2168. ioutil.WriteFile(configName, []byte(config), 0644)
  2169. syscall.Kill(s.d.cmd.Process.Pid, syscall.SIGHUP)
  2170. // Give daemon time to reload config
  2171. <-time.After(1 * time.Second)
  2172. out, err = s.d.Cmd("run", "--rm", "busybox", "ls")
  2173. c.Assert(err, check.NotNil, check.Commentf(out))
  2174. c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2175. // Run with default runtime explicitly
  2176. out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2177. c.Assert(err, check.IsNil, check.Commentf(out))
  2178. }
  2179. func (s *DockerDaemonSuite) TestRunWithRuntimeFromCommandLine(c *check.C) {
  2180. err := s.d.StartWithBusybox("--add-runtime", "oci=docker-runc", "--add-runtime", "vm=/usr/local/bin/vm-manager")
  2181. c.Assert(err, check.IsNil)
  2182. // Run with default runtime
  2183. out, err := s.d.Cmd("run", "--rm", "busybox", "ls")
  2184. c.Assert(err, check.IsNil, check.Commentf(out))
  2185. // Run with default runtime explicitly
  2186. out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2187. c.Assert(err, check.IsNil, check.Commentf(out))
  2188. // Run with oci (same path as default) but keep it around
  2189. out, err = s.d.Cmd("run", "--name", "oci-runtime-ls", "--runtime=oci", "busybox", "ls")
  2190. c.Assert(err, check.IsNil, check.Commentf(out))
  2191. // Run with "vm"
  2192. out, err = s.d.Cmd("run", "--rm", "--runtime=vm", "busybox", "ls")
  2193. c.Assert(err, check.NotNil, check.Commentf(out))
  2194. c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2195. // Start a daemon without any extra runtimes
  2196. s.d.Stop()
  2197. err = s.d.StartWithBusybox()
  2198. c.Assert(err, check.IsNil)
  2199. // Run with default runtime
  2200. out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2201. c.Assert(err, check.IsNil, check.Commentf(out))
  2202. // Run with "oci"
  2203. out, err = s.d.Cmd("run", "--rm", "--runtime=oci", "busybox", "ls")
  2204. c.Assert(err, check.NotNil, check.Commentf(out))
  2205. c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2206. // Start previously created container with oci
  2207. out, err = s.d.Cmd("start", "oci-runtime-ls")
  2208. c.Assert(err, check.NotNil, check.Commentf(out))
  2209. c.Assert(out, checker.Contains, "Unknown runtime specified oci")
  2210. // Check that we can't override the default runtime
  2211. s.d.Stop()
  2212. err = s.d.Start("--add-runtime", "runc=my-runc")
  2213. c.Assert(err, check.NotNil)
  2214. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  2215. c.Assert(string(content), checker.Contains, `runtime name 'runc' is reserved`)
  2216. // Check that we can select a default runtime
  2217. s.d.Stop()
  2218. err = s.d.StartWithBusybox("--default-runtime=vm", "--add-runtime", "oci=docker-runc", "--add-runtime", "vm=/usr/local/bin/vm-manager")
  2219. c.Assert(err, check.IsNil)
  2220. out, err = s.d.Cmd("run", "--rm", "busybox", "ls")
  2221. c.Assert(err, check.NotNil, check.Commentf(out))
  2222. c.Assert(out, checker.Contains, "/usr/local/bin/vm-manager: no such file or directory")
  2223. // Run with default runtime explicitly
  2224. out, err = s.d.Cmd("run", "--rm", "--runtime=runc", "busybox", "ls")
  2225. c.Assert(err, check.IsNil, check.Commentf(out))
  2226. }
  2227. func (s *DockerDaemonSuite) TestDaemonRestartWithAutoRemoveContainer(c *check.C) {
  2228. err := s.d.StartWithBusybox()
  2229. c.Assert(err, checker.IsNil)
  2230. // top1 will exist after daemon restarts
  2231. out, err := s.d.Cmd("run", "-d", "--name", "top1", "busybox:latest", "top")
  2232. c.Assert(err, checker.IsNil, check.Commentf("run top1: %v", out))
  2233. // top2 will be removed after daemon restarts
  2234. out, err = s.d.Cmd("run", "-d", "--rm", "--name", "top2", "busybox:latest", "top")
  2235. c.Assert(err, checker.IsNil, check.Commentf("run top2: %v", out))
  2236. out, err = s.d.Cmd("ps")
  2237. c.Assert(out, checker.Contains, "top1", check.Commentf("top1 should be running"))
  2238. c.Assert(out, checker.Contains, "top2", check.Commentf("top2 should be running"))
  2239. // now restart daemon gracefully
  2240. err = s.d.Restart()
  2241. c.Assert(err, checker.IsNil)
  2242. out, err = s.d.Cmd("ps", "-a")
  2243. c.Assert(err, checker.IsNil, check.Commentf("out: %v", out))
  2244. c.Assert(out, checker.Contains, "top1", check.Commentf("top1 should exist after daemon restarts"))
  2245. c.Assert(out, checker.Not(checker.Contains), "top2", check.Commentf("top2 should be removed after daemon restarts"))
  2246. }