docker_cli_daemon_test.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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(`Missing 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(`Missing 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(`Missing 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. bridgeName := "external-bridge"
  391. bridgeIp := "192.169.1.1/24"
  392. _, bridgeIPNet, _ := net.ParseCIDR(bridgeIp)
  393. createInterface(c, "bridge", bridgeName, bridgeIp)
  394. err = d.StartWithBusybox("--bridge", bridgeName)
  395. c.Assert(err, check.IsNil)
  396. ipTablesSearchString := bridgeIPNet.String()
  397. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  398. out, _, err := runCommandWithOutput(ipTablesCmd)
  399. c.Assert(err, check.IsNil)
  400. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  401. check.Commentf("iptables output should have contained %q, but was %q",
  402. ipTablesSearchString, out))
  403. _, err = d.Cmd("run", "-d", "--name", "ExtContainer", "busybox", "top")
  404. c.Assert(err, check.IsNil)
  405. containerIp := d.findContainerIP("ExtContainer")
  406. ip := net.ParseIP(containerIp)
  407. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  408. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  409. containerIp))
  410. // Reset to Defaults
  411. deleteInterface(c, bridgeName)
  412. d.Restart()
  413. }
  414. func createInterface(c *check.C, ifType string, ifName string, ipNet string) {
  415. args := []string{"link", "add", "name", ifName, "type", ifType}
  416. ipLinkCmd := exec.Command("ip", args...)
  417. out, _, err := runCommandWithOutput(ipLinkCmd)
  418. c.Assert(err, check.IsNil, check.Commentf(out))
  419. ifCfgCmd := exec.Command("ifconfig", ifName, ipNet, "up")
  420. out, _, err = runCommandWithOutput(ifCfgCmd)
  421. c.Assert(err, check.IsNil, check.Commentf(out))
  422. }
  423. func deleteInterface(c *check.C, bridge string) {
  424. ifCmd := exec.Command("ip", "link", "delete", bridge)
  425. out, _, err := runCommandWithOutput(ifCmd)
  426. c.Assert(err, check.IsNil, check.Commentf(out))
  427. flushCmd := exec.Command("iptables", "-t", "nat", "--flush")
  428. out, _, err = runCommandWithOutput(flushCmd)
  429. c.Assert(err, check.IsNil, check.Commentf(out))
  430. flushCmd = exec.Command("iptables", "--flush")
  431. out, _, err = runCommandWithOutput(flushCmd)
  432. c.Assert(err, check.IsNil, check.Commentf(out))
  433. }
  434. func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
  435. // TestDaemonBridgeIP Steps
  436. // 1. Delete the existing docker0 Bridge
  437. // 2. Set --bip daemon configuration and start the new Docker Daemon
  438. // 3. Check if the bip config has taken effect using ifconfig and iptables commands
  439. // 4. Launch a Container and make sure the IP-Address is in the expected subnet
  440. // 5. Delete the docker0 Bridge
  441. // 6. Restart the Docker Daemon (with no --bip settings)
  442. // This Restart takes care of bringing docker0 interface back to auto-assigned IP
  443. // 7. Stop the Docker Daemon (via defered action)
  444. defaultNetworkBridge := "docker0"
  445. deleteInterface(c, defaultNetworkBridge)
  446. d := s.d
  447. bridgeIp := "192.169.1.1/24"
  448. ip, bridgeIPNet, _ := net.ParseCIDR(bridgeIp)
  449. err := d.StartWithBusybox("--bip", bridgeIp)
  450. c.Assert(err, check.IsNil)
  451. ifconfigSearchString := ip.String()
  452. ifconfigCmd := exec.Command("ifconfig", defaultNetworkBridge)
  453. out, _, _, err := runCommandWithStdoutStderr(ifconfigCmd)
  454. c.Assert(err, check.IsNil)
  455. c.Assert(strings.Contains(out, ifconfigSearchString), check.Equals, true,
  456. check.Commentf("ifconfig output should have contained %q, but was %q",
  457. ifconfigSearchString, out))
  458. ipTablesSearchString := bridgeIPNet.String()
  459. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  460. out, _, err = runCommandWithOutput(ipTablesCmd)
  461. c.Assert(err, check.IsNil)
  462. c.Assert(strings.Contains(out, ipTablesSearchString), check.Equals, true,
  463. check.Commentf("iptables output should have contained %q, but was %q",
  464. ipTablesSearchString, out))
  465. out, err = d.Cmd("run", "-d", "--name", "test", "busybox", "top")
  466. c.Assert(err, check.IsNil)
  467. containerIp := d.findContainerIP("test")
  468. ip = net.ParseIP(containerIp)
  469. c.Assert(bridgeIPNet.Contains(ip), check.Equals, true,
  470. check.Commentf("Container IP-Address must be in the same subnet range : %s",
  471. containerIp))
  472. // Reset to Defaults
  473. deleteInterface(c, defaultNetworkBridge)
  474. d.Restart()
  475. pingContainers(c, nil, false)
  476. }
  477. func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
  478. d := s.d
  479. bridgeName := "external-bridge"
  480. bridgeIp := "192.169.1.1/24"
  481. createInterface(c, "bridge", bridgeName, bridgeIp)
  482. args := []string{"--bridge", bridgeName, "--fixed-cidr", "192.169.1.0/30"}
  483. err := d.StartWithBusybox(args...)
  484. c.Assert(err, check.IsNil)
  485. for i := 0; i < 4; i++ {
  486. cName := "Container" + strconv.Itoa(i)
  487. out, err := d.Cmd("run", "-d", "--name", cName, "busybox", "top")
  488. if err != nil {
  489. c.Assert(strings.Contains(out, "no available ip addresses"), check.Equals, true,
  490. check.Commentf("Could not run a Container : %s %s", err.Error(), out))
  491. }
  492. }
  493. // Reset to Defaults
  494. deleteInterface(c, bridgeName)
  495. d.Restart()
  496. }
  497. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  498. d := s.d
  499. ipStr := "192.170.1.1/24"
  500. ip, _, _ := net.ParseCIDR(ipStr)
  501. args := []string{"--ip", ip.String()}
  502. err := d.StartWithBusybox(args...)
  503. c.Assert(err, check.IsNil)
  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. createInterface(c, "dummy", ifName, ipStr)
  510. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  511. c.Assert(err, check.IsNil)
  512. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  513. out, _, err = runCommandWithOutput(ipTablesCmd)
  514. c.Assert(err, check.IsNil)
  515. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  516. matched, _ := regexp.MatchString(regex, out)
  517. c.Assert(matched, check.Equals, true,
  518. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  519. // Reset to Defaults
  520. deleteInterface(c, ifName)
  521. d.Restart()
  522. }
  523. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  524. d := s.d
  525. bridgeName := "external-bridge"
  526. bridgeIp := "192.169.1.1/24"
  527. createInterface(c, "bridge", bridgeName, bridgeIp)
  528. args := []string{"--bridge", bridgeName, "--icc=false"}
  529. err := d.StartWithBusybox(args...)
  530. c.Assert(err, check.IsNil)
  531. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  532. out, _, err := runCommandWithOutput(ipTablesCmd)
  533. c.Assert(err, check.IsNil)
  534. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  535. matched, _ := regexp.MatchString(regex, out)
  536. c.Assert(matched, check.Equals, true,
  537. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  538. // Pinging another container must fail with --icc=false
  539. pingContainers(c, d, true)
  540. ipStr := "192.171.1.1/24"
  541. ip, _, _ := net.ParseCIDR(ipStr)
  542. ifName := "icc-dummy"
  543. createInterface(c, "dummy", ifName, ipStr)
  544. // But, Pinging external or a Host interface must succeed
  545. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  546. runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd}
  547. _, err = d.Cmd("run", runArgs...)
  548. c.Assert(err, check.IsNil)
  549. // Reset to Defaults
  550. deleteInterface(c, ifName)
  551. d.Restart()
  552. }
  553. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  554. d := s.d
  555. bridgeName := "external-bridge"
  556. bridgeIp := "192.169.1.1/24"
  557. createInterface(c, "bridge", bridgeName, bridgeIp)
  558. args := []string{"--bridge", bridgeName, "--icc=false"}
  559. err := d.StartWithBusybox(args...)
  560. c.Assert(err, check.IsNil)
  561. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  562. out, _, err := runCommandWithOutput(ipTablesCmd)
  563. c.Assert(err, check.IsNil)
  564. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  565. matched, _ := regexp.MatchString(regex, out)
  566. c.Assert(matched, check.Equals, true,
  567. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  568. _, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  569. c.Assert(err, check.IsNil)
  570. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  571. c.Assert(err, check.IsNil, check.Commentf(out))
  572. // Reset to Defaults
  573. deleteInterface(c, bridgeName)
  574. d.Restart()
  575. }
  576. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  577. testRequires(c, NativeExecDriver)
  578. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  579. c.Fatal(err)
  580. }
  581. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  582. if err != nil {
  583. c.Fatal(out, err)
  584. }
  585. outArr := strings.Split(out, "\n")
  586. if len(outArr) < 2 {
  587. c.Fatalf("got unexpected output: %s", out)
  588. }
  589. nofile := strings.TrimSpace(outArr[0])
  590. nproc := strings.TrimSpace(outArr[1])
  591. if nofile != "42" {
  592. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  593. }
  594. if nproc != "2048" {
  595. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  596. }
  597. // Now restart daemon with a new default
  598. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  599. c.Fatal(err)
  600. }
  601. out, err = s.d.Cmd("start", "-a", "test")
  602. if err != nil {
  603. c.Fatal(err)
  604. }
  605. outArr = strings.Split(out, "\n")
  606. if len(outArr) < 2 {
  607. c.Fatalf("got unexpected output: %s", out)
  608. }
  609. nofile = strings.TrimSpace(outArr[0])
  610. nproc = strings.TrimSpace(outArr[1])
  611. if nofile != "43" {
  612. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  613. }
  614. if nproc != "2048" {
  615. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  616. }
  617. }
  618. // #11315
  619. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  620. if err := s.d.StartWithBusybox(); err != nil {
  621. c.Fatal(err)
  622. }
  623. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  624. c.Fatal(err, out)
  625. }
  626. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  627. c.Fatal(err, out)
  628. }
  629. if err := s.d.Restart(); err != nil {
  630. c.Fatal(err)
  631. }
  632. if out, err := s.d.Cmd("start", "test2"); err != nil {
  633. c.Fatal(err, out)
  634. }
  635. }
  636. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  637. if err := s.d.StartWithBusybox(); err != nil {
  638. c.Fatal(err)
  639. }
  640. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  641. if err != nil {
  642. c.Fatal(out, err)
  643. }
  644. id := strings.TrimSpace(out)
  645. if out, err := s.d.Cmd("wait", id); err != nil {
  646. c.Fatal(out, err)
  647. }
  648. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  649. if _, err := os.Stat(logPath); err != nil {
  650. c.Fatal(err)
  651. }
  652. f, err := os.Open(logPath)
  653. if err != nil {
  654. c.Fatal(err)
  655. }
  656. var res struct {
  657. Log string `json:"log"`
  658. Stream string `json:"stream"`
  659. Time time.Time `json:"time"`
  660. }
  661. if err := json.NewDecoder(f).Decode(&res); err != nil {
  662. c.Fatal(err)
  663. }
  664. if res.Log != "testline\n" {
  665. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  666. }
  667. if res.Stream != "stdout" {
  668. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  669. }
  670. if !time.Now().After(res.Time) {
  671. c.Fatalf("Log time %v in future", res.Time)
  672. }
  673. }
  674. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  675. if err := s.d.StartWithBusybox(); err != nil {
  676. c.Fatal(err)
  677. }
  678. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  679. if err != nil {
  680. c.Fatal(out, err)
  681. }
  682. id := strings.TrimSpace(out)
  683. if out, err := s.d.Cmd("wait", id); err != nil {
  684. c.Fatal(out, err)
  685. }
  686. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  687. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  688. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  689. }
  690. }
  691. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  692. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  693. c.Fatal(err)
  694. }
  695. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  696. if err != nil {
  697. c.Fatal(out, err)
  698. }
  699. id := strings.TrimSpace(out)
  700. if out, err := s.d.Cmd("wait", id); err != nil {
  701. c.Fatal(out, err)
  702. }
  703. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  704. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  705. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  706. }
  707. }
  708. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  709. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  710. c.Fatal(err)
  711. }
  712. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  713. if err != nil {
  714. c.Fatal(out, err)
  715. }
  716. id := strings.TrimSpace(out)
  717. if out, err := s.d.Cmd("wait", id); err != nil {
  718. c.Fatal(out, err)
  719. }
  720. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  721. if _, err := os.Stat(logPath); err != nil {
  722. c.Fatal(err)
  723. }
  724. f, err := os.Open(logPath)
  725. if err != nil {
  726. c.Fatal(err)
  727. }
  728. var res struct {
  729. Log string `json:"log"`
  730. Stream string `json:"stream"`
  731. Time time.Time `json:"time"`
  732. }
  733. if err := json.NewDecoder(f).Decode(&res); err != nil {
  734. c.Fatal(err)
  735. }
  736. if res.Log != "testline\n" {
  737. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  738. }
  739. if res.Stream != "stdout" {
  740. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  741. }
  742. if !time.Now().After(res.Time) {
  743. c.Fatalf("Log time %v in future", res.Time)
  744. }
  745. }
  746. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  747. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  748. c.Fatal(err)
  749. }
  750. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  751. if err != nil {
  752. c.Fatal(out, err)
  753. }
  754. id := strings.TrimSpace(out)
  755. out, err = s.d.Cmd("logs", id)
  756. if err == nil {
  757. c.Fatalf("Logs should fail with \"none\" driver")
  758. }
  759. if !strings.Contains(out, `\"logs\" command is supported only for \"json-file\" logging driver`) {
  760. c.Fatalf("There should be error about non-json-file driver, got %s", out)
  761. }
  762. }
  763. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  764. if err := s.d.StartWithBusybox(); err != nil {
  765. c.Fatal(err)
  766. }
  767. // Now create 4 containers
  768. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  769. c.Fatalf("Error creating container: %q", err)
  770. }
  771. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  772. c.Fatalf("Error creating container: %q", err)
  773. }
  774. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  775. c.Fatalf("Error creating container: %q", err)
  776. }
  777. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  778. c.Fatalf("Error creating container: %q", err)
  779. }
  780. s.d.Stop()
  781. s.d.Start("--log-level=debug")
  782. s.d.Stop()
  783. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  784. if strings.Contains(string(content), "....") {
  785. c.Fatalf("Debug level should not have ....\n%s", string(content))
  786. }
  787. s.d.Start("--log-level=error")
  788. s.d.Stop()
  789. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  790. if strings.Contains(string(content), "....") {
  791. c.Fatalf("Error level should not have ....\n%s", string(content))
  792. }
  793. s.d.Start("--log-level=info")
  794. s.d.Stop()
  795. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  796. if !strings.Contains(string(content), "....") {
  797. c.Fatalf("Info level should have ....\n%s", string(content))
  798. }
  799. }
  800. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  801. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  802. if err != nil {
  803. c.Fatal(err)
  804. }
  805. defer os.RemoveAll(dir)
  806. sockPath := filepath.Join(dir, "docker.sock")
  807. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  808. c.Fatal(err)
  809. }
  810. if _, err := os.Stat(sockPath); err != nil {
  811. c.Fatal("socket does not exist")
  812. }
  813. if err := s.d.Stop(); err != nil {
  814. c.Fatal(err)
  815. }
  816. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  817. c.Fatal("unix socket is not cleaned up")
  818. }
  819. }
  820. func (s *DockerDaemonSuite) TestDaemonwithwrongkey(c *check.C) {
  821. type Config struct {
  822. Crv string `json:"crv"`
  823. D string `json:"d"`
  824. Kid string `json:"kid"`
  825. Kty string `json:"kty"`
  826. X string `json:"x"`
  827. Y string `json:"y"`
  828. }
  829. os.Remove("/etc/docker/key.json")
  830. if err := s.d.Start(); err != nil {
  831. c.Fatalf("Failed to start daemon: %v", err)
  832. }
  833. if err := s.d.Stop(); err != nil {
  834. c.Fatalf("Could not stop daemon: %v", err)
  835. }
  836. config := &Config{}
  837. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  838. if err != nil {
  839. c.Fatalf("Error reading key.json file: %s", err)
  840. }
  841. // byte[] to Data-Struct
  842. if err := json.Unmarshal(bytes, &config); err != nil {
  843. c.Fatalf("Error Unmarshal: %s", err)
  844. }
  845. //replace config.Kid with the fake value
  846. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  847. // NEW Data-Struct to byte[]
  848. newBytes, err := json.Marshal(&config)
  849. if err != nil {
  850. c.Fatalf("Error Marshal: %s", err)
  851. }
  852. // write back
  853. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  854. c.Fatalf("Error ioutil.WriteFile: %s", err)
  855. }
  856. defer os.Remove("/etc/docker/key.json")
  857. if err := s.d.Start(); err == nil {
  858. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  859. }
  860. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  861. if !strings.Contains(string(content), "Public Key ID does not match") {
  862. c.Fatal("Missing KeyID message from daemon logs")
  863. }
  864. }
  865. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  866. if err := s.d.StartWithBusybox(); err != nil {
  867. c.Fatalf("Could not start daemon with busybox: %v", err)
  868. }
  869. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  870. if err != nil {
  871. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  872. }
  873. containerID := strings.TrimSpace(out)
  874. if out, err := s.d.Cmd("kill", containerID); err != nil {
  875. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  876. }
  877. if err := s.d.Restart(); err != nil {
  878. c.Fatalf("Could not restart daemon: %v", err)
  879. }
  880. errchan := make(chan error)
  881. go func() {
  882. if out, err := s.d.Cmd("wait", containerID); err != nil {
  883. errchan <- fmt.Errorf("%v:\n%s", err, out)
  884. }
  885. close(errchan)
  886. }()
  887. select {
  888. case <-time.After(5 * time.Second):
  889. c.Fatal("Waiting on a stopped (killed) container timed out")
  890. case err := <-errchan:
  891. if err != nil {
  892. c.Fatal(err)
  893. }
  894. }
  895. }
  896. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  897. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  898. const (
  899. testDaemonHttpsAddr = "localhost:4271"
  900. )
  901. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  902. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  903. c.Fatalf("Could not start daemon with busybox: %v", err)
  904. }
  905. //force tcp protocol
  906. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  907. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  908. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  909. if err != nil {
  910. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  911. }
  912. }
  913. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  914. // by using a rogue client certificate and checks that it fails with the expected error.
  915. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  916. const (
  917. errBadCertificate = "remote error: bad certificate"
  918. testDaemonHttpsAddr = "localhost:4271"
  919. )
  920. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  921. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  922. c.Fatalf("Could not start daemon with busybox: %v", err)
  923. }
  924. //force tcp protocol
  925. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  926. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  927. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  928. if err == nil || !strings.Contains(out, errBadCertificate) {
  929. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  930. }
  931. }
  932. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  933. // which provides a rogue server certificate and checks that it fails with the expected error
  934. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  935. const (
  936. errCaUnknown = "x509: certificate signed by unknown authority"
  937. testDaemonRogueHttpsAddr = "localhost:4272"
  938. )
  939. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  940. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHttpsAddr); err != nil {
  941. c.Fatalf("Could not start daemon with busybox: %v", err)
  942. }
  943. //force tcp protocol
  944. host := fmt.Sprintf("tcp://%s", testDaemonRogueHttpsAddr)
  945. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  946. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  947. if err == nil || !strings.Contains(out, errCaUnknown) {
  948. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  949. }
  950. }
  951. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  952. var dargs []string
  953. if d != nil {
  954. dargs = []string{"--host", d.sock()}
  955. }
  956. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  957. _, err := runCommand(exec.Command(dockerBinary, args...))
  958. c.Assert(err, check.IsNil)
  959. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  960. pingCmd := "ping -c 1 %s -W 1"
  961. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  962. _, err = runCommand(exec.Command(dockerBinary, args...))
  963. if expectFailure {
  964. c.Assert(err, check.NotNil)
  965. } else {
  966. c.Assert(err, check.IsNil)
  967. }
  968. args = append(dargs, "rm", "-f", "container1")
  969. runCommand(exec.Command(dockerBinary, args...))
  970. }