container_test.go 15 KB

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