docker_cli_daemon_test.go 38 KB

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