container_test.go 38 KB

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