docker_cli_daemon_test.go 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  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) TestDaemonDefaultGatewayIPv4Implicit(c *check.C) {
  450. defaultNetworkBridge := "docker0"
  451. deleteInterface(c, defaultNetworkBridge)
  452. d := s.d
  453. bridgeIp := "192.169.1.1"
  454. bridgeIpNet := fmt.Sprintf("%s/24", bridgeIp)
  455. err := d.StartWithBusybox("--bip", bridgeIpNet)
  456. c.Assert(err, check.IsNil)
  457. defer d.Restart()
  458. expectedMessage := fmt.Sprintf("default via %s dev", bridgeIp)
  459. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  460. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  461. check.Commentf("Implicit default gateway should be bridge IP %s, but default route was '%s'",
  462. bridgeIp, strings.TrimSpace(out)))
  463. deleteInterface(c, defaultNetworkBridge)
  464. }
  465. func (s *DockerDaemonSuite) TestDaemonDefaultGatewayIPv4Explicit(c *check.C) {
  466. defaultNetworkBridge := "docker0"
  467. deleteInterface(c, defaultNetworkBridge)
  468. d := s.d
  469. bridgeIp := "192.169.1.1"
  470. bridgeIpNet := fmt.Sprintf("%s/24", bridgeIp)
  471. gatewayIp := "192.169.1.254"
  472. err := d.StartWithBusybox("--bip", bridgeIpNet, "--default-gateway", gatewayIp)
  473. c.Assert(err, check.IsNil)
  474. defer d.Restart()
  475. expectedMessage := fmt.Sprintf("default via %s dev", gatewayIp)
  476. out, err := d.Cmd("run", "busybox", "ip", "-4", "route", "list", "0/0")
  477. c.Assert(strings.Contains(out, expectedMessage), check.Equals, true,
  478. check.Commentf("Explicit default gateway should be %s, but default route was '%s'",
  479. gatewayIp, strings.TrimSpace(out)))
  480. deleteInterface(c, defaultNetworkBridge)
  481. }
  482. func (s *DockerDaemonSuite) TestDaemonIP(c *check.C) {
  483. d := s.d
  484. ipStr := "192.170.1.1/24"
  485. ip, _, _ := net.ParseCIDR(ipStr)
  486. args := []string{"--ip", ip.String()}
  487. err := d.StartWithBusybox(args...)
  488. c.Assert(err, check.IsNil)
  489. defer d.Restart()
  490. out, err := d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  491. c.Assert(err, check.NotNil,
  492. check.Commentf("Running a container must fail with an invalid --ip option"))
  493. c.Assert(strings.Contains(out, "Error starting userland proxy"), check.Equals, true)
  494. ifName := "dummy"
  495. out, err = createInterface(c, "dummy", ifName, ipStr)
  496. c.Assert(err, check.IsNil, check.Commentf(out))
  497. defer deleteInterface(c, ifName)
  498. _, err = d.Cmd("run", "-d", "-p", "8000:8000", "busybox", "top")
  499. c.Assert(err, check.IsNil)
  500. ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
  501. out, _, err = runCommandWithOutput(ipTablesCmd)
  502. c.Assert(err, check.IsNil)
  503. regex := fmt.Sprintf("DNAT.*%s.*dpt:8000", ip.String())
  504. matched, _ := regexp.MatchString(regex, out)
  505. c.Assert(matched, check.Equals, true,
  506. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  507. }
  508. func (s *DockerDaemonSuite) TestDaemonICCPing(c *check.C) {
  509. d := s.d
  510. bridgeName := "external-bridge"
  511. bridgeIp := "192.169.1.1/24"
  512. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  513. c.Assert(err, check.IsNil, check.Commentf(out))
  514. defer deleteInterface(c, bridgeName)
  515. args := []string{"--bridge", bridgeName, "--icc=false"}
  516. err = d.StartWithBusybox(args...)
  517. c.Assert(err, check.IsNil)
  518. defer d.Restart()
  519. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  520. out, _, err = runCommandWithOutput(ipTablesCmd)
  521. c.Assert(err, check.IsNil)
  522. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  523. matched, _ := regexp.MatchString(regex, out)
  524. c.Assert(matched, check.Equals, true,
  525. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  526. // Pinging another container must fail with --icc=false
  527. pingContainers(c, d, true)
  528. ipStr := "192.171.1.1/24"
  529. ip, _, _ := net.ParseCIDR(ipStr)
  530. ifName := "icc-dummy"
  531. createInterface(c, "dummy", ifName, ipStr)
  532. // But, Pinging external or a Host interface must succeed
  533. pingCmd := fmt.Sprintf("ping -c 1 %s -W 1", ip.String())
  534. runArgs := []string{"--rm", "busybox", "sh", "-c", pingCmd}
  535. _, err = d.Cmd("run", runArgs...)
  536. c.Assert(err, check.IsNil)
  537. }
  538. func (s *DockerDaemonSuite) TestDaemonICCLinkExpose(c *check.C) {
  539. d := s.d
  540. bridgeName := "external-bridge"
  541. bridgeIp := "192.169.1.1/24"
  542. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  543. c.Assert(err, check.IsNil, check.Commentf(out))
  544. defer deleteInterface(c, bridgeName)
  545. args := []string{"--bridge", bridgeName, "--icc=false"}
  546. err = d.StartWithBusybox(args...)
  547. c.Assert(err, check.IsNil)
  548. defer d.Restart()
  549. ipTablesCmd := exec.Command("iptables", "-nvL", "FORWARD")
  550. out, _, err = runCommandWithOutput(ipTablesCmd)
  551. c.Assert(err, check.IsNil)
  552. regex := fmt.Sprintf("DROP.*all.*%s.*%s", bridgeName, bridgeName)
  553. matched, _ := regexp.MatchString(regex, out)
  554. c.Assert(matched, check.Equals, true,
  555. check.Commentf("iptables output should have contained %q, but was %q", regex, out))
  556. out, err = d.Cmd("run", "-d", "--expose", "4567", "--name", "icc1", "busybox", "nc", "-l", "-p", "4567")
  557. c.Assert(err, check.IsNil, check.Commentf(out))
  558. out, err = d.Cmd("run", "--link", "icc1:icc1", "busybox", "nc", "icc1", "4567")
  559. c.Assert(err, check.IsNil, check.Commentf(out))
  560. }
  561. func (s *DockerDaemonSuite) TestDaemonLinksIpTablesRulesWhenLinkAndUnlink(c *check.C) {
  562. bridgeName := "external-bridge"
  563. bridgeIp := "192.169.1.1/24"
  564. out, err := createInterface(c, "bridge", bridgeName, bridgeIp)
  565. c.Assert(err, check.IsNil, check.Commentf(out))
  566. defer deleteInterface(c, bridgeName)
  567. args := []string{"--bridge", bridgeName, "--icc=false"}
  568. err = s.d.StartWithBusybox(args...)
  569. c.Assert(err, check.IsNil)
  570. defer s.d.Restart()
  571. _, err = s.d.Cmd("run", "-d", "--name", "child", "--publish", "8080:80", "busybox", "top")
  572. c.Assert(err, check.IsNil)
  573. _, err = s.d.Cmd("run", "-d", "--name", "parent", "--link", "child:http", "busybox", "top")
  574. c.Assert(err, check.IsNil)
  575. childIP := s.d.findContainerIP("child")
  576. parentIP := s.d.findContainerIP("parent")
  577. sourceRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", childIP, "--sport", "80", "-d", parentIP, "-j", "ACCEPT"}
  578. destinationRule := []string{"-i", bridgeName, "-o", bridgeName, "-p", "tcp", "-s", parentIP, "--dport", "80", "-d", childIP, "-j", "ACCEPT"}
  579. if !iptables.Exists("filter", "DOCKER", sourceRule...) || !iptables.Exists("filter", "DOCKER", destinationRule...) {
  580. c.Fatal("Iptables rules not found")
  581. }
  582. s.d.Cmd("rm", "--link", "parent/http")
  583. if iptables.Exists("filter", "DOCKER", sourceRule...) || iptables.Exists("filter", "DOCKER", destinationRule...) {
  584. c.Fatal("Iptables rules should be removed when unlink")
  585. }
  586. s.d.Cmd("kill", "child")
  587. s.d.Cmd("kill", "parent")
  588. }
  589. func (s *DockerDaemonSuite) TestDaemonUlimitDefaults(c *check.C) {
  590. testRequires(c, NativeExecDriver)
  591. if err := s.d.StartWithBusybox("--default-ulimit", "nofile=42:42", "--default-ulimit", "nproc=1024:1024"); err != nil {
  592. c.Fatal(err)
  593. }
  594. out, err := s.d.Cmd("run", "--ulimit", "nproc=2048", "--name=test", "busybox", "/bin/sh", "-c", "echo $(ulimit -n); echo $(ulimit -p)")
  595. if err != nil {
  596. c.Fatal(out, err)
  597. }
  598. outArr := strings.Split(out, "\n")
  599. if len(outArr) < 2 {
  600. c.Fatalf("got unexpected output: %s", out)
  601. }
  602. nofile := strings.TrimSpace(outArr[0])
  603. nproc := strings.TrimSpace(outArr[1])
  604. if nofile != "42" {
  605. c.Fatalf("expected `ulimit -n` to be `42`, got: %s", nofile)
  606. }
  607. if nproc != "2048" {
  608. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  609. }
  610. // Now restart daemon with a new default
  611. if err := s.d.Restart("--default-ulimit", "nofile=43"); err != nil {
  612. c.Fatal(err)
  613. }
  614. out, err = s.d.Cmd("start", "-a", "test")
  615. if err != nil {
  616. c.Fatal(err)
  617. }
  618. outArr = strings.Split(out, "\n")
  619. if len(outArr) < 2 {
  620. c.Fatalf("got unexpected output: %s", out)
  621. }
  622. nofile = strings.TrimSpace(outArr[0])
  623. nproc = strings.TrimSpace(outArr[1])
  624. if nofile != "43" {
  625. c.Fatalf("expected `ulimit -n` to be `43`, got: %s", nofile)
  626. }
  627. if nproc != "2048" {
  628. c.Fatalf("exepcted `ulimit -p` to be 2048, got: %s", nproc)
  629. }
  630. }
  631. // #11315
  632. func (s *DockerDaemonSuite) TestDaemonRestartRenameContainer(c *check.C) {
  633. if err := s.d.StartWithBusybox(); err != nil {
  634. c.Fatal(err)
  635. }
  636. if out, err := s.d.Cmd("run", "--name=test", "busybox"); err != nil {
  637. c.Fatal(err, out)
  638. }
  639. if out, err := s.d.Cmd("rename", "test", "test2"); err != nil {
  640. c.Fatal(err, out)
  641. }
  642. if err := s.d.Restart(); err != nil {
  643. c.Fatal(err)
  644. }
  645. if out, err := s.d.Cmd("start", "test2"); err != nil {
  646. c.Fatal(err, out)
  647. }
  648. }
  649. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefault(c *check.C) {
  650. if err := s.d.StartWithBusybox(); err != nil {
  651. c.Fatal(err)
  652. }
  653. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  654. if err != nil {
  655. c.Fatal(out, err)
  656. }
  657. id := strings.TrimSpace(out)
  658. if out, err := s.d.Cmd("wait", id); err != nil {
  659. c.Fatal(out, err)
  660. }
  661. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  662. if _, err := os.Stat(logPath); err != nil {
  663. c.Fatal(err)
  664. }
  665. f, err := os.Open(logPath)
  666. if err != nil {
  667. c.Fatal(err)
  668. }
  669. var res struct {
  670. Log string `json:"log"`
  671. Stream string `json:"stream"`
  672. Time time.Time `json:"time"`
  673. }
  674. if err := json.NewDecoder(f).Decode(&res); err != nil {
  675. c.Fatal(err)
  676. }
  677. if res.Log != "testline\n" {
  678. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  679. }
  680. if res.Stream != "stdout" {
  681. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  682. }
  683. if !time.Now().After(res.Time) {
  684. c.Fatalf("Log time %v in future", res.Time)
  685. }
  686. }
  687. func (s *DockerDaemonSuite) TestDaemonLoggingDriverDefaultOverride(c *check.C) {
  688. if err := s.d.StartWithBusybox(); err != nil {
  689. c.Fatal(err)
  690. }
  691. out, err := s.d.Cmd("run", "-d", "--log-driver=none", "busybox", "echo", "testline")
  692. if err != nil {
  693. c.Fatal(out, err)
  694. }
  695. id := strings.TrimSpace(out)
  696. if out, err := s.d.Cmd("wait", id); err != nil {
  697. c.Fatal(out, err)
  698. }
  699. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  700. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  701. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  702. }
  703. }
  704. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNone(c *check.C) {
  705. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  706. c.Fatal(err)
  707. }
  708. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  709. if err != nil {
  710. c.Fatal(out, err)
  711. }
  712. id := strings.TrimSpace(out)
  713. if out, err := s.d.Cmd("wait", id); err != nil {
  714. c.Fatal(out, err)
  715. }
  716. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  717. if _, err := os.Stat(logPath); err == nil || !os.IsNotExist(err) {
  718. c.Fatalf("%s shouldn't exits, error on Stat: %s", logPath, err)
  719. }
  720. }
  721. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneOverride(c *check.C) {
  722. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  723. c.Fatal(err)
  724. }
  725. out, err := s.d.Cmd("run", "-d", "--log-driver=json-file", "busybox", "echo", "testline")
  726. if err != nil {
  727. c.Fatal(out, err)
  728. }
  729. id := strings.TrimSpace(out)
  730. if out, err := s.d.Cmd("wait", id); err != nil {
  731. c.Fatal(out, err)
  732. }
  733. logPath := filepath.Join(s.d.folder, "graph", "containers", id, id+"-json.log")
  734. if _, err := os.Stat(logPath); err != nil {
  735. c.Fatal(err)
  736. }
  737. f, err := os.Open(logPath)
  738. if err != nil {
  739. c.Fatal(err)
  740. }
  741. var res struct {
  742. Log string `json:"log"`
  743. Stream string `json:"stream"`
  744. Time time.Time `json:"time"`
  745. }
  746. if err := json.NewDecoder(f).Decode(&res); err != nil {
  747. c.Fatal(err)
  748. }
  749. if res.Log != "testline\n" {
  750. c.Fatalf("Unexpected log line: %q, expected: %q", res.Log, "testline\n")
  751. }
  752. if res.Stream != "stdout" {
  753. c.Fatalf("Unexpected stream: %q, expected: %q", res.Stream, "stdout")
  754. }
  755. if !time.Now().After(res.Time) {
  756. c.Fatalf("Log time %v in future", res.Time)
  757. }
  758. }
  759. func (s *DockerDaemonSuite) TestDaemonLoggingDriverNoneLogsError(c *check.C) {
  760. if err := s.d.StartWithBusybox("--log-driver=none"); err != nil {
  761. c.Fatal(err)
  762. }
  763. out, err := s.d.Cmd("run", "-d", "busybox", "echo", "testline")
  764. if err != nil {
  765. c.Fatal(out, err)
  766. }
  767. id := strings.TrimSpace(out)
  768. out, err = s.d.Cmd("logs", id)
  769. if err == nil {
  770. c.Fatalf("Logs should fail with \"none\" driver")
  771. }
  772. if !strings.Contains(out, `"logs" command is supported only for "json-file" logging driver`) {
  773. c.Fatalf("There should be error about non-json-file driver, got: %s", out)
  774. }
  775. }
  776. func (s *DockerDaemonSuite) TestDaemonDots(c *check.C) {
  777. if err := s.d.StartWithBusybox(); err != nil {
  778. c.Fatal(err)
  779. }
  780. // Now create 4 containers
  781. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  782. c.Fatalf("Error creating container: %q", err)
  783. }
  784. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  785. c.Fatalf("Error creating container: %q", err)
  786. }
  787. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  788. c.Fatalf("Error creating container: %q", err)
  789. }
  790. if _, err := s.d.Cmd("create", "busybox"); err != nil {
  791. c.Fatalf("Error creating container: %q", err)
  792. }
  793. s.d.Stop()
  794. s.d.Start("--log-level=debug")
  795. s.d.Stop()
  796. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  797. if strings.Contains(string(content), "....") {
  798. c.Fatalf("Debug level should not have ....\n%s", string(content))
  799. }
  800. s.d.Start("--log-level=error")
  801. s.d.Stop()
  802. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  803. if strings.Contains(string(content), "....") {
  804. c.Fatalf("Error level should not have ....\n%s", string(content))
  805. }
  806. s.d.Start("--log-level=info")
  807. s.d.Stop()
  808. content, _ = ioutil.ReadFile(s.d.logFile.Name())
  809. if !strings.Contains(string(content), "....") {
  810. c.Fatalf("Info level should have ....\n%s", string(content))
  811. }
  812. }
  813. func (s *DockerDaemonSuite) TestDaemonUnixSockCleanedUp(c *check.C) {
  814. dir, err := ioutil.TempDir("", "socket-cleanup-test")
  815. if err != nil {
  816. c.Fatal(err)
  817. }
  818. defer os.RemoveAll(dir)
  819. sockPath := filepath.Join(dir, "docker.sock")
  820. if err := s.d.Start("--host", "unix://"+sockPath); err != nil {
  821. c.Fatal(err)
  822. }
  823. if _, err := os.Stat(sockPath); err != nil {
  824. c.Fatal("socket does not exist")
  825. }
  826. if err := s.d.Stop(); err != nil {
  827. c.Fatal(err)
  828. }
  829. if _, err := os.Stat(sockPath); err == nil || !os.IsNotExist(err) {
  830. c.Fatal("unix socket is not cleaned up")
  831. }
  832. }
  833. func (s *DockerDaemonSuite) TestDaemonWithWrongkey(c *check.C) {
  834. type Config struct {
  835. Crv string `json:"crv"`
  836. D string `json:"d"`
  837. Kid string `json:"kid"`
  838. Kty string `json:"kty"`
  839. X string `json:"x"`
  840. Y string `json:"y"`
  841. }
  842. os.Remove("/etc/docker/key.json")
  843. if err := s.d.Start(); err != nil {
  844. c.Fatalf("Failed to start daemon: %v", err)
  845. }
  846. if err := s.d.Stop(); err != nil {
  847. c.Fatalf("Could not stop daemon: %v", err)
  848. }
  849. config := &Config{}
  850. bytes, err := ioutil.ReadFile("/etc/docker/key.json")
  851. if err != nil {
  852. c.Fatalf("Error reading key.json file: %s", err)
  853. }
  854. // byte[] to Data-Struct
  855. if err := json.Unmarshal(bytes, &config); err != nil {
  856. c.Fatalf("Error Unmarshal: %s", err)
  857. }
  858. //replace config.Kid with the fake value
  859. config.Kid = "VSAJ:FUYR:X3H2:B2VZ:KZ6U:CJD5:K7BX:ZXHY:UZXT:P4FT:MJWG:HRJ4"
  860. // NEW Data-Struct to byte[]
  861. newBytes, err := json.Marshal(&config)
  862. if err != nil {
  863. c.Fatalf("Error Marshal: %s", err)
  864. }
  865. // write back
  866. if err := ioutil.WriteFile("/etc/docker/key.json", newBytes, 0400); err != nil {
  867. c.Fatalf("Error ioutil.WriteFile: %s", err)
  868. }
  869. defer os.Remove("/etc/docker/key.json")
  870. if err := s.d.Start(); err == nil {
  871. c.Fatalf("It should not be successful to start daemon with wrong key: %v", err)
  872. }
  873. content, _ := ioutil.ReadFile(s.d.logFile.Name())
  874. if !strings.Contains(string(content), "Public Key ID does not match") {
  875. c.Fatal("Missing KeyID message from daemon logs")
  876. }
  877. }
  878. func (s *DockerDaemonSuite) TestDaemonRestartKillWait(c *check.C) {
  879. if err := s.d.StartWithBusybox(); err != nil {
  880. c.Fatalf("Could not start daemon with busybox: %v", err)
  881. }
  882. out, err := s.d.Cmd("run", "-id", "busybox", "/bin/cat")
  883. if err != nil {
  884. c.Fatalf("Could not run /bin/cat: err=%v\n%s", err, out)
  885. }
  886. containerID := strings.TrimSpace(out)
  887. if out, err := s.d.Cmd("kill", containerID); err != nil {
  888. c.Fatalf("Could not kill %s: err=%v\n%s", containerID, err, out)
  889. }
  890. if err := s.d.Restart(); err != nil {
  891. c.Fatalf("Could not restart daemon: %v", err)
  892. }
  893. errchan := make(chan error)
  894. go func() {
  895. if out, err := s.d.Cmd("wait", containerID); err != nil {
  896. errchan <- fmt.Errorf("%v:\n%s", err, out)
  897. }
  898. close(errchan)
  899. }()
  900. select {
  901. case <-time.After(5 * time.Second):
  902. c.Fatal("Waiting on a stopped (killed) container timed out")
  903. case err := <-errchan:
  904. if err != nil {
  905. c.Fatal(err)
  906. }
  907. }
  908. }
  909. // TestHttpsInfo connects via two-way authenticated HTTPS to the info endpoint
  910. func (s *DockerDaemonSuite) TestHttpsInfo(c *check.C) {
  911. const (
  912. testDaemonHttpsAddr = "localhost:4271"
  913. )
  914. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  915. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  916. c.Fatalf("Could not start daemon with busybox: %v", err)
  917. }
  918. //force tcp protocol
  919. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  920. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-cert.pem", "--tlskey", "fixtures/https/client-key.pem"}
  921. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  922. if err != nil {
  923. c.Fatalf("Error Occurred: %s and output: %s", err, out)
  924. }
  925. }
  926. // TestHttpsInfoRogueCert connects via two-way authenticated HTTPS to the info endpoint
  927. // by using a rogue client certificate and checks that it fails with the expected error.
  928. func (s *DockerDaemonSuite) TestHttpsInfoRogueCert(c *check.C) {
  929. const (
  930. errBadCertificate = "remote error: bad certificate"
  931. testDaemonHttpsAddr = "localhost:4271"
  932. )
  933. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-cert.pem",
  934. "--tlskey", "fixtures/https/server-key.pem", "-H", testDaemonHttpsAddr); err != nil {
  935. c.Fatalf("Could not start daemon with busybox: %v", err)
  936. }
  937. //force tcp protocol
  938. host := fmt.Sprintf("tcp://%s", testDaemonHttpsAddr)
  939. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  940. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  941. if err == nil || !strings.Contains(out, errBadCertificate) {
  942. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errBadCertificate, err, out)
  943. }
  944. }
  945. // TestHttpsInfoRogueServerCert connects via two-way authenticated HTTPS to the info endpoint
  946. // which provides a rogue server certificate and checks that it fails with the expected error
  947. func (s *DockerDaemonSuite) TestHttpsInfoRogueServerCert(c *check.C) {
  948. const (
  949. errCaUnknown = "x509: certificate signed by unknown authority"
  950. testDaemonRogueHttpsAddr = "localhost:4272"
  951. )
  952. if err := s.d.Start("--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/server-rogue-cert.pem",
  953. "--tlskey", "fixtures/https/server-rogue-key.pem", "-H", testDaemonRogueHttpsAddr); err != nil {
  954. c.Fatalf("Could not start daemon with busybox: %v", err)
  955. }
  956. //force tcp protocol
  957. host := fmt.Sprintf("tcp://%s", testDaemonRogueHttpsAddr)
  958. daemonArgs := []string{"--host", host, "--tlsverify", "--tlscacert", "fixtures/https/ca.pem", "--tlscert", "fixtures/https/client-rogue-cert.pem", "--tlskey", "fixtures/https/client-rogue-key.pem"}
  959. out, err := s.d.CmdWithArgs(daemonArgs, "info")
  960. if err == nil || !strings.Contains(out, errCaUnknown) {
  961. c.Fatalf("Expected err: %s, got instead: %s and output: %s", errCaUnknown, err, out)
  962. }
  963. }
  964. func pingContainers(c *check.C, d *Daemon, expectFailure bool) {
  965. var dargs []string
  966. if d != nil {
  967. dargs = []string{"--host", d.sock()}
  968. }
  969. args := append(dargs, "run", "-d", "--name", "container1", "busybox", "top")
  970. _, err := runCommand(exec.Command(dockerBinary, args...))
  971. c.Assert(err, check.IsNil)
  972. args = append(dargs, "run", "--rm", "--link", "container1:alias1", "busybox", "sh", "-c")
  973. pingCmd := "ping -c 1 %s -W 1"
  974. args = append(args, fmt.Sprintf(pingCmd, "alias1"))
  975. _, err = runCommand(exec.Command(dockerBinary, args...))
  976. if expectFailure {
  977. c.Assert(err, check.NotNil)
  978. } else {
  979. c.Assert(err, check.IsNil)
  980. }
  981. args = append(dargs, "rm", "-f", "container1")
  982. runCommand(exec.Command(dockerBinary, args...))
  983. }
  984. func (s *DockerDaemonSuite) TestDaemonRestartWithSocketAsVolume(c *check.C) {
  985. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  986. socket := filepath.Join(s.d.folder, "docker.sock")
  987. out, err := s.d.Cmd("run", "-d", "-v", socket+":/sock", "busybox")
  988. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  989. c.Assert(s.d.Restart(), check.IsNil)
  990. }
  991. func (s *DockerDaemonSuite) TestCleanupMountsAfterCrash(c *check.C) {
  992. c.Assert(s.d.StartWithBusybox(), check.IsNil)
  993. out, err := s.d.Cmd("run", "-d", "busybox", "top")
  994. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  995. id := strings.TrimSpace(out)
  996. c.Assert(s.d.cmd.Process.Signal(os.Kill), check.IsNil)
  997. c.Assert(s.d.Start(), check.IsNil)
  998. mountOut, err := exec.Command("mount").CombinedOutput()
  999. c.Assert(err, check.IsNil, check.Commentf("Output: %s", mountOut))
  1000. c.Assert(strings.Contains(string(mountOut), id), check.Equals, false, check.Commentf("Something mounted from older daemon start: %s", mountOut))
  1001. }
  1002. func (s *DockerDaemonSuite) TestRunContainerWithBridgeNone(c *check.C) {
  1003. testRequires(c, NativeExecDriver)
  1004. c.Assert(s.d.StartWithBusybox("-b", "none"), check.IsNil)
  1005. out, err := s.d.Cmd("run", "--rm", "busybox", "ip", "l")
  1006. c.Assert(err, check.IsNil, check.Commentf("Output: %s", out))
  1007. c.Assert(strings.Contains(out, "eth0"), check.Equals, false,
  1008. check.Commentf("There shouldn't be eth0 in container when network is disabled: %s", out))
  1009. }
  1010. func (s *DockerDaemonSuite) TestDaemonRestartWithContainerRunning(t *check.C) {
  1011. if err := s.d.StartWithBusybox(); err != nil {
  1012. t.Fatal(err)
  1013. }
  1014. if out, err := s.d.Cmd("run", "-ti", "-d", "--name", "test", "busybox"); err != nil {
  1015. t.Fatal(out, err)
  1016. }
  1017. if err := s.d.Restart(); err != nil {
  1018. t.Fatal(err)
  1019. }
  1020. // Container 'test' should be removed without error
  1021. if out, err := s.d.Cmd("rm", "test"); err != nil {
  1022. t.Fatal(out, err)
  1023. }
  1024. }