container_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. package docker
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path"
  8. "strings"
  9. "testing"
  10. "time"
  11. "github.com/dotcloud/docker/runconfig"
  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.State.WaitStop(-1 * time.Second)
  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 TestRestartStdin(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{"cat"},
  70. OpenStdin: true,
  71. },
  72. "",
  73. )
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. defer daemon.Destroy(container)
  78. stdin, err := container.StdinPipe()
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. stdout, err := container.StdoutPipe()
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if err := container.Start(); err != nil {
  87. t.Fatal(err)
  88. }
  89. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  90. t.Fatal(err)
  91. }
  92. if err := stdin.Close(); err != nil {
  93. t.Fatal(err)
  94. }
  95. container.State.WaitStop(-1 * time.Second)
  96. output, err := ioutil.ReadAll(stdout)
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. if err := stdout.Close(); err != nil {
  101. t.Fatal(err)
  102. }
  103. if string(output) != "hello world" {
  104. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  105. }
  106. // Restart and try again
  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 #2"); err != nil {
  119. t.Fatal(err)
  120. }
  121. if err := stdin.Close(); err != nil {
  122. t.Fatal(err)
  123. }
  124. container.State.WaitStop(-1 * time.Second)
  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 #2" {
  133. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  134. }
  135. }
  136. func TestStdin(t *testing.T) {
  137. daemon := mkDaemon(t)
  138. defer nuke(daemon)
  139. container, _, err := daemon.Create(&runconfig.Config{
  140. Image: GetTestImage(daemon).ID,
  141. Cmd: []string{"cat"},
  142. OpenStdin: true,
  143. },
  144. "",
  145. )
  146. if err != nil {
  147. t.Fatal(err)
  148. }
  149. defer daemon.Destroy(container)
  150. stdin, err := container.StdinPipe()
  151. if err != nil {
  152. t.Fatal(err)
  153. }
  154. stdout, err := container.StdoutPipe()
  155. if err != nil {
  156. t.Fatal(err)
  157. }
  158. if err := container.Start(); err != nil {
  159. t.Fatal(err)
  160. }
  161. defer stdin.Close()
  162. defer stdout.Close()
  163. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  164. t.Fatal(err)
  165. }
  166. if err := stdin.Close(); err != nil {
  167. t.Fatal(err)
  168. }
  169. container.State.WaitStop(-1 * time.Second)
  170. output, err := ioutil.ReadAll(stdout)
  171. if err != nil {
  172. t.Fatal(err)
  173. }
  174. if string(output) != "hello world" {
  175. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  176. }
  177. }
  178. func TestTty(t *testing.T) {
  179. daemon := mkDaemon(t)
  180. defer nuke(daemon)
  181. container, _, err := daemon.Create(&runconfig.Config{
  182. Image: GetTestImage(daemon).ID,
  183. Cmd: []string{"cat"},
  184. OpenStdin: true,
  185. },
  186. "",
  187. )
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. defer daemon.Destroy(container)
  192. stdin, err := container.StdinPipe()
  193. if err != nil {
  194. t.Fatal(err)
  195. }
  196. stdout, err := container.StdoutPipe()
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. if err := container.Start(); err != nil {
  201. t.Fatal(err)
  202. }
  203. defer stdin.Close()
  204. defer stdout.Close()
  205. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  206. t.Fatal(err)
  207. }
  208. if err := stdin.Close(); err != nil {
  209. t.Fatal(err)
  210. }
  211. container.State.WaitStop(-1 * time.Second)
  212. output, err := ioutil.ReadAll(stdout)
  213. if err != nil {
  214. t.Fatal(err)
  215. }
  216. if string(output) != "hello world" {
  217. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  218. }
  219. }
  220. func TestEntrypoint(t *testing.T) {
  221. daemon := mkDaemon(t)
  222. defer nuke(daemon)
  223. container, _, err := daemon.Create(
  224. &runconfig.Config{
  225. Image: GetTestImage(daemon).ID,
  226. Entrypoint: []string{"/bin/echo"},
  227. Cmd: []string{"-n", "foobar"},
  228. },
  229. "",
  230. )
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. defer daemon.Destroy(container)
  235. output, err := container.Output()
  236. if err != nil {
  237. t.Fatal(err)
  238. }
  239. if string(output) != "foobar" {
  240. t.Error(string(output))
  241. }
  242. }
  243. func TestEntrypointNoCmd(t *testing.T) {
  244. daemon := mkDaemon(t)
  245. defer nuke(daemon)
  246. container, _, err := daemon.Create(
  247. &runconfig.Config{
  248. Image: GetTestImage(daemon).ID,
  249. Entrypoint: []string{"/bin/echo", "foobar"},
  250. },
  251. "",
  252. )
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. defer daemon.Destroy(container)
  257. output, err := container.Output()
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. if strings.Trim(string(output), "\r\n") != "foobar" {
  262. t.Error(string(output))
  263. }
  264. }
  265. func BenchmarkRunSequential(b *testing.B) {
  266. daemon := mkDaemon(b)
  267. defer nuke(daemon)
  268. for i := 0; i < b.N; i++ {
  269. container, _, err := daemon.Create(&runconfig.Config{
  270. Image: GetTestImage(daemon).ID,
  271. Cmd: []string{"echo", "-n", "foo"},
  272. },
  273. "",
  274. )
  275. if err != nil {
  276. b.Fatal(err)
  277. }
  278. defer daemon.Destroy(container)
  279. output, err := container.Output()
  280. if err != nil {
  281. b.Fatal(err)
  282. }
  283. if string(output) != "foo" {
  284. b.Fatalf("Unexpected output: %s", output)
  285. }
  286. if err := daemon.Destroy(container); err != nil {
  287. b.Fatal(err)
  288. }
  289. }
  290. }
  291. func BenchmarkRunParallel(b *testing.B) {
  292. daemon := mkDaemon(b)
  293. defer nuke(daemon)
  294. var tasks []chan error
  295. for i := 0; i < b.N; i++ {
  296. complete := make(chan error)
  297. tasks = append(tasks, complete)
  298. go func(i int, complete chan error) {
  299. container, _, err := daemon.Create(&runconfig.Config{
  300. Image: GetTestImage(daemon).ID,
  301. Cmd: []string{"echo", "-n", "foo"},
  302. },
  303. "",
  304. )
  305. if err != nil {
  306. complete <- err
  307. return
  308. }
  309. defer daemon.Destroy(container)
  310. if err := container.Start(); err != nil {
  311. complete <- err
  312. return
  313. }
  314. if _, err := container.State.WaitStop(15 * time.Second); err != nil {
  315. complete <- err
  316. return
  317. }
  318. // if string(output) != "foo" {
  319. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  320. // }
  321. if err := daemon.Destroy(container); err != nil {
  322. complete <- err
  323. return
  324. }
  325. complete <- nil
  326. }(i, complete)
  327. }
  328. var errors []error
  329. for _, task := range tasks {
  330. err := <-task
  331. if err != nil {
  332. errors = append(errors, err)
  333. }
  334. }
  335. if len(errors) > 0 {
  336. b.Fatal(errors)
  337. }
  338. }
  339. func tempDir(t *testing.T) string {
  340. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  341. if err != nil {
  342. t.Fatal(err)
  343. }
  344. return tmpDir
  345. }
  346. // Test for #1737
  347. func TestCopyVolumeUidGid(t *testing.T) {
  348. eng := NewTestEngine(t)
  349. r := mkDaemonFromEngine(eng, t)
  350. defer r.Nuke()
  351. // Add directory not owned by root
  352. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test && chown daemon.daemon /hello"}, t)
  353. defer r.Destroy(container1)
  354. if container1.State.IsRunning() {
  355. t.Errorf("Container shouldn't be running")
  356. }
  357. if err := container1.Run(); err != nil {
  358. t.Fatal(err)
  359. }
  360. if container1.State.IsRunning() {
  361. t.Errorf("Container shouldn't be running")
  362. }
  363. img, err := r.Commit(container1, "", "", "unit test commited image", "", true, nil)
  364. if err != nil {
  365. t.Error(err)
  366. }
  367. // Test that the uid and gid is copied from the image to the volume
  368. tmpDir1 := tempDir(t)
  369. defer os.RemoveAll(tmpDir1)
  370. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  371. if !strings.Contains(stdout1, "daemon daemon") {
  372. t.Fatal("Container failed to transfer uid and gid to volume")
  373. }
  374. container2, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && chown daemon.daemon /hello"}, t)
  375. defer r.Destroy(container1)
  376. if container2.State.IsRunning() {
  377. t.Errorf("Container shouldn't be running")
  378. }
  379. if err := container2.Run(); err != nil {
  380. t.Fatal(err)
  381. }
  382. if container2.State.IsRunning() {
  383. t.Errorf("Container shouldn't be running")
  384. }
  385. img2, err := r.Commit(container2, "", "", "unit test commited image", "", true, nil)
  386. if err != nil {
  387. t.Error(err)
  388. }
  389. // Test that the uid and gid is copied from the image to the volume
  390. tmpDir2 := tempDir(t)
  391. defer os.RemoveAll(tmpDir2)
  392. stdout2, _ := runContainer(eng, r, []string{"-v", "/hello", img2.ID, "stat", "-c", "%U %G", "/hello"}, t)
  393. if !strings.Contains(stdout2, "daemon daemon") {
  394. t.Fatal("Container failed to transfer uid and gid to volume")
  395. }
  396. }
  397. // Test for #1582
  398. func TestCopyVolumeContent(t *testing.T) {
  399. eng := NewTestEngine(t)
  400. r := mkDaemonFromEngine(eng, t)
  401. defer r.Nuke()
  402. // Put some content in a directory of a container and commit it
  403. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  404. defer r.Destroy(container1)
  405. if container1.State.IsRunning() {
  406. t.Errorf("Container shouldn't be running")
  407. }
  408. if err := container1.Run(); err != nil {
  409. t.Fatal(err)
  410. }
  411. if container1.State.IsRunning() {
  412. t.Errorf("Container shouldn't be running")
  413. }
  414. img, err := r.Commit(container1, "", "", "unit test commited image", "", true, nil)
  415. if err != nil {
  416. t.Error(err)
  417. }
  418. // Test that the content is copied from the image to the volume
  419. tmpDir1 := tempDir(t)
  420. defer os.RemoveAll(tmpDir1)
  421. stdout1, _ := runContainer(eng, r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  422. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  423. t.Fatal("Container failed to transfer content to volume")
  424. }
  425. }
  426. func TestBindMounts(t *testing.T) {
  427. eng := NewTestEngine(t)
  428. r := mkDaemonFromEngine(eng, t)
  429. defer r.Nuke()
  430. tmpDir := tempDir(t)
  431. defer os.RemoveAll(tmpDir)
  432. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  433. // Test reading from a read-only bind mount
  434. stdout, _ := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  435. if !strings.Contains(stdout, "touch-me") {
  436. t.Fatal("Container failed to read from bind mount")
  437. }
  438. // test writing to bind mount
  439. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  440. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  441. // test mounting to an illegal destination directory
  442. if _, err := runContainer(eng, r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  443. t.Fatal("Container bind mounted illegal directory")
  444. }
  445. // test mount a file
  446. runContainer(eng, r, []string{"-v", fmt.Sprintf("%s/holla:/tmp/holla:rw", tmpDir), "_", "sh", "-c", "echo -n 'yotta' > /tmp/holla"}, t)
  447. content := readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  448. if content != "yotta" {
  449. t.Fatal("Container failed to write to bind mount file")
  450. }
  451. }