container_test.go 39 KB

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