container_test.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603
  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. defer runtime.Destroy(container)
  984. container.generateLXCConfig(nil)
  985. grepFile(t, container.lxcConfigPath(), "lxc.utsname = foobar")
  986. grepFile(t, container.lxcConfigPath(),
  987. fmt.Sprintf("lxc.cgroup.memory.limit_in_bytes = %d", mem))
  988. grepFile(t, container.lxcConfigPath(),
  989. fmt.Sprintf("lxc.cgroup.memory.memsw.limit_in_bytes = %d", mem*2))
  990. }
  991. func TestCustomLxcConfig(t *testing.T) {
  992. runtime := mkRuntime(t)
  993. defer nuke(runtime)
  994. container, err := runtime.Create(&Config{
  995. Image: GetTestImage(runtime).ID,
  996. Cmd: []string{"/bin/true"},
  997. Hostname: "foobar",
  998. },
  999. )
  1000. if err != nil {
  1001. t.Fatal(err)
  1002. }
  1003. defer runtime.Destroy(container)
  1004. hostConfig := &HostConfig{LxcConf: []KeyValuePair{
  1005. {
  1006. Key: "lxc.utsname",
  1007. Value: "docker",
  1008. },
  1009. {
  1010. Key: "lxc.cgroup.cpuset.cpus",
  1011. Value: "0,1",
  1012. },
  1013. }}
  1014. container.generateLXCConfig(hostConfig)
  1015. grepFile(t, container.lxcConfigPath(), "lxc.utsname = docker")
  1016. grepFile(t, container.lxcConfigPath(), "lxc.cgroup.cpuset.cpus = 0,1")
  1017. }
  1018. func BenchmarkRunSequencial(b *testing.B) {
  1019. runtime := mkRuntime(b)
  1020. defer nuke(runtime)
  1021. for i := 0; i < b.N; i++ {
  1022. container, err := runtime.Create(&Config{
  1023. Image: GetTestImage(runtime).ID,
  1024. Cmd: []string{"echo", "-n", "foo"},
  1025. },
  1026. )
  1027. if err != nil {
  1028. b.Fatal(err)
  1029. }
  1030. defer runtime.Destroy(container)
  1031. output, err := container.Output()
  1032. if err != nil {
  1033. b.Fatal(err)
  1034. }
  1035. if string(output) != "foo" {
  1036. b.Fatalf("Unexpected output: %s", output)
  1037. }
  1038. if err := runtime.Destroy(container); err != nil {
  1039. b.Fatal(err)
  1040. }
  1041. }
  1042. }
  1043. func BenchmarkRunParallel(b *testing.B) {
  1044. runtime := mkRuntime(b)
  1045. defer nuke(runtime)
  1046. var tasks []chan error
  1047. for i := 0; i < b.N; i++ {
  1048. complete := make(chan error)
  1049. tasks = append(tasks, complete)
  1050. go func(i int, complete chan error) {
  1051. container, err := runtime.Create(&Config{
  1052. Image: GetTestImage(runtime).ID,
  1053. Cmd: []string{"echo", "-n", "foo"},
  1054. },
  1055. )
  1056. if err != nil {
  1057. complete <- err
  1058. return
  1059. }
  1060. defer runtime.Destroy(container)
  1061. hostConfig := &HostConfig{}
  1062. if err := container.Start(hostConfig); err != nil {
  1063. complete <- err
  1064. return
  1065. }
  1066. if err := container.WaitTimeout(15 * time.Second); err != nil {
  1067. complete <- err
  1068. return
  1069. }
  1070. // if string(output) != "foo" {
  1071. // complete <- fmt.Errorf("Unexecpted output: %v", string(output))
  1072. // }
  1073. if err := runtime.Destroy(container); err != nil {
  1074. complete <- err
  1075. return
  1076. }
  1077. complete <- nil
  1078. }(i, complete)
  1079. }
  1080. var errors []error
  1081. for _, task := range tasks {
  1082. err := <-task
  1083. if err != nil {
  1084. errors = append(errors, err)
  1085. }
  1086. }
  1087. if len(errors) > 0 {
  1088. b.Fatal(errors)
  1089. }
  1090. }
  1091. func tempDir(t *testing.T) string {
  1092. tmpDir, err := ioutil.TempDir("", "docker-test-container")
  1093. if err != nil {
  1094. t.Fatal(err)
  1095. }
  1096. return tmpDir
  1097. }
  1098. // Test for #1737
  1099. func TestCopyVolumeUidGid(t *testing.T) {
  1100. r := mkRuntime(t)
  1101. defer nuke(r)
  1102. // Add directory not owned by root
  1103. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello && touch /hello/test.txt && chown daemon.daemon /hello"}, t)
  1104. defer r.Destroy(container1)
  1105. if container1.State.Running {
  1106. t.Errorf("Container shouldn't be running")
  1107. }
  1108. if err := container1.Run(); err != nil {
  1109. t.Fatal(err)
  1110. }
  1111. if container1.State.Running {
  1112. t.Errorf("Container shouldn't be running")
  1113. }
  1114. rwTar, err := container1.ExportRw()
  1115. if err != nil {
  1116. t.Error(err)
  1117. }
  1118. img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  1119. if err != nil {
  1120. t.Error(err)
  1121. }
  1122. // Test that the uid and gid is copied from the image to the volume
  1123. tmpDir1 := tempDir(t)
  1124. defer os.RemoveAll(tmpDir1)
  1125. stdout1, _ := runContainer(r, []string{"-v", "/hello", img.ID, "stat", "-c", "%U %G", "/hello"}, t)
  1126. if !strings.Contains(stdout1, "daemon daemon") {
  1127. t.Fatal("Container failed to transfer uid and gid to volume")
  1128. }
  1129. }
  1130. // Test for #1582
  1131. func TestCopyVolumeContent(t *testing.T) {
  1132. r := mkRuntime(t)
  1133. defer nuke(r)
  1134. // Put some content in a directory of a container and commit it
  1135. container1, _, _ := mkContainer(r, []string{"_", "/bin/sh", "-c", "mkdir -p /hello/local && echo hello > /hello/local/world"}, t)
  1136. defer r.Destroy(container1)
  1137. if container1.State.Running {
  1138. t.Errorf("Container shouldn't be running")
  1139. }
  1140. if err := container1.Run(); err != nil {
  1141. t.Fatal(err)
  1142. }
  1143. if container1.State.Running {
  1144. t.Errorf("Container shouldn't be running")
  1145. }
  1146. rwTar, err := container1.ExportRw()
  1147. if err != nil {
  1148. t.Error(err)
  1149. }
  1150. img, err := r.graph.Create(rwTar, container1, "unit test commited image", "", nil)
  1151. if err != nil {
  1152. t.Error(err)
  1153. }
  1154. // Test that the content is copied from the image to the volume
  1155. tmpDir1 := tempDir(t)
  1156. defer os.RemoveAll(tmpDir1)
  1157. stdout1, _ := runContainer(r, []string{"-v", "/hello", img.ID, "find", "/hello"}, t)
  1158. if !(strings.Contains(stdout1, "/hello/local/world") && strings.Contains(stdout1, "/hello/local")) {
  1159. t.Fatal("Container failed to transfer content to volume")
  1160. }
  1161. }
  1162. func TestBindMounts(t *testing.T) {
  1163. r := mkRuntime(t)
  1164. defer nuke(r)
  1165. tmpDir := tempDir(t)
  1166. defer os.RemoveAll(tmpDir)
  1167. writeFile(path.Join(tmpDir, "touch-me"), "", t)
  1168. // Test reading from a read-only bind mount
  1169. stdout, _ := runContainer(r, []string{"-v", fmt.Sprintf("%s:/tmp:ro", tmpDir), "_", "ls", "/tmp"}, t)
  1170. if !strings.Contains(stdout, "touch-me") {
  1171. t.Fatal("Container failed to read from bind mount")
  1172. }
  1173. // test writing to bind mount
  1174. runContainer(r, []string{"-v", fmt.Sprintf("%s:/tmp:rw", tmpDir), "_", "touch", "/tmp/holla"}, t)
  1175. readFile(path.Join(tmpDir, "holla"), t) // Will fail if the file doesn't exist
  1176. // test mounting to an illegal destination directory
  1177. if _, err := runContainer(r, []string{"-v", fmt.Sprintf("%s:.", tmpDir), "_", "ls", "."}, nil); err == nil {
  1178. t.Fatal("Container bind mounted illegal directory")
  1179. }
  1180. }
  1181. // Test that VolumesRW values are copied to the new container. Regression test for #1201
  1182. func TestVolumesFromReadonlyMount(t *testing.T) {
  1183. runtime := mkRuntime(t)
  1184. defer nuke(runtime)
  1185. container, err := runtime.Create(
  1186. &Config{
  1187. Image: GetTestImage(runtime).ID,
  1188. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1189. Volumes: map[string]struct{}{"/test": {}},
  1190. },
  1191. )
  1192. if err != nil {
  1193. t.Fatal(err)
  1194. }
  1195. defer runtime.Destroy(container)
  1196. _, err = container.Output()
  1197. if err != nil {
  1198. t.Fatal(err)
  1199. }
  1200. if !container.VolumesRW["/test"] {
  1201. t.Fail()
  1202. }
  1203. container2, err := runtime.Create(
  1204. &Config{
  1205. Image: GetTestImage(runtime).ID,
  1206. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1207. VolumesFrom: container.ID,
  1208. },
  1209. )
  1210. if err != nil {
  1211. t.Fatal(err)
  1212. }
  1213. defer runtime.Destroy(container2)
  1214. _, err = container2.Output()
  1215. if err != nil {
  1216. t.Fatal(err)
  1217. }
  1218. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1219. t.Fail()
  1220. }
  1221. actual, exists := container2.VolumesRW["/test"]
  1222. if !exists {
  1223. t.Fail()
  1224. }
  1225. if container.VolumesRW["/test"] != actual {
  1226. t.Fail()
  1227. }
  1228. }
  1229. // Test that restarting a container with a volume does not create a new volume on restart. Regression test for #819.
  1230. func TestRestartWithVolumes(t *testing.T) {
  1231. runtime := mkRuntime(t)
  1232. defer nuke(runtime)
  1233. container, err := runtime.Create(&Config{
  1234. Image: GetTestImage(runtime).ID,
  1235. Cmd: []string{"echo", "-n", "foobar"},
  1236. Volumes: map[string]struct{}{"/test": {}},
  1237. },
  1238. )
  1239. if err != nil {
  1240. t.Fatal(err)
  1241. }
  1242. defer runtime.Destroy(container)
  1243. for key := range container.Config.Volumes {
  1244. if key != "/test" {
  1245. t.Fail()
  1246. }
  1247. }
  1248. _, err = container.Output()
  1249. if err != nil {
  1250. t.Fatal(err)
  1251. }
  1252. expected := container.Volumes["/test"]
  1253. if expected == "" {
  1254. t.Fail()
  1255. }
  1256. // Run the container again to verify the volume path persists
  1257. _, err = container.Output()
  1258. if err != nil {
  1259. t.Fatal(err)
  1260. }
  1261. actual := container.Volumes["/test"]
  1262. if expected != actual {
  1263. t.Fatalf("Expected volume path: %s Actual path: %s", expected, actual)
  1264. }
  1265. }
  1266. // Test for #1351
  1267. func TestVolumesFromWithVolumes(t *testing.T) {
  1268. runtime := mkRuntime(t)
  1269. defer nuke(runtime)
  1270. container, err := runtime.Create(&Config{
  1271. Image: GetTestImage(runtime).ID,
  1272. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1273. Volumes: map[string]struct{}{"/test": {}},
  1274. },
  1275. )
  1276. if err != nil {
  1277. t.Fatal(err)
  1278. }
  1279. defer runtime.Destroy(container)
  1280. for key := range container.Config.Volumes {
  1281. if key != "/test" {
  1282. t.Fail()
  1283. }
  1284. }
  1285. _, err = container.Output()
  1286. if err != nil {
  1287. t.Fatal(err)
  1288. }
  1289. expected := container.Volumes["/test"]
  1290. if expected == "" {
  1291. t.Fail()
  1292. }
  1293. container2, err := runtime.Create(
  1294. &Config{
  1295. Image: GetTestImage(runtime).ID,
  1296. Cmd: []string{"cat", "/test/foo"},
  1297. VolumesFrom: container.ID,
  1298. Volumes: map[string]struct{}{"/test": {}},
  1299. },
  1300. )
  1301. if err != nil {
  1302. t.Fatal(err)
  1303. }
  1304. defer runtime.Destroy(container2)
  1305. output, err := container2.Output()
  1306. if err != nil {
  1307. t.Fatal(err)
  1308. }
  1309. if string(output) != "bar" {
  1310. t.Fail()
  1311. }
  1312. if container.Volumes["/test"] != container2.Volumes["/test"] {
  1313. t.Fail()
  1314. }
  1315. // Ensure it restarts successfully
  1316. _, err = container2.Output()
  1317. if err != nil {
  1318. t.Fatal(err)
  1319. }
  1320. }
  1321. func TestOnlyLoopbackExistsWhenUsingDisableNetworkOption(t *testing.T) {
  1322. runtime := mkRuntime(t)
  1323. defer nuke(runtime)
  1324. config, hc, _, err := ParseRun([]string{"-n=false", GetTestImage(runtime).ID, "ip", "addr", "show"}, nil)
  1325. if err != nil {
  1326. t.Fatal(err)
  1327. }
  1328. c, err := runtime.Create(config)
  1329. if err != nil {
  1330. t.Fatal(err)
  1331. }
  1332. stdout, err := c.StdoutPipe()
  1333. if err != nil {
  1334. t.Fatal(err)
  1335. }
  1336. defer runtime.Destroy(c)
  1337. if err := c.Start(hc); err != nil {
  1338. t.Fatal(err)
  1339. }
  1340. c.WaitTimeout(500 * time.Millisecond)
  1341. c.Wait()
  1342. output, err := ioutil.ReadAll(stdout)
  1343. if err != nil {
  1344. t.Fatal(err)
  1345. }
  1346. interfaces := regexp.MustCompile(`(?m)^[0-9]+: [a-zA-Z0-9]+`).FindAllString(string(output), -1)
  1347. if len(interfaces) != 1 {
  1348. t.Fatalf("Wrong interface count in test container: expected [*: lo], got %s", interfaces)
  1349. }
  1350. if !strings.HasSuffix(interfaces[0], ": lo") {
  1351. t.Fatalf("Wrong interface in test container: expected [*: lo], got %s", interfaces)
  1352. }
  1353. }
  1354. func TestPrivilegedCanMknod(t *testing.T) {
  1355. runtime := mkRuntime(t)
  1356. defer nuke(runtime)
  1357. if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mknod /tmp/sda b 8 0 && echo ok"}, t); output != "ok\n" {
  1358. t.Fatal("Could not mknod into privileged container")
  1359. }
  1360. }
  1361. func TestPrivilegedCanMount(t *testing.T) {
  1362. runtime := mkRuntime(t)
  1363. defer nuke(runtime)
  1364. if output, _ := runContainer(runtime, []string{"-privileged", "_", "sh", "-c", "mount -t tmpfs none /tmp && echo ok"}, t); output != "ok\n" {
  1365. t.Fatal("Could not mount into privileged container")
  1366. }
  1367. }
  1368. func TestPrivilegedCannotMknod(t *testing.T) {
  1369. runtime := mkRuntime(t)
  1370. defer nuke(runtime)
  1371. if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mknod /tmp/sda b 8 0 || echo ok"}, t); output != "ok\n" {
  1372. t.Fatal("Could mknod into secure container")
  1373. }
  1374. }
  1375. func TestPrivilegedCannotMount(t *testing.T) {
  1376. runtime := mkRuntime(t)
  1377. defer nuke(runtime)
  1378. if output, _ := runContainer(runtime, []string{"_", "sh", "-c", "mount -t tmpfs none /tmp || echo ok"}, t); output != "ok\n" {
  1379. t.Fatal("Could mount into secure container")
  1380. }
  1381. }
  1382. func TestMultipleVolumesFrom(t *testing.T) {
  1383. runtime := mkRuntime(t)
  1384. defer nuke(runtime)
  1385. container, err := runtime.Create(&Config{
  1386. Image: GetTestImage(runtime).ID,
  1387. Cmd: []string{"sh", "-c", "echo -n bar > /test/foo"},
  1388. Volumes: map[string]struct{}{"/test": {}},
  1389. },
  1390. )
  1391. if err != nil {
  1392. t.Fatal(err)
  1393. }
  1394. defer runtime.Destroy(container)
  1395. for key := range container.Config.Volumes {
  1396. if key != "/test" {
  1397. t.Fail()
  1398. }
  1399. }
  1400. _, err = container.Output()
  1401. if err != nil {
  1402. t.Fatal(err)
  1403. }
  1404. expected := container.Volumes["/test"]
  1405. if expected == "" {
  1406. t.Fail()
  1407. }
  1408. container2, err := runtime.Create(
  1409. &Config{
  1410. Image: GetTestImage(runtime).ID,
  1411. Cmd: []string{"sh", "-c", "echo -n bar > /other/foo"},
  1412. Volumes: map[string]struct{}{"/other": {}},
  1413. },
  1414. )
  1415. if err != nil {
  1416. t.Fatal(err)
  1417. }
  1418. defer runtime.Destroy(container2)
  1419. for key := range container2.Config.Volumes {
  1420. if key != "/other" {
  1421. t.FailNow()
  1422. }
  1423. }
  1424. if _, err := container2.Output(); err != nil {
  1425. t.Fatal(err)
  1426. }
  1427. container3, err := runtime.Create(
  1428. &Config{
  1429. Image: GetTestImage(runtime).ID,
  1430. Cmd: []string{"/bin/echo", "-n", "foobar"},
  1431. VolumesFrom: strings.Join([]string{container.ID, container2.ID}, ","),
  1432. })
  1433. if err != nil {
  1434. t.Fatal(err)
  1435. }
  1436. defer runtime.Destroy(container3)
  1437. if _, err := container3.Output(); err != nil {
  1438. t.Fatal(err)
  1439. }
  1440. t.Log(container3.Volumes)
  1441. if container3.Volumes["/test"] != container.Volumes["/test"] {
  1442. t.Fail()
  1443. }
  1444. if container3.Volumes["/other"] != container2.Volumes["/other"] {
  1445. t.Fail()
  1446. }
  1447. }