commands_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "strings"
  10. "testing"
  11. "time"
  12. "github.com/docker/docker/api/client"
  13. "github.com/docker/docker/daemon"
  14. "github.com/docker/docker/pkg/term"
  15. "github.com/docker/docker/utils"
  16. )
  17. func closeWrap(args ...io.Closer) error {
  18. e := false
  19. ret := fmt.Errorf("Error closing elements")
  20. for _, c := range args {
  21. if err := c.Close(); err != nil {
  22. e = true
  23. ret = fmt.Errorf("%s\n%s", ret, err)
  24. }
  25. }
  26. if e {
  27. return ret
  28. }
  29. return nil
  30. }
  31. func setRaw(t *testing.T, c *daemon.Container) *term.State {
  32. pty, err := c.GetPtyMaster()
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. state, err := term.MakeRaw(pty.Fd())
  37. if err != nil {
  38. t.Fatal(err)
  39. }
  40. return state
  41. }
  42. func unsetRaw(t *testing.T, c *daemon.Container, state *term.State) {
  43. pty, err := c.GetPtyMaster()
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. term.RestoreTerminal(pty.Fd(), state)
  48. }
  49. func waitContainerStart(t *testing.T, timeout time.Duration) *daemon.Container {
  50. var container *daemon.Container
  51. setTimeout(t, "Waiting for the container to be started timed out", timeout, func() {
  52. for {
  53. l := globalDaemon.List()
  54. if len(l) == 1 && l[0].State.IsRunning() {
  55. container = l[0]
  56. break
  57. }
  58. time.Sleep(10 * time.Millisecond)
  59. }
  60. })
  61. if container == nil {
  62. t.Fatal("An error occured while waiting for the container to start")
  63. }
  64. return container
  65. }
  66. func setTimeout(t *testing.T, msg string, d time.Duration, f func()) {
  67. c := make(chan bool)
  68. // Make sure we are not too long
  69. go func() {
  70. time.Sleep(d)
  71. c <- true
  72. }()
  73. go func() {
  74. f()
  75. c <- false
  76. }()
  77. if <-c && msg != "" {
  78. t.Fatal(msg)
  79. }
  80. }
  81. func expectPipe(expected string, r io.Reader) error {
  82. o, err := bufio.NewReader(r).ReadString('\n')
  83. if err != nil {
  84. return err
  85. }
  86. if strings.Trim(o, " \r\n") != expected {
  87. return fmt.Errorf("Unexpected output. Expected [%s], received [%s]", expected, o)
  88. }
  89. return nil
  90. }
  91. func assertPipe(input, output string, r io.Reader, w io.Writer, count int) error {
  92. for i := 0; i < count; i++ {
  93. if _, err := w.Write([]byte(input)); err != nil {
  94. return err
  95. }
  96. if err := expectPipe(output, r); err != nil {
  97. return err
  98. }
  99. }
  100. return nil
  101. }
  102. func TestRunExit(t *testing.T) {
  103. stdin, stdinPipe := io.Pipe()
  104. stdout, stdoutPipe := io.Pipe()
  105. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  106. defer cleanup(globalEngine, t)
  107. c1 := make(chan struct{})
  108. go func() {
  109. cli.CmdRun("-i", unitTestImageID, "/bin/cat")
  110. close(c1)
  111. }()
  112. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  113. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  114. t.Fatal(err)
  115. }
  116. })
  117. container := globalDaemon.List()[0]
  118. // Closing /bin/cat stdin, expect it to exit
  119. if err := stdin.Close(); err != nil {
  120. t.Fatal(err)
  121. }
  122. // as the process exited, CmdRun must finish and unblock. Wait for it
  123. setTimeout(t, "Waiting for CmdRun timed out", 10*time.Second, func() {
  124. <-c1
  125. go func() {
  126. cli.CmdWait(container.ID)
  127. }()
  128. if _, err := bufio.NewReader(stdout).ReadString('\n'); err != nil {
  129. t.Fatal(err)
  130. }
  131. })
  132. // Make sure that the client has been disconnected
  133. setTimeout(t, "The client should have been disconnected once the remote process exited.", 2*time.Second, func() {
  134. // Expecting pipe i/o error, just check that read does not block
  135. stdin.Read([]byte{})
  136. })
  137. // Cleanup pipes
  138. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  139. t.Fatal(err)
  140. }
  141. }
  142. // Expected behaviour: the process dies when the client disconnects
  143. func TestRunDisconnect(t *testing.T) {
  144. stdin, stdinPipe := io.Pipe()
  145. stdout, stdoutPipe := io.Pipe()
  146. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  147. defer cleanup(globalEngine, t)
  148. c1 := make(chan struct{})
  149. go func() {
  150. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  151. // fact that CmdRun returns.
  152. cli.CmdRun("-i", unitTestImageID, "/bin/cat")
  153. close(c1)
  154. }()
  155. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  156. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  157. t.Fatal(err)
  158. }
  159. })
  160. // Close pipes (simulate disconnect)
  161. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  162. t.Fatal(err)
  163. }
  164. // as the pipes are close, we expect the process to die,
  165. // therefore CmdRun to unblock. Wait for CmdRun
  166. setTimeout(t, "Waiting for CmdRun timed out", 2*time.Second, func() {
  167. <-c1
  168. })
  169. // Client disconnect after run -i should cause stdin to be closed, which should
  170. // cause /bin/cat to exit.
  171. setTimeout(t, "Waiting for /bin/cat to exit timed out", 2*time.Second, func() {
  172. container := globalDaemon.List()[0]
  173. container.State.WaitStop(-1 * time.Second)
  174. if container.State.IsRunning() {
  175. t.Fatalf("/bin/cat is still running after closing stdin")
  176. }
  177. })
  178. }
  179. // Expected behaviour: the process stay alive when the client disconnects
  180. // but the client detaches.
  181. func TestRunDisconnectTty(t *testing.T) {
  182. stdin, stdinPipe := io.Pipe()
  183. stdout, stdoutPipe := io.Pipe()
  184. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  185. defer cleanup(globalEngine, t)
  186. c1 := make(chan struct{})
  187. go func() {
  188. defer close(c1)
  189. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  190. // fact that CmdRun returns.
  191. if err := cli.CmdRun("-i", "-t", unitTestImageID, "/bin/cat"); err != nil {
  192. utils.Debugf("Error CmdRun: %s", err)
  193. }
  194. }()
  195. container := waitContainerStart(t, 10*time.Second)
  196. state := setRaw(t, container)
  197. defer unsetRaw(t, container, state)
  198. // Client disconnect after run -i should keep stdin out in TTY mode
  199. setTimeout(t, "Read/Write assertion timed out", 2*time.Second, func() {
  200. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  201. t.Fatal(err)
  202. }
  203. })
  204. // Close pipes (simulate disconnect)
  205. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  206. t.Fatal(err)
  207. }
  208. // wait for CmdRun to return
  209. setTimeout(t, "Waiting for CmdRun timed out", 5*time.Second, func() {
  210. <-c1
  211. })
  212. // In tty mode, we expect the process to stay alive even after client's stdin closes.
  213. // Give some time to monitor to do his thing
  214. container.State.WaitStop(500 * time.Millisecond)
  215. if !container.State.IsRunning() {
  216. t.Fatalf("/bin/cat should still be running after closing stdin (tty mode)")
  217. }
  218. }
  219. // TestRunDetach checks attaching and detaching with the escape sequence.
  220. func TestRunDetach(t *testing.T) {
  221. stdin, stdinPipe := io.Pipe()
  222. stdout, stdoutPipe := io.Pipe()
  223. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  224. defer cleanup(globalEngine, t)
  225. ch := make(chan struct{})
  226. go func() {
  227. defer close(ch)
  228. cli.CmdRun("-i", "-t", unitTestImageID, "cat")
  229. }()
  230. container := waitContainerStart(t, 10*time.Second)
  231. state := setRaw(t, container)
  232. defer unsetRaw(t, container, state)
  233. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  234. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  235. t.Fatal(err)
  236. }
  237. })
  238. setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
  239. stdinPipe.Write([]byte{16})
  240. time.Sleep(100 * time.Millisecond)
  241. stdinPipe.Write([]byte{17})
  242. })
  243. // wait for CmdRun to return
  244. setTimeout(t, "Waiting for CmdRun timed out", 15*time.Second, func() {
  245. <-ch
  246. })
  247. closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
  248. time.Sleep(500 * time.Millisecond)
  249. if !container.State.IsRunning() {
  250. t.Fatal("The detached container should be still running")
  251. }
  252. setTimeout(t, "Waiting for container to die timed out", 20*time.Second, func() {
  253. container.Kill()
  254. })
  255. }
  256. // TestAttachDetach checks that attach in tty mode can be detached using the long container ID
  257. func TestAttachDetach(t *testing.T) {
  258. stdin, stdinPipe := io.Pipe()
  259. stdout, stdoutPipe := io.Pipe()
  260. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  261. defer cleanup(globalEngine, t)
  262. ch := make(chan struct{})
  263. go func() {
  264. defer close(ch)
  265. if err := cli.CmdRun("-i", "-t", "-d", unitTestImageID, "cat"); err != nil {
  266. t.Fatal(err)
  267. }
  268. }()
  269. container := waitContainerStart(t, 10*time.Second)
  270. setTimeout(t, "Reading container's id timed out", 10*time.Second, func() {
  271. buf := make([]byte, 1024)
  272. n, err := stdout.Read(buf)
  273. if err != nil {
  274. t.Fatal(err)
  275. }
  276. if strings.Trim(string(buf[:n]), " \r\n") != container.ID {
  277. t.Fatalf("Wrong ID received. Expect %s, received %s", container.ID, buf[:n])
  278. }
  279. })
  280. setTimeout(t, "Starting container timed out", 10*time.Second, func() {
  281. <-ch
  282. })
  283. state := setRaw(t, container)
  284. defer unsetRaw(t, container, state)
  285. stdin, stdinPipe = io.Pipe()
  286. stdout, stdoutPipe = io.Pipe()
  287. cli = client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  288. ch = make(chan struct{})
  289. go func() {
  290. defer close(ch)
  291. if err := cli.CmdAttach(container.ID); err != nil {
  292. if err != io.ErrClosedPipe {
  293. t.Fatal(err)
  294. }
  295. }
  296. }()
  297. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  298. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  299. if err != io.ErrClosedPipe {
  300. t.Fatal(err)
  301. }
  302. }
  303. })
  304. setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
  305. stdinPipe.Write([]byte{16})
  306. time.Sleep(100 * time.Millisecond)
  307. stdinPipe.Write([]byte{17})
  308. })
  309. // wait for CmdRun to return
  310. setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
  311. <-ch
  312. })
  313. closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
  314. time.Sleep(500 * time.Millisecond)
  315. if !container.State.IsRunning() {
  316. t.Fatal("The detached container should be still running")
  317. }
  318. setTimeout(t, "Waiting for container to die timedout", 5*time.Second, func() {
  319. container.Kill()
  320. })
  321. }
  322. // TestAttachDetachTruncatedID checks that attach in tty mode can be detached
  323. func TestAttachDetachTruncatedID(t *testing.T) {
  324. stdin, stdinPipe := io.Pipe()
  325. stdout, stdoutPipe := io.Pipe()
  326. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  327. defer cleanup(globalEngine, t)
  328. // Discard the CmdRun output
  329. go stdout.Read(make([]byte, 1024))
  330. setTimeout(t, "Starting container timed out", 2*time.Second, func() {
  331. if err := cli.CmdRun("-i", "-t", "-d", unitTestImageID, "cat"); err != nil {
  332. t.Fatal(err)
  333. }
  334. })
  335. container := waitContainerStart(t, 10*time.Second)
  336. state := setRaw(t, container)
  337. defer unsetRaw(t, container, state)
  338. stdin, stdinPipe = io.Pipe()
  339. stdout, stdoutPipe = io.Pipe()
  340. cli = client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  341. ch := make(chan struct{})
  342. go func() {
  343. defer close(ch)
  344. if err := cli.CmdAttach(utils.TruncateID(container.ID)); err != nil {
  345. if err != io.ErrClosedPipe {
  346. t.Fatal(err)
  347. }
  348. }
  349. }()
  350. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  351. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  352. if err != io.ErrClosedPipe {
  353. t.Fatal(err)
  354. }
  355. }
  356. })
  357. setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
  358. stdinPipe.Write([]byte{16})
  359. time.Sleep(100 * time.Millisecond)
  360. stdinPipe.Write([]byte{17})
  361. })
  362. // wait for CmdRun to return
  363. setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
  364. <-ch
  365. })
  366. closeWrap(stdin, stdinPipe, stdout, stdoutPipe)
  367. time.Sleep(500 * time.Millisecond)
  368. if !container.State.IsRunning() {
  369. t.Fatal("The detached container should be still running")
  370. }
  371. setTimeout(t, "Waiting for container to die timedout", 5*time.Second, func() {
  372. container.Kill()
  373. })
  374. }
  375. // Expected behaviour, the process stays alive when the client disconnects
  376. func TestAttachDisconnect(t *testing.T) {
  377. stdin, stdinPipe := io.Pipe()
  378. stdout, stdoutPipe := io.Pipe()
  379. cli := client.NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  380. defer cleanup(globalEngine, t)
  381. go func() {
  382. // Start a process in daemon mode
  383. if err := cli.CmdRun("-d", "-i", unitTestImageID, "/bin/cat"); err != nil {
  384. utils.Debugf("Error CmdRun: %s", err)
  385. }
  386. }()
  387. setTimeout(t, "Waiting for CmdRun timed out", 10*time.Second, func() {
  388. if _, err := bufio.NewReader(stdout).ReadString('\n'); err != nil {
  389. t.Fatal(err)
  390. }
  391. })
  392. setTimeout(t, "Waiting for the container to be started timed out", 10*time.Second, func() {
  393. for {
  394. l := globalDaemon.List()
  395. if len(l) == 1 && l[0].State.IsRunning() {
  396. break
  397. }
  398. time.Sleep(10 * time.Millisecond)
  399. }
  400. })
  401. container := globalDaemon.List()[0]
  402. // Attach to it
  403. c1 := make(chan struct{})
  404. go func() {
  405. // We're simulating a disconnect so the return value doesn't matter. What matters is the
  406. // fact that CmdAttach returns.
  407. cli.CmdAttach(container.ID)
  408. close(c1)
  409. }()
  410. setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
  411. if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 150); err != nil {
  412. t.Fatal(err)
  413. }
  414. })
  415. // Close pipes (client disconnects)
  416. if err := closeWrap(stdin, stdinPipe, stdout, stdoutPipe); err != nil {
  417. t.Fatal(err)
  418. }
  419. // Wait for attach to finish, the client disconnected, therefore, Attach finished his job
  420. setTimeout(t, "Waiting for CmdAttach timed out", 2*time.Second, func() {
  421. <-c1
  422. })
  423. // We closed stdin, expect /bin/cat to still be running
  424. // Wait a little bit to make sure container.monitor() did his thing
  425. _, err := container.State.WaitStop(500 * time.Millisecond)
  426. if err == nil || !container.State.IsRunning() {
  427. t.Fatalf("/bin/cat is not running after closing stdin")
  428. }
  429. // Try to avoid the timeout in destroy. Best effort, don't check error
  430. cStdin, _ := container.StdinPipe()
  431. cStdin.Close()
  432. container.State.WaitStop(-1 * time.Second)
  433. }
  434. // Expected behaviour: container gets deleted automatically after exit
  435. func TestRunAutoRemove(t *testing.T) {
  436. t.Skip("Fixme. Skipping test for now, race condition")
  437. stdout, stdoutPipe := io.Pipe()
  438. cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  439. defer cleanup(globalEngine, t)
  440. c := make(chan struct{})
  441. go func() {
  442. defer close(c)
  443. if err := cli.CmdRun("--rm", unitTestImageID, "hostname"); err != nil {
  444. t.Fatal(err)
  445. }
  446. }()
  447. var temporaryContainerID string
  448. setTimeout(t, "Reading command output time out", 2*time.Second, func() {
  449. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  450. if err != nil {
  451. t.Fatal(err)
  452. }
  453. temporaryContainerID = cmdOutput
  454. if err := closeWrap(stdout, stdoutPipe); err != nil {
  455. t.Fatal(err)
  456. }
  457. })
  458. setTimeout(t, "CmdRun timed out", 10*time.Second, func() {
  459. <-c
  460. })
  461. time.Sleep(500 * time.Millisecond)
  462. if len(globalDaemon.List()) > 0 {
  463. t.Fatalf("failed to remove container automatically: container %s still exists", temporaryContainerID)
  464. }
  465. }
  466. // Expected behaviour: error out when attempting to bind mount non-existing source paths
  467. func TestRunErrorBindNonExistingSource(t *testing.T) {
  468. cli := client.NewDockerCli(nil, nil, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  469. defer cleanup(globalEngine, t)
  470. c := make(chan struct{})
  471. go func() {
  472. defer close(c)
  473. // This check is made at runtime, can't be "unit tested"
  474. if err := cli.CmdRun("-v", "/i/dont/exist:/tmp", unitTestImageID, "echo 'should fail'"); err == nil {
  475. t.Fatal("should have failed to run when using /i/dont/exist as a source for the bind mount")
  476. }
  477. }()
  478. setTimeout(t, "CmdRun timed out", 5*time.Second, func() {
  479. <-c
  480. })
  481. }
  482. // #2098 - Docker cidFiles only contain short version of the containerId
  483. //sudo docker run --cidfile /tmp/docker_test.cid ubuntu echo "test"
  484. // TestRunCidFile tests that run --cidfile returns the longid
  485. func TestRunCidFileCheckIDLength(t *testing.T) {
  486. stdout, stdoutPipe := io.Pipe()
  487. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  488. if err != nil {
  489. t.Fatal(err)
  490. }
  491. tmpCidFile := path.Join(tmpDir, "cid")
  492. cli := client.NewDockerCli(nil, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  493. defer cleanup(globalEngine, t)
  494. c := make(chan struct{})
  495. go func() {
  496. defer close(c)
  497. if err := cli.CmdRun("--cidfile", tmpCidFile, unitTestImageID, "ls"); err != nil {
  498. t.Fatal(err)
  499. }
  500. }()
  501. defer os.RemoveAll(tmpDir)
  502. setTimeout(t, "Reading command output time out", 2*time.Second, func() {
  503. cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
  504. if err != nil {
  505. t.Fatal(err)
  506. }
  507. if len(cmdOutput) < 1 {
  508. t.Fatalf("'ls' should return something , not '%s'", cmdOutput)
  509. }
  510. //read the tmpCidFile
  511. buffer, err := ioutil.ReadFile(tmpCidFile)
  512. if err != nil {
  513. t.Fatal(err)
  514. }
  515. id := string(buffer)
  516. if len(id) != len("2bf44ea18873287bd9ace8a4cb536a7cbe134bed67e805fdf2f58a57f69b320c") {
  517. t.Fatalf("--cidfile should be a long id, not '%s'", id)
  518. }
  519. //test that its a valid cid? (though the container is gone..)
  520. //remove the file and dir.
  521. })
  522. setTimeout(t, "CmdRun timed out", 5*time.Second, func() {
  523. <-c
  524. })
  525. }
  526. // Ensure that CIDFile gets deleted if it's empty
  527. // Perform this test by making `docker run` fail
  528. func TestRunCidFileCleanupIfEmpty(t *testing.T) {
  529. tmpDir, err := ioutil.TempDir("", "TestRunCidFile")
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. tmpCidFile := path.Join(tmpDir, "cid")
  534. cli := client.NewDockerCli(nil, ioutil.Discard, ioutil.Discard, testDaemonProto, testDaemonAddr, nil)
  535. defer cleanup(globalEngine, t)
  536. c := make(chan struct{})
  537. go func() {
  538. defer close(c)
  539. if err := cli.CmdRun("--cidfile", tmpCidFile, unitTestImageID); err == nil {
  540. t.Fatal("running without a command should haveve failed")
  541. }
  542. if _, err := os.Stat(tmpCidFile); err == nil {
  543. t.Fatalf("empty CIDFile '%s' should've been deleted", tmpCidFile)
  544. }
  545. }()
  546. defer os.RemoveAll(tmpDir)
  547. setTimeout(t, "CmdRun timed out", 5*time.Second, func() {
  548. <-c
  549. })
  550. }