container_test.go 14 KB

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