docker_cli_daemon_test.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. // +build daemon
  2. package main
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "net"
  8. "os"
  9. "os/exec"
  10. "path/filepath"
  11. "regexp"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/docker/libtrust"
  16. "github.com/go-check/check"
  17. )
  18. func (s *DockerDaemonSuite) TestDaemonRestartWithRunningContainersPorts(c *check.C) {
  19. if err := s.d.StartWithBusybox(); err != nil {
  20. c.Fatalf("Could not start daemon with busybox: %v", err)
  21. }
  22. if out, err := s.d.Cmd("run", "-d", "--name", "top1", "-p", "1234:80", "--restart", "always", "busybox:latest", "top"); err != nil {
  23. c.Fatalf("Could not run top1: err=%v\n%s", err, out)
  24. }
  25. // --restart=no by default
  26. if out, err := s.d.Cmd("run", "-d", "--name", "top2", "-p", "80", "busybox:latest", "top"); err != nil {
  27. c.Fatalf("Could not run top2: err=%v\n%s", err, out)
  28. }
  29. testRun := func(m map[string]bool, prefix string) {
  30. var format string
  31. for cont, shouldRun := range m {
  32. out, err := s.d.Cmd("ps")
  33. if err != nil {
  34. c.Fatalf("Could not run ps: err=%v\n%q", err, out)
  35. }
  36. if shouldRun {
  37. format = "%scontainer %q is not running"
  38. } else {
  39. format = "%scontainer %q is running"
  40. }
  41. if shouldRun != strings.Contains(out, cont) {
  42. c.Fatalf(format, prefix, cont)
  43. }
  44. }
  45. }
  46. testRun(map[string]bool{"top1": true, "top2": true}, "")
  47. if err := s.d.Restart(); err != nil {
  48. c.Fatalf("Could not restart daemon: %v", err)
  49. }
  50. testRun(map[string]bool{"top1": true, "top2": false}, "After daemon restart: ")
  51. }
  52. func (s *DockerDaemonSuite) TestDaemonRestartWithVolumesRefs(c *check.C) {
  53. if err := s.d.StartWithBusybox(); err != nil {
  54. c.Fatal(err)
  55. }
  56. if out, err := s.d.Cmd("run", "-d", "--name", "volrestarttest1", "-v", "/foo", "busybox"); err != nil {
  57. c.Fatal(err, out)
  58. }
  59. if err := s.d.Restart(); err != nil {
  60. c.Fatal(err)
  61. }
  62. if _, err := s.d.Cmd("run", "-d", "--volumes-from", "volrestarttest1", "--name", "volrestarttest2", "busybox", "top"); err != nil {
  63. c.Fatal(err)
  64. }
  65. if out, err := s.d.Cmd("rm", "-fv", "volrestarttest2"); err != nil {
  66. c.Fatal(err, out)
  67. }
  68. v, err := s.d.Cmd("inspect", "--format", "{{ json .Volumes }}", "volrestarttest1")
  69. if err != nil {
  70. c.Fatal(err)
  71. }
  72. volumes := make(map[string]string)
  73. json.Unmarshal([]byte(v), &volumes)
  74. if _, err := os.Stat(volumes["/foo"]); err != nil {
  75. c.Fatalf("Expected volume to exist: %s - %s", volumes["/foo"], err)
  76. }
  77. }
  78. func (s *DockerDaemonSuite) TestDaemonStartIptablesFalse(c *check.C) {
  79. if err := s.d.Start("--iptables=false"); err != nil {
  80. c.Fatalf("we should have been able to start the daemon with passing iptables=false: %v", err)
  81. }
  82. }
  83. // Issue #8444: If docker0 bridge is modified (intentionally or unintentionally) and
  84. // no longer has an IP associated, we should gracefully handle that case and associate
  85. // an IP with it rather than fail daemon start
  86. func (s *DockerDaemonSuite) TestDaemonStartBridgeWithoutIPAssociation(c *check.C) {
  87. // rather than depending on brctl commands to verify docker0 is created and up
  88. // let's start the daemon and stop it, and then make a modification to run the
  89. // actual test
  90. if err := s.d.Start(); err != nil {
  91. c.Fatalf("Could not start daemon: %v", err)
  92. }
  93. if err := s.d.Stop(); err != nil {
  94. c.Fatalf("Could not stop daemon: %v", err)
  95. }
  96. // now we will remove the ip from docker0 and then try starting the daemon
  97. ipCmd := exec.Command("ip", "addr", "flush", "dev", "docker0")
  98. stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
  99. if err != nil {
  100. c.Fatalf("failed to remove docker0 IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
  101. }
  102. if err := s.d.Start(); err != nil {
  103. warning := "**WARNING: Docker bridge network in bad state--delete docker0 bridge interface to fix"
  104. c.Fatalf("Could not start daemon when docker0 has no IP address: %v\n%s", err, warning)
  105. }
  106. }
  107. func (s *DockerDaemonSuite) TestDaemonIptablesClean(c *check.C) {
  108. if err := s.d.StartWithBusybox(); err != nil {
  109. c.Fatalf("Could not start daemon with busybox: %v", err)
  110. }
  111. if out, err := s.d.Cmd("run", "-d", "--name", "top", "-p", "80", "busybox:latest", "top"); err != nil {
  112. c.Fatalf("Could not run top: %s, %v", out, err)
  113. }
  114. // get output from iptables with container running
  115. ipTablesSearchString := "tcp dpt:80"
  116. ipTablesCmd := exec.Command("iptables", "-nvL")
  117. out, _, err := runCommandWithOutput(ipTablesCmd)
  118. if err != nil {
  119. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  120. }
  121. if !strings.Contains(out, ipTablesSearchString) {
  122. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  123. }
  124. if err := s.d.Stop(); err != nil {
  125. c.Fatalf("Could not stop daemon: %v", err)
  126. }
  127. // get output from iptables after restart
  128. ipTablesCmd = exec.Command("iptables", "-nvL")
  129. out, _, err = runCommandWithOutput(ipTablesCmd)
  130. if err != nil {
  131. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  132. }
  133. if strings.Contains(out, ipTablesSearchString) {
  134. c.Fatalf("iptables output should not have contained %q, but was %q", ipTablesSearchString, out)
  135. }
  136. }
  137. func (s *DockerDaemonSuite) TestDaemonIptablesCreate(c *check.C) {
  138. if err := s.d.StartWithBusybox(); err != nil {
  139. c.Fatalf("Could not start daemon with busybox: %v", err)
  140. }
  141. if out, err := s.d.Cmd("run", "-d", "--name", "top", "--restart=always", "-p", "80", "busybox:latest", "top"); err != nil {
  142. c.Fatalf("Could not run top: %s, %v", out, err)
  143. }
  144. // get output from iptables with container running
  145. ipTablesSearchString := "tcp dpt:80"
  146. ipTablesCmd := exec.Command("iptables", "-nvL")
  147. out, _, err := runCommandWithOutput(ipTablesCmd)
  148. if err != nil {
  149. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  150. }
  151. if !strings.Contains(out, ipTablesSearchString) {
  152. c.Fatalf("iptables output should have contained %q, but was %q", ipTablesSearchString, out)
  153. }
  154. if err := s.d.Restart(); err != nil {
  155. c.Fatalf("Could not restart daemon: %v", err)
  156. }
  157. // make sure the container is not running
  158. runningOut, err := s.d.Cmd("inspect", "--format='{{.State.Running}}'", "top")
  159. if err != nil {
  160. c.Fatalf("Could not inspect on container: %s, %v", out, err)
  161. }
  162. if strings.TrimSpace(runningOut) != "true" {
  163. c.Fatalf("Container should have been restarted after daemon restart. Status running should have been true but was: %q", strings.TrimSpace(runningOut))
  164. }
  165. // get output from iptables after restart
  166. ipTablesCmd = exec.Command("iptables", "-nvL")
  167. out, _, err = runCommandWithOutput(ipTablesCmd)
  168. if err != nil {
  169. c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
  170. }
  171. if !strings.Contains(out, ipTablesSearchString) {
  172. c.Fatalf("iptables output after restart should have contained %q, but was %q", ipTablesSearchString, out)
  173. }
  174. }
  175. func (s *DockerDaemonSuite) TestDaemonLogLevelWrong(c *check.C) {
  176. c.Assert(s.d.Start("--log-level=bogus"), check.NotNil, check.Commentf("Daemon shouldn't start with wrong log level"))
  177. }
  178. func (s *DockerDaemonSuite) TestDaemonLogLevelDebug(c *check.C) {
  179. if err := s.d.Start("--log-level=debug"); err != nil {
  180. c.Fatal(err)
  181. }
  182. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  183. if !strings.Contains(string(content), `level=debug`) {
  184. c.Fatalf(`Missing level="debug" in log file:\n%s`, string(content))
  185. }
  186. }
  187. func (s *DockerDaemonSuite) TestDaemonLogLevelFatal(c *check.C) {
  188. // we creating new daemons to create new logFile
  189. if err := s.d.Start("--log-level=fatal"); err != nil {
  190. c.Fatal(err)
  191. }
  192. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  193. if strings.Contains(string(content), `level=debug`) {
  194. c.Fatalf(`Should not have level="debug" in log file:\n%s`, string(content))
  195. }
  196. }
  197. func (s *DockerDaemonSuite) TestDaemonFlagD(c *check.C) {
  198. if err := s.d.Start("-D"); err != nil {
  199. c.Fatal(err)
  200. }
  201. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  202. if !strings.Contains(string(content), `level=debug`) {
  203. c.Fatalf(`Should have level="debug" in log file using -D:\n%s`, string(content))
  204. }
  205. }
  206. func (s *DockerDaemonSuite) TestDaemonFlagDebug(c *check.C) {
  207. if err := s.d.Start("--debug"); err != nil {
  208. c.Fatal(err)
  209. }
  210. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  211. if !strings.Contains(string(content), `level=debug`) {
  212. c.Fatalf(`Should have level="debug" in log file using --debug:\n%s`, string(content))
  213. }
  214. }
  215. func (s *DockerDaemonSuite) TestDaemonFlagDebugLogLevelFatal(c *check.C) {
  216. if err := s.d.Start("--debug", "--log-level=fatal"); err != nil {
  217. c.Fatal(err)
  218. }
  219. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  220. if !strings.Contains(string(content), `level=debug`) {
  221. c.Fatalf(`Should have level="debug" in log file when using both --debug and --log-level=fatal:\n%s`, string(content))
  222. }
  223. }
  224. func (s *DockerDaemonSuite) TestDaemonAllocatesListeningPort(c *check.C) {
  225. listeningPorts := [][]string{
  226. {"0.0.0.0", "0.0.0.0", "5678"},
  227. {"127.0.0.1", "127.0.0.1", "1234"},
  228. {"localhost", "127.0.0.1", "1235"},
  229. }
  230. cmdArgs := []string{}
  231. for _, hostDirective := range listeningPorts {
  232. cmdArgs = append(cmdArgs, "--host", fmt.Sprintf("tcp://%s:%s", hostDirective[0], hostDirective[2]))
  233. }
  234. if err := s.d.StartWithBusybox(cmdArgs...); err != nil {
  235. c.Fatalf("Could not start daemon with busybox: %v", err)
  236. }
  237. for _, hostDirective := range listeningPorts {
  238. output, err := s.d.Cmd("run", "-p", fmt.Sprintf("%s:%s:80", hostDirective[1], hostDirective[2]), "busybox", "true")
  239. if err == nil {
  240. c.Fatalf("Container should not start, expected port already allocated error: %q", output)
  241. } else if !strings.Contains(output, "port is already allocated") {
  242. c.Fatalf("Expected port is already allocated error: %q", output)
  243. }
  244. }
  245. }
  246. // #9629
  247. func (s *DockerDaemonSuite) TestDaemonVolumesBindsRefs(c *check.C) {
  248. if err := s.d.StartWithBusybox(); err != nil {
  249. c.Fatal(err)
  250. }
  251. tmp, err := ioutil.TempDir(os.TempDir(), "")
  252. if err != nil {
  253. c.Fatal(err)
  254. }
  255. defer os.RemoveAll(tmp)
  256. if err := ioutil.WriteFile(tmp+"/test", []byte("testing"), 0655); err != nil {
  257. c.Fatal(err)
  258. }
  259. if out, err := s.d.Cmd("create", "-v", tmp+":/foo", "--name=voltest", "busybox"); err != nil {
  260. c.Fatal(err, out)
  261. }
  262. if err := s.d.Restart(); err != nil {
  263. c.Fatal(err)
  264. }
  265. if out, err := s.d.Cmd("run", "--volumes-from=voltest", "--name=consumer", "busybox", "/bin/sh", "-c", "[ -f /foo/test ]"); err != nil {
  266. c.Fatal(err, out)
  267. }
  268. }
  269. func (s *DockerDaemonSuite) TestDaemonKeyGeneration(c *check.C) {
  270. // TODO: skip or update for Windows daemon
  271. os.Remove("/etc/docker/key.json")
  272. if err := s.d.Start(); err != nil {
  273. c.Fatalf("Could not start daemon: %v", err)
  274. }
  275. s.d.Stop()
  276. k, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  277. if err != nil {
  278. c.Fatalf("Error opening key file")
  279. }
  280. kid := k.KeyID()
  281. // Test Key ID is a valid fingerprint (e.g. QQXN:JY5W:TBXI:MK3X:GX6P:PD5D:F56N:NHCS:LVRZ:JA46:R24J:XEFF)
  282. if len(kid) != 59 {
  283. c.Fatalf("Bad key ID: %s", kid)
  284. }
  285. }
  286. func (s *DockerDaemonSuite) TestDaemonKeyMigration(c *check.C) {
  287. // TODO: skip or update for Windows daemon
  288. os.Remove("/etc/docker/key.json")
  289. k1, err := libtrust.GenerateECP256PrivateKey()
  290. if err != nil {
  291. c.Fatalf("Error generating private key: %s", err)
  292. }
  293. if err := os.MkdirAll(filepath.Join(os.Getenv("HOME"), ".docker"), 0755); err != nil {
  294. c.Fatalf("Error creating .docker directory: %s", err)
  295. }
  296. if err := libtrust.SaveKey(filepath.Join(os.Getenv("HOME"), ".docker", "key.json"), k1); err != nil {
  297. c.Fatalf("Error saving private key: %s", err)
  298. }
  299. if err := s.d.Start(); err != nil {
  300. c.Fatalf("Could not start daemon: %v", err)
  301. }
  302. s.d.Stop()
  303. k2, err := libtrust.LoadKeyFile("/etc/docker/key.json")
  304. if err != nil {
  305. c.Fatalf("Error opening key file")
  306. }
  307. if k1.KeyID() != k2.KeyID() {
  308. c.Fatalf("Key not migrated")
  309. }
  310. }
  311. // Simulate an older daemon (pre 1.3) coming up with volumes specified in containers
  312. // without corresponding volume json
  313. func (s *DockerDaemonSuite) TestDaemonUpgradeWithVolumes(c *check.C) {
  314. graphDir := filepath.Join(os.TempDir(), "docker-test")
  315. defer os.RemoveAll(graphDir)
  316. if err := s.d.StartWithBusybox("-g", graphDir); err != nil {
  317. c.Fatal(err)
  318. }
  319. tmpDir := filepath.Join(os.TempDir(), "test")
  320. defer os.RemoveAll(tmpDir)
  321. if out, err := s.d.Cmd("create", "-v", tmpDir+":/foo", "--name=test", "busybox"); err != nil {
  322. c.Fatal(err, out)
  323. }
  324. if err := s.d.Stop(); err != nil {
  325. c.Fatal(err)
  326. }
  327. // Remove this since we're expecting the daemon to re-create it too
  328. if err := os.RemoveAll(tmpDir); err != nil {
  329. c.Fatal(err)
  330. }
  331. configDir := filepath.Join(graphDir, "volumes")
  332. if err := os.RemoveAll(configDir); err != nil {
  333. c.Fatal(err)
  334. }
  335. if err := s.d.Start("-g", graphDir); err != nil {
  336. c.Fatal(err)
  337. }
  338. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  339. c.Fatalf("expected volume path %s to exist but it does not", tmpDir)
  340. }
  341. dir, err := ioutil.ReadDir(configDir)
  342. if err != nil {
  343. c.Fatal(err)
  344. }
  345. if len(dir) == 0 {
  346. c.Fatalf("expected volumes config dir to contain data for new volume")
  347. }
  348. // Now with just removing the volume config and not the volume data
  349. if err := s.d.Stop(); err != nil {
  350. c.Fatal(err)
  351. }
  352. if err := os.RemoveAll(configDir); err != nil {
  353. c.Fatal(err)
  354. }
  355. if err := s.d.Start("-g", graphDir); err != nil {
  356. c.Fatal(err)
  357. }
  358. dir, err = ioutil.ReadDir(configDir)
  359. if err != nil {
  360. c.Fatal(err)
  361. }
  362. if len(dir) == 0 {
  363. c.Fatalf("expected volumes config dir to contain data for new volume")
  364. }
  365. }
  366. // GH#11320 - verify that the daemon exits on failure properly
  367. // Note that this explicitly tests the conflict of {-b,--bridge} and {--bip} options as the means
  368. // to get a daemon init failure; no other tests for -b/--bip conflict are therefore required
  369. func (s *DockerDaemonSuite) TestDaemonExitOnFailure(c *check.C) {
  370. //attempt to start daemon with incorrect flags (we know -b and --bip conflict)
  371. if err := s.d.Start("--bridge", "nosuchbridge", "--bip", "1.1.1.1"); err != nil {
  372. //verify we got the right error
  373. if !strings.Contains(err.Error(), "Daemon exited and never started") {
  374. c.Fatalf("Expected daemon not to start, got %v", err)
  375. }
  376. // look in the log and make sure we got the message that daemon is shutting down
  377. runCmd := exec.Command("grep", "Error starting daemon", s.d.LogfileName())
  378. if out, _, err := runCommandWithOutput(runCmd); err != nil {
  379. c.Fatalf("Expected 'Error starting daemon' message; but doesn't exist in log: %q, err: %v", out, err)
  380. }
  381. } else {
  382. //if we didn't get an error and the daemon is running, this is a failure
  383. c.Fatal("Conflicting options should cause the daemon to error out with a failure")
  384. }
  385. }
  386. func (s *DockerDaemonSuite) TestDaemonBridgeExternal(c *check.C) {
  387. d := s.d
  388. err := d.Start("--bridge", "nosuchbridge")
  389. c.Assert(err, check.NotNil, check.Commentf("--bridge option with an invalid bridge should cause the daemon to fail"))
  390. defer d.Restart()
  391. bridgeName := "external-bridge"
  392. bridgeIp := "192.169.1.1/24"
  393. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIp)
  394. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  395. c.Assert(err, check.IsNil, check.Commentf(out))
  396. defer deleteInterface(c, bridgeName)
  397. err = d.StartWithBusybox("--bridge", bridgeName)
  398. c.Assert(err, check.IsNil)
  399. ipTablesSearchString := bridgeIPNet.String()
  400. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  401. out, _, err = runCommandWithOutput(ipTablesCmd)
  402. c.Assert(err, check.IsNil)
  403. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  404. check.Commentf("iptables output should have contained %q, but was %q",
  405. ipTablesSearchString, out))
  406. _, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
  407. c.Assert(err, check.IsNil)
  408. containerIp := d.findContainerIP("ExtContainer")
  409. ip := net.ParseIP(containerIp)
  410. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  411. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  412. containerIp))
  413. }
  414. func createInterface(c *check.C, ifType string, ifName string, ipNet string) (string, error) {
  415. args := []string{"link", "add", "name", ifName, "type", ifType}
  416. ipLinkCmd := exec.Command("ip", args...)
  417. out, _, err := runCommandWithOutput(ipLinkCmd)
  418. if err != nil {
  419. return out, err
  420. }
  421. ifCfgCmd := exec.Command("ifconfig", ifName, ipNet, "up")
  422. out, _, err = runCommandWithOutput(ifCfgCmd)
  423. return out, err
  424. }
  425. func deleteInterface(c *check.C, ifName string) {
  426. ifCmd := exec.Command("ip", "link", "delete", ifName)
  427. out, _, err := runCommandWithOutput(ifCmd)
  428. c.Assert(err, check.IsNil, check.Commentf(out))
  429. flushCmd := exec.Command("iptables", "-t", "nat", "--flush")
  430. out, _, err = runCommandWithOutput(flushCmd)
  431. c.Assert(err, check.IsNil, check.Commentf(out))
  432. flushCmd = exec.Command("iptables", "--flush")
  433. out, _, err = runCommandWithOutput(flushCmd)
  434. c.Assert(err, check.IsNil, check.Commentf(out))
  435. }
  436. func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
  437. // TestDaemonBridgeIP Steps
  438. // 1. Delete the existing docker0 Bridge
  439. // 2. Set --bip daemon configuration and start the new Docker Daemon
  440. // 3. Check if the bip config has taken effect using ifconfig and iptables commands
  441. // 4. Launch a Container and make sure the IP-Address is in the expected subnet
  442. // 5. Delete the docker0 Bridge
  443. // 6. Restart the Docker Daemon (via defered action)
  444. // This Restart takes care of bringing docker0 interface back to auto-assigned IP
  445. defaultNetworkBridge := "docker0"
  446. deleteInterface(c, defaultNetworkBridge)
  447. d := s.d
  448. bridgeIp := "192.169.1.1/24"
  449. ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIp)
  450. err := d.StartWithBusybox("--bip", bridgeIp)
  451. c.Assert(err, check.IsNil)
  452. defer d.Restart()
  453. ifconfigSearchString := ip.String()
  454. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  455. out, _, _, err := runCommandWithStdoutStderr(ifconfigCmd)
  456. c.Assert(err, check.IsNil)
  457. c.Assert(strings.Contains(out, ifconfigSearchString), check.Equals, true,
  458. check.Commentf("ifconfig output should have contained %q, but was %q",
  459. ifconfigSearchString, out))
  460. ipTablesSearchString := bridgeIPNet.String()
  461. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  462. out, _, err = runCommandWithOutput(ipTablesCmd)
  463. c.Assert(err, check.IsNil)
  464. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  465. check.Commentf("iptables output should have contained %q, but was %q",
  466. ipTablesSearchString, out))
  467. out, err = d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  468. c.Assert(err, check.IsNil)
  469. containerIp := d.findContainerIP("test")
  470. ip = net.ParseIP(containerIp)
  471. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  472. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  473. containerIp))
  474. deleteInterface(c, defaultNetworkBridge)
  475. }
  476. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
  477. d := s.d
  478. bridgeName := "external-bridge"
  479. bridgeIp := "192.169.1.1/24"
  480. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  481. c.Assert(err, check.IsNil, check.Commentf(out))
  482. defer deleteInterface(c, bridgeName)
  483. args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"}
  484. err = d.StartWithBusybox(args...)
  485. c.Assert(err, check.IsNil)
  486. defer d.Restart()
  487. for i := 0; i < 4; i++ {
  488. cName := "Container" + strconv.Itoa(i)
  489. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  490. if err != nil {
  491. c.Assert(strings.Contains(out, "no available ip addresses"), check.Equals, true,
  492. check.Commentf("Could not run a Container : %s %s", err.Error(), out))
  493. }
  494. }
  495. }
  496. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  497. d := s.d
  498. ipStr := "192.170.1.1/24"
  499. ip, _, _ := net.ParseCIDR(ipStr)
  500. args := []string{"--ip", ip.String()}
  501. err := d.StartWithBusybox(args...)
  502. c.Assert(err, check.IsNil)
  503. defer d.Restart()
  504. out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  505. c.Assert(err, check.NotNil,
  506. check.Commentf("Running a container must fail with an invalid --ip option"))
  507. c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
  508. ifName := "dummy"
  509. out, err = createInterface(c, "dummy", ifName, ipStr)
  510. c.Assert(err, check.IsNil, check.Commentf(out))
  511. defer deleteInterface(c, ifName)
  512. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  513. c.Assert(err, check.IsNil)
  514. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  515. out, _, err = runCommandWithOutput(ipTablesCmd)
  516. c.Assert(err, check.IsNil)
  517. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  518. matched, _ := regexp.MatchString(regex, out)
  519. c.Assert(matched, check.Equals, true,
  520. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  521. }
  522. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  523. d := s.d
  524. bridgeName := "external-bridge"
  525. bridgeIp := "192.169.1.1/24"
  526. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  527. c.Assert(err, check.IsNil, check.Commentf(out))
  528. defer deleteInterface(c, bridgeName)
  529. args := []string{"--bridge", bridgeName, "--icc=false"}
  530. err = d.StartWithBusybox(args...)
  531. c.Assert(err, check.IsNil)
  532. defer d.Restart()
  533. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  534. out, _, err = runCommandWithOutput(ipTablesCmd)
  535. c.Assert(err, check.IsNil)
  536. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  537. matched, _ := regexp.MatchString(regex, out)
  538. c.Assert(matched, check.Equals, true,
  539. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  540. // Pinging another container must fail with --icc=false
  541. pingContainers(c, d, true)
  542. ipStr := "192.171.1.1/24"
  543. ip, _, _ := net.ParseCIDR(ipStr)
  544. ifName := "icc-dummy"
  545. createInterface(c, "dummy", ifName, ipStr)
  546. // But, Pinging external or a Host interface must succeed
  547. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  548. runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd}
  549. _, err = d.Cmd("run", runArgs...)
  550. c.Assert(err, check.IsNil)
  551. }
  552. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  553. d := s.d
  554. bridgeName := "external-bridge"
  555. bridgeIp := "192.169.1.1/24"
  556. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  557. c.Assert(err, check.IsNil, check.Commentf(out))
  558. defer deleteInterface(c, bridgeName)
  559. args := []string{"--bridge", bridgeName, "--icc=false"}
  560. err = d.StartWithBusybox(args...)
  561. c.Assert(err, check.IsNil)
  562. defer d.Restart()
  563. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  564. out, _, err = runCommandWithOutput(ipTablesCmd)
  565. c.Assert(err, check.IsNil)
  566. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  567. matched, _ := regexp.MatchString(regex, out)
  568. c.Assert(matched, check.Equals, true,
  569. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  570. _, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  571. c.Assert(err, check.IsNil)
  572. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  573. c.Assert(err, check.IsNil, check.Commentf(out))
  574. }
  575. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  576. testRequires(c, NativeExecDriver)
  577. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  578. c.Fatal(err)
  579. }
  580. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  581. if err != nil {
  582. c.Fatal(out, err)
  583. }
  584. outArr := strings.Split(out, "\n")
  585. if len(outArr) < 2 {
  586. c.Fatalf("got unexpected output: %s", out)
  587. }
  588. nofile := strings.TrimSpace(outArr[0])
  589. nproc := strings.TrimSpace(outArr[1])
  590. if nofile != "42" {
  591. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  592. }
  593. if nproc != "2048" {
  594. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  595. }
  596. // Now restart daemon with a new default
  597. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  598. c.Fatal(err)
  599. }
  600. out, err = s.d.Cmd("start", "-a", "test")
  601. if err != nil {
  602. c.Fatal(err)
  603. }
  604. outArr = strings.Split(out, "\n")
  605. if len(outArr) < 2 {
  606. c.Fatalf("got unexpected output: %s", out)
  607. }
  608. nofile = strings.TrimSpace(outArr[0])
  609. nproc = strings.TrimSpace(outArr[1])
  610. if nofile != "43" {
  611. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  612. }
  613. if nproc != "2048" {
  614. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  615. }
  616. }
  617. // #11315
  618. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  619. if err := s.d.StartWithBusybox(); err != nil {
  620. c.Fatal(err)
  621. }
  622. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  623. c.Fatal(err, out)
  624. }
  625. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  626. c.Fatal(err, out)
  627. }
  628. if err := s.d.Restart(); err != nil {
  629. c.Fatal(err)
  630. }
  631. if out, err := s.d.Cmd("start", "test2"); err != nil {
  632. c.Fatal(err, out)
  633. }
  634. }
  635. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  636. if err := s.d.StartWithBusybox(); err != nil {
  637. c.Fatal(err)
  638. }
  639. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  640. if err != nil {
  641. c.Fatal(out, err)
  642. }
  643. id := strings.TrimSpace(out)
  644. if out, err := s.d.Cmd("wait", id); err != nil {
  645. c.Fatal(out, err)
  646. }
  647. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  648. if _, err := os.Stat(logPath); err != nil {
  649. c.Fatal(err)
  650. }
  651. f, err := os.Open(logPath)
  652. if err != nil {
  653. c.Fatal(err)
  654. }
  655. var res struct {
  656. Log string `json:"log"`
  657. Stream string `json:"stream"`
  658. Time time.Time `json:"time"`
  659. }
  660. if err := json.NewDecoder(f).Decode(&res); err != nil {
  661. c.Fatal(err)
  662. }
  663. if res.Log != "testline\n" {
  664. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  665. }
  666. if res.Stream != "stdout" {
  667. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  668. }
  669. if !time.Now().After(res.Time) {
  670. c.Fatalf("Log time %v in future", res.Time)
  671. }
  672. }
  673. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  674. if err := s.d.StartWithBusybox(); err != nil {
  675. c.Fatal(err)
  676. }
  677. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  678. if err != nil {
  679. c.Fatal(out, err)
  680. }
  681. id := strings.TrimSpace(out)
  682. if out, err := s.d.Cmd("wait", id); err != nil {
  683. c.Fatal(out, err)
  684. }
  685. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  686. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  687. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  688. }
  689. }
  690. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  691. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  692. c.Fatal(err)
  693. }
  694. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  695. if err != nil {
  696. c.Fatal(out, err)
  697. }
  698. id := strings.TrimSpace(out)
  699. if out, err := s.d.Cmd("wait", id); err != nil {
  700. c.Fatal(out, err)
  701. }
  702. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  703. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  704. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  705. }
  706. }
  707. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  708. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  709. c.Fatal(err)
  710. }
  711. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  712. if err != nil {
  713. c.Fatal(out, err)
  714. }
  715. id := strings.TrimSpace(out)
  716. if out, err := s.d.Cmd("wait", id); err != nil {
  717. c.Fatal(out, err)
  718. }
  719. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  720. if _, err := os.Stat(logPath); err != nil {
  721. c.Fatal(err)
  722. }
  723. f, err := os.Open(logPath)
  724. if err != nil {
  725. c.Fatal(err)
  726. }
  727. var res struct {
  728. Log string `json:"log"`
  729. Stream string `json:"stream"`
  730. Time time.Time `json:"time"`
  731. }
  732. if err := json.NewDecoder(f).Decode(&res); err != nil {
  733. c.Fatal(err)
  734. }
  735. if res.Log != "testline\n" {
  736. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  737. }
  738. if res.Stream != "stdout" {
  739. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  740. }
  741. if !time.Now().After(res.Time) {
  742. c.Fatalf("Log time %v in future", res.Time)
  743. }
  744. }
  745. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  746. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  747. c.Fatal(err)
  748. }
  749. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  750. if err != nil {
  751. c.Fatal(out, err)
  752. }
  753. id := strings.TrimSpace(out)
  754. out, err = s.d.Cmd("logs", id)
  755. if err == nil {
  756. c.Fatalf("Logs should fail with \"none\" driver")
  757. }
  758. if !strings.Contains(out, `"logs" command is supported only for "json-file" logging driver`) {
  759. c.Fatalf("There should be error about non-json-file driver, got: %s", out)
  760. }
  761. }
  762. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  763. if err := s.d.StartWithBusybox(); err != nil {
  764. c.Fatal(err)
  765. }
  766. // Now create 4 containers
  767. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  768. c.Fatalf("Error creating container: %q", err)
  769. }
  770. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  771. c.Fatalf("Error creating container: %q", err)
  772. }
  773. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  774. c.Fatalf("Error creating container: %q", err)
  775. }
  776. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  777. c.Fatalf("Error creating container: %q", err)
  778. }
  779. s.d.Stop()
  780. s.d.Start("--log-level=debug")
  781. s.d.Stop()
  782. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  783. if strings.Contains(string(content), "....") {
  784. c.Fatalf("Debug level should not have ....\n%s", string(content))
  785. }
  786. s.d.Start("--log-level=error")
  787. s.d.Stop()
  788. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  789. if strings.Contains(string(content), "....") {
  790. c.Fatalf("Error level should not have ....\n%s", string(content))
  791. }
  792. s.d.Start("--log-level=info")
  793. s.d.Stop()
  794. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  795. if !strings.Contains(string(content), "....") {
  796. c.Fatalf("Info level should have ....\n%s", string(content))
  797. }
  798. }
  799. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  800. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  801. if err != nil {
  802. c.Fatal(err)
  803. }
  804. defer os.RemoveAll(dir)
  805. sockPath := filepath.Join(dir, "docker.sock")
  806. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  807. c.Fatal(err)
  808. }
  809. if _, err := os.Stat(sockPath); err != nil {
  810. c.Fatal("socket does not exist")
  811. }
  812. if err := s.d.Stop(); err != nil {
  813. c.Fatal(err)
  814. }
  815. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  816. c.Fatal("unix socket is not cleaned up")
  817. }
  818. }
  819. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  820. type Config struct {
  821. Crv string `json:"crv"`
  822. D string `json:"d"`
  823. Kid string `json:"kid"`
  824. Kty string `json:"kty"`
  825. X string `json:"x"`
  826. Y string `json:"y"`
  827. }
  828. os.Remove("/etc/docker/key.json")
  829. if err := s.d.Start(); err != nil {
  830. c.Fatalf("Failed to start daemon: %v", err)
  831. }
  832. if err := s.d.Stop(); err != nil {
  833. c.Fatalf("Could not stop daemon: %v", err)
  834. }
  835. config := &Config{}
  836. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  837. if err != nil {
  838. c.Fatalf("Error reading key.json file: %s", err)
  839. }
  840. // byte[] to Data-Struct
  841. if err := json.Unmarshal(bytes, &config); err != nil {
  842. c.Fatalf("Error Unmarshal: %s", err)
  843. }
  844. //replace config.Kid with the fake value
  845. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  846. // NEW Data-Struct to byte[]
  847. newBytes, err := json.Marshal(&config)
  848. if err != nil {
  849. c.Fatalf("Error Marshal: %s", err)
  850. }
  851. // write back
  852. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  853. c.Fatalf("Error ioutil.WriteFile: %s", err)
  854. }
  855. defer os.Remove("/etc/docker/key.json")
  856. if err := s.d.Start(); err == nil {
  857. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  858. }
  859. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  860. if !strings.Contains(string(content), "Public Key ID does not match") {
  861. c.Fatal("Missing KeyID message from daemon logs")
  862. }
  863. }
  864. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  865. if err := s.d.StartWithBusybox(); err != nil {
  866. c.Fatalf("Could not start daemon with busybox: %v", err)
  867. }
  868. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  869. if err != nil {
  870. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  871. }
  872. containerID := strings.TrimSpace(out)
  873. if out, err := s.d.Cmd("kill", containerID); err != nil {
  874. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  875. }
  876. if err := s.d.Restart(); err != nil {
  877. c.Fatalf("Could not restart daemon: %v", err)
  878. }
  879. errchan := make(chan error)
  880. go func() {
  881. if out, err := s.d.Cmd("wait", containerID); err != nil {
  882. errchan <- fmt.Errorf("%v:\n%s", err, out)
  883. }
  884. close(errchan)
  885. }()
  886. select {
  887. case <-time.After(5 * time.Second):
  888. c.Fatal("Waiting on a stopped (killed) container timed out")
  889. case err := <-errchan:
  890. if err != nil {
  891. c.Fatal(err)
  892. }
  893. }
  894. }
  895. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  896. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  897. const (
  898. testDaemonHttpsAddr = "localhost:4271"
  899. )
  900. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  901. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  902. c.Fatalf("Could not start daemon with busybox: %v", err)
  903. }
  904. //force tcp protocol
  905. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  906. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  907. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  908. if err != nil {
  909. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  910. }
  911. }
  912. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  913. // by using a rogue client certificate and checks that it fails with the expected error.
  914. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  915. const (
  916. errBadCertificate = "remote error: bad certificate"
  917. testDaemonHttpsAddr = "localhost:4271"
  918. )
  919. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  920. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  921. c.Fatalf("Could not start daemon with busybox: %v", err)
  922. }
  923. //force tcp protocol
  924. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  925. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  926. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  927. if err == nil || !strings.Contains(out, errBadCertificate) {
  928. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  929. }
  930. }
  931. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  932. // which provides a rogue server certificate and checks that it fails with the expected error
  933. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  934. const (
  935. errCaUnknown = "x509: certificate signed by unknown authority"
  936. testDaemonRogueHttpsAddr = "localhost:4272"
  937. )
  938. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  939. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHttpsAddr); err != nil {
  940. c.Fatalf("Could not start daemon with busybox: %v", err)
  941. }
  942. //force tcp protocol
  943. host := fmt.Sprintf("tcp://%s", testDaemonRogueHttpsAddr)
  944. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  945. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  946. if err == nil || !strings.Contains(out, errCaUnknown) {
  947. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  948. }
  949. }
  950. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  951. var dargs []string
  952. if d != nil {
  953. dargs = []string{"--host", d.sock()}
  954. }
  955. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  956. _, err := runCommand(exec.Command(dockerBinary, args...))
  957. c.Assert(err, check.IsNil)
  958. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  959. pingCmd := "ping -c 1 %s -W 1"
  960. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  961. _, err = runCommand(exec.Command(dockerBinary, args...))
  962. if expectFailure {
  963. c.Assert(err, check.NotNil)
  964. } else {
  965. c.Assert(err, check.IsNil)
  966. }
  967. args = append(dargs, "rm", "-f", "container1")
  968. runCommand(exec.Command(dockerBinary, args...))
  969. }
  970. func (s *DockerDaemonSuite) TestDaemonRestartWithSockerAsVolume(c *check.C) {
  971. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  972. socket := filepath.Join(s.d.folder, "docker.sock")
  973. out, err := s.d.Cmd("run", "-d", "-v", socket+":/sock", "busybox")
  974. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  975. c.Assert(s.d.Restart(), check.IsNil)
  976. }