container_test.go 20 KB

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