docker_cli_daemon_test.go 60 KB

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