container_test.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. package docker
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "math/rand"
  8. "os"
  9. "path"
  10. "regexp"
  11. "sort"
  12. "strings"
  13. "testing"
  14. "time"
  15. )
  16. func TestIDFormat(t *testing.T) {
  17. runtime := mkRuntime(t)
  18. defer nuke(runtime)
  19. container1, err := runtime.Create(
  20. &Config{
  21. Image: GetTestImage(runtime).ID,
  22. Cmd: []string{"/bin/sh", "-c", "echo hello world"},
  23. },
  24. )
  25. if err != nil {
  26. t.Fatal(err)
  27. }
  28. match, err := regexp.Match("^[0-9a-f]{64}$", []byte(container1.ID))
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if !match {
  33. t.Fatalf("Invalid container ID: %s", container1.ID)
  34. }
  35. }
  36. func TestMultipleAttachRestart(t *testing.T) {
  37. runtime := mkRuntime(t)
  38. defer nuke(runtime)
  39. container, hostConfig, _ := mkContainer(
  40. runtime,
  41. []string{"_", "/bin/sh", "-c", "i=1; while [ $i -le 5 ]; do i=`expr $i + 1`; echo hello; done"},
  42. t,
  43. )
  44. defer runtime.Destroy(container)
  45. // Simulate 3 client attaching to the container and stop/restart
  46. stdout1, err := container.StdoutPipe()
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. stdout2, err := container.StdoutPipe()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. stdout3, err := container.StdoutPipe()
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. if err := container.Start(hostConfig); err != nil {
  59. t.Fatal(err)
  60. }
  61. l1, err := bufio.NewReader(stdout1).ReadString('\n')
  62. if err != nil {
  63. t.Fatal(err)
  64. }
  65. if strings.Trim(l1, " \r\n") != "hello" {
  66. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
  67. }
  68. l2, err := bufio.NewReader(stdout2).ReadString('\n')
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if strings.Trim(l2, " \r\n") != "hello" {
  73. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
  74. }
  75. l3, err := bufio.NewReader(stdout3).ReadString('\n')
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if strings.Trim(l3, " \r\n") != "hello" {
  80. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
  81. }
  82. if err := container.Stop(10); err != nil {
  83. t.Fatal(err)
  84. }
  85. stdout1, err = container.StdoutPipe()
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. stdout2, err = container.StdoutPipe()
  90. if err != nil {
  91. t.Fatal(err)
  92. }
  93. stdout3, err = container.StdoutPipe()
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. if err := container.Start(hostConfig); err != nil {
  98. t.Fatal(err)
  99. }
  100. setTimeout(t, "Timeout reading from the process", 3*time.Second, func() {
  101. l1, err = bufio.NewReader(stdout1).ReadString('\n')
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. if strings.Trim(l1, " \r\n") != "hello" {
  106. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l1)
  107. }
  108. l2, err = bufio.NewReader(stdout2).ReadString('\n')
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. if strings.Trim(l2, " \r\n") != "hello" {
  113. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l2)
  114. }
  115. l3, err = bufio.NewReader(stdout3).ReadString('\n')
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. if strings.Trim(l3, " \r\n") != "hello" {
  120. t.Fatalf("Unexpected output. Expected [%s], received [%s]", "hello", l3)
  121. }
  122. })
  123. container.Wait()
  124. }
  125. func TestDiff(t *testing.T) {
  126. runtime := mkRuntime(t)
  127. defer nuke(runtime)
  128. // Create a container and remove a file
  129. container1, _, _ := mkContainer(runtime, []string{"_", "/bin/rm", "/etc/passwd"}, t)
  130. defer runtime.Destroy(container1)
  131. // The changelog should be empty and not fail before run. See #1705
  132. c, err := container1.Changes()
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if len(c) != 0 {
  137. t.Fatalf("Changelog should be empty before run")
  138. }
  139. if err := container1.Run(); err != nil {
  140. t.Fatal(err)
  141. }
  142. // Check the changelog
  143. c, err = container1.Changes()
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. success := false
  148. for _, elem := range c {
  149. if elem.Path == "/etc/passwd" && elem.Kind == 2 {
  150. success = true
  151. }
  152. }
  153. if !success {
  154. t.Fatalf("/etc/passwd as been removed but is not present in the diff")
  155. }
  156. // Commit the container
  157. rwTar, err := container1.ExportRw()
  158. if err != nil {
  159. t.Error(err)
  160. }
  161. img, err := runtime.graph.Create(rwTar, container1, "unit test commited image - diff", "", nil)
  162. if err != nil {
  163. t.Error(err)
  164. }
  165. // Create a new container from the commited image
  166. container2, _, _ := mkContainer(runtime, []string{img.ID, "cat", "/etc/passwd"}, t)
  167. defer runtime.Destroy(container2)
  168. if err := container2.Run(); err != nil {
  169. t.Fatal(err)
  170. }
  171. // Check the changelog
  172. c, err = container2.Changes()
  173. if err != nil {
  174. t.Fatal(err)
  175. }
  176. for _, elem := range c {
  177. if elem.Path == "/etc/passwd" {
  178. t.Fatalf("/etc/passwd should not be present in the diff after commit.")
  179. }
  180. }
  181. // Create a new container
  182. container3, _, _ := mkContainer(runtime, []string{"_", "rm", "/bin/httpd"}, t)
  183. defer runtime.Destroy(container3)
  184. if err := container3.Run(); err != nil {
  185. t.Fatal(err)
  186. }
  187. // Check the changelog
  188. c, err = container3.Changes()
  189. if err != nil {
  190. t.Fatal(err)
  191. }
  192. success = false
  193. for _, elem := range c {
  194. if elem.Path == "/bin/httpd" && elem.Kind == 2 {
  195. success = true
  196. }
  197. }
  198. if !success {
  199. t.Fatalf("/bin/httpd should be present in the diff after commit.")
  200. }
  201. }
  202. func TestCommitAutoRun(t *testing.T) {
  203. runtime := mkRuntime(t)
  204. defer nuke(runtime)
  205. container1, _, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
  206. defer runtime.Destroy(container1)
  207. if container1.State.Running {
  208. t.Errorf("Container shouldn't be running")
  209. }
  210. if err := container1.Run(); err != nil {
  211. t.Fatal(err)
  212. }
  213. if container1.State.Running {
  214. t.Errorf("Container shouldn't be running")
  215. }
  216. rwTar, err := container1.ExportRw()
  217. if err != nil {
  218. t.Error(err)
  219. }
  220. img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", &Config{Cmd: []string{"cat", "/world"}})
  221. if err != nil {
  222. t.Error(err)
  223. }
  224. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  225. container2, hostConfig, _ := mkContainer(runtime, []string{img.ID}, t)
  226. defer runtime.Destroy(container2)
  227. stdout, err := container2.StdoutPipe()
  228. if err != nil {
  229. t.Fatal(err)
  230. }
  231. stderr, err := container2.StderrPipe()
  232. if err != nil {
  233. t.Fatal(err)
  234. }
  235. if err := container2.Start(hostConfig); err != nil {
  236. t.Fatal(err)
  237. }
  238. container2.Wait()
  239. output, err := ioutil.ReadAll(stdout)
  240. if err != nil {
  241. t.Fatal(err)
  242. }
  243. output2, err := ioutil.ReadAll(stderr)
  244. if err != nil {
  245. t.Fatal(err)
  246. }
  247. if err := stdout.Close(); err != nil {
  248. t.Fatal(err)
  249. }
  250. if err := stderr.Close(); err != nil {
  251. t.Fatal(err)
  252. }
  253. if string(output) != "hello\n" {
  254. t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
  255. }
  256. }
  257. func TestCommitRun(t *testing.T) {
  258. runtime := mkRuntime(t)
  259. defer nuke(runtime)
  260. container1, hostConfig, _ := mkContainer(runtime, []string{"_", "/bin/sh", "-c", "echo hello > /world"}, t)
  261. defer runtime.Destroy(container1)
  262. if container1.State.Running {
  263. t.Errorf("Container shouldn't be running")
  264. }
  265. if err := container1.Run(); err != nil {
  266. t.Fatal(err)
  267. }
  268. if container1.State.Running {
  269. t.Errorf("Container shouldn't be running")
  270. }
  271. rwTar, err := container1.ExportRw()
  272. if err != nil {
  273. t.Error(err)
  274. }
  275. img, err := runtime.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  276. if err != nil {
  277. t.Error(err)
  278. }
  279. // FIXME: Make a TestCommit that stops here and check docker.root/layers/img.id/world
  280. container2, hostConfig, _ := mkContainer(runtime, []string{img.ID, "cat", "/world"}, t)
  281. defer runtime.Destroy(container2)
  282. stdout, err := container2.StdoutPipe()
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. stderr, err := container2.StderrPipe()
  287. if err != nil {
  288. t.Fatal(err)
  289. }
  290. if err := container2.Start(hostConfig); err != nil {
  291. t.Fatal(err)
  292. }
  293. container2.Wait()
  294. output, err := ioutil.ReadAll(stdout)
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. output2, err := ioutil.ReadAll(stderr)
  299. if err != nil {
  300. t.Fatal(err)
  301. }
  302. if err := stdout.Close(); err != nil {
  303. t.Fatal(err)
  304. }
  305. if err := stderr.Close(); err != nil {
  306. t.Fatal(err)
  307. }
  308. if string(output) != "hello\n" {
  309. t.Fatalf("Unexpected output. Expected %s, received: %s (err: %s)", "hello\n", output, output2)
  310. }
  311. }
  312. func TestStart(t *testing.T) {
  313. runtime := mkRuntime(t)
  314. defer nuke(runtime)
  315. container, hostConfig, _ := mkContainer(runtime, []string{"-m", "33554432", "-c", "1000", "-i", "_", "/bin/cat"}, t)
  316. defer runtime.Destroy(container)
  317. cStdin, err := container.StdinPipe()
  318. if err != nil {
  319. t.Fatal(err)
  320. }
  321. if err := container.Start(hostConfig); err != nil {
  322. t.Fatal(err)
  323. }
  324. // Give some time to the process to start
  325. container.WaitTimeout(500 * time.Millisecond)
  326. if !container.State.Running {
  327. t.Errorf("Container should be running")
  328. }
  329. if err := container.Start(hostConfig); err == nil {
  330. t.Fatalf("A running container should be able to be started")
  331. }
  332. // Try to avoid the timeout in destroy. Best effort, don't check error
  333. cStdin.Close()
  334. container.WaitTimeout(2 * time.Second)
  335. }
  336. func TestRun(t *testing.T) {
  337. runtime := mkRuntime(t)
  338. defer nuke(runtime)
  339. container, _, _ := mkContainer(runtime, []string{"_", "ls", "-al"}, t)
  340. defer runtime.Destroy(container)
  341. if container.State.Running {
  342. t.Errorf("Container shouldn't be running")
  343. }
  344. if err := container.Run(); err != nil {
  345. t.Fatal(err)
  346. }
  347. if container.State.Running {
  348. t.Errorf("Container shouldn't be running")
  349. }
  350. }
  351. func TestOutput(t *testing.T) {
  352. runtime := mkRuntime(t)
  353. defer nuke(runtime)
  354. container, err := runtime.Create(
  355. &Config{
  356. Image: GetTestImage(runtime).ID,
  357. Cmd: []string{"echo", "-n", "foobar"},
  358. },
  359. )
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. defer runtime.Destroy(container)
  364. output, err := container.Output()
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. if string(output) != "foobar" {
  369. t.Error(string(output))
  370. }
  371. }
  372. func TestKillDifferentUser(t *testing.T) {
  373. runtime := mkRuntime(t)
  374. defer nuke(runtime)
  375. container, err := runtime.Create(&Config{
  376. Image: GetTestImage(runtime).ID,
  377. Cmd: []string{"cat"},
  378. OpenStdin: true,
  379. User: "daemon",
  380. },
  381. )
  382. if err != nil {
  383. t.Fatal(err)
  384. }
  385. defer runtime.Destroy(container)
  386. defer container.stdin.Close()
  387. if container.State.Running {
  388. t.Errorf("Container shouldn't be running")
  389. }
  390. if err := container.Start(&HostConfig{}); err != nil {
  391. t.Fatal(err)
  392. }
  393. setTimeout(t, "Waiting for the container to be started timed out", 2*time.Second, func() {
  394. for !container.State.Running {
  395. time.Sleep(10 * time.Millisecond)
  396. }
  397. })
  398. setTimeout(t, "read/write assertion timed out", 2*time.Second, func() {
  399. out, _ := container.StdoutPipe()
  400. in, _ := container.StdinPipe()
  401. if err := assertPipe("hello\n", "hello", out, in, 15); err != nil {
  402. t.Fatal(err)
  403. }
  404. })
  405. if err := container.Kill(); err != nil {
  406. t.Fatal(err)
  407. }
  408. if container.State.Running {
  409. t.Errorf("Container shouldn't be running")
  410. }
  411. container.Wait()
  412. if container.State.Running {
  413. t.Errorf("Container shouldn't be running")
  414. }
  415. // Try stopping twice
  416. if err := container.Kill(); err != nil {
  417. t.Fatal(err)
  418. }
  419. }
  420. // Test that creating a container with a volume doesn't crash. Regression test for #995.
  421. func TestCreateVolume(t *testing.T) {
  422. runtime := mkRuntime(t)
  423. defer nuke(runtime)
  424. config, hc, _, err := ParseRun([]string{"-v", "/var/lib/data", GetTestImage(runtime).ID, "echo", "hello", "world"}, nil)
  425. if err != nil {
  426. t.Fatal(err)
  427. }
  428. c, err := runtime.Create(config)
  429. if err != nil {
  430. t.Fatal(err)
  431. }
  432. defer runtime.Destroy(c)
  433. if err := c.Start(hc); err != nil {
  434. t.Fatal(err)
  435. }
  436. c.WaitTimeout(500 * time.Millisecond)
  437. c.Wait()
  438. }
  439. func TestKill(t *testing.T) {
  440. runtime := mkRuntime(t)
  441. defer nuke(runtime)
  442. container, err := runtime.Create(&Config{
  443. Image: GetTestImage(runtime).ID,
  444. Cmd: []string{"sleep", "2"},
  445. },
  446. )
  447. if err != nil {
  448. t.Fatal(err)
  449. }
  450. defer runtime.Destroy(container)
  451. if container.State.Running {
  452. t.Errorf("Container shouldn't be running")
  453. }
  454. hostConfig := &HostConfig{}
  455. if err := container.Start(hostConfig); err != nil {
  456. t.Fatal(err)
  457. }
  458. // Give some time to lxc to spawn the process
  459. container.WaitTimeout(500 * time.Millisecond)
  460. if !container.State.Running {
  461. t.Errorf("Container should be running")
  462. }
  463. if err := container.Kill(); err != nil {
  464. t.Fatal(err)
  465. }
  466. if container.State.Running {
  467. t.Errorf("Container shouldn't be running")
  468. }
  469. container.Wait()
  470. if container.State.Running {
  471. t.Errorf("Container shouldn't be running")
  472. }
  473. // Try stopping twice
  474. if err := container.Kill(); err != nil {
  475. t.Fatal(err)
  476. }
  477. }
  478. func TestExitCode(t *testing.T) {
  479. runtime := mkRuntime(t)
  480. defer nuke(runtime)
  481. trueContainer, err := runtime.Create(&Config{
  482. Image: GetTestImage(runtime).ID,
  483. Cmd: []string{"/bin/true", ""},
  484. })
  485. if err != nil {
  486. t.Fatal(err)
  487. }
  488. defer runtime.Destroy(trueContainer)
  489. if err := trueContainer.Run(); err != nil {
  490. t.Fatal(err)
  491. }
  492. if trueContainer.State.ExitCode != 0 {
  493. t.Errorf("Unexpected exit code %d (expected 0)", trueContainer.State.ExitCode)
  494. }
  495. falseContainer, err := runtime.Create(&Config{
  496. Image: GetTestImage(runtime).ID,
  497. Cmd: []string{"/bin/false", ""},
  498. })
  499. if err != nil {
  500. t.Fatal(err)
  501. }
  502. defer runtime.Destroy(falseContainer)
  503. if err := falseContainer.Run(); err != nil {
  504. t.Fatal(err)
  505. }
  506. if falseContainer.State.ExitCode != 1 {
  507. t.Errorf("Unexpected exit code %d (expected 1)", falseContainer.State.ExitCode)
  508. }
  509. }
  510. func TestRestart(t *testing.T) {
  511. runtime := mkRuntime(t)
  512. defer nuke(runtime)
  513. container, err := runtime.Create(&Config{
  514. Image: GetTestImage(runtime).ID,
  515. Cmd: []string{"echo", "-n", "foobar"},
  516. },
  517. )
  518. if err != nil {
  519. t.Fatal(err)
  520. }
  521. defer runtime.Destroy(container)
  522. output, err := container.Output()
  523. if err != nil {
  524. t.Fatal(err)
  525. }
  526. if string(output) != "foobar" {
  527. t.Error(string(output))
  528. }
  529. // Run the container again and check the output
  530. output, err = container.Output()
  531. if err != nil {
  532. t.Fatal(err)
  533. }
  534. if string(output) != "foobar" {
  535. t.Error(string(output))
  536. }
  537. }
  538. func TestRestartStdin(t *testing.T) {
  539. runtime := mkRuntime(t)
  540. defer nuke(runtime)
  541. container, err := runtime.Create(&Config{
  542. Image: GetTestImage(runtime).ID,
  543. Cmd: []string{"cat"},
  544. OpenStdin: true,
  545. },
  546. )
  547. if err != nil {
  548. t.Fatal(err)
  549. }
  550. defer runtime.Destroy(container)
  551. stdin, err := container.StdinPipe()
  552. if err != nil {
  553. t.Fatal(err)
  554. }
  555. stdout, err := container.StdoutPipe()
  556. if err != nil {
  557. t.Fatal(err)
  558. }
  559. hostConfig := &HostConfig{}
  560. if err := container.Start(hostConfig); err != nil {
  561. t.Fatal(err)
  562. }
  563. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  564. t.Fatal(err)
  565. }
  566. if err := stdin.Close(); err != nil {
  567. t.Fatal(err)
  568. }
  569. container.Wait()
  570. output, err := ioutil.ReadAll(stdout)
  571. if err != nil {
  572. t.Fatal(err)
  573. }
  574. if err := stdout.Close(); err != nil {
  575. t.Fatal(err)
  576. }
  577. if string(output) != "hello world" {
  578. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  579. }
  580. // Restart and try again
  581. stdin, err = container.StdinPipe()
  582. if err != nil {
  583. t.Fatal(err)
  584. }
  585. stdout, err = container.StdoutPipe()
  586. if err != nil {
  587. t.Fatal(err)
  588. }
  589. if err := container.Start(hostConfig); err != nil {
  590. t.Fatal(err)
  591. }
  592. if _, err := io.WriteString(stdin, "hello world #2"); err != nil {
  593. t.Fatal(err)
  594. }
  595. if err := stdin.Close(); err != nil {
  596. t.Fatal(err)
  597. }
  598. container.Wait()
  599. output, err = ioutil.ReadAll(stdout)
  600. if err != nil {
  601. t.Fatal(err)
  602. }
  603. if err := stdout.Close(); err != nil {
  604. t.Fatal(err)
  605. }
  606. if string(output) != "hello world #2" {
  607. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world #2", string(output))
  608. }
  609. }
  610. func TestUser(t *testing.T) {
  611. runtime := mkRuntime(t)
  612. defer nuke(runtime)
  613. // Default user must be root
  614. container, err := runtime.Create(&Config{
  615. Image: GetTestImage(runtime).ID,
  616. Cmd: []string{"id"},
  617. },
  618. )
  619. if err != nil {
  620. t.Fatal(err)
  621. }
  622. defer runtime.Destroy(container)
  623. output, err := container.Output()
  624. if err != nil {
  625. t.Fatal(err)
  626. }
  627. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  628. t.Error(string(output))
  629. }
  630. // Set a username
  631. container, err = runtime.Create(&Config{
  632. Image: GetTestImage(runtime).ID,
  633. Cmd: []string{"id"},
  634. User: "root",
  635. },
  636. )
  637. if err != nil {
  638. t.Fatal(err)
  639. }
  640. defer runtime.Destroy(container)
  641. output, err = container.Output()
  642. if err != nil || container.State.ExitCode != 0 {
  643. t.Fatal(err)
  644. }
  645. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  646. t.Error(string(output))
  647. }
  648. // Set a UID
  649. container, err = runtime.Create(&Config{
  650. Image: GetTestImage(runtime).ID,
  651. Cmd: []string{"id"},
  652. User: "0",
  653. },
  654. )
  655. if err != nil || container.State.ExitCode != 0 {
  656. t.Fatal(err)
  657. }
  658. defer runtime.Destroy(container)
  659. output, err = container.Output()
  660. if err != nil || container.State.ExitCode != 0 {
  661. t.Fatal(err)
  662. }
  663. if !strings.Contains(string(output), "uid=0(root) gid=0(root)") {
  664. t.Error(string(output))
  665. }
  666. // Set a different user by uid
  667. container, err = runtime.Create(&Config{
  668. Image: GetTestImage(runtime).ID,
  669. Cmd: []string{"id"},
  670. User: "1",
  671. },
  672. )
  673. if err != nil {
  674. t.Fatal(err)
  675. }
  676. defer runtime.Destroy(container)
  677. output, err = container.Output()
  678. if err != nil {
  679. t.Fatal(err)
  680. } else if container.State.ExitCode != 0 {
  681. t.Fatalf("Container exit code is invalid: %d\nOutput:\n%s\n", container.State.ExitCode, output)
  682. }
  683. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  684. t.Error(string(output))
  685. }
  686. // Set a different user by username
  687. container, err = runtime.Create(&Config{
  688. Image: GetTestImage(runtime).ID,
  689. Cmd: []string{"id"},
  690. User: "daemon",
  691. },
  692. )
  693. if err != nil {
  694. t.Fatal(err)
  695. }
  696. defer runtime.Destroy(container)
  697. output, err = container.Output()
  698. if err != nil || container.State.ExitCode != 0 {
  699. t.Fatal(err)
  700. }
  701. if !strings.Contains(string(output), "uid=1(daemon) gid=1(daemon)") {
  702. t.Error(string(output))
  703. }
  704. // Test an wrong username
  705. container, err = runtime.Create(&Config{
  706. Image: GetTestImage(runtime).ID,
  707. Cmd: []string{"id"},
  708. User: "unknownuser",
  709. },
  710. )
  711. if err != nil {
  712. t.Fatal(err)
  713. }
  714. defer runtime.Destroy(container)
  715. output, err = container.Output()
  716. if container.State.ExitCode == 0 {
  717. t.Fatal("Starting container with wrong uid should fail but it passed.")
  718. }
  719. }
  720. func TestMultipleContainers(t *testing.T) {
  721. runtime := mkRuntime(t)
  722. defer nuke(runtime)
  723. container1, err := runtime.Create(&Config{
  724. Image: GetTestImage(runtime).ID,
  725. Cmd: []string{"sleep", "2"},
  726. },
  727. )
  728. if err != nil {
  729. t.Fatal(err)
  730. }
  731. defer runtime.Destroy(container1)
  732. container2, err := runtime.Create(&Config{
  733. Image: GetTestImage(runtime).ID,
  734. Cmd: []string{"sleep", "2"},
  735. },
  736. )
  737. if err != nil {
  738. t.Fatal(err)
  739. }
  740. defer runtime.Destroy(container2)
  741. // Start both containers
  742. hostConfig := &HostConfig{}
  743. if err := container1.Start(hostConfig); err != nil {
  744. t.Fatal(err)
  745. }
  746. if err := container2.Start(hostConfig); err != nil {
  747. t.Fatal(err)
  748. }
  749. // Make sure they are running before trying to kill them
  750. container1.WaitTimeout(250 * time.Millisecond)
  751. container2.WaitTimeout(250 * time.Millisecond)
  752. // If we are here, both containers should be running
  753. if !container1.State.Running {
  754. t.Fatal("Container not running")
  755. }
  756. if !container2.State.Running {
  757. t.Fatal("Container not running")
  758. }
  759. // Kill them
  760. if err := container1.Kill(); err != nil {
  761. t.Fatal(err)
  762. }
  763. if err := container2.Kill(); err != nil {
  764. t.Fatal(err)
  765. }
  766. }
  767. func TestStdin(t *testing.T) {
  768. runtime := mkRuntime(t)
  769. defer nuke(runtime)
  770. container, err := runtime.Create(&Config{
  771. Image: GetTestImage(runtime).ID,
  772. Cmd: []string{"cat"},
  773. OpenStdin: true,
  774. },
  775. )
  776. if err != nil {
  777. t.Fatal(err)
  778. }
  779. defer runtime.Destroy(container)
  780. stdin, err := container.StdinPipe()
  781. if err != nil {
  782. t.Fatal(err)
  783. }
  784. stdout, err := container.StdoutPipe()
  785. if err != nil {
  786. t.Fatal(err)
  787. }
  788. hostConfig := &HostConfig{}
  789. if err := container.Start(hostConfig); err != nil {
  790. t.Fatal(err)
  791. }
  792. defer stdin.Close()
  793. defer stdout.Close()
  794. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  795. t.Fatal(err)
  796. }
  797. if err := stdin.Close(); err != nil {
  798. t.Fatal(err)
  799. }
  800. container.Wait()
  801. output, err := ioutil.ReadAll(stdout)
  802. if err != nil {
  803. t.Fatal(err)
  804. }
  805. if string(output) != "hello world" {
  806. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  807. }
  808. }
  809. func TestTty(t *testing.T) {
  810. runtime := mkRuntime(t)
  811. defer nuke(runtime)
  812. container, err := runtime.Create(&Config{
  813. Image: GetTestImage(runtime).ID,
  814. Cmd: []string{"cat"},
  815. OpenStdin: true,
  816. },
  817. )
  818. if err != nil {
  819. t.Fatal(err)
  820. }
  821. defer runtime.Destroy(container)
  822. stdin, err := container.StdinPipe()
  823. if err != nil {
  824. t.Fatal(err)
  825. }
  826. stdout, err := container.StdoutPipe()
  827. if err != nil {
  828. t.Fatal(err)
  829. }
  830. hostConfig := &HostConfig{}
  831. if err := container.Start(hostConfig); err != nil {
  832. t.Fatal(err)
  833. }
  834. defer stdin.Close()
  835. defer stdout.Close()
  836. if _, err := io.WriteString(stdin, "hello world"); err != nil {
  837. t.Fatal(err)
  838. }
  839. if err := stdin.Close(); err != nil {
  840. t.Fatal(err)
  841. }
  842. container.Wait()
  843. output, err := ioutil.ReadAll(stdout)
  844. if err != nil {
  845. t.Fatal(err)
  846. }
  847. if string(output) != "hello world" {
  848. t.Fatalf("Unexpected output. Expected %s, received: %s", "hello world", string(output))
  849. }
  850. }
  851. func TestEnv(t *testing.T) {
  852. runtime := mkRuntime(t)
  853. defer nuke(runtime)
  854. container, err := runtime.Create(&Config{
  855. Image: GetTestImage(runtime).ID,
  856. Cmd: []string{"env"},
  857. },
  858. )
  859. if err != nil {
  860. t.Fatal(err)
  861. }
  862. defer runtime.Destroy(container)
  863. stdout, err := container.StdoutPipe()
  864. if err != nil {
  865. t.Fatal(err)
  866. }
  867. defer stdout.Close()
  868. hostConfig := &HostConfig{}
  869. if err := container.Start(hostConfig); err != nil {
  870. t.Fatal(err)
  871. }
  872. container.Wait()
  873. output, err := ioutil.ReadAll(stdout)
  874. if err != nil {
  875. t.Fatal(err)
  876. }
  877. actualEnv := strings.Split(string(output), "\n")
  878. if actualEnv[len(actualEnv)-1] == "" {
  879. actualEnv = actualEnv[:len(actualEnv)-1]
  880. }
  881. sort.Strings(actualEnv)
  882. goodEnv := []string{
  883. "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  884. "HOME=/",
  885. "container=lxc",
  886. "HOSTNAME=" + container.ShortID(),
  887. }
  888. sort.Strings(goodEnv)
  889. if len(goodEnv) != len(actualEnv) {
  890. t.Fatalf("Wrong environment: should be %d variables, not: '%s'\n", len(goodEnv), strings.Join(actualEnv, ", "))
  891. }
  892. for i := range goodEnv {
  893. if actualEnv[i] != goodEnv[i] {
  894. t.Fatalf("Wrong environment variable: should be %s, not %s", goodEnv[i], actualEnv[i])
  895. }
  896. }
  897. }
  898. func TestEntrypoint(t *testing.T) {
  899. runtime := mkRuntime(t)
  900. defer nuke(runtime)
  901. container, err := runtime.Create(
  902. &Config{
  903. Image: GetTestImage(runtime).ID,
  904. Entrypoint: []string{"/bin/echo"},
  905. Cmd: []string{"-n", "foobar"},
  906. },
  907. )
  908. if err != nil {
  909. t.Fatal(err)
  910. }
  911. defer runtime.Destroy(container)
  912. output, err := container.Output()
  913. if err != nil {
  914. t.Fatal(err)
  915. }
  916. if string(output) != "foobar" {
  917. t.Error(string(output))
  918. }
  919. }
  920. func TestEntrypointNoCmd(t *testing.T) {
  921. runtime := mkRuntime(t)
  922. defer nuke(runtime)
  923. container, err := runtime.Create(
  924. &Config{
  925. Image: GetTestImage(runtime).ID,
  926. Entrypoint: []string{"/bin/echo", "foobar"},
  927. },
  928. )
  929. if err != nil {
  930. t.Fatal(err)
  931. }
  932. defer runtime.Destroy(container)
  933. output, err := container.Output()
  934. if err != nil {
  935. t.Fatal(err)
  936. }
  937. if strings.Trim(string(output), "\r\n") != "foobar" {
  938. t.Error(string(output))
  939. }
  940. }
  941. func grepFile(t *testing.T, path string, pattern string) {
  942. f, err := os.Open(path)
  943. if err != nil {
  944. t.Fatal(err)
  945. }
  946. defer f.Close()
  947. r := bufio.NewReader(f)
  948. var (
  949. line string
  950. )
  951. err = nil
  952. for err == nil {
  953. line, err = r.ReadString('\n')
  954. if strings.Contains(line, pattern) == true {
  955. return
  956. }
  957. }
  958. t.Fatalf("grepFile: pattern \"%s\" not found in \"%s\"", pattern, path)
  959. }
  960. func TestLXCConfig(t *testing.T) {
  961. runtime := mkRuntime(t)
  962. defer nuke(runtime)
  963. // Memory is allocated randomly for testing
  964. rand.Seed(time.Now().UTC().UnixNano())
  965. memMin := 33554432
  966. memMax := 536870912
  967. mem := memMin + rand.Intn(memMax-memMin)
  968. // CPU shares as well
  969. cpuMin := 100
  970. cpuMax := 10000
  971. cpu := cpuMin + rand.Intn(cpuMax-cpuMin)
  972. container, err := runtime.Create(&Config{
  973. Image: GetTestImage(runtime).ID,
  974. Cmd: []string{"/bin/true"},
  975. Hostname: "foobar",
  976. Memory: int64(mem),
  977. CpuShares: int64(cpu),
  978. },
  979. )
  980. if err != nil {
  981. t.Fatal(err)
  982. }
  983. container.generateLXCConfig(nil)
  984. grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
  985. grepFile(t, container.lxcConfigPath(),
  986. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  987. grepFile(t, container.lxcConfigPath(),
  988. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  989. }
  990. func TestCustomLxcConfig(t *testing.T) {
  991. runtime := mkRuntime(t)
  992. defer nuke(runtime)
  993. container, err := runtime.Create(&Config{
  994. Image: GetTestImage(runtime).ID,
  995. Cmd: []string{"/bin/true"},
  996. Hostname: "foobar",
  997. },
  998. )
  999. if err != nil {
  1000. t.Fatal(err)
  1001. }
  1002. defer runtime.Destroy(container)
  1003. hostConfig := &HostConfig{LxcConf: []KeyValuePair{
  1004. {
  1005. Key: "lxc.utsname",
  1006. Value: "docker",
  1007. },
  1008. {
  1009. Key: "lxc.cgroup.cpuset.cpus",
  1010. Value: "0,1",
  1011. },
  1012. }}
  1013. container.generateLXCConfig(hostConfig)
  1014. grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
  1015. grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
  1016. }
  1017. func BenchmarkRunSequencial(b *testing.B) {
  1018. runtime := mkRuntime(b)
  1019. defer nuke(runtime)
  1020. for i := 0; i < b.N; i++ {
  1021. container, err := runtime.Create(&Config{
  1022. Image: GetTestImage(runtime).ID,
  1023. Cmd: []string{"echo", "-n", "foo"},
  1024. },
  1025. )
  1026. if err != nil {
  1027. b.Fatal(err)
  1028. }
  1029. defer runtime.Destroy(container)
  1030. output, err := container.Output()
  1031. if err != nil {
  1032. b.Fatal(err)
  1033. }
  1034. if string(output) != "foo" {
  1035. b.Fatalf("Unexpected output: %s", output)
  1036. }
  1037. if err := runtime.Destroy(container); err != nil {
  1038. b.Fatal(err)
  1039. }
  1040. }
  1041. }
  1042. func BenchmarkRunParallel(b *testing.B) {
  1043. runtime := mkRuntime(b)
  1044. defer nuke(runtime)
  1045. var tasks []chan error
  1046. for i := 0; i < b.N; i++ {
  1047. complete := make(chan error)
  1048. tasks = append(tasks, complete)
  1049. go func(i int, complete chan error) {
  1050. container, err := runtime.Create(&Config{
  1051. Image: GetTestImage(runtime).ID,
  1052. Cmd: []string{"echo", "-n", "foo"},
  1053. },
  1054. )
  1055. if err != nil {
  1056. complete <- err
  1057. return
  1058. }
  1059. defer runtime.Destroy(container)
  1060. hostConfig := &HostConfig{}
  1061. if err := container.Start(hostConfig); err != nil {
  1062. complete <- err
  1063. return
  1064. }
  1065. if err := container.WaitTimeout(15 * time.Second); err != nil {
  1066. complete <- err
  1067. return
  1068. }
  1069. // if string(output) != "foo" {
  1070. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  1071. // }
  1072. if err := runtime.Destroy(container); err != nil {
  1073. complete <- err
  1074. return
  1075. }
  1076. complete <- nil
  1077. }(i, complete)
  1078. }
  1079. var errors []error
  1080. for _, task := range tasks {
  1081. err := <-task
  1082. if err != nil {
  1083. errors = append(errors, err)
  1084. }
  1085. }
  1086. if len(errors) > 0 {
  1087. b.Fatal(errors)
  1088. }
  1089. }
  1090. func tempDir(t *testing.T) string {
  1091. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1092. if err != nil {
  1093. t.Fatal(err)
  1094. }
  1095. return tmpDir
  1096. }
  1097. // Test for #1737
  1098. func TestCopyVolumeUidGid(t *testing.T) {
  1099. r := mkRuntime(t)
  1100. defer nuke(r)
  1101. // Add directory not owned by root
  1102. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
  1103. defer r.Destroy(container1)
  1104. if container1.State.Running {
  1105. t.Errorf("Container shouldn't be running")
  1106. }
  1107. if err := container1.Run(); err != nil {
  1108. t.Fatal(err)
  1109. }
  1110. if container1.State.Running {
  1111. t.Errorf("Container shouldn't be running")
  1112. }
  1113. rwTar, err := container1.ExportRw()
  1114. if err != nil {
  1115. t.Error(err)
  1116. }
  1117. img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  1118. if err != nil {
  1119. t.Error(err)
  1120. }
  1121. // Test that the uid and gid is copied from the image to the volume
  1122. tmpDir1 := tempDir(t)
  1123. defer os.RemoveAll(tmpDir1)
  1124. stdout1, _ := runContainer(r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  1125. if !strings.Contains(stdout1, "daemon daemon") {
  1126. t.Fatal("Container failed to transfer uid and gid to volume")
  1127. }
  1128. }
  1129. // Test for #1582
  1130. func TestCopyVolumeContent(t *testing.T) {
  1131. r := mkRuntime(t)
  1132. defer nuke(r)
  1133. // Put some content in a directory of a container and commit it
  1134. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  1135. defer r.Destroy(container1)
  1136. if container1.State.Running {
  1137. t.Errorf("Container shouldn't be running")
  1138. }
  1139. if err := container1.Run(); err != nil {
  1140. t.Fatal(err)
  1141. }
  1142. if container1.State.Running {
  1143. t.Errorf("Container shouldn't be running")
  1144. }
  1145. rwTar, err := container1.ExportRw()
  1146. if err != nil {
  1147. t.Error(err)
  1148. }
  1149. img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  1150. if err != nil {
  1151. t.Error(err)
  1152. }
  1153. // Test that the content is copied from the image to the volume
  1154. tmpDir1 := tempDir(t)
  1155. defer os.RemoveAll(tmpDir1)
  1156. stdout1, _ := runContainer(r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  1157. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  1158. t.Fatal("Container failed to transfer content to volume")
  1159. }
  1160. }
  1161. func TestBindMounts(t *testing.T) {
  1162. r := mkRuntime(t)
  1163. defer nuke(r)
  1164. tmpDir := tempDir(t)
  1165. defer os.RemoveAll(tmpDir)
  1166. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1167. // Test reading from a read-only bind mount
  1168. stdout, _ := runContainer(r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  1169. if !strings.Contains(stdout, "touch-me") {
  1170. t.Fatal("Container failed to read from bind mount")
  1171. }
  1172. // test writing to bind mount
  1173. runContainer(r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  1174. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1175. // test mounting to an illegal destination directory
  1176. if _, err := runContainer(r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  1177. t.Fatal("Container bind mounted illegal directory")
  1178. }
  1179. }
  1180. // Test that VolumesRW values are copied to the new container. Regression test for #1201
  1181. func TestVolumesFromReadonlyMount(t *testing.T) {
  1182. runtime := mkRuntime(t)
  1183. defer nuke(runtime)
  1184. container, err := runtime.Create(
  1185. &Config{
  1186. Image: GetTestImage(runtime).ID,
  1187. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1188. Volumes: map[string]struct{}{"/test": {}},
  1189. },
  1190. )
  1191. if err != nil {
  1192. t.Fatal(err)
  1193. }
  1194. defer runtime.Destroy(container)
  1195. _, err = container.Output()
  1196. if err != nil {
  1197. t.Fatal(err)
  1198. }
  1199. if !container.VolumesRW["/test"] {
  1200. t.Fail()
  1201. }
  1202. container2, err := runtime.Create(
  1203. &Config{
  1204. Image: GetTestImage(runtime).ID,
  1205. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1206. VolumesFrom: container.ID,
  1207. },
  1208. )
  1209. if err != nil {
  1210. t.Fatal(err)
  1211. }
  1212. defer runtime.Destroy(container2)
  1213. _, err = container2.Output()
  1214. if err != nil {
  1215. t.Fatal(err)
  1216. }
  1217. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1218. t.Fail()
  1219. }
  1220. actual, exists := container2.VolumesRW["/test"]
  1221. if !exists {
  1222. t.Fail()
  1223. }
  1224. if container.VolumesRW["/test"] != actual {
  1225. t.Fail()
  1226. }
  1227. }
  1228. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  1229. func TestRestartWithVolumes(t *testing.T) {
  1230. runtime := mkRuntime(t)
  1231. defer nuke(runtime)
  1232. container, err := runtime.Create(&Config{
  1233. Image: GetTestImage(runtime).ID,
  1234. Cmd: []string{"echo", "-n", "foobar"},
  1235. Volumes: map[string]struct{}{"/test": {}},
  1236. },
  1237. )
  1238. if err != nil {
  1239. t.Fatal(err)
  1240. }
  1241. defer runtime.Destroy(container)
  1242. for key := range container.Config.Volumes {
  1243. if key != "/test" {
  1244. t.Fail()
  1245. }
  1246. }
  1247. _, err = container.Output()
  1248. if err != nil {
  1249. t.Fatal(err)
  1250. }
  1251. expected := container.Volumes["/test"]
  1252. if expected == "" {
  1253. t.Fail()
  1254. }
  1255. // Run the container again to verify the volume path persists
  1256. _, err = container.Output()
  1257. if err != nil {
  1258. t.Fatal(err)
  1259. }
  1260. actual := container.Volumes["/test"]
  1261. if expected != actual {
  1262. t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
  1263. }
  1264. }
  1265. // Test for #1351
  1266. func TestVolumesFromWithVolumes(t *testing.T) {
  1267. runtime := mkRuntime(t)
  1268. defer nuke(runtime)
  1269. container, err := runtime.Create(&Config{
  1270. Image: GetTestImage(runtime).ID,
  1271. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1272. Volumes: map[string]struct{}{"/test": {}},
  1273. },
  1274. )
  1275. if err != nil {
  1276. t.Fatal(err)
  1277. }
  1278. defer runtime.Destroy(container)
  1279. for key := range container.Config.Volumes {
  1280. if key != "/test" {
  1281. t.Fail()
  1282. }
  1283. }
  1284. _, err = container.Output()
  1285. if err != nil {
  1286. t.Fatal(err)
  1287. }
  1288. expected := container.Volumes["/test"]
  1289. if expected == "" {
  1290. t.Fail()
  1291. }
  1292. container2, err := runtime.Create(
  1293. &Config{
  1294. Image: GetTestImage(runtime).ID,
  1295. Cmd: []string{"cat", "/test/foo"},
  1296. VolumesFrom: container.ID,
  1297. Volumes: map[string]struct{}{"/test": {}},
  1298. },
  1299. )
  1300. if err != nil {
  1301. t.Fatal(err)
  1302. }
  1303. defer runtime.Destroy(container2)
  1304. output, err := container2.Output()
  1305. if err != nil {
  1306. t.Fatal(err)
  1307. }
  1308. if string(output) != "bar" {
  1309. t.Fail()
  1310. }
  1311. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1312. t.Fail()
  1313. }
  1314. // Ensure it restarts successfully
  1315. _, err = container2.Output()
  1316. if err != nil {
  1317. t.Fatal(err)
  1318. }
  1319. }
  1320. func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
  1321. runtime := mkRuntime(t)
  1322. defer nuke(runtime)
  1323. config, hc, _, err := ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
  1324. if err != nil {
  1325. t.Fatal(err)
  1326. }
  1327. c, err := runtime.Create(config)
  1328. if err != nil {
  1329. t.Fatal(err)
  1330. }
  1331. stdout, err := c.StdoutPipe()
  1332. if err != nil {
  1333. t.Fatal(err)
  1334. }
  1335. defer runtime.Destroy(c)
  1336. if err := c.Start(hc); err != nil {
  1337. t.Fatal(err)
  1338. }
  1339. c.WaitTimeout(500 * time.Millisecond)
  1340. c.Wait()
  1341. output, err := ioutil.ReadAll(stdout)
  1342. if err != nil {
  1343. t.Fatal(err)
  1344. }
  1345. interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
  1346. if len(interfaces) != 1 {
  1347. t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
  1348. }
  1349. if !strings.HasSuffix(interfaces[0], ": lo") {
  1350. t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
  1351. }
  1352. }
  1353. func TestPrivilegedCanMknod(t *testing.T) {
  1354. runtime := mkRuntime(t)
  1355. defer nuke(runtime)
  1356. if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
  1357. t.Fatal("Could not mknod into privileged container")
  1358. }
  1359. }
  1360. func TestPrivilegedCanMount(t *testing.T) {
  1361. runtime := mkRuntime(t)
  1362. defer nuke(runtime)
  1363. if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
  1364. t.Fatal("Could not mount into privileged container")
  1365. }
  1366. }
  1367. func TestPrivilegedCannotMknod(t *testing.T) {
  1368. runtime := mkRuntime(t)
  1369. defer nuke(runtime)
  1370. if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
  1371. t.Fatal("Could mknod into secure container")
  1372. }
  1373. }
  1374. func TestPrivilegedCannotMount(t *testing.T) {
  1375. runtime := mkRuntime(t)
  1376. defer nuke(runtime)
  1377. if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
  1378. t.Fatal("Could mount into secure container")
  1379. }
  1380. }
  1381. func TestMultipleVolumesFrom(t *testing.T) {
  1382. runtime := mkRuntime(t)
  1383. defer nuke(runtime)
  1384. container, err := runtime.Create(&Config{
  1385. Image: GetTestImage(runtime).ID,
  1386. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1387. Volumes: map[string]struct{}{"/test": {}},
  1388. },
  1389. )
  1390. if err != nil {
  1391. t.Fatal(err)
  1392. }
  1393. defer runtime.Destroy(container)
  1394. for key := range container.Config.Volumes {
  1395. if key != "/test" {
  1396. t.Fail()
  1397. }
  1398. }
  1399. _, err = container.Output()
  1400. if err != nil {
  1401. t.Fatal(err)
  1402. }
  1403. expected := container.Volumes["/test"]
  1404. if expected == "" {
  1405. t.Fail()
  1406. }
  1407. container2, err := runtime.Create(
  1408. &Config{
  1409. Image: GetTestImage(runtime).ID,
  1410. Cmd: []string{"sh", "-c", "echo -n bar > /other/foo"},
  1411. Volumes: map[string]struct{}{"/other": {}},
  1412. },
  1413. )
  1414. if err != nil {
  1415. t.Fatal(err)
  1416. }
  1417. defer runtime.Destroy(container2)
  1418. for key := range container2.Config.Volumes {
  1419. if key != "/other" {
  1420. t.FailNow()
  1421. }
  1422. }
  1423. if _, err := container2.Output(); err != nil {
  1424. t.Fatal(err)
  1425. }
  1426. container3, err := runtime.Create(
  1427. &Config{
  1428. Image: GetTestImage(runtime).ID,
  1429. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1430. VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
  1431. })
  1432. if err != nil {
  1433. t.Fatal(err)
  1434. }
  1435. defer runtime.Destroy(container3)
  1436. if _, err := container3.Output(); err != nil {
  1437. t.Fatal(err)
  1438. }
  1439. t.Log(container3.Volumes)
  1440. if container3.Volumes["/test"] != container.Volumes["/test"] {
  1441. t.Fail()
  1442. }
  1443. if container3.Volumes["/other"] != container2.Volumes["/other"] {
  1444. t.Fail()
  1445. }
  1446. }