docker_cli_daemon_test.go 37 KB

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