container_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. package docker
  2. import (
  3. "fmt"
  4. "github.com/dotcloud/docker/runconfig"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path"
  9. "strings"
  10. "testing"
  11. "time"
  12. )
  13. func TestKillDifferentUser(t *testing.T) {
  14. daemon := mkDaemon(t)
  15. defer nuke(daemon)
  16. container, _, err := daemon.Create(&runconfig.Config{
  17. Image: GetTestImage(daemon).ID,
  18. Cmd: []string{"cat"},
  19. OpenStdin: true,
  20. User: "daemon",
  21. },
  22. "",
  23. )
  24. if err != nil {
  25. t.Fatal(err)
  26. }
  27. defer daemon.Destroy(container)
  28. // FIXME @shykes: this seems redundant, but is very old, I'm leaving it in case
  29. // there is a side effect I'm not seeing.
  30. // defer container.stdin.Close()
  31. if container.State.IsRunning() {
  32. t.Errorf("Container shouldn't be running")
  33. }
  34. if err := container.Start(); err != nil {
  35. t.Fatal(err)
  36. }
  37. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  38. for !container.State.IsRunning() {
  39. time.Sleep(10 * time.Millisecond)
  40. }
  41. })
  42. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  43. out, _ := container.StdoutPipe()
  44. in, _ := container.StdinPipe()
  45. if err := assertPipe("hello\n", "hello", out, in, 150); err != nil {
  46. t.Fatal(err)
  47. }
  48. })
  49. if err := container.Kill(); err != nil {
  50. t.Fatal(err)
  51. }
  52. if container.State.IsRunning() {
  53. t.Errorf("Container shouldn't be running")
  54. }
  55. container.Wait()
  56. if container.State.IsRunning() {
  57. t.Errorf("Container shouldn't be running")
  58. }
  59. // Try stopping twice
  60. if err := container.Kill(); err != nil {
  61. t.Fatal(err)
  62. }
  63. }
  64. func TestRestart(t *testing.T) {
  65. daemon := mkDaemon(t)
  66. defer nuke(daemon)
  67. container, _, err := daemon.Create(&runconfig.Config{
  68. Image: GetTestImage(daemon).ID,
  69. Cmd: []string{"echo", "-n", "foobar"},
  70. },
  71. "",
  72. )
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. defer daemon.Destroy(container)
  77. output, err := container.Output()
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. if string(output) != "foobar" {
  82. t.Error(string(output))
  83. }
  84. // Run the container again and check the output
  85. output, err = container.Output()
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. if string(output) != "foobar" {
  90. t.Error(string(output))
  91. }
  92. }
  93. func TestRestartStdin(t *testing.T) {
  94. daemon := mkDaemon(t)
  95. defer nuke(daemon)
  96. container, _, err := daemon.Create(&runconfig.Config{
  97. Image: GetTestImage(daemon).ID,
  98. Cmd: []string{"cat"},
  99. OpenStdin: true,
  100. },
  101. "",
  102. )
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. defer daemon.Destroy(container)
  107. stdin, err := container.StdinPipe()
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. stdout, err := container.StdoutPipe()
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. if err := container.Start(); err != nil {
  116. t.Fatal(err)
  117. }
  118. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  119. t.Fatal(err)
  120. }
  121. if err := stdin.Close(); err != nil {
  122. t.Fatal(err)
  123. }
  124. container.Wait()
  125. output, err := ioutil.ReadAll(stdout)
  126. if err != nil {
  127. t.Fatal(err)
  128. }
  129. if err := stdout.Close(); err != nil {
  130. t.Fatal(err)
  131. }
  132. if string(output) != "hello world" {
  133. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  134. }
  135. // Restart and try again
  136. stdin, err = container.StdinPipe()
  137. if err != nil {
  138. t.Fatal(err)
  139. }
  140. stdout, err = container.StdoutPipe()
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. if err := container.Start(); err != nil {
  145. t.Fatal(err)
  146. }
  147. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  148. t.Fatal(err)
  149. }
  150. if err := stdin.Close(); err != nil {
  151. t.Fatal(err)
  152. }
  153. container.Wait()
  154. output, err = ioutil.ReadAll(stdout)
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. if err := stdout.Close(); err != nil {
  159. t.Fatal(err)
  160. }
  161. if string(output) != "hello world #2" {
  162. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  163. }
  164. }
  165. func TestStdin(t *testing.T) {
  166. daemon := mkDaemon(t)
  167. defer nuke(daemon)
  168. container, _, err := daemon.Create(&runconfig.Config{
  169. Image: GetTestImage(daemon).ID,
  170. Cmd: []string{"cat"},
  171. OpenStdin: true,
  172. },
  173. "",
  174. )
  175. if err != nil {
  176. t.Fatal(err)
  177. }
  178. defer daemon.Destroy(container)
  179. stdin, err := container.StdinPipe()
  180. if err != nil {
  181. t.Fatal(err)
  182. }
  183. stdout, err := container.StdoutPipe()
  184. if err != nil {
  185. t.Fatal(err)
  186. }
  187. if err := container.Start(); err != nil {
  188. t.Fatal(err)
  189. }
  190. defer stdin.Close()
  191. defer stdout.Close()
  192. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  193. t.Fatal(err)
  194. }
  195. if err := stdin.Close(); err != nil {
  196. t.Fatal(err)
  197. }
  198. container.Wait()
  199. output, err := ioutil.ReadAll(stdout)
  200. if err != nil {
  201. t.Fatal(err)
  202. }
  203. if string(output) != "hello world" {
  204. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  205. }
  206. }
  207. func TestTty(t *testing.T) {
  208. daemon := mkDaemon(t)
  209. defer nuke(daemon)
  210. container, _, err := daemon.Create(&runconfig.Config{
  211. Image: GetTestImage(daemon).ID,
  212. Cmd: []string{"cat"},
  213. OpenStdin: true,
  214. },
  215. "",
  216. )
  217. if err != nil {
  218. t.Fatal(err)
  219. }
  220. defer daemon.Destroy(container)
  221. stdin, err := container.StdinPipe()
  222. if err != nil {
  223. t.Fatal(err)
  224. }
  225. stdout, err := container.StdoutPipe()
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. if err := container.Start(); err != nil {
  230. t.Fatal(err)
  231. }
  232. defer stdin.Close()
  233. defer stdout.Close()
  234. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  235. t.Fatal(err)
  236. }
  237. if err := stdin.Close(); err != nil {
  238. t.Fatal(err)
  239. }
  240. container.Wait()
  241. output, err := ioutil.ReadAll(stdout)
  242. if err != nil {
  243. t.Fatal(err)
  244. }
  245. if string(output) != "hello world" {
  246. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  247. }
  248. }
  249. func TestEntrypoint(t *testing.T) {
  250. daemon := mkDaemon(t)
  251. defer nuke(daemon)
  252. container, _, err := daemon.Create(
  253. &runconfig.Config{
  254. Image: GetTestImage(daemon).ID,
  255. Entrypoint: []string{"/bin/echo"},
  256. Cmd: []string{"-n", "foobar"},
  257. },
  258. "",
  259. )
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. defer daemon.Destroy(container)
  264. output, err := container.Output()
  265. if err != nil {
  266. t.Fatal(err)
  267. }
  268. if string(output) != "foobar" {
  269. t.Error(string(output))
  270. }
  271. }
  272. func TestEntrypointNoCmd(t *testing.T) {
  273. daemon := mkDaemon(t)
  274. defer nuke(daemon)
  275. container, _, err := daemon.Create(
  276. &runconfig.Config{
  277. Image: GetTestImage(daemon).ID,
  278. Entrypoint: []string{"/bin/echo", "foobar"},
  279. },
  280. "",
  281. )
  282. if err != nil {
  283. t.Fatal(err)
  284. }
  285. defer daemon.Destroy(container)
  286. output, err := container.Output()
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if strings.Trim(string(output), "\r\n") != "foobar" {
  291. t.Error(string(output))
  292. }
  293. }
  294. func BenchmarkRunSequential(b *testing.B) {
  295. daemon := mkDaemon(b)
  296. defer nuke(daemon)
  297. for i := 0; i < b.N; i++ {
  298. container, _, err := daemon.Create(&runconfig.Config{
  299. Image: GetTestImage(daemon).ID,
  300. Cmd: []string{"echo", "-n", "foo"},
  301. },
  302. "",
  303. )
  304. if err != nil {
  305. b.Fatal(err)
  306. }
  307. defer daemon.Destroy(container)
  308. output, err := container.Output()
  309. if err != nil {
  310. b.Fatal(err)
  311. }
  312. if string(output) != "foo" {
  313. b.Fatalf("Unexpected output: %s", output)
  314. }
  315. if err := daemon.Destroy(container); err != nil {
  316. b.Fatal(err)
  317. }
  318. }
  319. }
  320. func BenchmarkRunParallel(b *testing.B) {
  321. daemon := mkDaemon(b)
  322. defer nuke(daemon)
  323. var tasks []chan error
  324. for i := 0; i < b.N; i++ {
  325. complete := make(chan error)
  326. tasks = append(tasks, complete)
  327. go func(i int, complete chan error) {
  328. container, _, err := daemon.Create(&runconfig.Config{
  329. Image: GetTestImage(daemon).ID,
  330. Cmd: []string{"echo", "-n", "foo"},
  331. },
  332. "",
  333. )
  334. if err != nil {
  335. complete <- err
  336. return
  337. }
  338. defer daemon.Destroy(container)
  339. if err := container.Start(); err != nil {
  340. complete <- err
  341. return
  342. }
  343. if err := container.WaitTimeout(15 * time.Second); err != nil {
  344. complete <- err
  345. return
  346. }
  347. // if string(output) != "foo" {
  348. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  349. // }
  350. if err := daemon.Destroy(container); err != nil {
  351. complete <- err
  352. return
  353. }
  354. complete <- nil
  355. }(i, complete)
  356. }
  357. var errors []error
  358. for _, task := range tasks {
  359. err := <-task
  360. if err != nil {
  361. errors = append(errors, err)
  362. }
  363. }
  364. if len(errors) > 0 {
  365. b.Fatal(errors)
  366. }
  367. }
  368. func tempDir(t *testing.T) string {
  369. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. return tmpDir
  374. }
  375. // Test for #1737
  376. func TestCopyVolumeUidGid(t *testing.T) {
  377. eng := NewTestEngine(t)
  378. r := mkDaemonFromEngine(eng, t)
  379. defer r.Nuke()
  380. // Add directory not owned by root
  381. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
  382. defer r.Destroy(container1)
  383. if container1.State.IsRunning() {
  384. t.Errorf("Container shouldn't be running")
  385. }
  386. if err := container1.Run(); err != nil {
  387. t.Fatal(err)
  388. }
  389. if container1.State.IsRunning() {
  390. t.Errorf("Container shouldn't be running")
  391. }
  392. img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
  393. if err != nil {
  394. t.Error(err)
  395. }
  396. // Test that the uid and gid is copied from the image to the volume
  397. tmpDir1 := tempDir(t)
  398. defer os.RemoveAll(tmpDir1)
  399. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  400. if !strings.Contains(stdout1, "daemon daemon") {
  401. t.Fatal("Container failed to transfer uid and gid to volume")
  402. }
  403. }
  404. // Test for #1582
  405. func TestCopyVolumeContent(t *testing.T) {
  406. eng := NewTestEngine(t)
  407. r := mkDaemonFromEngine(eng, t)
  408. defer r.Nuke()
  409. // Put some content in a directory of a container and commit it
  410. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  411. defer r.Destroy(container1)
  412. if container1.State.IsRunning() {
  413. t.Errorf("Container shouldn't be running")
  414. }
  415. if err := container1.Run(); err != nil {
  416. t.Fatal(err)
  417. }
  418. if container1.State.IsRunning() {
  419. t.Errorf("Container shouldn't be running")
  420. }
  421. img, err := r.Commit(container1, "", "", "unit test commited image", "", nil)
  422. if err != nil {
  423. t.Error(err)
  424. }
  425. // Test that the content is copied from the image to the volume
  426. tmpDir1 := tempDir(t)
  427. defer os.RemoveAll(tmpDir1)
  428. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  429. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  430. t.Fatal("Container failed to transfer content to volume")
  431. }
  432. }
  433. func TestBindMounts(t *testing.T) {
  434. eng := NewTestEngine(t)
  435. r := mkDaemonFromEngine(eng, t)
  436. defer r.Nuke()
  437. tmpDir := tempDir(t)
  438. defer os.RemoveAll(tmpDir)
  439. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  440. // Test reading from a read-only bind mount
  441. stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  442. if !strings.Contains(stdout, "touch-me") {
  443. t.Fatal("Container failed to read from bind mount")
  444. }
  445. // test writing to bind mount
  446. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  447. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  448. // test mounting to an illegal destination directory
  449. if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  450. t.Fatal("Container bind mounted illegal directory")
  451. }
  452. // test mount a file
  453. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
  454. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  455. if content != "yotta" {
  456. t.Fatal("Container failed to write to bind mount file")
  457. }
  458. }
  459. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  460. func TestRestartWithVolumes(t *testing.T) {
  461. daemon := mkDaemon(t)
  462. defer nuke(daemon)
  463. container, _, err := daemon.Create(&runconfig.Config{
  464. Image: GetTestImage(daemon).ID,
  465. Cmd: []string{"echo", "-n", "foobar"},
  466. Volumes: map[string]struct{}{"/test": {}},
  467. },
  468. "",
  469. )
  470. if err != nil {
  471. t.Fatal(err)
  472. }
  473. defer daemon.Destroy(container)
  474. for key := range container.Config.Volumes {
  475. if key != "/test" {
  476. t.Fail()
  477. }
  478. }
  479. _, err = container.Output()
  480. if err != nil {
  481. t.Fatal(err)
  482. }
  483. expected := container.Volumes["/test"]
  484. if expected == "" {
  485. t.Fail()
  486. }
  487. // Run the container again to verify the volume path persists
  488. _, err = container.Output()
  489. if err != nil {
  490. t.Fatal(err)
  491. }
  492. actual := container.Volumes["/test"]
  493. if expected != actual {
  494. t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
  495. }
  496. }