container_test.go 38 KB

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