docker_cli_daemon_test.go 102 KB

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