container_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. package docker
  2. import (
  3. "./fs"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "sort"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestCommitRun(t *testing.T) {
  13. docker, err := newTestDocker()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. defer nuke(docker)
  18. container1, err := docker.Create(
  19. "precommit_test",
  20. "/bin/sh",
  21. []string{"-c", "echo hello > /world"},
  22. GetTestImage(docker),
  23. &Config{
  24. Ram: 33554432,
  25. },
  26. )
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. defer docker.Destroy(container1)
  31. if container1.State.Running {
  32. t.Errorf("Container shouldn't be running")
  33. }
  34. if err := container1.Run(); err != nil {
  35. t.Fatal(err)
  36. }
  37. if container1.State.Running {
  38. t.Errorf("Container shouldn't be running")
  39. }
  40. // FIXME: freeze the container before copying it to avoid data corruption?
  41. rwTar, err := fs.Tar(container1.Mountpoint.Rw, fs.Uncompressed)
  42. if err != nil {
  43. t.Error(err)
  44. }
  45. // Create a new image from the container's base layers + a new layer from container changes
  46. parentImg, err := docker.Store.Get(container1.Image)
  47. if err != nil {
  48. t.Error(err)
  49. }
  50. img, err := docker.Store.Create(rwTar, parentImg, "test_commitrun", "unit test commited image")
  51. if err != nil {
  52. t.Error(err)
  53. }
  54. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  55. container2, err := docker.Create(
  56. "postcommit_test",
  57. "cat",
  58. []string{"/world"},
  59. img,
  60. &Config{
  61. Ram: 33554432,
  62. },
  63. )
  64. if err != nil {
  65. t.Fatal(err)
  66. }
  67. defer docker.Destroy(container2)
  68. stdout, err := container2.StdoutPipe()
  69. stderr, err := container2.StderrPipe()
  70. if err := container2.Start(); err != nil {
  71. t.Fatal(err)
  72. }
  73. container2.Wait()
  74. output, err := ioutil.ReadAll(stdout)
  75. output2, err := ioutil.ReadAll(stderr)
  76. stdout.Close()
  77. stderr.Close()
  78. if string(output) != "hello\n" {
  79. t.Fatalf("\nout: %s\nerr: %s\n", string(output), string(output2))
  80. }
  81. }
  82. func TestRun(t *testing.T) {
  83. docker, err := newTestDocker()
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. defer nuke(docker)
  88. container, err := docker.Create(
  89. "run_test",
  90. "ls",
  91. []string{"-al"},
  92. GetTestImage(docker),
  93. &Config{
  94. Ram: 33554432,
  95. },
  96. )
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. defer docker.Destroy(container)
  101. if container.State.Running {
  102. t.Errorf("Container shouldn't be running")
  103. }
  104. if err := container.Run(); err != nil {
  105. t.Fatal(err)
  106. }
  107. if container.State.Running {
  108. t.Errorf("Container shouldn't be running")
  109. }
  110. }
  111. func TestOutput(t *testing.T) {
  112. docker, err := newTestDocker()
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. defer nuke(docker)
  117. container, err := docker.Create(
  118. "output_test",
  119. "echo",
  120. []string{"-n", "foobar"},
  121. GetTestImage(docker),
  122. &Config{},
  123. )
  124. if err != nil {
  125. t.Fatal(err)
  126. }
  127. defer docker.Destroy(container)
  128. output, err := container.Output()
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if string(output) != "foobar" {
  133. t.Error(string(output))
  134. }
  135. }
  136. func TestKill(t *testing.T) {
  137. docker, err := newTestDocker()
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. defer nuke(docker)
  142. container, err := docker.Create(
  143. "stop_test",
  144. "cat",
  145. []string{"/dev/zero"},
  146. GetTestImage(docker),
  147. &Config{},
  148. )
  149. if err != nil {
  150. t.Fatal(err)
  151. }
  152. defer docker.Destroy(container)
  153. if container.State.Running {
  154. t.Errorf("Container shouldn't be running")
  155. }
  156. if err := container.Start(); err != nil {
  157. t.Fatal(err)
  158. }
  159. if !container.State.Running {
  160. t.Errorf("Container should be running")
  161. }
  162. if err := container.Kill(); err != nil {
  163. t.Fatal(err)
  164. }
  165. if container.State.Running {
  166. t.Errorf("Container shouldn't be running")
  167. }
  168. container.Wait()
  169. if container.State.Running {
  170. t.Errorf("Container shouldn't be running")
  171. }
  172. // Try stopping twice
  173. if err := container.Kill(); err != nil {
  174. t.Fatal(err)
  175. }
  176. }
  177. func TestExitCode(t *testing.T) {
  178. docker, err := newTestDocker()
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. defer nuke(docker)
  183. trueContainer, err := docker.Create(
  184. "exit_test_1",
  185. "/bin/true",
  186. []string{""},
  187. GetTestImage(docker),
  188. &Config{},
  189. )
  190. if err != nil {
  191. t.Fatal(err)
  192. }
  193. defer docker.Destroy(trueContainer)
  194. if err := trueContainer.Run(); err != nil {
  195. t.Fatal(err)
  196. }
  197. falseContainer, err := docker.Create(
  198. "exit_test_2",
  199. "/bin/false",
  200. []string{""},
  201. GetTestImage(docker),
  202. &Config{},
  203. )
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. defer docker.Destroy(falseContainer)
  208. if err := falseContainer.Run(); err != nil {
  209. t.Fatal(err)
  210. }
  211. if trueContainer.State.ExitCode != 0 {
  212. t.Errorf("Unexpected exit code %v", trueContainer.State.ExitCode)
  213. }
  214. if falseContainer.State.ExitCode != 1 {
  215. t.Errorf("Unexpected exit code %v", falseContainer.State.ExitCode)
  216. }
  217. }
  218. func TestRestart(t *testing.T) {
  219. docker, err := newTestDocker()
  220. if err != nil {
  221. t.Fatal(err)
  222. }
  223. defer nuke(docker)
  224. container, err := docker.Create(
  225. "restart_test",
  226. "echo",
  227. []string{"-n", "foobar"},
  228. GetTestImage(docker),
  229. &Config{},
  230. )
  231. if err != nil {
  232. t.Fatal(err)
  233. }
  234. defer docker.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. // Run the container again and check the output
  243. output, err = container.Output()
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. if string(output) != "foobar" {
  248. t.Error(string(output))
  249. }
  250. }
  251. func TestRestartStdin(t *testing.T) {
  252. docker, err := newTestDocker()
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. defer nuke(docker)
  257. container, err := docker.Create(
  258. "restart_stdin_test",
  259. "cat",
  260. []string{},
  261. GetTestImage(docker),
  262. &Config{
  263. OpenStdin: true,
  264. },
  265. )
  266. if err != nil {
  267. t.Fatal(err)
  268. }
  269. defer docker.Destroy(container)
  270. stdin, err := container.StdinPipe()
  271. stdout, err := container.StdoutPipe()
  272. if err := container.Start(); err != nil {
  273. t.Fatal(err)
  274. }
  275. io.WriteString(stdin, "hello world")
  276. stdin.Close()
  277. container.Wait()
  278. output, err := ioutil.ReadAll(stdout)
  279. stdout.Close()
  280. if string(output) != "hello world" {
  281. t.Fatal(string(output))
  282. }
  283. // Restart and try again
  284. stdin, err = container.StdinPipe()
  285. stdout, err = container.StdoutPipe()
  286. if err := container.Start(); err != nil {
  287. t.Fatal(err)
  288. }
  289. io.WriteString(stdin, "hello world #2")
  290. stdin.Close()
  291. container.Wait()
  292. output, err = ioutil.ReadAll(stdout)
  293. stdout.Close()
  294. if string(output) != "hello world #2" {
  295. t.Fatal(string(output))
  296. }
  297. }
  298. func TestUser(t *testing.T) {
  299. docker, err := newTestDocker()
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. defer nuke(docker)
  304. // Default user must be root
  305. container, err := docker.Create(
  306. "user_default",
  307. "id",
  308. []string{},
  309. GetTestImage(docker),
  310. &Config{},
  311. )
  312. if err != nil {
  313. t.Fatal(err)
  314. }
  315. defer docker.Destroy(container)
  316. output, err := container.Output()
  317. if err != nil {
  318. t.Fatal(err)
  319. }
  320. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  321. t.Error(string(output))
  322. }
  323. // Set a username
  324. container, err = docker.Create(
  325. "user_root",
  326. "/bin/id",
  327. []string{},
  328. GetTestImage(docker),
  329. &Config{
  330. User: "root",
  331. },
  332. )
  333. if err != nil {
  334. t.Fatal(err)
  335. }
  336. defer docker.Destroy(container)
  337. output, err = container.Output()
  338. if err != nil || container.State.ExitCode != 0 {
  339. t.Fatal(err)
  340. }
  341. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  342. t.Error(string(output))
  343. }
  344. // Set a UID
  345. container, err = docker.Create(
  346. "user_uid0",
  347. "id",
  348. []string{},
  349. GetTestImage(docker),
  350. &Config{
  351. User: "0",
  352. },
  353. )
  354. if err != nil || container.State.ExitCode != 0 {
  355. t.Fatal(err)
  356. }
  357. defer docker.Destroy(container)
  358. output, err = container.Output()
  359. if err != nil || container.State.ExitCode != 0 {
  360. t.Fatal(err)
  361. }
  362. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  363. t.Error(string(output))
  364. }
  365. // Set a different user by uid
  366. container, err = docker.Create(
  367. "user_uid1",
  368. "/usr/bin/id",
  369. []string{},
  370. GetTestImage(docker),
  371. &Config{
  372. User: "1",
  373. },
  374. )
  375. if err != nil {
  376. t.Fatal(err)
  377. }
  378. defer docker.Destroy(container)
  379. output, err = container.Output()
  380. if err != nil {
  381. t.Fatal(err)
  382. } else if container.State.ExitCode != 0 {
  383. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
  384. }
  385. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  386. t.Error(string(output))
  387. }
  388. // Set a different user by username
  389. container, err = docker.Create(
  390. "user_daemon",
  391. "/usr/bin/id",
  392. []string{},
  393. GetTestImage(docker),
  394. &Config{
  395. User: "daemon",
  396. },
  397. )
  398. if err != nil {
  399. t.Fatal(err)
  400. }
  401. defer docker.Destroy(container)
  402. output, err = container.Output()
  403. if err != nil || container.State.ExitCode != 0 {
  404. t.Fatal(err)
  405. }
  406. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  407. t.Error(string(output))
  408. }
  409. }
  410. func TestMultipleContainers(t *testing.T) {
  411. docker, err := newTestDocker()
  412. if err != nil {
  413. t.Fatal(err)
  414. }
  415. defer nuke(docker)
  416. container1, err := docker.Create(
  417. "container1",
  418. "cat",
  419. []string{"/dev/zero"},
  420. GetTestImage(docker),
  421. &Config{},
  422. )
  423. if err != nil {
  424. t.Fatal(err)
  425. }
  426. defer docker.Destroy(container1)
  427. container2, err := docker.Create(
  428. "container2",
  429. "cat",
  430. []string{"/dev/zero"},
  431. GetTestImage(docker),
  432. &Config{},
  433. )
  434. if err != nil {
  435. t.Fatal(err)
  436. }
  437. defer docker.Destroy(container2)
  438. // Start both containers
  439. if err := container1.Start(); err != nil {
  440. t.Fatal(err)
  441. }
  442. if err := container2.Start(); err != nil {
  443. t.Fatal(err)
  444. }
  445. // If we are here, both containers should be running
  446. if !container1.State.Running {
  447. t.Fatal("Container not running")
  448. }
  449. if !container2.State.Running {
  450. t.Fatal("Container not running")
  451. }
  452. // Kill them
  453. if err := container1.Kill(); err != nil {
  454. t.Fatal(err)
  455. }
  456. if err := container2.Kill(); err != nil {
  457. t.Fatal(err)
  458. }
  459. }
  460. func TestStdin(t *testing.T) {
  461. docker, err := newTestDocker()
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. defer nuke(docker)
  466. container, err := docker.Create(
  467. "stdin_test",
  468. "cat",
  469. []string{},
  470. GetTestImage(docker),
  471. &Config{
  472. OpenStdin: true,
  473. },
  474. )
  475. if err != nil {
  476. t.Fatal(err)
  477. }
  478. defer docker.Destroy(container)
  479. stdin, err := container.StdinPipe()
  480. stdout, err := container.StdoutPipe()
  481. defer stdin.Close()
  482. defer stdout.Close()
  483. if err := container.Start(); err != nil {
  484. t.Fatal(err)
  485. }
  486. io.WriteString(stdin, "hello world")
  487. stdin.Close()
  488. container.Wait()
  489. output, err := ioutil.ReadAll(stdout)
  490. if string(output) != "hello world" {
  491. t.Fatal(string(output))
  492. }
  493. }
  494. func TestTty(t *testing.T) {
  495. docker, err := newTestDocker()
  496. if err != nil {
  497. t.Fatal(err)
  498. }
  499. defer nuke(docker)
  500. container, err := docker.Create(
  501. "tty_test",
  502. "cat",
  503. []string{},
  504. GetTestImage(docker),
  505. &Config{
  506. OpenStdin: true,
  507. },
  508. )
  509. if err != nil {
  510. t.Fatal(err)
  511. }
  512. defer docker.Destroy(container)
  513. stdin, err := container.StdinPipe()
  514. stdout, err := container.StdoutPipe()
  515. defer stdin.Close()
  516. defer stdout.Close()
  517. if err := container.Start(); err != nil {
  518. t.Fatal(err)
  519. }
  520. io.WriteString(stdin, "hello world")
  521. stdin.Close()
  522. container.Wait()
  523. output, err := ioutil.ReadAll(stdout)
  524. if string(output) != "hello world" {
  525. t.Fatal(string(output))
  526. }
  527. }
  528. func TestEnv(t *testing.T) {
  529. docker, err := newTestDocker()
  530. if err != nil {
  531. t.Fatal(err)
  532. }
  533. defer nuke(docker)
  534. container, err := docker.Create(
  535. "env_test",
  536. "/usr/bin/env",
  537. []string{},
  538. GetTestImage(docker),
  539. &Config{},
  540. )
  541. if err != nil {
  542. t.Fatal(err)
  543. }
  544. defer docker.Destroy(container)
  545. stdout, err := container.StdoutPipe()
  546. if err != nil {
  547. t.Fatal(err)
  548. }
  549. defer stdout.Close()
  550. if err := container.Start(); err != nil {
  551. t.Fatal(err)
  552. }
  553. container.Wait()
  554. output, err := ioutil.ReadAll(stdout)
  555. if err != nil {
  556. t.Fatal(err)
  557. }
  558. actualEnv := strings.Split(string(output), "\n")
  559. if actualEnv[len(actualEnv)-1] == "" {
  560. actualEnv = actualEnv[:len(actualEnv)-1]
  561. }
  562. sort.Strings(actualEnv)
  563. goodEnv := []string{
  564. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  565. "HOME=/",
  566. }
  567. sort.Strings(goodEnv)
  568. if len(goodEnv) != len(actualEnv) {
  569. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  570. }
  571. for i := range goodEnv {
  572. if actualEnv[i] != goodEnv[i] {
  573. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  574. }
  575. }
  576. }
  577. func BenchmarkRunSequencial(b *testing.B) {
  578. docker, err := newTestDocker()
  579. if err != nil {
  580. b.Fatal(err)
  581. }
  582. defer nuke(docker)
  583. for i := 0; i < b.N; i++ {
  584. container, err := docker.Create(
  585. fmt.Sprintf("bench_%v", i),
  586. "echo",
  587. []string{"-n", "foo"},
  588. GetTestImage(docker),
  589. &Config{},
  590. )
  591. if err != nil {
  592. b.Fatal(err)
  593. }
  594. defer docker.Destroy(container)
  595. output, err := container.Output()
  596. if err != nil {
  597. b.Fatal(err)
  598. }
  599. if string(output) != "foo" {
  600. b.Fatalf("Unexecpted output: %v", string(output))
  601. }
  602. if err := docker.Destroy(container); err != nil {
  603. b.Fatal(err)
  604. }
  605. }
  606. }
  607. func BenchmarkRunParallel(b *testing.B) {
  608. docker, err := newTestDocker()
  609. if err != nil {
  610. b.Fatal(err)
  611. }
  612. defer nuke(docker)
  613. var tasks []chan error
  614. for i := 0; i < b.N; i++ {
  615. complete := make(chan error)
  616. tasks = append(tasks, complete)
  617. go func(i int, complete chan error) {
  618. container, err := docker.Create(
  619. fmt.Sprintf("bench_%v", i),
  620. "echo",
  621. []string{"-n", "foo"},
  622. GetTestImage(docker),
  623. &Config{},
  624. )
  625. if err != nil {
  626. complete <- err
  627. return
  628. }
  629. defer docker.Destroy(container)
  630. if err := container.Start(); err != nil {
  631. complete <- err
  632. return
  633. }
  634. if err := container.WaitTimeout(15 * time.Second); err != nil {
  635. complete <- err
  636. return
  637. }
  638. // if string(output) != "foo" {
  639. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  640. // }
  641. if err := docker.Destroy(container); err != nil {
  642. complete <- err
  643. return
  644. }
  645. complete <- nil
  646. }(i, complete)
  647. }
  648. var errors []error
  649. for _, task := range tasks {
  650. err := <-task
  651. if err != nil {
  652. errors = append(errors, err)
  653. }
  654. }
  655. if len(errors) > 0 {
  656. b.Fatal(errors)
  657. }
  658. }