container_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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(); 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 TestCommitRun(t *testing.T) {
  146. runtime, err := newTestRuntime()
  147. if err != nil {
  148. t.Fatal(err)
  149. }
  150. defer nuke(runtime)
  151. container1, err := runtime.Create(
  152. &Config{
  153. Image: GetTestImage(runtime).Id,
  154. Cmd: []string{"/bin/sh", "-c", "echo hello > /world"},
  155. Memory: 33554432,
  156. },
  157. )
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. defer runtime.Destroy(container1)
  162. if container1.State.Running {
  163. t.Errorf("Container shouldn't be running")
  164. }
  165. if err := container1.Run(); err != nil {
  166. t.Fatal(err)
  167. }
  168. if container1.State.Running {
  169. t.Errorf("Container shouldn't be running")
  170. }
  171. rwTar, err := container1.ExportRw()
  172. if err != nil {
  173. t.Error(err)
  174. }
  175. img, err := runtime.graph.Create(rwTar, container1, "unit test commited image")
  176. if err != nil {
  177. t.Error(err)
  178. }
  179. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  180. container2, err := runtime.Create(
  181. &Config{
  182. Image: img.Id,
  183. Memory: 33554432,
  184. Cmd: []string{"cat", "/world"},
  185. },
  186. )
  187. if err != nil {
  188. t.Fatal(err)
  189. }
  190. defer runtime.Destroy(container2)
  191. stdout, err := container2.StdoutPipe()
  192. if err != nil {
  193. t.Fatal(err)
  194. }
  195. stderr, err := container2.StderrPipe()
  196. if err != nil {
  197. t.Fatal(err)
  198. }
  199. if err := container2.Start(); err != nil {
  200. t.Fatal(err)
  201. }
  202. container2.Wait()
  203. output, err := ioutil.ReadAll(stdout)
  204. if err != nil {
  205. t.Fatal(err)
  206. }
  207. output2, err := ioutil.ReadAll(stderr)
  208. if err != nil {
  209. t.Fatal(err)
  210. }
  211. if err := stdout.Close(); err != nil {
  212. t.Fatal(err)
  213. }
  214. if err := stderr.Close(); err != nil {
  215. t.Fatal(err)
  216. }
  217. if string(output) != "hello\n" {
  218. t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
  219. }
  220. }
  221. func TestStart(t *testing.T) {
  222. runtime, err := newTestRuntime()
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. defer nuke(runtime)
  227. container, err := runtime.Create(
  228. &Config{
  229. Image: GetTestImage(runtime).Id,
  230. Memory: 33554432,
  231. Cmd: []string{"/bin/cat"},
  232. OpenStdin: true,
  233. },
  234. )
  235. if err != nil {
  236. t.Fatal(err)
  237. }
  238. defer runtime.Destroy(container)
  239. if err := container.Start(); err != nil {
  240. t.Fatal(err)
  241. }
  242. // Give some time to the process to start
  243. container.WaitTimeout(500 * time.Millisecond)
  244. if !container.State.Running {
  245. t.Errorf("Container should be running")
  246. }
  247. if err := container.Start(); err == nil {
  248. t.Fatalf("A running containter should be able to be started")
  249. }
  250. // Try to avoid the timeoout in destroy. Best effort, don't check error
  251. cStdin, _ := container.StdinPipe()
  252. cStdin.Close()
  253. container.WaitTimeout(500 * time.Millisecond)
  254. container.State.setStopped(0)
  255. }
  256. func TestRun(t *testing.T) {
  257. runtime, err := newTestRuntime()
  258. if err != nil {
  259. t.Fatal(err)
  260. }
  261. defer nuke(runtime)
  262. container, err := runtime.Create(
  263. &Config{
  264. Image: GetTestImage(runtime).Id,
  265. Memory: 33554432,
  266. Cmd: []string{"ls", "-al"},
  267. },
  268. )
  269. if err != nil {
  270. t.Fatal(err)
  271. }
  272. defer runtime.Destroy(container)
  273. if container.State.Running {
  274. t.Errorf("Container shouldn't be running")
  275. }
  276. if err := container.Run(); err != nil {
  277. t.Fatal(err)
  278. }
  279. if container.State.Running {
  280. t.Errorf("Container shouldn't be running")
  281. }
  282. }
  283. func TestOutput(t *testing.T) {
  284. runtime, err := newTestRuntime()
  285. if err != nil {
  286. t.Fatal(err)
  287. }
  288. defer nuke(runtime)
  289. container, err := runtime.Create(
  290. &Config{
  291. Image: GetTestImage(runtime).Id,
  292. Cmd: []string{"echo", "-n", "foobar"},
  293. },
  294. )
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. defer runtime.Destroy(container)
  299. output, err := container.Output()
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. if string(output) != "foobar" {
  304. t.Error(string(output))
  305. }
  306. }
  307. func TestKill(t *testing.T) {
  308. runtime, err := newTestRuntime()
  309. if err != nil {
  310. t.Fatal(err)
  311. }
  312. defer nuke(runtime)
  313. container, err := runtime.Create(&Config{
  314. Image: GetTestImage(runtime).Id,
  315. Cmd: []string{"cat", "/dev/zero"},
  316. },
  317. )
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. defer runtime.Destroy(container)
  322. if container.State.Running {
  323. t.Errorf("Container shouldn't be running")
  324. }
  325. if err := container.Start(); err != nil {
  326. t.Fatal(err)
  327. }
  328. if !container.State.Running {
  329. t.Errorf("Container should be running")
  330. }
  331. if err := container.Kill(); err != nil {
  332. t.Fatal(err)
  333. }
  334. if container.State.Running {
  335. t.Errorf("Container shouldn't be running")
  336. }
  337. container.Wait()
  338. if container.State.Running {
  339. t.Errorf("Container shouldn't be running")
  340. }
  341. // Try stopping twice
  342. if err := container.Kill(); err != nil {
  343. t.Fatal(err)
  344. }
  345. }
  346. func TestExitCode(t *testing.T) {
  347. runtime, err := newTestRuntime()
  348. if err != nil {
  349. t.Fatal(err)
  350. }
  351. defer nuke(runtime)
  352. trueContainer, err := runtime.Create(&Config{
  353. Image: GetTestImage(runtime).Id,
  354. Cmd: []string{"/bin/true", ""},
  355. })
  356. if err != nil {
  357. t.Fatal(err)
  358. }
  359. defer runtime.Destroy(trueContainer)
  360. if err := trueContainer.Run(); err != nil {
  361. t.Fatal(err)
  362. }
  363. if trueContainer.State.ExitCode != 0 {
  364. t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
  365. }
  366. falseContainer, err := runtime.Create(&Config{
  367. Image: GetTestImage(runtime).Id,
  368. Cmd: []string{"/bin/false", ""},
  369. })
  370. if err != nil {
  371. t.Fatal(err)
  372. }
  373. defer runtime.Destroy(falseContainer)
  374. if err := falseContainer.Run(); err != nil {
  375. t.Fatal(err)
  376. }
  377. if falseContainer.State.ExitCode != 1 {
  378. t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
  379. }
  380. }
  381. func TestRestart(t *testing.T) {
  382. runtime, err := newTestRuntime()
  383. if err != nil {
  384. t.Fatal(err)
  385. }
  386. defer nuke(runtime)
  387. container, err := runtime.Create(&Config{
  388. Image: GetTestImage(runtime).Id,
  389. Cmd: []string{"echo", "-n", "foobar"},
  390. },
  391. )
  392. if err != nil {
  393. t.Fatal(err)
  394. }
  395. defer runtime.Destroy(container)
  396. output, err := container.Output()
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. if string(output) != "foobar" {
  401. t.Error(string(output))
  402. }
  403. // Run the container again and check the output
  404. output, err = container.Output()
  405. if err != nil {
  406. t.Fatal(err)
  407. }
  408. if string(output) != "foobar" {
  409. t.Error(string(output))
  410. }
  411. }
  412. func TestRestartStdin(t *testing.T) {
  413. runtime, err := newTestRuntime()
  414. if err != nil {
  415. t.Fatal(err)
  416. }
  417. defer nuke(runtime)
  418. container, err := runtime.Create(&Config{
  419. Image: GetTestImage(runtime).Id,
  420. Cmd: []string{"cat"},
  421. OpenStdin: true,
  422. },
  423. )
  424. if err != nil {
  425. t.Fatal(err)
  426. }
  427. defer runtime.Destroy(container)
  428. stdin, err := container.StdinPipe()
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. stdout, err := container.StdoutPipe()
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. if err := container.Start(); err != nil {
  437. t.Fatal(err)
  438. }
  439. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  440. t.Fatal(err)
  441. }
  442. if err := stdin.Close(); err != nil {
  443. t.Fatal(err)
  444. }
  445. container.Wait()
  446. output, err := ioutil.ReadAll(stdout)
  447. if err != nil {
  448. t.Fatal(err)
  449. }
  450. if err := stdout.Close(); err != nil {
  451. t.Fatal(err)
  452. }
  453. if string(output) != "hello world" {
  454. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  455. }
  456. // Restart and try again
  457. stdin, err = container.StdinPipe()
  458. if err != nil {
  459. t.Fatal(err)
  460. }
  461. stdout, err = container.StdoutPipe()
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. if err := container.Start(); err != nil {
  466. t.Fatal(err)
  467. }
  468. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  469. t.Fatal(err)
  470. }
  471. if err := stdin.Close(); err != nil {
  472. t.Fatal(err)
  473. }
  474. container.Wait()
  475. output, err = ioutil.ReadAll(stdout)
  476. if err != nil {
  477. t.Fatal(err)
  478. }
  479. if err := stdout.Close(); err != nil {
  480. t.Fatal(err)
  481. }
  482. if string(output) != "hello world #2" {
  483. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  484. }
  485. }
  486. func TestUser(t *testing.T) {
  487. runtime, err := newTestRuntime()
  488. if err != nil {
  489. t.Fatal(err)
  490. }
  491. defer nuke(runtime)
  492. // Default user must be root
  493. container, err := runtime.Create(&Config{
  494. Image: GetTestImage(runtime).Id,
  495. Cmd: []string{"id"},
  496. },
  497. )
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. defer runtime.Destroy(container)
  502. output, err := container.Output()
  503. if err != nil {
  504. t.Fatal(err)
  505. }
  506. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  507. t.Error(string(output))
  508. }
  509. // Set a username
  510. container, err = runtime.Create(&Config{
  511. Image: GetTestImage(runtime).Id,
  512. Cmd: []string{"id"},
  513. User: "root",
  514. },
  515. )
  516. if err != nil {
  517. t.Fatal(err)
  518. }
  519. defer runtime.Destroy(container)
  520. output, err = container.Output()
  521. if err != nil || container.State.ExitCode != 0 {
  522. t.Fatal(err)
  523. }
  524. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  525. t.Error(string(output))
  526. }
  527. // Set a UID
  528. container, err = runtime.Create(&Config{
  529. Image: GetTestImage(runtime).Id,
  530. Cmd: []string{"id"},
  531. User: "0",
  532. },
  533. )
  534. if err != nil || container.State.ExitCode != 0 {
  535. t.Fatal(err)
  536. }
  537. defer runtime.Destroy(container)
  538. output, err = container.Output()
  539. if err != nil || container.State.ExitCode != 0 {
  540. t.Fatal(err)
  541. }
  542. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  543. t.Error(string(output))
  544. }
  545. // Set a different user by uid
  546. container, err = runtime.Create(&Config{
  547. Image: GetTestImage(runtime).Id,
  548. Cmd: []string{"id"},
  549. User: "1",
  550. },
  551. )
  552. if err != nil {
  553. t.Fatal(err)
  554. }
  555. defer runtime.Destroy(container)
  556. output, err = container.Output()
  557. if err != nil {
  558. t.Fatal(err)
  559. } else if container.State.ExitCode != 0 {
  560. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
  561. }
  562. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  563. t.Error(string(output))
  564. }
  565. // Set a different user by username
  566. container, err = runtime.Create(&Config{
  567. Image: GetTestImage(runtime).Id,
  568. Cmd: []string{"id"},
  569. User: "daemon",
  570. },
  571. )
  572. if err != nil {
  573. t.Fatal(err)
  574. }
  575. defer runtime.Destroy(container)
  576. output, err = container.Output()
  577. if err != nil || container.State.ExitCode != 0 {
  578. t.Fatal(err)
  579. }
  580. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  581. t.Error(string(output))
  582. }
  583. }
  584. func TestMultipleContainers(t *testing.T) {
  585. runtime, err := newTestRuntime()
  586. if err != nil {
  587. t.Fatal(err)
  588. }
  589. defer nuke(runtime)
  590. container1, err := runtime.Create(&Config{
  591. Image: GetTestImage(runtime).Id,
  592. Cmd: []string{"cat", "/dev/zero"},
  593. },
  594. )
  595. if err != nil {
  596. t.Fatal(err)
  597. }
  598. defer runtime.Destroy(container1)
  599. container2, err := runtime.Create(&Config{
  600. Image: GetTestImage(runtime).Id,
  601. Cmd: []string{"cat", "/dev/zero"},
  602. },
  603. )
  604. if err != nil {
  605. t.Fatal(err)
  606. }
  607. defer runtime.Destroy(container2)
  608. // Start both containers
  609. if err := container1.Start(); err != nil {
  610. t.Fatal(err)
  611. }
  612. if err := container2.Start(); err != nil {
  613. t.Fatal(err)
  614. }
  615. // If we are here, both containers should be running
  616. if !container1.State.Running {
  617. t.Fatal("Container not running")
  618. }
  619. if !container2.State.Running {
  620. t.Fatal("Container not running")
  621. }
  622. // Kill them
  623. if err := container1.Kill(); err != nil {
  624. t.Fatal(err)
  625. }
  626. if err := container2.Kill(); err != nil {
  627. t.Fatal(err)
  628. }
  629. }
  630. func TestStdin(t *testing.T) {
  631. runtime, err := newTestRuntime()
  632. if err != nil {
  633. t.Fatal(err)
  634. }
  635. defer nuke(runtime)
  636. container, err := runtime.Create(&Config{
  637. Image: GetTestImage(runtime).Id,
  638. Cmd: []string{"cat"},
  639. OpenStdin: true,
  640. },
  641. )
  642. if err != nil {
  643. t.Fatal(err)
  644. }
  645. defer runtime.Destroy(container)
  646. stdin, err := container.StdinPipe()
  647. if err != nil {
  648. t.Fatal(err)
  649. }
  650. stdout, err := container.StdoutPipe()
  651. if err != nil {
  652. t.Fatal(err)
  653. }
  654. if err := container.Start(); err != nil {
  655. t.Fatal(err)
  656. }
  657. defer stdin.Close()
  658. defer stdout.Close()
  659. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  660. t.Fatal(err)
  661. }
  662. if err := stdin.Close(); err != nil {
  663. t.Fatal(err)
  664. }
  665. container.Wait()
  666. output, err := ioutil.ReadAll(stdout)
  667. if err != nil {
  668. t.Fatal(err)
  669. }
  670. if string(output) != "hello world" {
  671. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  672. }
  673. }
  674. func TestTty(t *testing.T) {
  675. runtime, err := newTestRuntime()
  676. if err != nil {
  677. t.Fatal(err)
  678. }
  679. defer nuke(runtime)
  680. container, err := runtime.Create(&Config{
  681. Image: GetTestImage(runtime).Id,
  682. Cmd: []string{"cat"},
  683. OpenStdin: true,
  684. },
  685. )
  686. if err != nil {
  687. t.Fatal(err)
  688. }
  689. defer runtime.Destroy(container)
  690. stdin, err := container.StdinPipe()
  691. if err != nil {
  692. t.Fatal(err)
  693. }
  694. stdout, err := container.StdoutPipe()
  695. if err != nil {
  696. t.Fatal(err)
  697. }
  698. if err := container.Start(); err != nil {
  699. t.Fatal(err)
  700. }
  701. defer stdin.Close()
  702. defer stdout.Close()
  703. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  704. t.Fatal(err)
  705. }
  706. if err := stdin.Close(); err != nil {
  707. t.Fatal(err)
  708. }
  709. container.Wait()
  710. output, err := ioutil.ReadAll(stdout)
  711. if err != nil {
  712. t.Fatal(err)
  713. }
  714. if string(output) != "hello world" {
  715. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  716. }
  717. }
  718. func TestEnv(t *testing.T) {
  719. runtime, err := newTestRuntime()
  720. if err != nil {
  721. t.Fatal(err)
  722. }
  723. defer nuke(runtime)
  724. container, err := runtime.Create(&Config{
  725. Image: GetTestImage(runtime).Id,
  726. Cmd: []string{"/usr/bin/env"},
  727. },
  728. )
  729. if err != nil {
  730. t.Fatal(err)
  731. }
  732. defer runtime.Destroy(container)
  733. stdout, err := container.StdoutPipe()
  734. if err != nil {
  735. t.Fatal(err)
  736. }
  737. defer stdout.Close()
  738. if err := container.Start(); err != nil {
  739. t.Fatal(err)
  740. }
  741. container.Wait()
  742. output, err := ioutil.ReadAll(stdout)
  743. if err != nil {
  744. t.Fatal(err)
  745. }
  746. actualEnv := strings.Split(string(output), "\n")
  747. if actualEnv[len(actualEnv)-1] == "" {
  748. actualEnv = actualEnv[:len(actualEnv)-1]
  749. }
  750. sort.Strings(actualEnv)
  751. goodEnv := []string{
  752. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  753. "HOME=/",
  754. }
  755. sort.Strings(goodEnv)
  756. if len(goodEnv) != len(actualEnv) {
  757. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  758. }
  759. for i := range goodEnv {
  760. if actualEnv[i] != goodEnv[i] {
  761. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  762. }
  763. }
  764. }
  765. func grepFile(t *testing.T, path string, pattern string) {
  766. f, err := os.Open(path)
  767. if err != nil {
  768. t.Fatal(err)
  769. }
  770. defer f.Close()
  771. r := bufio.NewReader(f)
  772. var (
  773. line string
  774. )
  775. err = nil
  776. for err == nil {
  777. line, err = r.ReadString('\n')
  778. if strings.Contains(line, pattern) == true {
  779. return
  780. }
  781. }
  782. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  783. }
  784. func TestLXCConfig(t *testing.T) {
  785. runtime, err := newTestRuntime()
  786. if err != nil {
  787. t.Fatal(err)
  788. }
  789. defer nuke(runtime)
  790. // Memory is allocated randomly for testing
  791. rand.Seed(time.Now().UTC().UnixNano())
  792. memMin := 33554432
  793. memMax := 536870912
  794. mem := memMin + rand.Intn(memMax-memMin)
  795. container, err := runtime.Create(&Config{
  796. Image: GetTestImage(runtime).Id,
  797. Cmd: []string{"/bin/true"},
  798. Hostname: "foobar",
  799. Memory: int64(mem),
  800. },
  801. )
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. defer runtime.Destroy(container)
  806. container.generateLXCConfig()
  807. grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
  808. grepFile(t, container.lxcConfigPath(),
  809. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  810. grepFile(t, container.lxcConfigPath(),
  811. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  812. }
  813. func BenchmarkRunSequencial(b *testing.B) {
  814. runtime, err := newTestRuntime()
  815. if err != nil {
  816. b.Fatal(err)
  817. }
  818. defer nuke(runtime)
  819. for i := 0; i < b.N; i++ {
  820. container, err := runtime.Create(&Config{
  821. Image: GetTestImage(runtime).Id,
  822. Cmd: []string{"echo", "-n", "foo"},
  823. },
  824. )
  825. if err != nil {
  826. b.Fatal(err)
  827. }
  828. defer runtime.Destroy(container)
  829. output, err := container.Output()
  830. if err != nil {
  831. b.Fatal(err)
  832. }
  833. if string(output) != "foo" {
  834. b.Fatalf("Unexpected output: %s", output)
  835. }
  836. if err := runtime.Destroy(container); err != nil {
  837. b.Fatal(err)
  838. }
  839. }
  840. }
  841. func BenchmarkRunParallel(b *testing.B) {
  842. runtime, err := newTestRuntime()
  843. if err != nil {
  844. b.Fatal(err)
  845. }
  846. defer nuke(runtime)
  847. var tasks []chan error
  848. for i := 0; i < b.N; i++ {
  849. complete := make(chan error)
  850. tasks = append(tasks, complete)
  851. go func(i int, complete chan error) {
  852. container, err := runtime.Create(&Config{
  853. Image: GetTestImage(runtime).Id,
  854. Cmd: []string{"echo", "-n", "foo"},
  855. },
  856. )
  857. if err != nil {
  858. complete <- err
  859. return
  860. }
  861. defer runtime.Destroy(container)
  862. if err := container.Start(); err != nil {
  863. complete <- err
  864. return
  865. }
  866. if err := container.WaitTimeout(15 * time.Second); err != nil {
  867. complete <- err
  868. return
  869. }
  870. // if string(output) != "foo" {
  871. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  872. // }
  873. if err := runtime.Destroy(container); err != nil {
  874. complete <- err
  875. return
  876. }
  877. complete <- nil
  878. }(i, complete)
  879. }
  880. var errors []error
  881. for _, task := range tasks {
  882. err := <-task
  883. if err != nil {
  884. errors = append(errors, err)
  885. }
  886. }
  887. if len(errors) > 0 {
  888. b.Fatal(errors)
  889. }
  890. }