container_test.go 19 KB

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