container_test.go 21 KB

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