container_test.go 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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 TestMultipleAttachRestart(t *testing.T) {
  40. runtime, err := newTestRuntime()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer nuke(runtime)
  45. container, err := runtime.Create(
  46. &Config{
  47. Image: GetTestImage(runtime).Id,
  48. Cmd: []string{"/bin/sh", "-c",
  49. "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
  50. Memory: 33554432,
  51. },
  52. )
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. defer runtime.Destroy(container)
  57. // Simulate 3 client attaching to the container and stop/restart
  58. stdout1, err := container.StdoutPipe()
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. stdout2, err := container.StdoutPipe()
  63. if err != nil {
  64. t.Fatal(err)
  65. }
  66. stdout3, err := container.StdoutPipe()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. if err := container.Start(); err != nil {
  71. t.Fatal(err)
  72. }
  73. l1, err := bufio.NewReader(stdout1).ReadString('\n')
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. if strings.Trim(l1, " \r\n") != "hello" {
  78. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
  79. }
  80. l2, err := bufio.NewReader(stdout2).ReadString('\n')
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if strings.Trim(l2, " \r\n") != "hello" {
  85. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
  86. }
  87. l3, err := bufio.NewReader(stdout3).ReadString('\n')
  88. if err != nil {
  89. t.Fatal(err)
  90. }
  91. if strings.Trim(l3, " \r\n") != "hello" {
  92. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
  93. }
  94. if err := container.Stop(10); err != nil {
  95. t.Fatal(err)
  96. }
  97. stdout1, err = container.StdoutPipe()
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. stdout2, err = container.StdoutPipe()
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. stdout3, err = container.StdoutPipe()
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. if err := container.Start(); err != nil {
  110. t.Fatal(err)
  111. }
  112. timeout := make(chan bool)
  113. go func() {
  114. l1, err = bufio.NewReader(stdout1).ReadString('\n')
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if strings.Trim(l1, " \r\n") != "hello" {
  119. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
  120. }
  121. l2, err = bufio.NewReader(stdout2).ReadString('\n')
  122. if err != nil {
  123. t.Fatal(err)
  124. }
  125. if strings.Trim(l2, " \r\n") != "hello" {
  126. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
  127. }
  128. l3, err = bufio.NewReader(stdout3).ReadString('\n')
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. if strings.Trim(l3, " \r\n") != "hello" {
  133. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
  134. }
  135. timeout <- false
  136. }()
  137. go func() {
  138. time.Sleep(3 * time.Second)
  139. timeout <- true
  140. }()
  141. if <-timeout {
  142. t.Fatalf("Timeout reading from the process")
  143. }
  144. }
  145. func TestDiff(t *testing.T) {
  146. runtime, err := newTestRuntime()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. defer nuke(runtime)
  151. // Create a container and remove a file
  152. container1, err := runtime.Create(
  153. &Config{
  154. Image: GetTestImage(runtime).Id,
  155. Cmd: []string{"/bin/rm", "/etc/passwd"},
  156. },
  157. )
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. defer runtime.Destroy(container1)
  162. if err := container1.Run(); err != nil {
  163. t.Fatal(err)
  164. }
  165. // Check the changelog
  166. c, err := container1.Changes()
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. success := false
  171. for _, elem := range c {
  172. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  173. success = true
  174. }
  175. }
  176. if !success {
  177. t.Fatalf("/etc/passwd as been removed but is not present in the diff")
  178. }
  179. // Commit the container
  180. rwTar, err := container1.ExportRw()
  181. if err != nil {
  182. t.Error(err)
  183. }
  184. img, err := runtime.graph.Create(rwTar, container1, "unit test commited image - diff", "", nil)
  185. if err != nil {
  186. t.Error(err)
  187. }
  188. // Create a new container from the commited image
  189. container2, err := runtime.Create(
  190. &Config{
  191. Image: img.Id,
  192. Cmd: []string{"cat", "/etc/passwd"},
  193. },
  194. )
  195. if err != nil {
  196. t.Fatal(err)
  197. }
  198. defer runtime.Destroy(container2)
  199. if err := container2.Run(); err != nil {
  200. t.Fatal(err)
  201. }
  202. // Check the changelog
  203. c, err = container2.Changes()
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. for _, elem := range c {
  208. if elem.Path == "/etc/passwd" {
  209. t.Fatalf("/etc/passwd should not be present in the diff after commit.")
  210. }
  211. }
  212. }
  213. func TestCommitRun(t *testing.T) {
  214. runtime, err := newTestRuntime()
  215. if err != nil {
  216. t.Fatal(err)
  217. }
  218. defer nuke(runtime)
  219. container1, err := runtime.Create(
  220. &Config{
  221. Image: GetTestImage(runtime).Id,
  222. Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
  223. Memory: 33554432,
  224. },
  225. )
  226. if err != nil {
  227. t.Fatal(err)
  228. }
  229. defer runtime.Destroy(container1)
  230. if container1.State.Running {
  231. t.Errorf("Container shouldn't be running")
  232. }
  233. if err := container1.Run(); err != nil {
  234. t.Fatal(err)
  235. }
  236. if container1.State.Running {
  237. t.Errorf("Container shouldn't be running")
  238. }
  239. rwTar, err := container1.ExportRw()
  240. if err != nil {
  241. t.Error(err)
  242. }
  243. img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  244. if err != nil {
  245. t.Error(err)
  246. }
  247. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  248. container2, err := runtime.Create(
  249. &Config{
  250. Image: img.Id,
  251. Memory: 33554432,
  252. Cmd: []string{"cat", "/world"},
  253. },
  254. )
  255. if err != nil {
  256. t.Fatal(err)
  257. }
  258. defer runtime.Destroy(container2)
  259. stdout, err := container2.StdoutPipe()
  260. if err != nil {
  261. t.Fatal(err)
  262. }
  263. stderr, err := container2.StderrPipe()
  264. if err != nil {
  265. t.Fatal(err)
  266. }
  267. if err := container2.Start(); err != nil {
  268. t.Fatal(err)
  269. }
  270. container2.Wait()
  271. output, err := ioutil.ReadAll(stdout)
  272. if err != nil {
  273. t.Fatal(err)
  274. }
  275. output2, err := ioutil.ReadAll(stderr)
  276. if err != nil {
  277. t.Fatal(err)
  278. }
  279. if err := stdout.Close(); err != nil {
  280. t.Fatal(err)
  281. }
  282. if err := stderr.Close(); err != nil {
  283. t.Fatal(err)
  284. }
  285. if string(output) != "hello\n" {
  286. t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
  287. }
  288. }
  289. func TestStart(t *testing.T) {
  290. runtime, err := newTestRuntime()
  291. if err != nil {
  292. t.Fatal(err)
  293. }
  294. defer nuke(runtime)
  295. container, err := runtime.Create(
  296. &Config{
  297. Image: GetTestImage(runtime).Id,
  298. Memory: 33554432,
  299. Cmd: []string{"/bin/cat"},
  300. OpenStdin: true,
  301. },
  302. )
  303. if err != nil {
  304. t.Fatal(err)
  305. }
  306. defer runtime.Destroy(container)
  307. if err := container.Start(); err != nil {
  308. t.Fatal(err)
  309. }
  310. // Give some time to the process to start
  311. container.WaitTimeout(500 * time.Millisecond)
  312. if !container.State.Running {
  313. t.Errorf("Container should be running")
  314. }
  315. if err := container.Start(); err == nil {
  316. t.Fatalf("A running containter should be able to be started")
  317. }
  318. // Try to avoid the timeoout in destroy. Best effort, don't check error
  319. cStdin, _ := container.StdinPipe()
  320. cStdin.Close()
  321. container.WaitTimeout(2 * time.Second)
  322. }
  323. func TestRun(t *testing.T) {
  324. runtime, err := newTestRuntime()
  325. if err != nil {
  326. t.Fatal(err)
  327. }
  328. defer nuke(runtime)
  329. container, err := runtime.Create(
  330. &Config{
  331. Image: GetTestImage(runtime).Id,
  332. Memory: 33554432,
  333. Cmd: []string{"ls", "-al"},
  334. },
  335. )
  336. if err != nil {
  337. t.Fatal(err)
  338. }
  339. defer runtime.Destroy(container)
  340. if container.State.Running {
  341. t.Errorf("Container shouldn't be running")
  342. }
  343. if err := container.Run(); err != nil {
  344. t.Fatal(err)
  345. }
  346. if container.State.Running {
  347. t.Errorf("Container shouldn't be running")
  348. }
  349. }
  350. func TestOutput(t *testing.T) {
  351. runtime, err := newTestRuntime()
  352. if err != nil {
  353. t.Fatal(err)
  354. }
  355. defer nuke(runtime)
  356. container, err := runtime.Create(
  357. &Config{
  358. Image: GetTestImage(runtime).Id,
  359. Cmd: []string{"echo", "-n", "foobar"},
  360. },
  361. )
  362. if err != nil {
  363. t.Fatal(err)
  364. }
  365. defer runtime.Destroy(container)
  366. output, err := container.Output()
  367. if err != nil {
  368. t.Fatal(err)
  369. }
  370. if string(output) != "foobar" {
  371. t.Error(string(output))
  372. }
  373. }
  374. func TestKillDifferentUser(t *testing.T) {
  375. runtime, err := newTestRuntime()
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. defer nuke(runtime)
  380. container, err := runtime.Create(&Config{
  381. Image: GetTestImage(runtime).Id,
  382. Cmd: []string{"tail", "-f", "/etc/resolv.conf"},
  383. User: "daemon",
  384. },
  385. )
  386. if err != nil {
  387. t.Fatal(err)
  388. }
  389. defer runtime.Destroy(container)
  390. if container.State.Running {
  391. t.Errorf("Container shouldn't be running")
  392. }
  393. if err := container.Start(); err != nil {
  394. t.Fatal(err)
  395. }
  396. // Give some time to lxc to spawn the process (setuid might take some time)
  397. container.WaitTimeout(500 * time.Millisecond)
  398. if !container.State.Running {
  399. t.Errorf("Container should be running")
  400. }
  401. if err := container.Kill(); err != nil {
  402. t.Fatal(err)
  403. }
  404. if container.State.Running {
  405. t.Errorf("Container shouldn't be running")
  406. }
  407. container.Wait()
  408. if container.State.Running {
  409. t.Errorf("Container shouldn't be running")
  410. }
  411. // Try stopping twice
  412. if err := container.Kill(); err != nil {
  413. t.Fatal(err)
  414. }
  415. }
  416. func TestKill(t *testing.T) {
  417. runtime, err := newTestRuntime()
  418. if err != nil {
  419. t.Fatal(err)
  420. }
  421. defer nuke(runtime)
  422. container, err := runtime.Create(&Config{
  423. Image: GetTestImage(runtime).Id,
  424. Cmd: []string{"cat", "/dev/zero"},
  425. },
  426. )
  427. if err != nil {
  428. t.Fatal(err)
  429. }
  430. defer runtime.Destroy(container)
  431. if container.State.Running {
  432. t.Errorf("Container shouldn't be running")
  433. }
  434. if err := container.Start(); err != nil {
  435. t.Fatal(err)
  436. }
  437. // Give some time to lxc to spawn the process
  438. container.WaitTimeout(500 * time.Millisecond)
  439. if !container.State.Running {
  440. t.Errorf("Container should be running")
  441. }
  442. if err := container.Kill(); err != nil {
  443. t.Fatal(err)
  444. }
  445. if container.State.Running {
  446. t.Errorf("Container shouldn't be running")
  447. }
  448. container.Wait()
  449. if container.State.Running {
  450. t.Errorf("Container shouldn't be running")
  451. }
  452. // Try stopping twice
  453. if err := container.Kill(); err != nil {
  454. t.Fatal(err)
  455. }
  456. }
  457. func TestExitCode(t *testing.T) {
  458. runtime, err := newTestRuntime()
  459. if err != nil {
  460. t.Fatal(err)
  461. }
  462. defer nuke(runtime)
  463. trueContainer, err := runtime.Create(&Config{
  464. Image: GetTestImage(runtime).Id,
  465. Cmd: []string{"/bin/true", ""},
  466. })
  467. if err != nil {
  468. t.Fatal(err)
  469. }
  470. defer runtime.Destroy(trueContainer)
  471. if err := trueContainer.Run(); err != nil {
  472. t.Fatal(err)
  473. }
  474. if trueContainer.State.ExitCode != 0 {
  475. t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
  476. }
  477. falseContainer, err := runtime.Create(&Config{
  478. Image: GetTestImage(runtime).Id,
  479. Cmd: []string{"/bin/false", ""},
  480. })
  481. if err != nil {
  482. t.Fatal(err)
  483. }
  484. defer runtime.Destroy(falseContainer)
  485. if err := falseContainer.Run(); err != nil {
  486. t.Fatal(err)
  487. }
  488. if falseContainer.State.ExitCode != 1 {
  489. t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
  490. }
  491. }
  492. func TestRestart(t *testing.T) {
  493. runtime, err := newTestRuntime()
  494. if err != nil {
  495. t.Fatal(err)
  496. }
  497. defer nuke(runtime)
  498. container, err := runtime.Create(&Config{
  499. Image: GetTestImage(runtime).Id,
  500. Cmd: []string{"echo", "-n", "foobar"},
  501. },
  502. )
  503. if err != nil {
  504. t.Fatal(err)
  505. }
  506. defer runtime.Destroy(container)
  507. output, err := container.Output()
  508. if err != nil {
  509. t.Fatal(err)
  510. }
  511. if string(output) != "foobar" {
  512. t.Error(string(output))
  513. }
  514. // Run the container again and check the output
  515. output, err = container.Output()
  516. if err != nil {
  517. t.Fatal(err)
  518. }
  519. if string(output) != "foobar" {
  520. t.Error(string(output))
  521. }
  522. }
  523. func TestRestartStdin(t *testing.T) {
  524. runtime, err := newTestRuntime()
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. defer nuke(runtime)
  529. container, err := runtime.Create(&Config{
  530. Image: GetTestImage(runtime).Id,
  531. Cmd: []string{"cat"},
  532. OpenStdin: true,
  533. },
  534. )
  535. if err != nil {
  536. t.Fatal(err)
  537. }
  538. defer runtime.Destroy(container)
  539. stdin, err := container.StdinPipe()
  540. if err != nil {
  541. t.Fatal(err)
  542. }
  543. stdout, err := container.StdoutPipe()
  544. if err != nil {
  545. t.Fatal(err)
  546. }
  547. if err := container.Start(); err != nil {
  548. t.Fatal(err)
  549. }
  550. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  551. t.Fatal(err)
  552. }
  553. if err := stdin.Close(); err != nil {
  554. t.Fatal(err)
  555. }
  556. container.Wait()
  557. output, err := ioutil.ReadAll(stdout)
  558. if err != nil {
  559. t.Fatal(err)
  560. }
  561. if err := stdout.Close(); err != nil {
  562. t.Fatal(err)
  563. }
  564. if string(output) != "hello world" {
  565. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  566. }
  567. // Restart and try again
  568. stdin, err = container.StdinPipe()
  569. if err != nil {
  570. t.Fatal(err)
  571. }
  572. stdout, err = container.StdoutPipe()
  573. if err != nil {
  574. t.Fatal(err)
  575. }
  576. if err := container.Start(); err != nil {
  577. t.Fatal(err)
  578. }
  579. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  580. t.Fatal(err)
  581. }
  582. if err := stdin.Close(); err != nil {
  583. t.Fatal(err)
  584. }
  585. container.Wait()
  586. output, err = ioutil.ReadAll(stdout)
  587. if err != nil {
  588. t.Fatal(err)
  589. }
  590. if err := stdout.Close(); err != nil {
  591. t.Fatal(err)
  592. }
  593. if string(output) != "hello world #2" {
  594. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  595. }
  596. }
  597. func TestUser(t *testing.T) {
  598. runtime, err := newTestRuntime()
  599. if err != nil {
  600. t.Fatal(err)
  601. }
  602. defer nuke(runtime)
  603. // Default user must be root
  604. container, err := runtime.Create(&Config{
  605. Image: GetTestImage(runtime).Id,
  606. Cmd: []string{"id"},
  607. },
  608. )
  609. if err != nil {
  610. t.Fatal(err)
  611. }
  612. defer runtime.Destroy(container)
  613. output, err := container.Output()
  614. if err != nil {
  615. t.Fatal(err)
  616. }
  617. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  618. t.Error(string(output))
  619. }
  620. // Set a username
  621. container, err = runtime.Create(&Config{
  622. Image: GetTestImage(runtime).Id,
  623. Cmd: []string{"id"},
  624. User: "root",
  625. },
  626. )
  627. if err != nil {
  628. t.Fatal(err)
  629. }
  630. defer runtime.Destroy(container)
  631. output, err = container.Output()
  632. if err != nil || container.State.ExitCode != 0 {
  633. t.Fatal(err)
  634. }
  635. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  636. t.Error(string(output))
  637. }
  638. // Set a UID
  639. container, err = runtime.Create(&Config{
  640. Image: GetTestImage(runtime).Id,
  641. Cmd: []string{"id"},
  642. User: "0",
  643. },
  644. )
  645. if err != nil || container.State.ExitCode != 0 {
  646. t.Fatal(err)
  647. }
  648. defer runtime.Destroy(container)
  649. output, err = container.Output()
  650. if err != nil || container.State.ExitCode != 0 {
  651. t.Fatal(err)
  652. }
  653. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  654. t.Error(string(output))
  655. }
  656. // Set a different user by uid
  657. container, err = runtime.Create(&Config{
  658. Image: GetTestImage(runtime).Id,
  659. Cmd: []string{"id"},
  660. User: "1",
  661. },
  662. )
  663. if err != nil {
  664. t.Fatal(err)
  665. }
  666. defer runtime.Destroy(container)
  667. output, err = container.Output()
  668. if err != nil {
  669. t.Fatal(err)
  670. } else if container.State.ExitCode != 0 {
  671. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
  672. }
  673. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  674. t.Error(string(output))
  675. }
  676. // Set a different user by username
  677. container, err = runtime.Create(&Config{
  678. Image: GetTestImage(runtime).Id,
  679. Cmd: []string{"id"},
  680. User: "daemon",
  681. },
  682. )
  683. if err != nil {
  684. t.Fatal(err)
  685. }
  686. defer runtime.Destroy(container)
  687. output, err = container.Output()
  688. if err != nil || container.State.ExitCode != 0 {
  689. t.Fatal(err)
  690. }
  691. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  692. t.Error(string(output))
  693. }
  694. }
  695. func TestMultipleContainers(t *testing.T) {
  696. runtime, err := newTestRuntime()
  697. if err != nil {
  698. t.Fatal(err)
  699. }
  700. defer nuke(runtime)
  701. container1, err := runtime.Create(&Config{
  702. Image: GetTestImage(runtime).Id,
  703. Cmd: []string{"cat", "/dev/zero"},
  704. },
  705. )
  706. if err != nil {
  707. t.Fatal(err)
  708. }
  709. defer runtime.Destroy(container1)
  710. container2, err := runtime.Create(&Config{
  711. Image: GetTestImage(runtime).Id,
  712. Cmd: []string{"cat", "/dev/zero"},
  713. },
  714. )
  715. if err != nil {
  716. t.Fatal(err)
  717. }
  718. defer runtime.Destroy(container2)
  719. // Start both containers
  720. if err := container1.Start(); err != nil {
  721. t.Fatal(err)
  722. }
  723. if err := container2.Start(); err != nil {
  724. t.Fatal(err)
  725. }
  726. // Make sure they are running before trying to kill them
  727. container1.WaitTimeout(250 * time.Millisecond)
  728. container2.WaitTimeout(250 * time.Millisecond)
  729. // If we are here, both containers should be running
  730. if !container1.State.Running {
  731. t.Fatal("Container not running")
  732. }
  733. if !container2.State.Running {
  734. t.Fatal("Container not running")
  735. }
  736. // Kill them
  737. if err := container1.Kill(); err != nil {
  738. t.Fatal(err)
  739. }
  740. if err := container2.Kill(); err != nil {
  741. t.Fatal(err)
  742. }
  743. }
  744. func TestStdin(t *testing.T) {
  745. runtime, err := newTestRuntime()
  746. if err != nil {
  747. t.Fatal(err)
  748. }
  749. defer nuke(runtime)
  750. container, err := runtime.Create(&Config{
  751. Image: GetTestImage(runtime).Id,
  752. Cmd: []string{"cat"},
  753. OpenStdin: true,
  754. },
  755. )
  756. if err != nil {
  757. t.Fatal(err)
  758. }
  759. defer runtime.Destroy(container)
  760. stdin, err := container.StdinPipe()
  761. if err != nil {
  762. t.Fatal(err)
  763. }
  764. stdout, err := container.StdoutPipe()
  765. if err != nil {
  766. t.Fatal(err)
  767. }
  768. if err := container.Start(); err != nil {
  769. t.Fatal(err)
  770. }
  771. defer stdin.Close()
  772. defer stdout.Close()
  773. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  774. t.Fatal(err)
  775. }
  776. if err := stdin.Close(); err != nil {
  777. t.Fatal(err)
  778. }
  779. container.Wait()
  780. output, err := ioutil.ReadAll(stdout)
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. if string(output) != "hello world" {
  785. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  786. }
  787. }
  788. func TestTty(t *testing.T) {
  789. runtime, err := newTestRuntime()
  790. if err != nil {
  791. t.Fatal(err)
  792. }
  793. defer nuke(runtime)
  794. container, err := runtime.Create(&Config{
  795. Image: GetTestImage(runtime).Id,
  796. Cmd: []string{"cat"},
  797. OpenStdin: true,
  798. },
  799. )
  800. if err != nil {
  801. t.Fatal(err)
  802. }
  803. defer runtime.Destroy(container)
  804. stdin, err := container.StdinPipe()
  805. if err != nil {
  806. t.Fatal(err)
  807. }
  808. stdout, err := container.StdoutPipe()
  809. if err != nil {
  810. t.Fatal(err)
  811. }
  812. if err := container.Start(); err != nil {
  813. t.Fatal(err)
  814. }
  815. defer stdin.Close()
  816. defer stdout.Close()
  817. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  818. t.Fatal(err)
  819. }
  820. if err := stdin.Close(); err != nil {
  821. t.Fatal(err)
  822. }
  823. container.Wait()
  824. output, err := ioutil.ReadAll(stdout)
  825. if err != nil {
  826. t.Fatal(err)
  827. }
  828. if string(output) != "hello world" {
  829. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  830. }
  831. }
  832. func TestEnv(t *testing.T) {
  833. runtime, err := newTestRuntime()
  834. if err != nil {
  835. t.Fatal(err)
  836. }
  837. defer nuke(runtime)
  838. container, err := runtime.Create(&Config{
  839. Image: GetTestImage(runtime).Id,
  840. Cmd: []string{"/usr/bin/env"},
  841. },
  842. )
  843. if err != nil {
  844. t.Fatal(err)
  845. }
  846. defer runtime.Destroy(container)
  847. stdout, err := container.StdoutPipe()
  848. if err != nil {
  849. t.Fatal(err)
  850. }
  851. defer stdout.Close()
  852. if err := container.Start(); err != nil {
  853. t.Fatal(err)
  854. }
  855. container.Wait()
  856. output, err := ioutil.ReadAll(stdout)
  857. if err != nil {
  858. t.Fatal(err)
  859. }
  860. actualEnv := strings.Split(string(output), "\n")
  861. if actualEnv[len(actualEnv)-1] == "" {
  862. actualEnv = actualEnv[:len(actualEnv)-1]
  863. }
  864. sort.Strings(actualEnv)
  865. goodEnv := []string{
  866. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  867. "HOME=/",
  868. }
  869. sort.Strings(goodEnv)
  870. if len(goodEnv) != len(actualEnv) {
  871. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  872. }
  873. for i := range goodEnv {
  874. if actualEnv[i] != goodEnv[i] {
  875. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  876. }
  877. }
  878. }
  879. func grepFile(t *testing.T, path string, pattern string) {
  880. f, err := os.Open(path)
  881. if err != nil {
  882. t.Fatal(err)
  883. }
  884. defer f.Close()
  885. r := bufio.NewReader(f)
  886. var (
  887. line string
  888. )
  889. err = nil
  890. for err == nil {
  891. line, err = r.ReadString('\n')
  892. if strings.Contains(line, pattern) == true {
  893. return
  894. }
  895. }
  896. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  897. }
  898. func TestLXCConfig(t *testing.T) {
  899. runtime, err := newTestRuntime()
  900. if err != nil {
  901. t.Fatal(err)
  902. }
  903. defer nuke(runtime)
  904. // Memory is allocated randomly for testing
  905. rand.Seed(time.Now().UTC().UnixNano())
  906. memMin := 33554432
  907. memMax := 536870912
  908. mem := memMin + rand.Intn(memMax-memMin)
  909. container, err := runtime.Create(&Config{
  910. Image: GetTestImage(runtime).Id,
  911. Cmd: []string{"/bin/true"},
  912. Hostname: "foobar",
  913. Memory: int64(mem),
  914. },
  915. )
  916. if err != nil {
  917. t.Fatal(err)
  918. }
  919. defer runtime.Destroy(container)
  920. container.generateLXCConfig()
  921. grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
  922. grepFile(t, container.lxcConfigPath(),
  923. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  924. grepFile(t, container.lxcConfigPath(),
  925. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  926. }
  927. func BenchmarkRunSequencial(b *testing.B) {
  928. runtime, err := newTestRuntime()
  929. if err != nil {
  930. b.Fatal(err)
  931. }
  932. defer nuke(runtime)
  933. for i := 0; i < b.N; i++ {
  934. container, err := runtime.Create(&Config{
  935. Image: GetTestImage(runtime).Id,
  936. Cmd: []string{"echo", "-n", "foo"},
  937. },
  938. )
  939. if err != nil {
  940. b.Fatal(err)
  941. }
  942. defer runtime.Destroy(container)
  943. output, err := container.Output()
  944. if err != nil {
  945. b.Fatal(err)
  946. }
  947. if string(output) != "foo" {
  948. b.Fatalf("Unexpected output: %s", output)
  949. }
  950. if err := runtime.Destroy(container); err != nil {
  951. b.Fatal(err)
  952. }
  953. }
  954. }
  955. func BenchmarkRunParallel(b *testing.B) {
  956. runtime, err := newTestRuntime()
  957. if err != nil {
  958. b.Fatal(err)
  959. }
  960. defer nuke(runtime)
  961. var tasks []chan error
  962. for i := 0; i < b.N; i++ {
  963. complete := make(chan error)
  964. tasks = append(tasks, complete)
  965. go func(i int, complete chan error) {
  966. container, err := runtime.Create(&Config{
  967. Image: GetTestImage(runtime).Id,
  968. Cmd: []string{"echo", "-n", "foo"},
  969. },
  970. )
  971. if err != nil {
  972. complete <- err
  973. return
  974. }
  975. defer runtime.Destroy(container)
  976. if err := container.Start(); err != nil {
  977. complete <- err
  978. return
  979. }
  980. if err := container.WaitTimeout(15 * time.Second); err != nil {
  981. complete <- err
  982. return
  983. }
  984. // if string(output) != "foo" {
  985. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  986. // }
  987. if err := runtime.Destroy(container); err != nil {
  988. complete <- err
  989. return
  990. }
  991. complete <- nil
  992. }(i, complete)
  993. }
  994. var errors []error
  995. for _, task := range tasks {
  996. err := <-task
  997. if err != nil {
  998. errors = append(errors, err)
  999. }
  1000. }
  1001. if len(errors) > 0 {
  1002. b.Fatal(errors)
  1003. }
  1004. }