container_test.go 24 KB

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